jQuery.noConflict();
     
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
	initializeTabs();
	initializeSlider();
	correctBackgroundPosition();	
	insertResourceText();
	gaTagLinks();
	if(ie6) DD_belatedPNG.fix("#product-tabs li");	
	jQuery("div#panel-resources ul:last").addClass("last-child");
});

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

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

function initializeTabs()
{	
	//Adds first child class
	jQuery("#product-tabs li:first-child").addClass("first-child");
	
	jQuery("#product-tabs a").click(function(){
		//Hides panel where the ID = the href of the previously selected anchors tab
		jQuery(jQuery("#product-tabs .selected a").attr("href")).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
		jQuery(jQuery(this).attr("href")).fadeIn(400);
	
		//Adds .selected class to parent LI
		jQuery(this).parent().addClass("selected");		

		//Cancels out anchors href
		return false;
	});
}

//Hierarchy
//updateData() > updateImage()
//updateData() > updateAvailableImages()

//Updates price and title. Then calls image updating functions
function updateData(what)
{
	var title = productTitles[what];
	var price = productPrices[what];
	var number = productNumbers[what];
	var imagesArray = productImages[what].split(",");
	var src = "/products/welding_protection/images/product/individual/small/" + imagesArray[0];
	var availableImages = productImages[what];

	//jQuery("#product-title").html(title);
	jQuery("#product-title").fadeOut(100,
	function()
	{	
		jQuery("#product-title").html(title).fadeIn();
	});	
	
	//jQuery("#product-price").html(price);
	jQuery("#product-price").fadeOut(100,
	function()
	{	
		if(price != "$0.00")
		{
			jQuery("#product-price").html(price).fadeIn();
		}
	});	
	
	if(number != "")
	{
		//jQuery("#product-number").html(number);
		jQuery("#product-number").fadeOut(100,
		function()
		{	
			if(isNaN(number))
			{
				jQuery("#product-number").html(number).fadeIn();
			} else {
				jQuery("#product-number").html("#" + number).fadeIn();
			}
		});		
	} else {
			jQuery("#product-number").html("");
	}
	
	updateAvailableImages(productImages[what]);
	updateImage(src,title);
}

//Updates #product-image-height
function updateImage(source,title)
{
	var mediumSource = source.replace("small", "medium");
	var largeSource = source.replace("small", "large");

	jQuery("#product-image a").attr("title",title);
	jQuery("#product-image a").attr("href",largeSource);

	Shadowbox.setup("#product-image a, a[rel='shadowbox']", {});

	jQuery("#product-image-height img").fadeOut(200,
	function()
	{
		jQuery("#product-image-height img").attr("src","/products/welding_protection/images/product/loading.gif");
		jQuery("#product-image-height img").attr("src",mediumSource).fadeIn();
	});
}

//Updates available images
function updateAvailableImages(input)
{
	var images = new Array();
	images = input.split(",");

	jQuery("#product-image li").fadeOut(200,
	function()
	{
		jQuery("#product-image ul").html("");

		for (var i=0, len=images.length; i<len; ++i)
		{
			li = '<li><img src="/products/welding_protection/images/product/individual/small/' + images[i] + '" id="img-'+ i +'" onclick="updateImage(this.src)" /></li>';
			jQuery("#product-image ul").append(li);
		}

		jQuery("#product-image ul li").fadeIn();
	});
}

//Slider
var currentSlide = 1;
var totalSlides;
var visibleSlides = 5;

function initializeSlider()
{	
	jQuery("#carousel_ul li").click(function(){
		//updateData(this);
		updateData(jQuery("#carousel_ul li").index(this));
	});

	totalSlides = jQuery("#carousel_ul li").size();

	if(totalSlides <= visibleSlides)
	{
		jQuery("#right_scroll img").css("display","none");
	}

	jQuery("#left_scroll img").click(function(){
		currentSlide--;
		slide("left");
	});


	jQuery("#right_scroll img").click(function(){
		currentSlide++;
		slide("right");
	});
}

function correctBackgroundPosition()
{
	//Flexible backgroundPosition top
	var psHeight = jQuery("#posstatment").height();
	//http://www.javascriptkit.com/javatutors/conditionalcompile.shtml
	/*@cc_on
	   /*@if (@_jscript_version <= 5.7)
		if (document.attachEvent)
		{
			psHeight = psHeight - 15;
		}
	   /*@end
	@*/	
	var bgTop = "left " + (psHeight - 15)+"px";	
	jQuery("#main").css("background-position",bgTop);
}

function insertResourceText()
{
	jQuery("#panel-resources img").each(function(i){											 
		jQuery(this).parent().parent().append("<div>" + jQuery(this).attr("alt") + "</div>");											 
	});
	
	jQuery("#panel-resources ul li:first-child").each(function(i){											 
		jQuery(this).addClass("first-child");
	});
}

function slide(where,what){
	//testing
	//jQuery("#available-designs h3").html(currentSlide)

	if(currentSlide == (totalSlides+1)-visibleSlides)
	{
		jQuery("#left_scroll img").fadeIn();
		jQuery("#right_scroll img").fadeOut();
	} else {
		jQuery("#right_scroll img").fadeIn();
	}

	if(currentSlide == 1)
	{
		jQuery("#left_scroll img").fadeOut();
	} else {
		jQuery("#left_scroll img").fadeIn();
	}

	var item_width = jQuery("#carousel_ul li").outerWidth() + 10;

	var current_position = parseInt(jQuery("#carousel_ul").css("left"));

	if(where == "left"){
	    var left_indent =  current_position + item_width;
	}else{
	    var left_indent = current_position - item_width;
	}

	jQuery("#carousel_ul:not(:animated)").animate({"left": left_indent},500,function(){});
}

function changeLocUrl()
{
	var f = document.modelSelectForm; 
	for(i=0; i<f.url.length; i++)
	{
		if(f.url[i].selected)
		{
			window.location = f.url[i].value;
		}
	}
}

function gaTagLinks()
{
	jQuery('#product-info a[href$=".pdf"]').click(function(){
		pageTracker._trackPageview(jQuery(this).attr("href"));		
	});	
}


function selectPanel(what)
{
	jQuery("#panel-details div").css("display","none");
	jQuery("#" + what).fadeIn(600);
}
