jQuery.noConflict();

// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
	jQuery("#product-tabs a").click(function(){
		selectPanel(jQuery(this).attr("href"));
	});
	
	productImagePreview();

	jQuery(".select-guide td:first-child, .select-guide th:first-child").addClass("first-child");

	var lh = location.hash;
	if(lh.search("load") == 1)
	{
		selectPanel(lh);
	} else {
		selectPanel(jQuery("#product-tabs li a:first").attr("href"));
	}

	if(ie6)
	{
		DD_belatedPNG.fix(".product-image img, #product-tabs li");
	}
});

if(ie6)
{
	jQuery("head").append('<script type="text/javascript" src="/includes/DD_belatedPNG.js"></script>');
}

Shadowbox.init({
	language: "en",
	players: ["flv","swf","iframe"]
});

function productImagePreview()
{
	jQuery(".choose-series dl a").hover(function(){
		
		//No point in swaping the image if there's only 1
		var rev = jQuery(this).attr("rev");				
		if(jQuery("#" + rev + " dd").length != 1)
		{	
			/* No cache of images (for testing)
			var d = new Date();
			var sec = d.getSeconds();
			?"+sec;
			*/
			
			var image = "/products/welding_protection/images/section/" + jQuery(this).attr("rel") + ".png";
			var alt = jQuery(this).text();
			var selector = "#" + rev + " .product-image img";
			
			var i = new Image();
			
			if(ie6==false) jQuery(selector).attr("src","/products/welding_protection/images/product/loading.gif");
			
			//http://stackoverflow.com/questions/280049/javascript-callback-for-knowing-when-an-image-is-loading
			i.onload = function (){			
				//IE hack
				if (document.attachEvent)
				{
					jQuery(selector).attr({src: image, alt: alt});
				} else {
					jQuery(selector).fadeOut(100,
					function()
					{	
						jQuery(selector).attr({src: image, alt: alt}).fadeIn();
					});
				}
			
			};
			i.src = image;
		}
	});
}

function selectPanel(panel)
{
	var panel = panel.replace("load-", "");
	
	//Hides panel where the ID = the href of the previously selected anchors tab
	var current = jQuery("#product-tabs .selected a").attr("href");
	current = current.replace("load-", "");
	jQuery(current).css("display","none");

	//Removes .selected class from previously selected item
	jQuery("#product-tabs .selected").removeClass("selected");

	//Fades in panel where ID = href of anchor's tab
	//IE hack
	if (document.attachEvent)
	{
		jQuery(panel).css("display","block");
	} else {
		jQuery(panel).fadeIn(400);
	}	

	//Adds .selected class to parent LI
	panel = panel.replace("#", "#load-");
	var a = jQuery(jQuery("#product-tabs a[href='"+panel+"']"));
	a.parent().addClass("selected");
		
	//Tracks click in GA
	pageTracker._trackEvent(window.location.pathname, "Tab Click", a.text(), 1);	
}

function changeLocUrl()
{
	var f = document.modelSelectForm;    
	for(i=0; i<f.url.length; i++)
	{
		if(f.url[i].selected)
		{
			var val = f.url[i].value;	  
			valarr = val.split("#");	  
			window.location = val;
			if(valarr[0] == window.location.pathname)
			{	  
				selectPanel(location.hash);
			} 
		}
	}
}
