$(document).ready(function(){
	$(".navigation li").hover( 
		function() { $(this).addClass("over");$(this).removeClass("not-over"); },
		function() { $(this).removeClass("over");$(this).addClass("not-over"); }
	);
	// This is for the personalities / prod options behaviours
	$(".option-list img").click(
		function() {
			//$(this).parent().find("input").each(function() {this.checked=true});
			var selectedRef = this;
			tb_remove();
			
			$(".table_preference td img").each(
				function() {
					if($(this).parent()[0].tagName!="A") {
						if($(this).attr("src")==$(selectedRef).attr("src")||$(this).attr("src")=="") {
							$(this).attr("alt", $(selectedRef).attr("alt"));
							if($(this).parent().attr("class")!="selected-item-container") $(this).wrap('<div class="selected-item-container"></div>');
							$(this).parent().hover(
								function() { $("img", this).css("left", "auto").css("right", "33px"); },
								function() { $("img", this).css("left", "-1000em").css("right", "auto"); }
							);
							// update sampler
							if(document.getElementById("labelSampler"))
							{
							    if(!$(this).parents("table").attr("class").match("sampler-na")) {
								    if($(this).parents("tr").attr("class") && $(this).parents("tr")[0].className.match("tp-picture")) { updateSamplerPicture(); }
								    if($(this).parents("tr").attr("class") && $(this).parents("tr")[0].className.match("tp-font")) { updateSamplerText(); }
							    }
							}
						}
					}
				}
			);
		}
	);
	$("a.thickbox").click(
		function() {
			/*if($("img", this).attr("src").match("viewoptions")) {
				$("#TB_ajaxContent li").vjustify();
			}*/
			if($(this).children().attr("src"))
			{
			    if($(this).children("img").attr("src").match("viewoptions"))
			    {
				    $("#TB_ajaxContent li").vjustify();
				}
			}
		}
	);
	
	// Accessible New Window Links
	$(".accessible-target").click( function(e) {
		// Abort if a modifier key is pressed
		if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
			return true;
		}
		else {
			// Change "_blank" to something like "newWindow" to load all links in the same new window
		    var newWindow = window.open(this.getAttribute('href'), '_blank');
			if (newWindow) {
				if (newWindow.focus) {
					newWindow.focus();
				}
				return false;
			}
			return true;
		}
		
	});
	$(".accessible-target").attr("title", "Link opens in an external window");
	
	
	/*SWFIR*/
	/*reset the background image on the product image div if there is an image there(for product details)*/
	if($("img.main").length > 0) {
		$("div.product_image").css("background-image", "none");
	}
	

	/*product details main image change*/
	$("ul.thumbs li a").click(
		function() {
			$("div.product_image span").remove(".main");
			$("div.product_image").prepend("<img src='"+$(this).attr("href")+"' class='main' height='197' width='197'/>");
			$("div.product_image a.thickbox").attr("href", $(this).attr("href").replace(/197/g, "390"));
			
			var borders = new swfir();
			borders.specify("border-radius", "2");
			borders.specify("border-color", "#ccc");
			borders.specify("border-width", "1");
			borders.specify("shadow-angle", "90");
			borders.specify("shadow-color", "#aaa");
			borders.specify("shadow-blur", "3");
			borders.specify("shadow-offset", "3");
			borders.specify("rotate", "0");
			borders.swap("div.product_image img.main"); 
			
			$("ul.thumbs li").removeClass("selected");				
			$(this).parent().addClass("selected");
			
			return false;
		}
	);
	
	 var borders = new swfir();
	 if($(".pricelist").length>0) {		
	 	borders.specify("border-radius", "2");
		borders.specify("border-color", "#bbb");
		borders.specify("border-width", "1");
		borders.specify("shadow-angle", "90");
		borders.specify("shadow-color", "#999");
		borders.specify("shadow-blur", "3");
		borders.specify("shadow-offset", "3");
		borders.specify("rotate", "0");
		borders.swap("div.pricelist div.item-image img"); 
	 }
	//for listings default
	if($(".product_image").length>0) {
		borders.specify("border-radius", "2");
		borders.specify("border-color", "#bbb");
		borders.specify("border-width", "1");
		borders.specify("shadow-angle", "90");
		borders.specify("shadow-color", "#999");
		borders.specify("shadow-blur", "3");
		borders.specify("shadow-offset", "3");
		borders.specify("rotate", "0");
		borders.swap("div.pricelist div.item-image img"); 
		borders.swap("div.product_image img.main"); 
		var bordersThree = new swfir();
		bordersThree.specify("border-radius", "2");
		bordersThree.specify("border-color", "#ccc");
		bordersThree.specify("border-width", "1");
		bordersThree.specify("shadow-angle", "90");
		bordersThree.specify("shadow-color", "#aaa");
		bordersThree.specify("rotate", "0");
		bordersThree.specify("shadow-blur", "3");
		bordersThree.specify("shadow-offset", "3");
		bordersThree.swap("div.product_image ul.thumbs img.thumb"); 
	}
	
	
	
	/*FAQ list*/
	$("div.faq_content h4 a").click(
		function()
		{
			$("div.faq_content li").removeClass("selected");
			$(this).parents("li").addClass("selected");
			
			return false;
		}
	)
	
	/*check the height of the h2s before sifr*/
	/*if one goes across two lines, set a difference height for it*/
	$(".flash-stack-info ul li h2").each(
		function(i) {
			if(this.offsetHeight > 35) { //alert("im on two lines");				
				$(this).css("font-size", "24px");
			}
		}
	)
	
	/*set the homepage stack items*/
	if(typeof sIFR == "function"){
		sIFR.replaceElement(".dialogtype01 .content h2", named({sFlashSrc: "media/sifr/black-jack.swf", sColor: "#f066a7", sWmode: "transparent", sBgColor: "#f7f7f4"}));
	}
	
	$(".flash-stack-info ul li").addClass("hide");
	$(".flash-stack-info ul li:eq("+parseInt($(".flash-stack-info ul li").length - 1)+")").removeClass("hide");
	
	/*insert Flash*/
	if(document.getElementById('flash-stack')) {
		$(".flash-stack-info img.stack-image").removeAttr("id");
		$(".flash-stack-info li a").removeAttr("id");
		$(".flash-stack-info li").each( 
			function() {
				pText = $("p", this).text();
				$("p", this).remove();
				$("h2", this).after("<p>"+pText+"</p>");
			}
		);
		
		
		var so = new SWFObject("media/flash/photo-stack.swf", "nextDigitalFlashStackComponent", "500", "222", "8", "#f7f7f4");
		so.addParam("wmode", "transparent");
		var list = getList();
		//alert("list: \n" + list);
		
		so.addVariable('list', list);
		so.write("flash-stack");
		$(".flash-stack-info").css("right", "20px");
		window.nextDigitalFlashStackComponent = document.getElementById('nextDigitalFlashStackComponent');
	}
	//a print
	$("a.print").click(
		function() {
			printPage();
		}
	);
	
	// Flash :: Sampler
	if($(".label-sampler").length>0) {
		$(".label-sampler").removeClass("label-sampler").attr("id", "labelSampler");
		// check if we have the fields to display
		if(samplerFieldsAvailable()) {
			var so = new SWFObject("media/flash/sampler.swf", "labelSamplerINS", "398", "89", "8", "#ffffff");
			so.addParam("wmode", "transparent");
			so.addParam("allowScriptAccess", "always");
			so.addVariable('fv_sampleType', $("#labelSampler").attr("class"));
			so.write("labelSampler");
			window.labelSamplerINS = document.getElementById('labelSamplerINS');		
		} else {
			// Hide the Sampler stuff
			$("#labelSampler").parents("table").addClass("sampler-na").prev().addClass("sampler-na");
			$("#labelSampler").prev().remove();
			$("#labelSampler").next().next().remove();
			$("#labelSampler").remove();
		}
		
		if($(".tp-text").length>0) {
			$(".tp-text").each(
				function() { $("td:eq(1) input", this).keyup(function() { updateSamplerText(); }); }
			);
		}
	}

	// Custom Form Behaviours
	if($("h1").text().match("Check Out - Customer Details")) {
		fundraisingField = $("td:contains('Fundraising Code:')").next().find("input");
		promoField = $("td:contains('Promotion Code:')").next().find("input");
		fundraisingField.focus(	function() {
			promoField.attr("alt", promoField.val());
			promoField.val("");
		});
		fundraisingField.blur( function() {
			if($(this).val()=="") {
				promoField.val(promoField.attr("alt"));
				promoField.attr("alt", "");
			}
		});
		promoField.focus( function() {
			fundraisingField.attr("alt", fundraisingField.val());
			fundraisingField.val("");
		});
		promoField.blur( function() {
			if($(this).val()=="") {
				fundraisingField.val(fundraisingField.attr("alt"));
				fundraisingField.attr("alt", "");
			}
		});
	}
	
	
	// Stuff you wouldn't usually do
	$("font").attr("color", ""); // Remove any obtrusive formatting to let the themes do thier job
	$(".sublevel").each( function() { if($("li", this).length<1) $(this).remove(); } ); // If empty nav lists were generated, delete them
	$("body").attr("id", $(".site").attr("id"));
	$(".product_image a.thickbox").removeAttr("id");
	$(".productlink01").click( function() { window.location = $("a", this).attr("href"); });
	$(".productlink02").click( function() { window.location = $("a", this).attr("href"); });
	$(".productlink03").click( function() { window.location = $("a", this).attr("href"); });
	$(".textBold").removeClass("textBold");
});

// Sampler :: General Functions
function samplerFieldsAvailable() {
	samplerType = $("#labelSampler").attr("class");
	if($(".tp-text").length<1) return false; // there's no text to choose from
//	if($(".tp-font").length!=1) return false; // there's no font to choose from or there are multiples
//	if($(".tp-picture").length!=1) return false; // there's no picture to choose from or there are multiple to choose from
//	if(samplerType.match("multi") && getNumTextLines()<2) return false; // It's two lines but not two text field types available
//	if(samplerType.match("single") && getNumTextLines()>1) return false // It's a single line but two text fields are available
	
	return true;
}

// Sampler :: Functions that Flash calls
function getCurrentLabelText() {
	//return $("td:contains('Characters')").next().find("input").val();

	numLinesText = $(".tp-text[type!='hidden']").length;
	if(numLinesText>1) {
		allText = "";
		for(i=0;i<numLinesText;i++) {
			var obj = $(".tp-text:eq(" + i.toString() + ") td:eq(1) input");
            if(obj!=null && obj.length > 0){
                allText += obj.val();
				if((i+1)<numLinesText) allText += "~";
			}
		}

		return allText;
	} else {
		if($(".tp-text td:eq(1) input").val()!="")	
		return $(".tp-text td:eq(1) input").val();
		else
		return "";
	}

}
function getCurrentPicture() {
	//return $("td:contains('Name For Label')").next().find("input").val();
	imgSource = $(".tp-picture td:eq(1) img:first").attr("src");
	if(!imgSource) 
	{
	    imgSource="none.jpg";
	}
	else
	{
	    ind = 0;
	    lastind = 0;
	    while(ind >= 0)
	    {
	        ind = imgSource.indexOf("/", ind+1);
	        if(ind > lastind)
	        {
	            lastind = ind;
	        }
	    }
	    imgSource = imgSource.substring(lastind+1, imgSource.length);
	}
	return imgSource;
	//return $(".tp-picture td:eq(1) img:first").attr("src");
}
function getCurrentLabelFont() {
	fontName="Blackjack";
	if($(".tp-font").length>0) fontName = $(".tp-font td:eq(1) img:first").attr("alt");
	if(fontName == "NSW/ACTSchool"){fontName="NSWSchool";}
	if(fontName == "NTSchool"){fontName="VICSchool";}
	if(fontName == "WASchool"){fontName="VICSchool";}
	return (fontName ? fontName : "Blackjack");
}
function getNumTextLines() {
	//return $(".tp-text").length;
numLinesText = $(".tp-text").length;

var len = 0;
		for(i=0;i<numLinesText;i++) {
			obj = $(".tp-text:eq("+i+") td:eq(1) input");
			if(obj.length>0){
				len++;
}
}
	return len;
}

// Sampler :: Calls to functions in flash
function updateSamplerText() {
	thisMovie("labelSamplerINS").updateText(getCurrentLabelText());
}
function updateSamplerPicture() {
	thisMovie("labelSamplerINS").updatePic(getCurrentPicture());
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}


function printPage() { 
	if (window.print) 	{ 
		window.print();
	} else { 
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = ""; 
	} 
}

function updateStackInfoFadeIn(num)
{
	$(".flash-stack-info ul li:eq("+parseInt(num)+") h2").css("left", "auto");
	$(".flash-stack-info ul li:eq("+parseInt(num)+") h2").css("position", "static");
	//alert("fade in: " + num);
	$(".flash-stack-info ul li:eq("+parseInt(num)+")").removeClass("hide").fadeIn("medium", function(){
		$(".flash-stack-info").css("z-index", "1001");$(".flash-stack").css("z-index", "1000");
	//$(".flash-stack-info ul li:eq("+parseInt(num)+") h2").css("visibility", "visible");
	});
}

function updateStackInfoFadeOut(num)
{
	$(".flash-stack-info ul li:eq("+parseInt(num)+") h2").css("position", "absolute");
	$(".flash-stack-info ul li:eq("+parseInt(num)+") h2").css("left", "-9999em");
	$(".flash-stack-info").css("z-index", "1000");
	$(".flash-stack").css("z-index", "1001");
		
	$(".flash-stack-info ul li:eq("+parseInt(num)+")").fadeOut("fast", function(){
		$(this).addClass("hide");
	});
}

function getList()
{
	var holder = $(".flash-stack-info ul").html();
	holder = holder.toLowerCase();
	
	var counter = 0;
	$(".flash-stack-info ul li").each(
		function(i)
		{
			counter++;
		}
	)
	
	var indexFrom = -1;
	var first = 0;
	var second;
	
	var newString = "";
	//count the number of images in the listing
	var imgCounter = 0;
	$(".flash-stack-info img").each(
		function(i)
		{
			imgCounter++;
		}
	)
	
	//alert("imgCounter: " + imgCounter);
	var stackImageCounter = 0;
	for(var i = 0;i<imgCounter;i++)
	{
		first = holder.indexOf("<img", indexFrom+1);
		indexFrom = first;
		second = holder.indexOf(">", indexFrom);
		//trying = holder.substring(0, second) + "/>" + holder.substring(second+1, holder.length);
		var img = holder.substring(first, second) + "/>";
		if(img.match("stack-image"))
		{
			newString = newString + "<li>" + holder.substring(first, second).replace("imgservices", "ImgServices") + "/></li>";
			stackImageCounter++;
		}
		//alert("trying: " + newString);
	}
	
	//newString = newString.replace("'", "~");
	newString = newString.replace(/'/g, "~");
	newString = newString.replace(/"/g, "~");
	
	if($.browser.msie)	{
		var first = 0;
		var second = 0;
		var indexFrom = -1;
		//add in ' ' marks aroung the height and the width attributes of the images/
		//get the list of images
		var imgList = getElementsByClassName(document, "img", "stack-image");
		for(var k=0;k<stackImageCounter;k++)
		{
			first = newString.indexOf("height=", indexFrom+1);
			indexFrom = first;
			var currHeight = parseInt($(imgList[k]).css("height").replace("px", ""));
			var height = "";
			if(currHeight > 99)
			{
				height = newString.substring(first+7, first+10);
				newString = newString.substring(0, first+7) + "~" + height + "~" + newString.substring(first+10, newString.length) ;
			}
			else
			{
				height = newString.substring(first+7, first+9);	
				newString = newString.substring(0, first+7) + "~" + height + "~" + newString.substring(first+9, newString.length) ;
			}
		}
		indexFrom= -1;
		for(var l=0;l<stackImageCounter;l++)
		{
			first = newString.indexOf("width=", indexFrom+1);
			indexFrom = first;
			var currWidth = parseInt($(imgList[l]).css("width").replace("px", ""));
			var width = "";
			if(currWidth > 99)
			{
				width = newString.substring(first+6, first+9);
				newString = newString.substring(0, first+6) + "~" + width + "~" + newString.substring(first+9, newString.length);
			}
			else
			{
				width = newString.substring(first+6, first+8);
				newString = newString.substring(0, first+6) + "~" + width + "~" + newString.substring(first+8, newString.length);
			}
		}
		newString = newString = newString.replace(/class=stack-image/g, "");
	}
	
	//alert("the List: \n" + newString);
	
	return newString;
}



function enableJSClass()
{
	document.body.className = "jsEnabled";
}

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};