var setContent = function(){
    		
	try{
		if($('div#ProductImageThumbs').html().length <= 0){
		
			$('div#ProductImageThumbs').html($('iframe#ProductImageThumbsSrc').contents().find('#ProductImageThumbsList').html());	
			   
			$('a', 'div#ProductImageThumbs').hover(
				function(){
				    $('div#ProductImageThumbs a').removeClass('selected');
				   
				    var imageMainSrc = $('img', this).attr('src').replace('crosssell', 'detailnew');
				   
				    $('img.productImageMain').attr('src', imageMainSrc).fadeIn('slow');
				},
				function(){
				    $(this).addClass('selected');                 
			    }
			);     
	    
			$('a:first', 'div#ProductImageThumbs').addClass('selected');
		
		} else {
			clearInterval(setContent);	    	
		}
	} catch(err){		
		 return false;
	}
}

$(document).ready(function() {
   
    /* START Index page */
    $('div.refineSelectionContainer:not(.refineSelectionPaging)').hover(
        function(){
           
            if($('span.refineSelectionClearAll').is(':visible')){
                $('ul.refineSelectionList').css('padding-top', 0);
            }

            var leftPosition = $('h3.refineSelectionHeader', this).width();
            var leftOffSet = $('ul.refineSelectionList', this).width();
            leftPosition = leftPosition - leftOffSet;

            $('ul.refineSelectionList', this).stop(true, true).slideDown('slow').css('left', leftPosition);
        },
        function(){
            $('ul.refineSelectionList', this).stop(true, true).slideUp('slow');
        }
    );

    if($('span.refineSelectionClear:not(.refineSelectionClearAll)').is(':visible')){
        $('span.refineSelectionClearAll').css('display', 'block');           
    }

    /* Enables productThumbImage hover for all platforms except for iOS */
    if(!isiOS()){
        	$('#productThumbs td.productThumbsImage').stop(true, true).hover(
    	        function() {

    	            if($('ul:has(li.video)', this).length > 0){
    	                $('div.productThumbsImageOverlay ul li').css('width', '95px');
    	                $('ul li.zoom').css('background-position', 'right');
    	                $('ul li.video').css('background-position', 'left');
    	            }
    	            if($('ul:has(li.quickAdd)', this).length > 0){
    	                if($('ul:has(li.video)', this).length > 0){
    	                    $('div.productThumbsImageOverlay ul li').css('width', '58px');                                   
    	                    $('div.productThumbsImageOverlay ul li.quickAdd').css('width', '73px');
    	                } else if ($('ul:has(li.zoom)', this).length > 0){
    	                    $('div.productThumbsImageOverlay ul li').css('width', '95px');
    	                    $('ul li.zoom').css('background-position', 'right');
    	                    $('ul li.quickAdd').css('background-position', 'left');
    	                }
    	            }                                                           

    	            $('div.productThumbsImageOverlay', this).stop(true, true).fadeIn(400);           
    	        },
    	        function() {
    	            $('div.productThumbsImageOverlay', this).fadeOut('slow');           
    	        }
    	    );       	
    }
    
         
     $('div.thumbheader a').each(
        function() {   
            if($('span.moreColors').length){
                $('span.price', this).animate({'padding-bottom' : '2px'}, 400);
            }                                                                                 
        }
    );
     
     $('div.thumbheader a img').stop(true, true).hover(
            function() {       
                           
                var position = $(this).position();
                var bottomHeight = 17;
                                   
                // height offset
                var topAdjustment = ($('div.readyToShipInfo').height() + $('span.price').height() + bottomHeight);
                var topOffSet = (position.top - topAdjustment);                                           
                var topOffSetBottom = (topOffSet + (topAdjustment-$('span.price').height()));
   
                // left offset
                var leftOffSet = ((position.left - ($('div.readyToShipInfo').width()/2))+3.5);                   
               
                $('div.readyToShipInfo').stop(true, true).animate({top : topOffSet, left : leftOffSet, opacity : 1});
                $('div.readyToShipInfoBottom').stop(true, true).animate({top : topOffSetBottom, left : leftOffSet, opacity : 1});
                $('div.readyToShipInfo').stop(true, true).fadeIn(400);   
                $('div.readyToShipInfoBottom').stop(true, true).fadeIn(400);
               
            },
            function() {               
                $('div.readyToShipInfo').fadeOut('fast');
                $('div.readyToShipInfoBottom').fadeOut('fast');           
            }
        );
     
     $('div.thumbheader a').each(
    	        function() {   
    	            if($('span.moreColors').length){
    	                $('span.price', this).animate({'padding-bottom' : '2px'}, 400);
    	            }                                                                                 
    	        }
    	    );
  
     
    /* END Index page */
    /* START Product Detail page */
     
    $('ul#ProductImageYourZoomFeatures li:not(".zoom", ".viewLarger")').hover(
        function() {               

            var className = $(this).attr('class');   
            var position = $(this).position();
            var bottomHeight = 9;
           
            // height offset
            var rootElementHeight = $(this).height() + bottomHeight;                   
            var targetElementHeight = $('div.productImageYourZoomInfo.'+className).height();                   
            var heightOffSet = (targetElementHeight + rootElementHeight) * -1;

            // left offset
            var rootElementWidth = $('div.productImageYourZoomInfo').css('width');
            rootElementWidth = (parseInt(rootElementWidth, 10)/2);                   
            var leftOffSet = position.left - rootElementWidth;                           
           
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfo.'+className).stop(true, true).animate({top : heightOffSet, left : leftOffSet});
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfoBottom').stop(true, true).animate({left : leftOffSet});
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfo.'+className).stop(true, true).fadeIn(400);   
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfoBottom').stop(true, true).fadeIn(400);
        },
        function() {

            var className = $(this).attr('class');   
           
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfo.'+className).fadeOut('fast');
            $('#ProductImageYourZoom').find('div.productImageYourZoomInfoBottom').fadeOut('fast');           
        }
    );
   
    var currentTab = 0;
   
    function openTab(clickedTab) {
        var thisTab = $("ul#info-tabs li a").index(clickedTab);
        $("ul#info-tabs li a").removeClass("selected");
        $("ul#info-tabs li a:eq("+thisTab+")").addClass("selected");
        $("div.productInfo .info-tab").hide();
        $("div.productInfo .info-tab:eq("+thisTab+")").show();
        currentTab = thisTab;
    }

    $("ul#info-tabs li a").click(
        function(){
            openTab($(this));
            return false;
        }
    );   

    $('#info-tabs li a:first').addClass('selected');
    $('div.info-tab:first').stop(true, true).show();
   
    if($('div.detailitemlist').length){
   
        var thisList = '';                       
        var startString = '<li>';   
       
        $('div.detailitemlist li').removeAttr('sizcache');
        $('div.detailitemlist li').removeAttr('sizset');
       
        thisList = $('div.detailitemlist').html();
        thisList = thisList.replace(/<li>/i, '');
        thisList = thisList.replace(/<\/li>/gi, '');       
        thisList = thisList.replace(/<li>/gi, '</li><li>');       
        thisList = thisList.replace(/(<\/li><\/li>)/gi, '</li>');
        thisList = startString.concat(thisList);
        thisList = thisList.split('</li>');       
       
        var liCount = thisList.length;
        var splitAt = Math.round(liCount / 2);                   
       
        var newString = "<ul class='itemListLeft'>";
        for(i=0;i<liCount;i++){                       
            if(i == splitAt){
                newString += "</ul><ul class='itemListRight'>" + thisList[i] + "</li>";
            } else {
                newString += thisList[i] + "</li>";
            }                           
        }
        newString += "</ul>"
   
        $('div.detailitemlist').html(newString);
    }
   
    $("div.detailitemlist ul li a.size-fit").click(
        function(){
            openTab($('#tabhdr-size-fit'));   
            return false;
        }
    );
   
    $("div.detailitemlist ul li a.care").click(
        function(){
            openTab($('#tabhdr-care'));   
            return false;
        }
    );
   
    /* START Option Chip Selector */
    var ua = navigator.userAgent,
    clickEvent = (ua.match(/(iPhone|iPod|iPad)/i)) ? "touchstart" : "click";

    $("a.optionChip").not('.sizeFit').bind(clickEvent, function(e) {
        selectOption($(this));
        return false;
   });

   $('a.optionChip').not('.sizeFit').stop(true, true).hover(
        function(){

            var thisText = $(this).text().replace('sale', '');
            thisText = jQuery.trim(thisText);
           
            var thisClassNames = $(this).attr('class').split(' ');
            var thisType = thisClassNames[1].split('_');
            thisType = thisType[1];
           
            var compareText = $('span.optionSelected'+thisType).text().replace('sale', '');
            compareText = jQuery.trim(compareText);       

            var isSelected = $('span.optionSelected'+thisType).hasClass('selected');
                       
            if(thisText != compareText){           
                $('span.optionSelected'+thisType).hide();
                $('span.optionSelected'+thisType).html(thisText);
                $('span.optionSelected'+thisType).stop(true, true).fadeIn(500);
                if(isSelected){
                    $('span.optionSelected'+thisType).removeClass('selected');
                }
            }
           
            var elements = thisClassNames[2].split('_');
            var optionType = elements[1];
            var optionValue = elements[2];
           
            selectedValues[optionType] = optionValue;
           
            /* Out of Stock */
            $('a.optionChip').not('.sizeFit').each(               
                function(){           
                    var thisClass = $(this).attr('class').split(' ');
                    var elements = thisClass[2].split('_');
       
                    if (availableCombos.indexOf(getSelectKey(selectedValues, elements[1], elements[2])) == -1) {
                        $(this).addClass("outofstock");
                    } else {
                        $(this).removeClass("outofstock");
                    }
                }
            );
           
            /* Sale and Stock messaging */           
            //$('div.addToBasketMessaging')       
           
        },
        function(){
       
            var thisText = $(this).text().replace('sale', '');
            thisText = jQuery.trim(thisText);
           
            var thisClassNames = $(this).attr('class').split(' ');
            var thisType = thisClassNames[1].split('_');
            thisType = thisType[1];
           
            var compareText = $('span.optionSelected'+thisType).text().replace('sale', '');
            compareText = jQuery.trim(compareText);       

            var isSelected = $('span.optionSelected'+thisType).hasClass('selected');

            if($('a.option_'+thisType+'').hasClass('selected')){

                var thisSelectedChip = $('a.option_'+thisType+'').index($('a.option_'+thisType+'.selected'));
                var thisSelectedText = $('a.option_'+thisType+':eq('+thisSelectedChip+')').text().replace('sale', '');
                thisSelectedText = jQuery.trim(thisSelectedText);

                if(thisText != compareText){
                    $('span.optionSelected'+thisType).hide();
                    $('span.optionSelected'+thisType).html(thisText);
                    $('span.optionSelected'+thisType).stop(true, true).fadeIn(500);
                    if(isSelected){
                        $('span.optionSelected'+thisType).removeClass('selected');
                    }
                } else {
                    $('span.optionSelected'+thisType).text(thisSelectedText);
                    $('span.optionSelected'+thisType).addClass('selected');
                }
            } else {
                $('span.optionSelected'+thisType).html('');
            }
           
            /* Out of Stock */
           
            $('a.optionChip').not('.sizeFit').removeClass('outofstock');
            delete selectedValues[thisType];
           
            if($('a.optionChip').hasClass('selected')){
                $('a.optionChip.selected').each(
                    function(){
                       
                        var elementSelected = $(this).attr('class').split(' ');
                       
                        var elements = elementSelected[2].split('_');
                        var optionType = elements[1];
                        var optionValue = elements[2];
                       
                        selectedValues[optionType] = optionValue;   

                        $('a.optionChip').not('.sizeFit').each(               
                            function(){           
                                var thisClass = $(this).attr('class').split(' ');
                                var elements = thisClass[2].split('_');

                                if (availableCombos.indexOf(getSelectKey(selectedValues, elements[1], elements[2])) == -1) {
                                    $(this).addClass("outofstock");
                                } else {
                                    $(this).removeClass("outofstock");
                                }
                            }
                        );
                    }
                );
            }
        }
    );

   $('a.optionChip').not('.sizeFit').each(
        function(){
            if($(this).hasClass('option_COLR')){
               
                var borderColor = $(this).css('border-top-color');               
                if(borderColor == "rgb(228, 221, 192)"){
                    var bgColor = $(this).css('background-color');
                    $(this).css('border-color', bgColor);
                } else {
                    $(this).css('height', '14px')
                        .css('width', '23px');
                }
               
                var hasBackgroundImage = $(this).css('background-image');
                if(hasBackgroundImage != 'none' && hasBackgroundImage){
                    $(this).css('border', 'none')
                        .css('height', '16px')
                        .css('width', '25px');
                }
               
            }
           
            if(allOnSale && allSamePrice){
                $('span.sale').hide();
               
            }
        }
    );
   
    $('div.optionChipGroup a.sizeFit').stop(true, true).hover(
        function() {               

            var className = $(this).attr('class').split(' ');   
            var position = $(this).position();
            var bottomHeight = 13;
           
            // height offset
            var rootElementHeight = $(this).height() + bottomHeight;                   
            var targetElementHeight = $('div.productImageYourZoomInfo.'+className[0]).height();                   
            var heightOffSet = (targetElementHeight + rootElementHeight) * -1;

            // left offset
            var rootElementWidth = $('div.productImageYourZoomInfo.'+className[0]).css('width');
            rootElementWidth = (parseInt(rootElementWidth, 10)/2);                   
            var leftOffSet = position.left - rootElementWidth + 40;
           
            $('div.optionChipGroup').find('div.productImageYourZoomInfo.'+className[0]).stop(true, true).animate({top : heightOffSet, left : leftOffSet});
            $('div.optionChipGroup').find('div.productImageYourZoomInfoBottom').stop(true, true).animate({left : leftOffSet});
            $('div.optionChipGroup').find('div.productImageYourZoomInfo.'+className[0]).stop(true, true).fadeIn(400);   
            $('div.optionChipGroup').find('div.productImageYourZoomInfoBottom').stop(true, true).fadeIn(400);
        },
        function() {

            var className = $(this).attr('class').split(' ');   
           
            $('div.optionChipGroup').find('div.productImageYourZoomInfo.'+className[0]).fadeOut('fast');
            $('div.optionChipGroup').find('div.productImageYourZoomInfoBottom').fadeOut('fast');           
        }
    );   
   
    /* END Option Chip Selector */
   
    $('input[name=addToBasket]').click(
        function(){

            $('a.checkOutButton').hide('fast');
            $('div.addToBasketMessaging').hide('fast');
           
            var optionsCount = $('input[name=optionTypes]').val();
            var optionsSelCount = $('span.optionSelected.selected').size();
           
            if(optionsCount != optionsSelCount){
                
            	var trimThis = "";
                var optionsMessagingContentPreFix = $('span.optionTypeAddToBagMessagePrefix').html();
                var optionsMessagingContentSufFix = $('span.optionTypeAddToBagMessageSuffix').html();
                var optionsMessagingContent = "";                       
               
                $('span.optionSelected:not(.selected)').each(
                    function(index){                        
                    	trimThis = $(this).parent().find('span.optionTypeAddToBagMessage');                    	
                    	optionsMessagingContent += '<br />' + trimThis.html();                   
                    }
                )
               
                $('div.addToBasketOptionsMessagingContent').html(optionsMessagingContentPreFix + optionsMessagingContent + '<br />' + optionsMessagingContentSufFix);
                var topOffSet = ((($('div.addToBasketOptionsMessaging').height() / 2) * -1) + 8);

                $('div.addToBasketOptionsMessaging').stop(true, true).css('top', topOffSet).show('slow');                       
                return false;
            }               
        }
    );
   
    var currentCrossTab = 0;
   
    function openCrossSellTab(clickedTab) {
        var thisCrossTab = $("ul#crosssells-tabs li a").index(clickedTab);
        $("ul#crosssells-tabs li a").removeClass("selected");
        $("ul#crosssells-tabs li a:eq("+thisCrossTab+")").addClass("selected");
        $("div#crosssells .tab-content").hide();
        $("div#crosssells .tab-content:eq("+thisCrossTab+")").fadeIn(500);
        currentCrossTab = thisCrossTab;
    }

    $("ul#crosssells-tabs li a").click(
        function(){
            openCrossSellTab($(this));
            return false;
        }
    );
    
    if(!isiOS()){
	    var heightOffSet = 0;
	    $('div.crosssellimage').hover(       
	        function() {               
	            heightOffSet = ($(this).find('div.thumbheader a').height() + 2) * -1;
	           
	            $(this).find('div.thumbheader a').stop(true, true).animate({top : heightOffSet});
	            $(this).find('div.thumbheader a').stop(true, true).fadeIn(400);           
	        },
	        function() {
	            $(this).find('div.thumbheader a').fadeOut('slow');           
	        }
	    );
    }
        
    $("#crosssells div.tab-content:first").show();
   
    // Cross Sells Carousel
    var step = 1;
    var current = 0;
    var maximum = $('div.crosssellItem').size();
    var visible = 3;
    var speed = 900;
    var liSize = 176;
    var carousel_height = 528;

    var ulSize = liSize * maximum;  
    var divSize = liSize * visible; 

    $('div#crossSellContainer div.crosssellItem').css("height", liSize+"px").css("visibility", "visible").css("position", "relative");

    $('div#crossSellContainer').css("height", divSize+"px").css("overflow", "hidden").css("position", "relative");             

    $('#crosssells div.downArrow').click(function() {
        if(current + step < 0 || current + step > maximum - visible) {
            $('#crosssells div.downArrow').slideUp('slow');
            return;
        } else {
           
            if($('#crosssells div.upArrow:visible').length <= 0){
                $('#crosssells div.upArrow').slideDown('slow');
            }   
            current = current + step;
            $('div.crosssellItem').animate({top: -(liSize * current)}, speed, null);
            if((current + visible) == maximum){
                $('#crosssells div.downArrow').slideUp('slow');
            }
        }
        return false;
    });

    $('#crosssells div.upArrow').click(function() {
        if(current - step < 0 || current - step > maximum - visible) {
            $('#crosssells div.upArrow').slideUp('slow');
            return;
        } else {
            if($('#crosssells div.downArrow:visible').length <= 0 && current < maximum){
                $('#crosssells div.downArrow').slideDown('slow');
            }       
            current = current - step;               
            $('div.crosssellItem').animate({top: -(liSize * current)}, speed, null);
            if(current == 0){
                $('#crosssells div.upArrow').slideUp('slow');
            }           
        }
        return false;
    });

    if(maximum > 3){
        $('div.downArrow').show();
    }
    
});

$('iframe#ProductImageThumbsSrc').ready(
	function(){
		if($('iframe#ProductImageThumbsSrc').length > 0){			
			setInterval(setContent, 100);			
		}
	}
);
/*
jQuery.fn.loadProductAltImages = function() {
   
    try{

        var thisContent = $('#ProductImageThumbsSrc').contents().find('#ProductImageThumbsList').html();
       
        $(this).html(thisContent);
       
        var targetElement = $(this);
           
         $('a', this).hover(
            function(){
                $('a', targetElement).removeClass('selected');
               
                var imageMainSrc = $('img', this).attr('src').replace('crosssell', 'detailnew');
               
                $('img.productImageMain').attr('src', imageMainSrc).fadeIn('slow');
            },
            function(){
                $(this).addClass('selected');                 
            }
        );     
             
        $('a:first', this).addClass('selected');
       
    } catch(err){
    
        return false;
    }   
}
*/
jQuery.fn.showHideContent = function(charToShow, openText) {
   
    var elementHtml = $(this).html();
   
    $(this).click(
        function(){
            if($('span.more').is(':visible')){           
                $('span.more').stop(true, true).animate({opacity : 'toggle', height : 'toggle'}, 600);                                   
                $('span.hiddenHtml').stop(true, true).delay(400).animate({opacity : 'toggle', height : 'toggle'}, 800);
                return false;
            } else {
                $('span.hiddenHtml').stop(true, true).animate({opacity : 'toggle', height : 'toggle'}, 800);
                $('span.more').stop(true, true).delay(600).animate({opacity : 'toggle', height : 'toggle'}, 600);
                return false;
            }           
        }
    )
   
    if(elementHtml && elementHtml.length > charToShow){

        $(this).html(elementHtml.substring(0, charToShow));
        $(this).append("<span class='more'> <span style=style='cursor: pointer; text-decoration: underline;'>(..." + openText + ")</span></span>");       
        $(this).append("<span class='hiddenHtml' style='display:none;'>" + elementHtml.substring(charToShow, elementHtml.length) + "</span>");
    }
}


function isiOS(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
    );
}


