var message = "";
var isMac = navigator.userAgent.indexOf("Mac") != -1;
						   
$(document).ready(function() {
						   
	if (isMac && jQuery.browser.mozilla) {
		ffMacTweaks();
	}
	if (jQuery.browser.safari) {
		safariTweaks();
	}
						   
	var removeContent = new Array();
	removeContent.push("h1");
	removeContent.push("a:not(.galleryThumb)");
	removeContent.push("li:not(#buyLinkedSKUs li)");
	removeContent.push("ul");
	removeContent.push("p");
	removeContent.push("h4");
	removeContent.push(".contentText");
	removeContent.push(".contentBlock");
	removeContent.push("#footerInner");
	removeEmptyContent(removeContent);
	
	$("#mainTextContent .contentBlock").append('<div class="floatClear"></div>');
	$("#linksAndFiles").append('<div class="floatClear"></div>');
	$("#movieImageLinks").append('<div class="floatClear"></div>');
	$("#movieImageLinks li:nth-child(6n)").after('<div class="floatClear"></div>');
	
	$("#footerInner").append('<div class="floatClear"></div>');
	$("#footerInner li:first").addClass("first");
	$("#footerInner li:last").addClass("last");
	
	$(".checkBoxList label").after('<div class="floatClear"></div>');
	
	$(".utilityTextContent .contentBlock").append('<div class="floatClear"></div>');
	$(".utilityTextContent .contentBlock:first-child").addClass("first");
	$(".utilityTextContent .contentBlock").not(".contentBlock:has(h2), .first").addClass("noH2");

	// equaliseColumns("#movieImageLinks");
	equaliseGalleryThumbs();
	
	replaceDownloadIcons();
	replaceLinkIcons();
	headerMainNavLevels();
	buttonHovers();
	
	message += "Browser: \n";
	jQuery.each(jQuery.browser, function(i, val) {
		message += i + ": " + val + "\n";
    });
	// alert(message);
});

function ffMacTweaks() {
	$("input.submit, .skuAddToBasket input, input.btnShowLink").css("padding-top","1px");
	$(".skuAddToBasket input").css("padding-left","5px");
	$("#movieImageLinks li a span.linkSubHeadline").css("padding","3px 9px 2px");
	$("#movieImageLinks li").css("min-height","243px");
}

function safariTweaks() {
	$("body").addClass("safari");
}

function removeEmptyContent(removeContent) {
	for (var i in removeContent) {
		$(removeContent[i]).children("div.floatClear").remove();
		$(removeContent[i]).each(function() {
			var htmlStr = $(this).html().toLowerCase();
			var htmlStr = jQuery.trim(htmlStr);
			if (htmlStr == "") {
				$(this).replaceWith("");
			}
		});
	}
}

function equaliseColumns(rootSelector) {
	$(rootSelector + " ul li:nth-child(2n)").addClass("second");
	$(rootSelector + " ul li:nth-child(2n)").after('<div class="floatClear"></div>');
	$(rootSelector + " ul li.second").each( function() {
		var thisHeight = $(this).height();
		var prevHeight = $(this).prev().height();
		if ( thisHeight > prevHeight ) {
			$(this).prev().height(thisHeight);
		} else if ( prevHeight > thisHeight ) {
			$(this).height(prevHeight);
		}
	});
}

function equaliseGalleryThumbs() {
	var defaultTitle = "Gallery image";
	$("#gallery ul li:nth-child(3n)").addClass("third");
	$("#left #gallery ul li.third, #mainContent #gallery ul li.third:nth-child(2n)").after('<div class="floatClear"></div>');
	$("#gallery").append('<div class="floatClear"></div>');
}

function replaceDownloadIcons() {
	$(".downloadIcon").each( function() {
	 	var linkHref = $(this).find("a").attr("href");
		var docType = linkHref.slice(-3).toLowerCase();
		if (docType == "pdf" || docType == "doc") {
			$(this).addClass(docType);
		}
	});
}

function replaceLinkIcons() {
	$(".linkIcon").each( function() {
	 	var linkHref = $(this).find("a").attr("href");
		var protocol = linkHref.slice(0,4).toLowerCase();
		if (protocol == "http") {
			$(this).addClass("external");
			$(this).find("a").attr("target", "_blank");
		}
	});
}

function headerMainNavLevels() {
	$(".headerMainNav li:not(.headerMainNav li ul li)").addClass("level1");
	$("li.level1").not("li.level1:has(ul)").addClass("noChildren");
	$("li.level1 li:not(li.level1 li ul li)").addClass("level2");
	$("li.level2 li:not(li.level2 li ul li)").addClass("level3");
	$("li.level3 li:not(li.level3 li ul li)").addClass("level4");
	$(".headerMainNav li").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	$(".headerMainNav li:first-child").addClass("first");
	var maxHeight = 22;
	$(".headerMainNav li.level2").each( function(i) {
		var itemHeight = $(this).height();
		if ( $(this).children("ul").height() > itemHeight ) {
			itemHeight = $(this).children("ul").height() + $(this).position().top;
		} else {
			// itemHeight = 22 + $(this).position().top;
			itemHeight += $(this).position().top;
		}
		if (itemHeight > maxHeight) {
			maxHeight = itemHeight;
		}
	});
	$(".headerMainNav li.level1").children("ul").height(maxHeight);
	$(".headerMainNav li.level2:has(ul)").each( function() {
		$(this).width(160);
	});
}

function buttonHovers() {
	$(".skuAddToBasket input, .btnShowLink").hover(function() {
		$(this).addClass("hover");
	},
	  function () {
		$(this).removeClass("hover");
	});	
}

