var emailMessage = "Email address";

var message = "";
var isMac = navigator.userAgent.indexOf("Mac") != -1;
var isIPhone = navigator.userAgent.indexOf("iPhone") != -1;
var isIPod = navigator.userAgent.indexOf("iPod") != -1;
var isIPad = navigator.userAgent.indexOf("iPad") != -1;

// var Houschka = { src: '../swf/Houschka.swf' };
// sIFR.activate( Houschka );
						   
$(document).ready(function() {
						   
	if (isMac && jQuery.browser.mozilla) {
		$("body").addClass("ffMac");
	}
	if (jQuery.browser.safari) {
		$("body").addClass("safari");
	}
						   
	var removeContent = new Array();
	removeContent.push("span");
	removeContent.push("a.buttonLink");
	removeEmptyContent(removeContent);
	
	$("#footerNavigation li:first").addClass("first");
	$("#footerNavigation li:last").addClass("last");
	
	$(".checkBoxList label").after('<div class="floatClear"></div>');
	
	$("input:text,input:password").addClass('text');
	
	prePopulateEmailField();
	$(".subscriptionEmail input").focus(function(e) {
		clearEmailField();
	});
	$(".subscriptionEmail input").blur(function(e) { 
		prePopulateEmailField();
	});
	
	replaceDownloadIcons();
	replaceLinkIcons();
	headerMainNavLevels();
	buttonHovers();
	sIFRPageTitle();
	priceLists();
	skuDescriptions();
	mailingListForm();
	
	message += "Browser: \n";
	jQuery.each(jQuery.browser, function(i, val) {
		message += i + ": " + val + "\n";
    });
	// alert(message);
});

function sIFRPageTitle() {
	var pageTitle = $('h2.pageTitle').html();
	if (pageTitle != null) {
		$('h2.pageTitle').html( pageTitle.replace('help:','<span class="normal">help:</span>') );
		
		var color = $('h2.pageTitle').css('color');
		if ( color.substr(0,3) == 'rgb' ) {
			hexColor = rgbToHex(color);
		} else {
			hexColor = color;
		}
		
		message += "hexColor: " + hexColor + "\n";
		
		sIFR.replace(Houschka, {
			selector: 'h2.pageTitle',
			css: ['.sIFR-root { leading:-5;letter-spacing:-3;font-weight:bold;color:' + hexColor + ' } .normal { font-weight:normal }'],
			offsetLeft: -5,
			offsetTop: -4,
			tuneHeight: -9,
			transparent: true
		});
	}
}

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 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:last-child").addClass("last");
	// $("#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);
	});
	*/
	// $('#headerMainNav li ul').append('<div class="floatClear"></div>');
}

function buttonHovers() {
	$("input:submit, input:button").addClass('button');
	$("input.button").hover(
		function() {
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		}
	);	
}

function priceLists() {
	$(".prices li:first-child").addClass("first");
	$(".prices li:last-child").addClass("last");
	$(".prices li:not(.first)").each(function(){
		var top1 = $(this).prev().offset().top;
		var top2 = $(this).offset().top;
		// message += "top1: " + top1 + ", top2: " + top2 + "\n";
		if ( top1 != top2 ) {
			$(this).prev().addClass("last");
			$(this).addClass("first");
		}
	});
}

function skuDescriptions() {
	$(".skuDescription").each(function(){
		var top = $(this).position().top;
		var targetHeight = 401 - Math.round(top) - 20;
		$(this).css("height",targetHeight); 
		// message += "top: " + Math.ceil(top) + ", targetHeight: " + targetHeight + "\n";
	});
}

function mailingListForm() {
	$(".showMailingList").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	$(".showMailingList").has(".error:visible").addClass("formError");
	$(".showMailingList").has("p.subscriptionSuccess").addClass("formSuccess");
	setTimeout('fadeMailingListSuccess()',3000);
	$(".hideMailingList").click(function(e) {
		$(".showMailingList").removeClass("hover formError formSuccess");
	});
}

function fadeMailingListSuccess() {
	$(".formSuccess .mailingListForm").fadeOut();
}

function prePopulateEmailField() {
	if ( $(".subscriptionEmail input").val() == "" || $(".subscriptionEmail input").val() == emailMessage ) {
		$(".subscriptionEmail input").val(emailMessage);
		$(".subscriptionEmail input").addClass("prePopulated");
	}
}

function clearEmailField() {
	if ( $(".subscriptionEmail input").val() == emailMessage ) {
		$(".subscriptionEmail input").val("");
		$(".subscriptionEmail input").removeClass("prePopulated");
	}
}

