var message = "";
var isMac = navigator.userAgent.indexOf("Mac") != -1;


$(document).ready(function() {
	checkEmpty();
	
	if (isMac && jQuery.browser.mozilla) {
		ffMacTweaks();
	}
	if (jQuery.browser.safari) {
		safariTweaks();
	}
	
	hideRelatedItems();
	emptyLinks();
	noRelatedItems();
	missingMainImg();
	aboutPeopleList();
	showPeopleListItem(0);
	categoriesNav();
	peopleNavMargin();
    equalHeight($(".aboutList_thumbs li"));
	lastStockist();
	tableRowBgs();	
	hideProdDiv();
	// hideEmptyFeatItems(); // function does not exist
	hideVidDiv();
	footerLastLink();
	hideEmptyRelatedLinks();
	hideEmptyCatNav();
	getFileName();
});

function checkEmpty() {
	if($.trim($('.introText p').text()) == "") {
	  $('.introText').parent().remove();
	}
	if($.trim($('h2').text()) == "Contact us") {
	  $('.signUpNewsReg').remove();
	}
}
	


function safariTweaks() {
	$('.signUpNewsReg input').css('margin-bottom','4px');
}

function ffMacTweaks() {
	$('.signUpNewsReg input').css('margin-bottom','3px');
}

function hideRelatedItems() {
	if($('.relatedItem').length) {
		$('#rightCol p.pageTitle').addClass('show'); 
	}
}
function emptyLinks() {
	if($('.relatedLinks li a').attr('href') == null ) {
		$('.relatedLinks').remove();
	}
}
/*
function noRelatedItems() {
	if($(".relatedItem").length) {
		$("#rightCol").css('margin','0');  // No margin on rightCol if it contains 'related items'
	}
}
*/
function missingMainImg() {
	if($(".mainImage a img").length) {
		$(".mainImage").css('display','block');  // Display div if it contains an image
	}
}
function showPeopleListItem(i) {
	$(".aboutList_chosen li").hide();
	$(".aboutList_chosen li").eq(i).show();
	$(".aboutList_thumbs li a").removeClass('current');
	$(".aboutList_thumbs li a").eq(i).addClass('current');
}
function aboutPeopleList() {
	$(".aboutList_thumbs li a").each(function(i) {
		$(this).click(function() {
			showPeopleListItem(i);
		});
   	});
}

function productThumbnails(i) {
	$("a img.prodCentreImage").hide();
	$("a img.prodCentreImage").eq(i).show();
}
function showThumbnails() {
	$("#otherImages li a.thumb").each(function(i) {
		$(this).click(function() {
			productThumbnails(i);					   
		});
	});
}

function categoriesNav() {
	$(".newsFeat_bottom ul li:nth-child(5n)").css('border-bottom','none')
	$(".newsFeat_bottom ul li:last").css('border-bottom','none');	
}
function peopleNavMargin() {
	$(".aboutList_thumbs li:eq(6)").css('margin-right','0');
	$(".aboutList_thumbs li:eq(13)").css('margin-right','0'); 	
	$(".aboutList_thumbs li:eq(20)").css('margin-right','0');	
	$(".aboutList_thumbs li:eq(27)").css('margin-right','0');	
}

function equalHeight(group) { // Equalise heights of list items for floaty goodness
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function lastStockist() {
	$('.stockist:last').addClass('lastStockist');	
}
function tableRowBgs() {
	$('#productTable_overflow table tr:even').addClass('even');
	$('#productTable_overflow table tr td:first').addClass('wishListCell');
}
function hideProdDiv() {
	if($.trim($("#leftCol div#product").text()) == "") { // Remove white space from div
	  $("#leftCol div#product").remove(); // Remove empty div
	}
}
function hideVidDiv() {
	$(".RHvideo").each(function() {
		if ($.trim($(".RHvideo").text()) == "") {
			$(this).remove();
		}							  
	});
}

function footerLastLink() {
	$("#footer li:last").css("background","none");	
}
function hideEmptyRelatedLinks() {
	$(".relatedLinks li a").each(function() {
		if ($.trim($(".relatedLinks li a").text()) == "") {
			$(this).parent().parent().remove();
		}							  
	});
}
function hideEmptyCatNav() {
	$(".pagedImageLinks").each(function() {
		if ($.trim($(".pagedImageLinks").text()) == "") {
			$(this).parent().parent().parent().remove();
			$(".contentBlock .pageTitle").text("No upcoming events in this category");
		}							  
	});
}

function getFileName() {
	var extractedFileName = $('.fileUpload').val();	
	
	$('.fileUpload').change(function() {
		$('#fake input').val('File selected');
		//$('#fake input').val('');
		//$('#fake input').val(extractedFileName);
	});
}
