var imagePath = "/products/welding_protection/images/product/individual/";
var threeSixtyPath = "/products/welding_protection/swf/360-view/";
var globalIndex = 0;

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");		

		//Tracks click in GA
		pageTracker._trackEvent(window.location.pathname, "Tab Click", jQuery(this).text(), 1);

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

//Hierarchy
//updateData() > displayShadowbox()
//Updates price and title. Then calls image updating functions
function updateData(index)
{
	globalIndex = index;
	
	//Grab values from arrays used on page. Get specific item via global index
	var title = productTitles[globalIndex];
	var price = productPrices[globalIndex];
	var number = productNumbers[globalIndex];
	var imagesArray = productImages[globalIndex].split(",");
	var src = imagePath + "small/" + imagesArray[0];
	var mediumSource = src.replace("small", "medium");
	var largeSource = src.replace("small", "large");

	//Toggle product title
	jQuery("#product-title").fadeOut(100,
	function()
	{	
		jQuery("#product-title").html(title).fadeIn();
	});	
	
	//Toggle product price
	jQuery("#product-price").fadeOut(100,
	function()
	{	
		if(price != "$0.00")
		{
			jQuery("#product-price").html(price).fadeIn();
		}
	});	
	
	//Display product stock number
	if(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("");
	}
	
	//Fadein main image
	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();
	});

	//Bind click to main pnroduct image
	jQuery("#product-image-height img").unbind("click").click(function() {
		displayShadowbox(largeSource,"clickimage")
    });

	//Display overview video link if array is present
	var t = jQuery("#product-image-gutter a");	
	if(typeof productVideos != "undefined")
	{
		if(productVideos[globalIndex] != "")
		{		
			t.html("Product Overview Video");
			t.attr("href","http://www.youtube.com/v/" + productVideos[globalIndex] + "?autoplay=1");
			t.attr("rel","shadowbox;width=640;height=480");
			t.attr("title",productTitles[globalIndex]);
			Shadowbox.setup("#product-image-gutter a, a[rel='shadowbox']", {});
			t.fadeIn(200);
		} else {
			t.fadeOut(200);
		}		
	} else {
		t.hide(200);
	}
		
	//Updates available images
	var images = new Array();
	images = productImages[globalIndex].split(",");
			
	jQuery("#product-image li").fadeOut(200,
	function()
	{
		jQuery("#product-image ul").html("");
		
		//Add for random cases where there isn't anything to fade out. Create faux element to fade out for - jQuery("#product-image li").fadeOut(200,
		if(images.length == 1 && productThreeSixtyView[globalIndex] == "")
		{
			jQuery("#product-image ul").append("<li>");
		}
		
		//Place individual additional views. Skips first one according to logic from Miller
		for (var i=1, len=images.length; i<len; ++i)
		{
			fullpath = imagePath + 'small/' + images[i];
			fullpathescaped = "'" + fullpath + "'";
			li = '<li><img src="' + fullpath + '" id="img-'+ i +'" onclick="displayShadowbox(' + fullpathescaped + ',\'clickimage\')" /></li>';
			jQuery("#product-image ul").append(li);
		}
			
		//Place 360 view at end
		if(productThreeSixtyView[globalIndex] != "")
		{
			video = "'" + threeSixtyPath + productThreeSixtyView[globalIndex] + "'";
			li = '<li><img src="/products/welding_protection/images/product/individual/small/360-icon.png" onclick="displayShadowbox('+video+',\'clickvideo\')" /></li>';
			jQuery("#product-image ul").append(li);
		}
	
		jQuery("#product-image ul li").fadeIn();
	});
	
	//Tracks click in GA
	pageTracker._trackEvent(window.location.pathname, "Available Designs", title, 1);
}

function displayShadowbox(source,iniatedfrom)
{
	//Uses iniatedfrom, Didn't want to update function call on each of the AA pages. This also makes it so shadowbox doesn't popup on page load.
	if(iniatedfrom)
	{
		if(iniatedfrom == "clickvideo")
		{		
			href = source;
			w = 500;
			h = 500;
			playertype = "iframe";
		} else if(iniatedfrom == "clickimage") {		
			var largeSource = source.replace("small", "large");
			href = largeSource;
			w = "";
			h = "";
			playertype = "img";
		}
		
		Shadowbox.open({
			content:    href,
			player:     playertype,
			title:      productTitles[globalIndex],
			height:     h,
			width:      w
		});
		
		//Tracks click in GA
		pageTracker._trackEvent(window.location.pathname, "Additional Views", href, 1);
	}
}

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

function initializeSlider()
{	
	jQuery("#carousel_ul li").click(function(){
		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(){
		if(sliderInUse == "")
		{
		currentSlide--;
		slide("left");
		}
	});


	jQuery("#right_scroll img").click(function(){
		if(sliderInUse == "")
		{
		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");
	});
}

var sliderInUse = ""; //Flag for slider to stop multiple clicks from occurring

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

	sliderInUse = what; //Set flag

	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(){
		sliderInUse = ""; //Remove flag
	});
}

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);
}
