var g_initialHeight = 0;
var g_currentProduct = 0;
var g_currentColor = 0;
var g_currentColorPretty = 0;
var g_currentAngle = 0;
var g_currentImage = 0;
var g_currentZoomImage = 0;
var g_currentColorTitle = 0;
var g_currentProductCategory = 0;
var g_currentProductName = 0;
var preAvaiText = $('div.actConfirm p:eq(0) strong').text();
var preColorText = $('div.actConfirm p:eq(1) strong').text();
var preSizeText = $('div.actConfirm p:eq(2) strong').text();
var preInStockDate = $('div.actConfirm p.date strong').text();
var preTab = $('div.actSizes .tabs-selected').text().substr(0,1);
var isJibbitz = false;
var isBisn = false;
var isColorOnly = false;

$(function() {

	// check for size chip sz_oneeu.png and apply a custom style to parent div
	var hasSizeChipOne = ".prodDet div.actSizes ul li label img";
	var srcSizeChipOne = "[src*='sz_oneeu.png']";	
	if ($(hasSizeChipOne).is(hasSizeChipOne + srcSizeChipOne)) {
		$('div.actSizes').addClass('szSizeOne');
	} 
	
	$("div.prod img").attr("lang", lang);
	
	// Grab the title tag from the right column color chips 
	// Create a UL/LI with the results and place them into the details tab 
	if ($("#details .colorsBox").is("#details .colorsBox:has(ul)")) {
	} else {
		buildColorsBox("#details .colorsBox");
	}
	function buildColorsBox(sColorNode) {
		var uList = $('<ul class="colorsNode">');
		$(".actions ul.actColors label img").each(function() {
		uList.append($("<li>" + $(this).attr("title") + "</li>"));
		});
	$(sColorNode).append(uList);
	}
	
	// take alt and title attributes from color chips, size chips and thumbnails and create a span with the same info next to it
	// this has to be done as the tooltip function clears both so the browser does not show the standard alt/title display
	$('ul.actColors li ul li').each(function() {
		var alt = $(this).children('label').children('img').attr('alt');
		var title = $(this).children('label').children('img').attr('title');
		$(this).append('<span title="' + alt + '">' + title + '</span>');
	});
	/*$('ul.actSizes li').each(function() {
		var alt = $(this).children('label').children('img').attr('alt');
		var title = $(this).children('label').children('img').attr('title');
		$(this).append('<span title="' + title + '">' + alt + '</span>');
	});*/
	$('div.thumbs ul li').each(function() {
		var alt = $(this).children('img').attr('alt');
		var title = $(this).children('img').attr('title');
		$(this).append('<span title="' + alt + '">' + title + '</span>');															
	});
	
	// set global variables
	// Strip out spaces of the product name
	var productName = $('div.desc h1#pname').text();
    
    // uppercase all the letter which follow after a space
    var spacePos;
    // use a counter to not produce a infinite loop
    var saftyCounter = 0;
    while((spacePos = productName.indexOf(" ")) != -1) {
        productName = productName.replace(/ .?/, productName.charAt(spacePos+1).toUpperCase());
        saftyCounter++;
        if(saftyCounter > 100) { break; }
    }
    
   	// uppercase all the letter which follow after a hyphen
   	var spacePos;
   	// use a counter to not produce a infinite loop
   	var saftyCounter = 0;
   	while((spacePos = productName.indexOf("-")) != -1) {
       	productName = productName.replace(/-.?/, productName.charAt(spacePos+1).toUpperCase());
       	saftyCounter++;
       	if(saftyCounter > 100) { break; }
   	} 

	// strip out special characters
	productName = productName.replace(/[^a-zA-Z 0-9]+/g,'');

	// Lower case the first letter of the product name
	productName = productName.charAt(0).toLowerCase() + productName.substring(1);

	g_currentProduct = productName;
	g_currentPID = $('div.prodBx span.prodID').attr('title');
	// get selected color: this is the typical scenario
	g_currentColor = $('ul.actColors li ul li.sel span:first').attr('title');
	g_currentColorTitle = refineColor($('ul.actColors li ul li.sel span:first').text());
	
	// if no selected color: special case - could be an unavailable variation product (with suppressed color chip), but it was requested from the Product Detail Page 
	if (g_currentColor == null) { 
		g_currentColor = $('ul.actColors li ul span#unavailvariant').attr('title');
		g_currentColorTitle = refineColor($('ul.actColors li ul span#unavailvariant').text());
		//$('ul.actColors li:first').append('<span class="nfoTxt">This color is currently out of stock.</span>');
	}
	// if still null: special case - a master product, get the first color chip's color
	if (g_currentColor == null) { 
		g_currentColor = $('ul.actColors li ul li span:first').attr('title'); 
		g_currentColorTitle = refineColor($('ul.actColors li ul li span:first').text());
		$('ul.actColors li:eq(0) ul li:nth-child(1)').addClass('sel');
	}
	// if still null, it's for Jibbitz product that never has color swatches, defaulting it to "001"
	if (g_currentColor == null) { g_currentColor = "001"; isJibbitz = true; g_currentColorTitle = "Black"}
	
	
	g_currentProductCategory = refineCategory($('#crumbs a:eq(1)').text());
	g_curProdImgPresetAppendix = $.trim($('#imagePresetAppendix').text()).replace(' ', '', 'g');
	g_currentColorPretty = $('ul.actColors li ul li.sel span:first').text();
	g_currentAngle = $('div.thumbs ul li.sel span').attr('title');
	g_currentProductName = refineProduct($('div.desc h1#pname').text());
	
	g_currentImage = selectPictureSource(g_path+'product-images/standard/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
			scene7Path + "/" + standardImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
	g_currentZoomImage = selectPictureSource(g_path+'product-images/large/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
			scene7Path + "/" + standardImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + "-" + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
	
	// colors
	$('ul.actColors li ul li:nth-child(10n)').css('margin-right','0px');	
	$('ul.actColors li p').text(g_currentColorPretty);
    $('.prodFR h3').text(g_currentColorPretty);
	$('div.actConfirm p:eq(1) strong').text(g_currentColorPretty);
	// size
	$('div.actConfirm p:eq(2) strong').text($('ul.actSizes li.sel span').text());
	
	// on page load - variant without size attribute: get the currently selected color
	if ($('div.actSizes span#size').text() != "") {
		var curSzValue = $('div.actSizes span#size').text();
		isColorOnly = true;

		$('div.actConfirm p.size').hide();
		
		// update the pid field and enable add to basket if product is in stock
		updateVariationProduct({"variationSize": curSzValue, "variationColor": g_currentColor, "colorOnly": isColorOnly});
	}
	else {
		// on page load - update display of color and size chips for default selection
		updateVariationProduct({"variationSize": $('ul.actSizes li.sel input').attr("value"), "variationColor": g_currentColor, "onpageload": true});
	}
	
	// on page load - display default selected color's price and applicable promotions/tiered pricing applied (set in the PriceBook)
	if (!isJibbitz) {
		$('div.priceBL>div').css('display','none');
		$('#'+g_currentColor).css('display','block');
	}
	
	$('ul.actColors li ul li img').click(function() {		
		if($('div.notify table').hasClass('notified'))$('div.notify table').addClass('notified');
		if(!$(this).parents('li').hasClass('oos_inAllSizes')) {
			
			//maintain old size value to determine whether we need to default
			var outsize = $('ul.actSizes li.sel span').attr('title');
			
			if($(this).parents('li').hasClass('oos')) {
				// color is not in stock - disable add to cart and unselect size
				$('div.actConfirm button').attr('disabled','disabled').css('cursor','default').children('span').addClass('disabled').parents('button').siblings('strong').show();
				$('a#addtowishlistlink').attr('style', 'display: none;');
				$('div.ps p.acts span.dis').attr('style', 'display: block;');
			}
			
			// slide up the BISN if it was slide down previously and reset
			if($('#bisnresponse').html() != '') {
				$('#bisnresponse').html('');
				$('#bisnform').css('display','block');
			}
			if(!$(this).parents('li').hasClass('bisn')) {
				$('#bisncallout, div.notify').slideUp('fast');
			}
			
			$('ul.actColors li ul li').removeClass('sel');
			$(this).parents('li').addClass('sel');
			
			var curClr = $(this).parent().siblings('span').text();
	        $('.prodFR h3').text(curClr);
			var curClrID = $(this).parent().siblings('span').attr('title');
			var curClrTitle = refineColor($(this).parent().siblings('span').text());
			// display selected color in both color selectors
			$('label[for=color-'+curClrID+']').children('img').parents('li').addClass('sel');
			
			$('ul.actColors li p').text(curClr);
			// replace all the thumbs images if another color is selected
			$('div.thumbs ul li img').each(function () {
					var $this = $(this);
					var angle = $this.siblings('span').attr('title');
					var picSrc = selectPictureSource(g_path+'product-images/mini/' + g_currentPID + '_' + angle + '_' + curClrID + '.jpg', 
							scene7Path + "/" + miniImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + curClrTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + curClrID.toUpperCase() + "_" + angle.toUpperCase() + ".jpg")
					$this.attr('src', picSrc);
				}
			);
			
			var g_currentImage;
			if ($('div.prod').hasClass('zoom')) {
				g_currentImage = selectPictureSource(g_path+'product-images/large/' + g_currentPID + '_' + g_currentAngle + '_' + curClrID + '.jpg', 
						scene7Path + "/" + largeImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + curClrTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + curClrID.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
			} else {
				g_currentImage = selectPictureSource(g_path+'product-images/standard/' + g_currentPID + '_' + g_currentAngle + '_' + curClrID + '.jpg', 
						scene7Path + "/" + standardImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + curClrTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + curClrID.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
			}
			
			$('div.prod img').attr('src', g_currentImage);
			$('div.actConfirm p:eq(1) strong').text(curClr);
			
			// display selected color's price and applicable promotions/tiered pricing applied (set in the PriceBook)
			$('div.priceBL>div').css('display','none');
			$('#'+curClrID).css('display','block');
			
			// get the currently selected colors <div class="actSizes">
			var curSzValue;
			isColorOnly = false;
			if ($('div.actSizes span#size').text() == "") {
		        if ($('div.actSizes .tabs-selected').text().substr(0,1) == preTab && $('ul.actSizes li.sel span').text() != "") {
					curSzValue = $('ul.actSizes li.sel input').attr("value");
		        	$('div.actConfirm p:eq(0) strong').text(preAvaiText);
					$('div.actConfirm p:eq(2) strong').text($('ul.actSizes li.sel span').text());
		        	//$('div.actConfirm p:eq(2) strong').text(preSizeText);
		        }
		        else {
		        	$('div.actConfirm p:eq(0) strong').text("");
		        	$('div.actConfirm p:eq(2) strong').text("");
		        }
			}
			else {
				curSzValue = $('div.actSizes span#size').text(); // for variant without size attribute
				isColorOnly = true;
			}
			var curClrValue = $('ul.actColors li ul li.sel input').attr("value");
			
			// update the pid field and enable add to basket if product is in stock
			updateVariationProduct({"variationSize": curSzValue, "variationColor": curClrValue, "colorOnly": isColorOnly});
			
			// set new email a friend link
			setColorEmailFriend(g_currentPID,curClrID,outsize);
			
		}
		SetSizeTabPreselected (); // trigger selection of preselected size tab on color change
	});
	// sizes
	if (!$('ul.actSizes').hasClass('mixed') && !$('ul.actSizes').hasClass('alpha')) {
		$('ul.actSizes li:nth-child(10n)').css('margin-right','0px');
	}
	
	///////H4 "PLEASE SELECT" TXT HIDE ////////
	function hideH4() {
		$('div.actSizes h4').css('display','none');
	}
	//If only one tab is present, hide h4
	if ($('div.actSizes ul.tabs li a').length <= 1){
		hideH4();
	}
    
    updateTooltips();
    var selectedTab = 0; // no tabs selected
    //var isSelected = false;	
    // try to select default size panel
	try {
		// mainCategory could be Women, Men, Girls, Boys, Outlet 
		var mainCategory = category;
		mainCategory = mainCategory.split("-",1);
		var TanbsCount = $('div.actSizes ul.tabs li').length;		
		if ( TanbsCount > 2 && (mainCategory == 'men' || mainCategory == 'women') ) {			
			for(var i = 0;i<$('div.actSizes ul.tabs li a').length;i++) {
				var tabName = $('div.actSizes ul.tabs li a:eq('+i+')').text();
				if(tabName == mainCategory){
					selectedTab = i+1;
					hideH4();
					//isSelected = true;
					break;
				}
			 }
		} else if (TanbsCount == 2) {			
			hideH4();	//If men or women's gender path is known, hide H4
			selectedTab = 1;
			//isSelected = true;
		}
		//if(($('div.actSizes ul.tabs li a:contains("Women")').length) && (!isSelected)){
			//selectedTab = $('div.actSizes ul.tabs li').index($('div.actSizes ul.tabs li a:contains("Women")').parent());
		//}
	} catch(e) {
		// an error happened :-(
		alert(e);
	}
	
	
	/////// Preselection of a single size option if a product only has a single size  ////////
	function SetSizeTabPreselected () {
		var sizeTabsActivePreselected = false;
		var sizeTabsContainer = $('div.actSizes div.tabs-container').not('.tabs-hide'); // define if there is active container
		if (sizeTabsContainer.length == 0) {			
			sizeTabsContainer = $('div.actSizes'); // define container for products without tabs
		}
		if (sizeTabsContainer.length > 0) {
			var sizeTabsActive = $(sizeTabsContainer[0]).find('ul.actSizes li').not('.oos'); // filter out-of-stock stock sizes
			var sizeTabsActiveQty = sizeTabsActive.length;
			if (sizeTabsActiveQty >= 1) {
				$(sizeTabsActive).each(function(index) {			
					if (this.style.display == 'none') {	 // filter hidden sizes			
						sizeTabsActiveQty --;
					}
					if (sizeTabsActiveQty == 1) sizeTabsActivePreselected = this;
					if (sizeTabsActiveQty == 0) sizeTabsActivePreselected = false;
				});		
			} 
		}
		if (sizeTabsActivePreselected != false) {
			$(sizeTabsActivePreselected).find('img').trigger('click'); // trigger selection of preselected size tab
		};
	}

		
	var sizeTabs = $('div.actSizes').tabs(selectedTab+1, {
		onShow: function () {
		// set the current shown tab
        $('#sizeLabelInput').val($('div.actSizes .tabs-selected img').attr("alt"));
        //alert ($('div.actSizes .tabs-selected').text());
		//alert($('ul.actColors li ul li.sel span').text() + ":" + preColorText);
		
        if ($('div.actSizes .tabs-selected').text().substr(0,1) == preTab && $('ul.actColors li ul li.sel span').text() == preColorText && $('ul.actSizes li.sel span').text() == preSizeText && (preAvaiText == "In Stock" || preAvaiText == "Pre-Order" || preAvaiText == "Backorder") ) {
			if (isBisn) $('div.actConfirm div.notify').slideDown('fast');
			
        	$('div.actConfirm button:disabled').removeAttr('disabled').css('cursor','pointer').children('span').removeClass('disabled').parents('button').siblings('strong').hide();
    		$('a#addtowishlistlink').attr('style', 'display: block;');
    		$('div.ps p.acts span.dis').attr('style', 'display: none;');
    		$('div.actConfirm p:eq(0) strong').text(preAvaiText);
        	$('div.actConfirm p:eq(2) strong').text(preSizeText);
    		if((preAvaiText == "Pre-Order" || preAvaiText == "Backorder") && preInStockDate != "") {
    			$('div.actConfirm p.date strong').text(preInStockDate);
    			$('div.actConfirm p.date').attr('style', 'display: block;');
    		}
        }
        
        else {
			// slide up the BISN if it was slide down previously
			$('#bisncallout, div.notify').slideUp('fast');
			
            $('div.actConfirm button').attr('disabled','disabled').css('cursor','default').children('span').addClass('disabled').parents('button').siblings('strong').show();
    		$('a#addtowishlistlink').attr('style', 'display: none;');
    		$('div.ps p.acts span.dis').attr('style', 'display: block;');
        	$('div.actConfirm p:eq(0) strong').text("");
        	$('div.actConfirm p:eq(2) strong').text("");
    		$('div.actConfirm p.date').attr('style', 'display: none;');

			// get the currently selected colors <div class="actSizes">
			var curSzValue;

	        if ($('div.actSizes .tabs-selected').text().substr(0,1) == preTab && $('ul.actSizes li.sel span').text() != "") {
				curSzValue = $('ul.actSizes li.sel input').attr("value");
	        	$('div.actConfirm p:eq(0) strong').text(preAvaiText);
				$('div.actConfirm p:eq(2) strong').text($('ul.actSizes li.sel span').text());
	        }
	        else {
	        	$('div.actConfirm p:eq(0) strong').text("");
	        	$('div.actConfirm p:eq(2) strong').text("");
	        }
	
			var curClrValue = $('ul.actColors li ul li.sel input').attr("value");
			// update the pid field and enable add to basket if product is in stock
			updateVariationProduct({"variationSize": curSzValue, "variationColor": curClrValue});
        }
        SetSizeTabPreselected (); // trigger selection of preselected size tab on gender tab open
    }});
	
    // set the tab which is initially shown
    $('#sizeLabelInput').val($('div.actSizes .tabs-selected img').attr("alt"));
    
	$('ul.actSizes li img').click(function() {
        var variant = getVariantByColorAndSize($('ul.actColors li ul li.sel span').attr('title'), $(this).parent().siblings('span').attr('title'));
        if (variant != undefined && variant.isBisn && !variant.inStock) {
        	isBisn = true;
        }else{
        	isBisn = false;
        }
		if(!$(this).parents('li').hasClass('oos') || isBisn) {
			// slide up the BISN if it was slide down previously and reset
			if($('#bisnresponse').html() != '') {
				$('#bisnresponse').html('');
				$('#bisnform').css('display','block');
			}
			if(!isBisn) {
				$('#bisncallout, div.notify').slideUp('fast');				
			}
			if($('div.notify table').hasClass('notified'))$('div.notify table').addClass('notified');

            $('ul.actSizes li').removeClass('sel');
    		$(this).parents('li').addClass('sel');
    		var curSz = $(this).parent().siblings('span').text();
    		$('div.actConfirm p:eq(2) strong').text(curSz);
    		var curSzValue = $('ul.actSizes li.sel input').attr("value");
    		var curClrValue = $('ul.actColors li ul li.sel input').attr("value");
    		
    		updateVariationProduct({"variationSize": curSzValue, "variationColor": curClrValue});
    		
    		setSizeEmailFriend(g_currentPID, $(this).parent().siblings('span').attr('title'), $('ul.actColors li ul li.sel input').attr("value") != undefined ? $('ul.actColors li ul li.sel input').attr("value") : $('ul.actColors li ul li span:first').attr('title'));
    		
        }
		
	});
	
	///////ERROR MESSAGE ON CLICK///////
	// Show "Add to Cart" click error messaging when btn is disabled only if product has more than one tab and the error message is not already present
	$('div.actConfirm p:has(button:disabled)').click(function(){
		if ($('div.actConfirm button').attr('disabled') && (!$('div.actSizes').hasClass('.taberror')) && ($('div.actSizes ul.tabs li a').length >= 2)){
			$('div.actSizes').addClass('taberror');
			$('div.actSizes h4').addClass('taberror');
			$('div.actions h3.actSizes').addClass('taberror');		
		}
	});
	//set click listener on tab to hide error message if present on tab click
	$('div.actions ul.tabs li img, ul.actSizes li').click(function() {
		if($('div.actSizes').hasClass('.taberror')){
			$('div.actSizes').removeClass('taberror');
			$('div.actSizes h4').removeClass('taberror');
			$('div.actions h3.actSizes').removeClass('taberror');
		}
	});
	
	// confirm
	$('div.actConfirm p:has(button:disabled) strong').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		fixPNG: true,
		top: 25,
		left: -130,
		extraClass: 'dis',
		bodyHandler: function() {
			if (!isColorOnly) {
	        	//c = $('#addcartselectcolor').text();
				//If  you need a tooltip error that request a user to select a color 
				//use the above div div#addcartselectcolor
				
	        	c = $('#addcartselectsize').text();
	    	}
			else {
				c = $('#addcartselectcolorsize').text();
			}
	        return c;
    	}
	});
	
	$('div.actConfirm p:has(button:disabled)').mouseover(function(){
		isBisn = $('div.actConfirm span#isBisn').text() == "true" ? true : false;

		if(isJibbitz && isBisn) {
			$('div.actConfirm div.notify').slideDown('fast');
		}
	});
	
	$('div.ps p.off a').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		fixPNG: true,
		top: -80,
		left: -9,
		extraClass: 'img',
		bodyHandler: function() {
			c = '<img src="' + g_path + $(this).attr('name') + '" alt="" title="" width="90" height="60"/>';
			return c; 
    }
	});
	// content
	// product box
	$('div.thumbs ul li img').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		fixPNG: true,
		top: 25,
		left: -9
	});
	var isIE6 = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 7);
    
    // ugly hack to make the img line work in internet explorer 6
    if(isIE6) {
	   $('.prodBx').css('position', 'static');
	}
    
    $('<img>').appendTo('div.prod').addClass('p').attr('src', g_currentImage).attr('alt','').attr('title','').attr("lang", lang);
	
    if(isIE6) {
    	//fix for review write page were absolute breaks layout
    	if($('#review-product-detail').length > 0) {
        	$('#review-product-detail div.prodBx').css('position', 'relative');
        }else{
        	$('.prodBx').css('position', 'absolute');
        }
    }
    
    $('div.prod').onImagesLoad({
		itemCallback: firstImageLoaded
	});

    $('div.thumbs ul li img:not(div.thumbs ul li.sel img)').fadeTo('fast', 0.5);
	var hiCfg = {
		sensitivity: 3,
		interval: 75,
		over: thumbOvr,
		timeout: 300,
		out: thumbOut
	};
	$('div.thumbs ul li img').hoverIntent(hiCfg);
	
	//enlarge
	$('div.prodF a.enlargeButton').click(function() {
		$('div.zoomedPicker').show();
		$('div.prod').empty().addClass('zoom');
		$('div.prodBx').animate({
			width: '720px',
			height: '432px'
		}, 'slow', function() { 
			$('div.prodF a.enlargeButton').animate({
				width: '0px'
			}, 'fast');
			$('div.zoomCls').animate({
				width: '67px'
			}, 'fast');
			$('div.zoomBrdr').fadeIn('fast');
			
			g_currentColor = $('ul.actColors li ul li.sel span').attr('title');
			g_currentColorTitle = refineColor($('ul.actColors li ul li.sel span').text());
			if (g_currentColor == null) { 
				g_currentColor = $('ul.actColors li ul li span').attr('title'); 
				$('ul.actColors li ul li:nth-child(1)').addClass('sel');
				// for Jibbitz product that never has color swatches
				g_currentColorTitle = refineColor($('ul.actColors li ul li span').text());
				if (g_currentColor == null) { 
					g_currentColor = "001";
					g_currentColorTitle = "Black";
				}
			}
			
			g_currentAngle = $('div.thumbs ul li.sel span').attr('title');
			
			var picSrc = selectPictureSource(g_path+'product-images/large/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
					scene7Path + "/" + largeImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
			$('<img>').appendTo('div.zoom').addClass('z').attr('src', picSrc).attr('alt','').attr('title','').attr('lang', lang);;
			$('div.zoom img.z').onImagesLoad({
				itemCallback: zoomImageLoaded
			});
		});
		$('div.prod').animate({
			width: '718px',
			height: '399px',
			marginLeft: '1px',
			marginTop: '1px'
		}, 'slow');
		$('div.thumbs').animate({
			marginRight: '85px'
		}, 'slow');
		$('div.desc').animate({
			marginTop: '442px'
		}, 'slow');
        //$('div.zoomCls img').css('visibility', 'visible');
	});
	
	// close enlarge
	$('div.zoomCls img').click(function() {
		$('div.zoomedPicker').hide();
		$('div.prod').empty().removeClass('zoom');
		$('div.zoomBrdr').fadeOut('fast');
		$('div.prodF a.enlargeButton').animate({
			width: '67px'
		}, 'fast');
		$('div.zoomCls').animate({
			width: '0px'
		}, 'fast', function() {
			$('div.prodBx').animate({
				width: '503px',
				height: g_initialHeight + 32 + 'px'
			}, 'slow');
			$('div.prod').animate({
				width: '503px',
				height: g_initialHeight,
				marginLeft: '0px',
				marginTop: '0px'
			}, 'slow', function() {
				g_currentColor = $('ul.actColors li ul li.sel span').attr('title');
				g_currentColorTitle = refineColor($('ul.actColors li ul li.sel span').text());
				if (g_currentColor == null) { 
					g_currentColor = $('ul.actColors li ul li span').attr('title'); 
					g_currentColorTitle = refineColor($('ul.actColors li ul li span').text());
					$('ul.actColors li ul li:nth-child(1)').addClass('sel');
					// for Jibbitz product that never has color swatches
					if (g_currentColor == null) { g_currentColor = "001"; g_currentColorTitle = "Black"}
				}
				
				g_currentAngle = $('div.thumbs ul li.sel span').attr('title');
				
				var picSrc = selectPictureSource (g_path+'product-images/standard/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
						scene7Path + "/" + standardImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
				$('<img>').appendTo('div.prod').addClass('p').attr('src', picSrc).attr('alt','').attr('title','').attr("lang", lang);
				$('div.prod img.p').onImagesLoad({
					itemCallback: imageLoaded
				});
			});
			$('div.thumbs').animate({
				marginRight: '4px'
			}, 'slow');
			$('div.desc').animate({
				marginTop: g_initialHeight + 42 + 'px'
			}, 'slow');
		});
	});
	
	$('div.descCont').tabs();
	
	if($('ul.actSizes li.sel').length == 0) {
		var curClr = $('.actColors li.sel label').attr('for');
		var curSz = $('.actSizes li[class!=oos]:visible label:first-child').attr('for');
		var curLink = $('#emailfriendlink').attr('href');
		if (curClr != undefined && curSz != undefined) {
			var curClrArray = curClr.split('-');
			var curSzArray = curSz.split('-');
			//$('#emailfriendlink').attr('href',curLink + '-' + curClrArray[1] + '-' + curSzArray[1]);
			setColorEmailFriend(g_currentPID,curClrArray[1],curSzArray[1]);
		}
	}
	
	$('#bisnLink').click(function(){
		var bisnpidurl = $(this).attr('href');
		$('#bisnresponse').load(bisnpidurl, function(){
			if($('#bisnresponse div').hasClass('notified')) {
				$('#bisncallout').slideUp('fast');
				$('#bisnform').css('display','none');
			}
		});
		return false;		
	});
	
	$('#startWriteReview').click(function(){
		$('#reviewCurrentColor').attr('value',$('ul.actColors li ul li.sel input').attr("value"));
	});
	
	//Review feedback response panel
	$('.reviewFeedback a.button, #reviewFlag a.button').click(function(){
		$.get($(this).attr('href'),'',
			function(data){$('#feedbackResponsePanel').html(data);
			tb_show('Review Feedback','#TB_inline?height=500&width=510&inlineId=feedbackResponsePanel');
		});
		
		return false;
	});
	
	$('div.review-paging-bar :select').change(function(){$(this).parent('form').submit()});
	
	$('#productReviewSummary').html($('#reviewsteaser').html());
	
	$('#productReviewSummary a.readAll').click(function() {
		$('div.descCont').triggerTab($('div.descCont li').index($('div.descCont li div.reviews').parent())+1); 
	    return false;
	});
	
		
	//if slider summary is available create hover display
	if ($('#overallsliders').length > 0 ) {	
		$('#productReviewSummary').mouseenter(function() {
			if($(this).hasClass('reviewSummaryExpanded')== false && $('#productReviewSummary .reviewSummaryExpanded').length == 0){
				$('#productReviewSummary').append($('#productReviewSummary').clone(true).addClass('reviewSummaryExpanded'));
				$('#productReviewSummary .reviewSummaryExpanded').mouseleave(function() {
					$(this).remove();
				});
				$('#productReviewSummary .reviewSummaryExpanded').css( { 
					
					border : '1px solid #787878' 
				} );
				//$('#productReviewSummary .reviewSummaryExpanded #overallsliders').show();
			}
		});
	}
	
	SetSizeTabPreselected (); // trigger selection of preselected size tab on page load
	
	if (fgEnabled == 'true') {
		// If size tab is preselected, show the correct size chart link
		if ($('ul.tabs li:eq(1)').hasClass('tabs-selected')) {
			$('a#onesc').show();
		} else if ($('ul.tabs li:eq(2)').hasClass('tabs-selected')) {
			$('a#twosc').show();
		} else if ($('ul.tabs li:eq(3)').hasClass('tabs-selected')) {
			$('a#threesc').show();
		} else if ($('ul.tabs li:eq(4)').hasClass('tabs-selected')) {
			$('a#foursc').show();
		}
		
		//Add text to size chart link and lightbox for first size tab
		if ($('ul.tabs li:eq(1) a').hasClass('wChart')) {
			$('a#onesc, div#fgLbCont div:nth-child(1) h1').append(wScLbTitle);
		} else if ($('ul.tabs li:eq(1) a').hasClass('mChart')) {
			$('a#onesc, div#fgLbCont div:nth-child(1) h1').append(mScLbTitle);
		} else if ($('ul.tabs li:eq(1) a').hasClass('cChart')) {
			$('a#onesc, div#fgLbCont div:nth-child(1) h1').append(cScLbTitle);
		} else if ($('ul.tabs li:eq(1) a').hasClass('jChart')) {
			$('a#onesc, div#fgLbCont div:nth-child(1) h1').append(jScLbTitle);
		}
		//Add text to size chart link and lightbox for second size tab
		if ($('ul.tabs li:eq(2) a').hasClass('wChart')) {
			$('a#twosc, div#fgLbCont div:nth-child(2) h1').append(wScLbTitle);
		} else if ($('ul.tabs li:eq(2) a').hasClass('mChart')) {
			$('a#twosc, div#fgLbCont div:nth-child(2) h1').append(mScLbTitle);
		} else if ($('ul.tabs li:eq(2) a').hasClass('cChart')) {
			$('a#twosc, div#fgLbCont div:nth-child(2) h1').append(cScLbTitle);
		} else if ($('ul.tabs li:eq(2) a').hasClass('jChart')) {
			$('a#twosc, div#fgLbCont div:nth-child(2) h1').append(jScLbTitle);
		}
		//Add text to size chart link and lightbox for third size tab
		if ($('ul.tabs li:eq(3) a').hasClass('wChart')) {
			$('a#threesc, div#fgLbCont div:nth-child(3) h1').append(wScLbTitle);
		} else if ($('ul.tabs li:eq(3) a').hasClass('mChart')) {
			$('a#threesc, div#fgLbCont div:nth-child(3) h1').append(mScLbTitle);
		} else if ($('ul.tabs li:eq(3) a').hasClass('cChart')) {
			$('a#threesc, div#fgLbCont div:nth-child(3) h1').append(cScLbTitle);
		} else if ($('ul.tabs li:eq(3) a').hasClass('jChart')) {
			$('a#threesc, div#fgLbCont div:nth-child(3) h1').append(jScLbTitle);
		}
		//Add text to size chart link and lightbox for fourth size tab
		if ($('ul.tabs li:eq(4) a').hasClass('wChart')) {
			$('a#foursc, div#fgLbCont div:nth-child(4) h1').append(wScLbTitle);
		} else if ($('ul.tabs li:eq(4) a').hasClass('mChart')) {
			$('a#foursc, div#fgLbCont div:nth-child(4) h1').append(mScLbTitle);
		} else if ($('ul.tabs li:eq(4) a').hasClass('cChart')) {
			$('a#foursc, div#fgLbCont div:nth-child(4) h1').append(cScLbTitle);
		} else if ($('ul.tabs li:eq(4) a').hasClass('jChart')) {
			$('a#foursc, div#fgLbCont div:nth-child(4) h1').append(jScLbTitle);
		}
		
		// Change the size chart link/lightbox when a new size tab is selected
		$('ul.tabs li:eq(1) a').click(function () {
			$('a.conversionChart').hide();
			$('a#onesc').show();
		});
		$('ul.tabs li:eq(2) a').click(function () {
			$('a.conversionChart').hide();
			$('a#twosc').show();
		});
		$('ul.tabs li:eq(3) a').click(function () {
			$('a.conversionChart').hide();
			$('a#threesc').show();
		});
		$('ul.tabs li:eq(4) a').click(function () {
			$('a.conversionChart').hide();
			$('a#foursc').show();
		});
	}
	
});

////This is for Tracking customer purchase data via Invodo for pages with Video
//Invodo Tracking add to cart
function myCartAddHandler() {
	try {
	Invodo.conversion("cartAdd",
	{ SKU: "${pdict.Product.ID}" });
	} catch(e) {
		// Ignore this; it should never happen,
	}
	};

//Invodo Tracking add to cart and check out
function myAddFunction() {
	myCartAddHandler()
	return true;
	};
//////////////////////////////////////////////////////////
	
function thumbOvr() {
	$('div.thumbs ul li.sel').removeClass('sel');
	$('div.thumbs ul li img:not(div.thumbs ul li.sel img)').fadeTo('fast', 0.5);
				
	g_currentColor = $('ul.actColors li ul li.sel span').attr('title');
	g_currentColorTitle = refineColor($('ul.actColors li ul li.sel span').html());
	if (g_currentColor == null) { 
		g_currentColor = $('ul.actColors li ul li span').attr('title');
		g_currentColorTitle = refineColor($('ul.actColors li ul li span').text());
		$('ul.actColors li ul li:nth-child(1)').addClass('sel');
		// for Jibbitz product that never has color swatches
		if (g_currentColor == null) { g_currentColor = "001"; g_currentColorTitle = "Black"}
	}
	
	g_currentAngle = $(this).siblings('span').attr('title');
	
	if ($('div.prod').hasClass('zoom')) {
		var picSrc = selectPictureSource(g_path+'product-images/large/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
				scene7Path + "/" + largeImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
		$('<img>').appendTo('div.zoom').addClass('z').attr('src', picSrc).attr('alt','').attr('title','').attr('lang', lang);
		$('div.loading').show();
		$('div.zoom img.z').onImagesLoad({
			itemCallback: newZoomImageLoaded
		});
	} else {
		var picSrc = selectPictureSource(g_path+'product-images/standard/' + g_currentPID + '_' + g_currentAngle + '_' + g_currentColor + '.jpg', 
				scene7Path + "/" + standardImagePreset + g_curProdImgPresetAppendix + "/" + g_currentProductCategory + g_currentColorTitle + "-" + g_currentProductName + "-_" + g_currentPID.toUpperCase() + "_" + g_currentColor.toUpperCase() + "_" + g_currentAngle.toUpperCase() + ".jpg");
		$('<img>').appendTo('div.prod').addClass('p').attr('src', picSrc).attr('alt','').attr('title','').attr('lang', lang);
		$('div.prod img.p').onImagesLoad({
			itemCallback: newImageLoaded
		});
	}
	
	$(this).parents('li').addClass('sel');
	$(this).fadeTo('fast', 0.99);
}
function thumbOut() {
	
}

function firstImageLoaded() {
	g_initialHeight = $('div.prod img').height();
	$('div.prod').animate({
		height: g_initialHeight + 'px'
	}, 'fast', function() {
		$('div.prod img.p').fadeIn('fast').removeClass('p');
		$('div.desc').animate({
			marginTop: g_initialHeight + 42 + 'px'
		}, 'fast', function() {
			$(".video_iconT").show();
		});
	});
	
}
function imageLoaded() {
	$('div.prod img.p').fadeIn('fast').removeClass('p');
}
function newImageLoaded() {
	$('div.prod img.p').fadeIn('fast', function() { $('div.prod img:first-child').remove(); }).removeClass('p');
}
function zoomImageLoaded() {
	$('div.zoom img.z').fadeIn('fast').removeClass('z');
}
function newZoomImageLoaded() {
	$('div.loading').hide();
	$('div.zoom img.z').fadeIn('fast', function() { $('div.zoom img:first-child').remove(); }).removeClass('z');
}

function updateVariationProduct(attrs) {
	// hide the no size available message, just in case it was shown before
	$('div.actSizes span#noSizeAtAll').hide();
	
	// remove swatches which are not available and mark swatches which are out of stock
    // as out of stock
    if(attrs["variationColor"] !== undefined && attrs["variationColor"] !== "") {	
    	// get all size line items
    	jQuery("ul.actSizes li").each(function () {
    		var $this = jQuery(this);
    		var size = $this.children("span").attr("title");
    		var variant = getVariantByColorAndSize(attrs["variationColor"], size);
    		// if the variant exists
    		if(variant !== undefined) {
	    		// show it, just in case it is not shown yet
	    		$this.show();
	    		// if the variant is in stock
	   			if(variant.inStock) {
	   				$this.removeClass("oos");
	   				$this.removeClass("bisn");
    			}else if(variant.isBisn) {
    				$this.removeClass("oos");
	   				$this.addClass("bisn");
	   			} else {
	   				// not in stock not bisn so out of stock
	   				$this.removeClass("bisn");
	   				$this.addClass("oos");
	   			}
	   			//alert(variant.avStatus + ":" + variant.inStockDate);
	   		// if the variant does not exists in this size mark it as hidden
    		} else {
    			$this.hide();
    		}
    	});

    	// show the no size available message, if all sizes are not available in the gender tab
    	if ($('div.actSizes .tabs-container ul li:visible').length == 0 && !attrs["onpageload"] && !($('div.actSizes ul.tabs-nav li:eq(0)').hasClass('tabs-selected'))) {
    		$('div.actSizes span#noSizeAtAll').attr('style', 'display: block;');
    	}
    }

    /* if it's on page load, just return and skip the rest
    if (attrs["onpageload"]) {
    	return;
	}*/
    
    // do nearly the same with size
    if(attrs["variationSize"] !== undefined && attrs["variationSize"] !== "") {
    	// get all color line items
    	jQuery("ul.actColors li ul li").each(function () {
    		var $this = jQuery(this);
    		//$this.show();
    		var color = $this.children("span").attr("title");
    		var variant = getVariantByColorAndSize(color, attrs["variationSize"]);
    		// if the variant exists
    		if(variant !== undefined) {
	    		// show it, just in case it is not shown yet
	    		$this.show();
	    		// if the variant is in stock
	   			if(variant.inStock) {
	   				$this.removeClass("oos");
	   				$this.removeClass("bisn");
    			}else if(variant.isBisn) {
    				$this.removeClass("oos");
	   				$this.addClass("bisn");
	   			} else {
	   				// not in stock not bisn so out of stock
	   				$this.removeClass("bisn");
	   				$this.addClass("oos");
	   			}
    		} else {
    			$this.removeClass("bisn");
    			$this.addClass("oos");
    		}
    	});
    }
    
	// if not all properties are set in attrs just return.
	if (!attrs["colorOnly"]) {
	    for(var property in attrs) {
			if(attrs[property] === undefined || (property != "colorOnly" && attrs[property] == "")) {
				updateTooltips();
				return;
			}
		}
	}
	// append all the attribute values to one string
	// size: "L", color: "010"
	// becomes: "L010"
	// FIXME: What happens if I have color 00, 0 and size 00, 0. What would '000' be?
	var attrToStr = function(attrObj) {
		var result = "";
		jQuery.each(variations.variations.attributes, function(){
			result += attrObj[this.id];
		});
		return result;
	}
	
	var avaiText = "";
	var inStockDate = "";
	var attrsStr = attrToStr(attrs);
	var found;
	for (var i=0; i< variations.variations.variants.length; i++) {
		variant = variations.variations.variants[i];
		if (attrToStr(variant.attributes) === attrsStr) {
			found = variant;
			avaiText = found.avStatus;
			inStockDate = found.inStockDate;
			isBisn = found.isBisn;
		}
	}
		
	// TODO: add other price display
	// jQuery("#price").html(found.price);
	if(found === undefined || !found.inStock && !found.isBisn) {
		$('div.actConfirm span.backorderInfo').attr('style', 'display: none;');
		$('div.actConfirm p:eq(0) strong').removeClass('bisn').html($('#oostext').text()).addClass('oos'); // <a href="">(?)</a>');
		$('div.actConfirm button').attr('disabled','disabled').css('cursor','default').children('span').addClass('disabled').parents('button').siblings('strong').show();
		$('a#addtowishlistlink').attr('style', 'display: none;');
		$('div.ps p.acts span.dis').attr('style', 'display: block;');
		
		if (found != undefined && !found.inStock) {
			jQuery("#pid").val(found.id);
			preAvaiText = $('div.actConfirm p:eq(0) strong').text();
			preColorText = $('div.actConfirm p:eq(1) strong').text();
			preSizeText = $('div.actConfirm p:eq(2) strong').text();
			preInStockDate = $('div.actConfirm p.date strong').text();
			preTab = $('div.actSizes .tabs-selected').text().substr(0,1);
		}
	
	} else if (!found.inStock && found.isBisn) {
		
		//if bisn show bisn box
		if (isBisn) $('#bisncallout, div.notify').slideDown('fast');
		$('div.actConfirm span.backorderInfo').attr('style', 'display: inline;');
		$('div.actConfirm p:eq(0) strong').removeClass('oos').html(avaiText).addClass('bisn'); // <a href="">(?)</a>');
		$('div.actConfirm button').attr('disabled','disabled').css('cursor','default').children('span').addClass('disabled').parents('button').siblings('strong').show();
		$('a#addtowishlistlink').attr('style', 'display: none;');
		$('div.ps p.acts span.dis').attr('style', 'display: block;');
		
		if (!found.inStock) {
			jQuery("#pid").val(found.id);
			preAvaiText = $('div.actConfirm p:eq(0) strong').text();
			preColorText = $('div.actConfirm p:eq(1) strong').text();
			preSizeText = $('div.actConfirm p:eq(2) strong').text();
			preInStockDate = $('div.actConfirm p.date strong').text();
			preTab = $('div.actSizes .tabs-selected').text().substr(0,1);
		}		
		
	} else {
		jQuery("#pid").val(found.id);
		$('div.actConfirm span.backorderInfo').attr('style', 'display: none;');
        // update the price display
        jQuery("#standard-price-label").text((found.pricing.currencyCode == "USD" ? "$" : "") + ( found.pricing.standard == 0.0 ? found.pricing.sale : found.pricing.standard));
		$('div.actConfirm p:eq(0) strong').removeClass('oos bisn').text(avaiText).parents('p').children('a').remove();
		$('div.actConfirm button:disabled').removeAttr('disabled').css('cursor','pointer').children('span').removeClass('disabled').parents('button').siblings('strong').hide();
		$('div.ps p.acts span.dis').attr('style', 'display: none;');
		$('a#addtowishlistlink').attr('style', 'display: block;');
		preAvaiText = $('div.actConfirm p:eq(0) strong').text();
		preColorText = $('div.actConfirm p:eq(1) strong').text();
		preSizeText = $('div.actConfirm p:eq(2) strong').text();
		preInStockDate = $('div.actConfirm p.date strong').text();
		preTab = $('div.actSizes .tabs-selected').text().substr(0,1);
	}
	
	// Display date for Pre-Order
	if((avaiText == "Pre-Order" || avaiText == "Preorden")) {
		if (inStockDate != "") {
			$('div.actConfirm p.date strong').text(inStockDate);
			$('div.actConfirm p.date').attr('style', 'display: block;');
		}
		else{
			$('div.actConfirm p.date').attr('style', 'display: none;');
		}
		$('div.actConfirm span.preorderInfo').attr('style', 'display: inline;');
		$('div.actConfirm span.preorderInfo').siblings('strong').addClass('preorder');
		$('div.actConfirm p.add button span').removeClass('addButton');
		$('div.actConfirm p.add button span').addClass('addButtonPO');
		$('div.actConfirm p.addCo button span').removeClass('addCoButton');
		$('div.actConfirm p.addCo button span').addClass('addCoButtonPO');
	}
	else {
		$('div.actConfirm p.date').attr('style', 'display: none;');
		$('div.actConfirm span.preorderInfo').attr('style', 'display: none;');
		$('div.actConfirm span.preorderInfo').siblings('strong').removeClass('preorder');
		$('div.actConfirm p.add button span').removeClass('addButtonPO');
		$('div.actConfirm p.add button span').addClass('addButton');
		$('div.actConfirm p.addCo button span').removeClass('addCoButtonPO');
		$('div.actConfirm p.addCo button span').addClass('addCoButton');
	}
	
    updateTooltips();
}

function updateTooltips() {
    $('ul.actColors li ul li img:not(ul.actColors li ul li.oos img)').tooltip({
        delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 25,
        left: -9,
        extraClass: 'chip',
        bodyHandler: function() {
            c = $(this).parent().siblings('span').text();
            return c;
        }
    });

    $('ul.actColors li ul li.oos img').tooltip({
        delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 25,
        left: -130,
        extraClass: 'oos',
        bodyHandler: function() {
            c = $(this).parent().siblings('span').text();

            if (isColorOnly) {
            	c = c + $('#ooscolornotbisn').text();
            }
            else {
            	c = c + $('#ooscolorsize').text();
            }
            return c;            
    	}
    });
    
    $('ul.actColors li ul li.bisn img').tooltip({
        
    	delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 25,
        left: -130,
        extraClass: 'oos',
        bodyHandler: function() {
            c = $(this).parent().siblings('span').text();

           	c = c + $('#ooscolorbisn').text();

            return c;            
    	}    	  	
    	
    });
    
	$('ul.actSizes li img:not(ul.actSizes li.oos img)').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		fixPNG: true,
		top: 25,
		left: -9
	});
	
    $('ul.actSizes li.oos img').tooltip({
    	delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 25,
        left: -130,
        extraClass: 'oos',

    	bodyHandler: function() {
        	c = $('#oossize').text();
        	c = c + $(this).parent().siblings('span').text();

        	isBisn = false; // must update this value when mouseover
            c = c + $('#ooscolorsize').text();
            
            return c;
    	}
    });
    
    $('ul.actSizes li.bisn img').tooltip({
    	delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 25,
        left: -130,
        extraClass: 'oos',

    	bodyHandler: function() {
        	c = $('#oossize').text();
        	c = c + $(this).parent().siblings('span').text();

        	isBisn = true; // must update this value when mouseover
        	c = c + $('#oossizebisn').text();

            return c;
    	}
    });
    
}

function getVariantByColorAndSize(color, size) {
	for (var i=0; i< variations.variations.variants.length; i++) {
		var variant = variations.variations.variants[i];
		// alert(attrToStr(variant.attributes)+" === "+attrsStr);
		if (variant.attributes.variationColor === color && variant.attributes.variationSize === size) {
			return variant;
		}
	}
	return undefined;
}

function setColorEmailFriend(pid,cid,size) {
	// add the color id to the email a friend link
	var currentLink = $('#emailfriendlink').attr('href');
	var linkArray = currentLink.split('&');	
	var sizeCode = '';
	if(typeof size == 'undefined' || $('ul.actSizes li span[title='+size+']').parent('li').hasClass('oos')) {
		sizeCode = $('.actSizes li[class!=oos] label:first-child').attr('for').split('-')[1];
	}else{
		sizeCode = size;
	}
	var updateLink = linkArray[0] + '&' + linkArray[1] + '&' + 'pid=' + pid.toUpperCase() + '-' + cid + '-' + sizeCode; 
	$('#emailfriendlink').attr('href',updateLink);
}

function setSizeEmailFriend(pid, size, cid) {
	// add the size to the email a friend link
	var currentLink = $('#emailfriendlink').attr('href');
	var linkArray = currentLink.split('&');
	var updateLink = linkArray[0] + '&' + linkArray[1] + '&' + 'pid=' + pid.toUpperCase() + '-' + cid + '-' + size; 
	$('#emailfriendlink').attr('href',updateLink);
}

function selectPictureSource(legacySource, scene7Source) {
	if (useScene7 == true || useScene7 == 'true') {
		var productImagesPath = $.trim($('#productImagesPath').text());
		return productImagesPath + scene7Source;
	}
	return legacySource;
}

function refineCategory(category) {
	category = category.indexOf("Search results for") != -1 ? "" : ($.trim(category) + "-");
	category = category.replace(/[^a-zA-Z 0-9 -]+/g,'').replace(' ', '-', 'g');
	if (category == "-") {
		category = "";
	}
	return category;
}

function refineColor(color) {
	color = $.trim(color).replace(/[^a-zA-Z 0-9 - \/]+/g,'').replace(' ', '-', 'g').replace('/', 'and', 'g');//TODO: localize and
	return color;
}

function refineProduct(product) {
	product = $.trim(product).replace(/[^a-zA-Z 0-9 -]+/g,'').replace(' ', '-', 'g');
	return product;
}

/*
 * jQuery 'onImagesLoaded' plugin v1.1.0
 * Fires callback functions when images have loaded within a particular selector.
 *
 * Copyright (c) Cirkuit Networks, Inc. (http://www.cirkuit.net), 2008.
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * For documentation and usage, visit "http://includes.cirkuit.net/js/jquery/plugins/onImagesLoad/1.1/documentation/"
 */
(function($){$.fn.onImagesLoad=function(options){var self=this;self.opts=$.extend({},$.fn.onImagesLoad.defaults,options);self.bindEvents=function($imgs,container,callback){if($imgs.length===0){if(self.opts.callbackIfNoImagesExist&&callback){callback(container);}}else{var loadedImages=[];if(!$imgs.jquery){$imgs=$($imgs);}$imgs.each(function(i,val){$(this).bind('load',function(){if(jQuery.inArray(i,loadedImages)<0){loadedImages.push(i);if(loadedImages.length==$imgs.length){if(callback){callback(container);}}}}).each(function(){if(this.complete||this.complete===undefined){this.src=this.src;}});});}};var imgAry=[];self.each(function(){if(self.opts.itemCallback){var $imgs;if(this.tagName=="IMG"){$imgs=this;}else{$imgs=$('img',this);}self.bindEvents($imgs,this,self.opts.itemCallback);}if(self.opts.selectorCallback){if(this.tagName=="IMG"){imgAry.push(this);}else{$('img',this).each(function(){imgAry.push(this);});}}});if(self.opts.selectorCallback){self.bindEvents(imgAry,this,self.opts.selectorCallback);}return self.each(function(){});};$.fn.onImagesLoad.defaults={selectorCallback:null,itemCallback:null,callbackIfNoImagesExist:false};})(jQuery);
