
jQuery.noConflict();
// Initialize detailPageItems on page ready
jQuery(function() {
   detailPageItems.initialize();
   //requires amazon.jquery.tabs to work properly
   if(jQuery.fn.tabs){
     var newOptions = {
        tabsClass: '.tabHeaders > li',
        tabsSelected: '.selected',
        tabPanels: '.tab',
        tabPrefix: '#productInfoTab'
     }
     jQuery(".productInfoTabs").tabs(newOptions);
   }    
});
var detailPageItems = {
   // reference to JSON data object
   jsonData : null, 
   // current asin
   asin : null, 
   //price element
   priceElement : null, 
   //color swatch element
   colorSwatchElement : null, 
   // size element
   sizeElement : null, 
   //temp size list (need this for IE fix)
   globalSizeList : null, 
   // quantity element
   quantityElement : null, 
   //cart element
   cartElement : null, 
   //EmailMeWhenAvailable element
   emwaElement : null, 
   //sign in element
   signIn : null, 
   // list of swatches
   colorSwatch : null, 
   // selected color value
   colorValue : "", 
    //emwa
   emwaButton:null,
   imageFrame:null,
   initialAsin:null,
   tafSigninLink:null,
   showFullDescriptionLink:null,
   //This string sets the "cartAddMethod" variable. This is used to populate Analytics info.
   cartAddMethodString: "",
   // add to cart button
   addToCartButton : null,
   
   initialize : function() {
      if(jQuery('.miniDetailPage').length > 0){
        this.cartAddMethodString = "Product Quick View";
      } else {
        this.cartAddMethodString = "Product Page";
      }
      this.jsonData = detailData;
      this.asin = jQuery("input:hidden[name=item.0.asin]");
      this.initializePriceElement();
      this.colorSwatchElement = jQuery("#colorSwatches");
      this.sizeElement = jQuery("[name=item.0.size]");
      // this is the global size list, need it for IE fix
      this.globalSizeList = this.sizeElement.children().clone();
      this.quantityElement = jQuery("[name=item.0.qty]");
      this.cartElement = jQuery("#addToCart");
      this.emwaElement = jQuery("#emwa");
      this.signIn = jQuery("#eSignIn");
      this.colorValue = jQuery("#colorSwatches").find("p");
      this.colorValue.attr('defaultMsg', this.colorValue.text());
      this.colorValue.attr('selectedValue','');
      this.colorSwatch = jQuery("#colorSwatches").find("a");
      this.emwaButton=jQuery("#emwaButton");
      this.tafSigninLink=jQuery("#tellAFriendSignInRedirect");
      this.showFullDescriptionLink=jQuery("#showFullDescription");
      this.addToCartButton = jQuery("#addToCartButton");
      this.bindElementWithEvents();
      this.setDefaultSwatchStates();
      // update items in case of single size/color at load time
      this.initialAsin=this.asin.val();
      if((this.colorValue.attr('selectedValue') != null && this.colorValue.attr('selectedValue') != "" ) || (this.sizeElement.length > 0 && this.sizeElement.val() != "") ) {
          this.updateItems();
      }
      this.restoreState();
   }
   , initializePriceElement : function() {
        var priceElement = jQuery(".productDetailPrice");
        var definitionList = priceElement.find("dl");
        var youSaveLabelElm = priceElement.find("dt.savings");
        var youSaveElement  = priceElement.find("dd.savings");

        //
        // If savings not initialized, add invisible youSave section
        // so buyable items can show savings

        if(youSaveLabelElm.length == 0 || youSaveElement.length == 0) {
            youSaveLabelElm.remove();
            youSaveElement.remove();
            youSaveLabelElm = jQuery("<dt/>").attr("class", "savings");
            definitionList.append(youSaveLabelElm);
            youSaveElement = jQuery("<dd/>").attr("class", "savings");
            definitionList.append(youSaveElement);
        }

        this.initialPriceElement = priceElement.clone();
    }
   , //all the event bindings for detail page goes here
   bindElementWithEvents : function() {
      this.addToCartButton.bindTooltip({considerParent: false, paddingTop: 8});
      var parentThis = this;
      
      
      // bind onchange event for size
      if(this.sizeElement.length > 0) {
         this.sizeElement.bind("change", function() {
            //uncomment the following line to enable clicking error msg
            //jQuery("#addToCartErrMsg").hide();
            parentThis.changeSize(); }
         );
      }

       // bind onchange event for quantity
      if(this.quantityElement.length > 0) {
	    this.quantityElement.bind("change", function() {
	      jQuery("#addToCartErrMsg").hide();
        });
      }
      // bind onclick for cart
      if(this.cartElement.length > 0) {
            var formObj = jQuery('#addItemMainFormFloating');
            formObj.submit( function() {
                parentThis.processAddToCartButtonClick(this);
                return false; 
            });
     
       }
       //bind onclick for show fill description (View More)
       if(this.showFullDescriptionLink.length > 0)
       {
           this.showFullDescriptionLink.click(function(){
              jQuery("#productDescriptionContent").html(productDescription.description);
              parentThis.showFullDescriptionLink.hide();
              return false;
           });
       }
       
       this.emwaButton.click(function(){
           EmwaPopoverItem.showEMWAPopUp(parentThis.asin.val(),null);
       });
       this.showJsVersion();
       this.hideNonJsVersion();
      //bind swatch events
      this.findAndAttachSwatchEvents();
      //bind quantity events
      if(this.quantityElement.length > 0) {
          this.quantityElement.bind('change', function() {
              if(parentThis.quantityElement.val() > 0) {
                  parentThis.addToCartButton.attr('title','');
              } else {
                  var msg = jQuery("input:hidden[name=quantityInvalidMessage]").val();
                  parentThis.addToCartButton.attr('title',msg);
              }
          });
       }
      }
   , changeSize : function() {
      var optionClass = this.sizeElement.find("option[value=" + this.sizeElement.val() + "]").attr("class");
      this.sizeElement.attr("class", optionClass);
      this.updateSwatchesBasedOnSizeEvent();
      this.updateItems();
      }
   , //update items in detail page
   updateItems : function() {
      var color = this.colorValue.attr('selectedValue');
      if (color == null || color == "") color = 'null';
      var size = this.sizeElement.val();
      if (size == null || size == "") size = 'null';
      var key = size + '_' + color;
      var asinVal = this.jsonData.sizeColorMap[key];
      if(asinVal != null) {
         this.asin.val(asinVal);
         //update the availability message to be displayed
         this.updateAvailability(asinVal);
         this.updateAddToCartButton(asinVal);
         var showEMWA = (this.jsonData.asinData[asinVal].isAsinAvailable == "false" && this.jsonData.asinData[asinVal].isEmailMeWhenAvailable == "true") ? asinVal : "";
         jQuery("#dialogButtons").find("input[name=asin]").attr("value", asinVal);
         this.updateEMWA(showEMWA);
         tafData.tafAsin=asinVal;
         }
      else {
         this.asin.val("");
         this.updateAvailability("");
         this.updateAddToCartButton("");
         this.updateEMWA("");
         tafData.tafAsin=this.initialAsin;
         }
      this.updatePrice();
      if(this.quantityElement.length > 0)this.updateQuantity();
      if(this.tafSigninLink.length>0) 
          this.updateTAFRedirectURLString();
      }
   , updatePrice : function() {
      var asinVal = this.asin.val();
      var newPriceElement = this.initialPriceElement.clone();
      if(asinVal != null && asinVal != "") {
         //Get the elements associated with price
         var ourPriceLabelElm = newPriceElement.find("dt.salePrice");
         var ourPriceElement  = newPriceElement.find("dd.salePrice");
         var ourPriceNonSaleLabelElm = newPriceElement.find("dt.price");
         var ourPriceNonSaleElement  = newPriceElement.find("dd.price");
         var regularPriceLabelElm = newPriceElement.find("dt.listPrice");
         var regularPriceElement  = newPriceElement.find("dd.listPrice");
         var youSaveLabelElm = newPriceElement.find("dt.savings");
         var youSaveElement  = newPriceElement.find("dd.savings");
         // fetch all price data
         var regularPrice = this.jsonData.asinData[asinVal].formattedRegularPrice;
         var buyingPrice = this.jsonData.asinData[asinVal].formattedBuyingPrice;
         var youSaveAmount = this.jsonData.asinData[asinVal].youSaveAmountOnRegularPrice;
         var youSavePercent = this.jsonData.asinData[asinVal].youSavePercentOnRegularPrice;
         // Store validity
         var regularPriceValid = (regularPrice != null && regularPrice != "");
         var buyingPriceValid = (buyingPrice != null && buyingPrice != "");
         var isSaleItem = (buyingPriceValid && regularPriceValid && buyingPrice < regularPrice) ? true : false;
         if(ourPriceElement.length > 0 || ourPriceNonSaleElement.length > 0) {
             var ourPriceLabelText = "";
             var classToUse = "price";
             if(isSaleItem) {
                 ourPriceLabelText = saleLabel;
                 classToUse = "salePrice";
             }
             if(buyingPriceValid) {
                 ourPriceLabelElm.html(ourPriceLabelText);
                 ourPriceLabelElm.attr("class",classToUse);
                 ourPriceElement.html(buyingPrice);
                 ourPriceElement.attr("class",classToUse);

                 ourPriceNonSaleLabelElm.html(ourPriceLabelText);
                 ourPriceNonSaleLabelElm.attr("class",classToUse);
                 ourPriceNonSaleElement.html(buyingPrice);
                 ourPriceNonSaleElement.attr("class",classToUse);
             }
         }
         if(regularPriceElement.length > 0 || ourPriceNonSaleElement.length > 0) {
             if(buyingPriceValid && regularPriceValid) {
                 if(isSaleItem){
                    regularPriceElement.html(regularPrice);
                    regularPriceElement.attr("class","listPrice");
                    youSaveLabelElm.text(youSaveLabel);
                    youSaveElement.text(youSaveAmount+" ("+youSavePercent+")");
                 }else{
                     regularPriceElement.html("");
                 }
                 newPriceElement.find('.productPricingMessage').hide();
             } else {
                 newPriceElement.find('.productPricingMessage').show();
             }
         }
      }
      jQuery(".productDetailPrice").replaceWith(newPriceElement);
    }
   , updateQuantity : function () {
      var asinVal = this.asin.val();
      var isBuyable = "false";
      if(this.jsonData.asinData[asinVal] != null) {
         isBuyable = this.jsonData.asinData[asinVal].IsBuyable;
         }
      if(isBuyable == "true") {
         this.quantityElement.removeAttr("disabled");
         }
      else {
         this.quantityElement.attr("disabled", "disabled");
         }
      }
, updateSizeBasedOnColorEvent : function() {
        var currentSize = this.sizeElement.val();
        this.sizeElement.empty();
        var parentThis = this;
        this.globalSizeList.each(function(i)
        {
             var thisSize = jQuery(parentThis.globalSizeList[i]);
             var thisSizeIsEmpty=(thisSize.val() == null || thisSize.val() == "");
             var colorSelectedIsEmpty=(parentThis.colorValue.attr('selectedValue') == null ||parentThis.colorValue.attr('selectedValue') == "");
             var key = thisSize.val() + "_" + parentThis.colorValue.attr('selectedValue'); 
             var asinVal = parentThis.jsonData.sizeColorMap[key]; 
             var asinValExists=asinVal != null && asinVal != "";
             //asinValExists denotes that the color-size combination under consideration is a valid one and its a valid variational child.
             //colorSelectedEmpty denotes that no color is selected using color swatches or color dropdown.
             //thisSizeEmpty evaluates to true only for the first element in globalSizeList which is "--Select--" element which should always be there in size drpdown.
             if(asinValExists||colorSelectedIsEmpty||thisSizeIsEmpty)
             {
                 var newOption=thisSize.clone();
                 if(asinValExists && parentThis.jsonData.asinData[asinVal].isAsinAvailable != "true") {
                     newOption.attr("class","emwa-size");
                 }
                 if(currentSize==thisSize.val()) {
                     newOption.attr("selected",true);
                     parentThis.sizeElement.attr("class",newOption.attr("class"));
                 }
                 parentThis.sizeElement.append(newOption);
             }
        }
         );
   }
   
    , findAndAttachSwatchEvents : function() {
     var parentThis = this;
      if(this.colorSwatch.length > 0) {
         this.colorSwatch.hover(function() {
                 parentThis.changeColorSwatchHoverIn(this); }
             , function() {
                 parentThis.changeColorSwatchHoverOut(this); }
             ).click(function() {
                 parentThis.changeColorSwatchClick(this); }
             ).keypress(function(e) {
                 if(e.which == 13 ) {
                     parentThis.changeColorSwatchClick(this);
                 }
         });
      }
      else {
         var colorEl = this.colorSwatchElement.find("input:hidden[name=item.0.color]");
         if(this.colorValue.length == 0){
          this.colorValue = colorEl;
         }
         this.colorValue.attr('selectedValue', (colorEl.length > 0) ? colorEl.val() : 'null');
         }
      }
   , setDefaultSwatchStates : function() {
      var parentThis = this;
      this.colorSwatch.each(function(colorIndex, colorDomEle) {
         var thisColor = jQuery(colorDomEle).find("input[name=swatchColor]").val(); 
		 var available = false; 
		 if (parentThis.sizeElement.find("option").length == 0) {
		 	//if a single size variation or color only variation
			var sizeValue = parentThis.sizeElement.val();
			if (sizeValue == null) {
				//color only
				var key = "null_" + thisColor;
			}
			else {
				//single size
				var key = sizeValue + "_" + thisColor;
			}
			var asinVal = parentThis.jsonData.sizeColorMap[key];
		 	if (asinVal != null) {
		 		if (parentThis.jsonData.asinData[asinVal].isAsinAvailable == "true") {
		 			available = true;
		 		}
		 	}
		 }
		 else {
		 	parentThis.sizeElement.find("option").each(function(sizeIndex, sizeDomEle){
		 		var sizeThis = this;
		 		var thisSize = jQuery(sizeDomEle).val();
		 		var key = thisSize + '_' + thisColor;
		 		var asinVal = parentThis.jsonData.sizeColorMap[key];
		 		if (asinVal != null) {
		 			if (parentThis.jsonData.asinData[asinVal].isAsinAvailable == "true") {
		 				available = true;
		 			}
		 		}
		 	});
		 }
		 if(!available) {
            jQuery(colorDomEle).attr('defaultState', 'swatchUnavailable'); jQuery(colorDomEle).attr('class', 'swatchUnavailable'); }
         }
      );
      this.updateColorSwatchTitle();
      }
   , changeColorSwatchHoverIn : function(colorThis) {
      var jObj = jQuery(colorThis);
      if ( jObj.attr("class").indexOf('swatchSelect') == - 1 ) {
         jObj.addClass("swatchHover");
         this.updateColorValue(jObj.find("input[name=swatchColor]").val());
         }
      }
   , changeColorSwatchHoverOut : function(colorThis) {
      var jObj = jQuery(colorThis);
      if (jObj.attr("class").indexOf('swatchSelect') == - 1) {
         jObj.removeClass("swatchHover");
         this.updateColorValue(this.colorValue.attr('selectedValue'));
         }
      }
   , changeColorSwatchClick : function(colorThis) {
      var jObj = jQuery(colorThis);
      var classValue = jObj.attr("class");
      //Ignore the select event if swatch is grayed out
      if(classValue.indexOf('swatchGray') != - 1) {
         return;
         }
      if(classValue.indexOf('swatchSelect') != - 1) {
         this.colorValue.attr('selectedValue', "");
         jObj.removeClass("swatchSelect").removeClass("swatchHover");
         }
      else {
         this.colorValue.attr('selectedValue', jObj.find("input[name=swatchColor]").val());
         this.resetSwatches();
         jObj.addClass("swatchSelect");
         }
      //uncomment the following line to enable clicking error msg
      //jQuery("#addToCartErrMsg").hide();
      this.updateSizeBasedOnColorEvent();
      this.updateItems();
      this.updateColorValue(this.colorValue.attr('selectedValue'));
      this.updateImage();
      this.changeAmazoom();
      }
   , updateColorValue : function(color) {
      if(color != "") {
         this.colorValue.text(color);
         }
      else {
         this.colorValue.text(this.colorValue.attr('defaultMsg'));
         }
      }
   , resetSwatches : function() {
      this.colorSwatch.removeClass('swatchSelect').removeClass('swatchHover');
      }
   , updateSwatchesBasedOnSizeEvent : function() {
      if (this.sizeElement.val() != null && this.sizeElement.val() != "") {
         //Size is selected
         var parentThis = this;
         this.colorSwatch.each(function(index, domEle) {
            var thisColor = jQuery(domEle).find("input[name=swatchColor]").val(); var key = parentThis.sizeElement.val() + '_' + thisColor; var asinVal = parentThis.jsonData.sizeColorMap[key]; if (parentThis.colorValue.attr('selectedValue') != thisColor) {
               //This color is not selected
               if(asinVal == null || asinVal == "") {
                  //No item exists with this combination. Gray it out
                  jQuery(domEle).addClass("swatchGray"); }
               else {
                  //Asin available
                  jQuery(domEle).removeClass("swatchGray"); if(parentThis.jsonData.asinData[asinVal].isAsinAvailable == "true") {
                     //In stock. Remove the swatchUnavailable, in case it was set previously
                     jQuery(domEle).removeClass("swatchUnavailable"); }
                  else if (parentThis.jsonData.asinData[asinVal].isAsinAvailable == "false" && parentThis.jsonData.asinData[asinVal].isEmailMeWhenAvailable == "true") {
                     //OOS+EMWA. Set the class accordingly
                     jQuery(domEle).addClass("swatchUnavailable"); }
                  }
               }
            else {
               //Color is selected
               if(asinVal == null || asinVal == "") {
                  //No item exists with this combination. De-select the color
                  parentThis.updateColorValue(""); jQuery(domEle).removeClass("swatchSelect").removeClass("swatchHover"); }
               }
            }
         );
         }
      else {
         //Size is reset
         this.colorSwatch.removeClass("swatchGray").removeClass("swatchUnavailable");
         this.colorSwatch.each(function(colorIndex, colorDomEle) {
            var jObj = jQuery(colorDomEle); jObj.addClass(jObj.attr('defaultState')); }
         );
         }
      this.updateColorSwatchTitle();
      }
   , //TODO move all this strings to world server strings once they are ready
   updateColorSwatchTitle : function() {
      var parentThis = this;
      this.colorSwatch.each(function(index, domEle) {
         var jEl = jQuery(domEle); 
         if (jEl.attr("class").indexOf("swatchGray") != - 1) {
            jEl.attr("title", swatchMessages.colorUnavailable);
         }
         else if (jEl.attr("class").indexOf("swatchUnavailable") != - 1) {
            if(parentThis.sizeElement.val()!=null&&parentThis.sizeElement.val()!="")
            {
                jEl.attr("title", swatchMessages.colorTempUnavailableForThisSize); 
            }
            else
            {
                jEl.attr("title", swatchMessages.colorTempUnavailableForAllSizes); 
            }
         }
         else {
            jEl.attr("title", ""); 
         }
		 jEl.bindTooltip();
         }
      );
      }
   , updateAvailability : function (asinVal) {
      var jsEnabled = jQuery(".javascriptEnabled");
      var hiddenAvail = jsEnabled.find(".hiddenAvailability");
      var avail = jsEnabled.find(".productAvailabilityMessage");
      //the condition below is true if some child variation is selected -- availability message should be updated in that case
      if(asinVal != "") {
         var availMsg = this.jsonData.asinData[asinVal].ShortAvailabilityMessage;
         //if this asin is OOS and emwa enabled pickup availability message from hiddenEmwaOOSAvailabilityMessage
         if (this.jsonData.asinData[asinVal].isAsinAvailable == "false" && this.jsonData.asinData[asinVal].isEmailMeWhenAvailable == "true") {
            availMsg = jsEnabled.find(".hiddenEmwaOOSAvailabilityMessage").html();
            }
         hiddenAvail.find(".hiddenAvailabilityMessage").html(availMsg);
         avail.hide();
         hiddenAvail.show();
         }
      else {
         hiddenAvail.hide();
         avail.show();
         }
      },
   updateAddToCartButton : function (asinVal) {
      if(asinVal != "") {
          this.addToCartButton.attr("title", "");
          this.addToCartButton.attr("class", "styledButton primaryActionLarge addToCartEnabledButton");
      }
      else {
          this.addToCartButton.attr("class", "styledButton primaryActionLarge disabledButton addToCartDisabledButton");
      }
      if(this.sizeElement.val() == "" && this.colorValue.attr('selectedValue') == "") {
          var msg = jQuery("input:hidden[name=select_size_color_msg]").val();
          this.addToCartButton.attr("title", msg);
         }
      else if(this.sizeElement.val() == "") {
          var msg = jQuery("input:hidden[name=addToCartHoverSizeMessage]").val();
          this.addToCartButton.attr("title", msg);
         }
      else if(this.colorValue.attr('selectedValue') == "") {
          var msg = jQuery("input:hidden[name=addToCartHoverColorMessage]").val();
          this.addToCartButton.attr("title", msg);
         }
      else if(this.quantityElement.val() <= 0) {
          var msg = jQuery("input:hidden[name=quantityInvalidMessage]").val();
          this.addToCartButton.attr("title", msg);
         }
      },
   processAddToCartButtonClick : function(formName) {
      if(this.quantityElement.length > 0 && (this.quantityElement.attr("disabled") == false)) {
         if( this.quantityElement.val() > 0 ) {
            this.addToCart("addItemMainFormFloating");
            }
         else {
            //uncomment the following 4 lines to enable clicking error msg
            //var msg = jQuery("input:hidden[name=quantityInvalidMessage]").val();
	    //jQuery("#addToCartErrMsg").html(msg);
            //jQuery("#addToCartErrMsg").show();
            //publishErrorMessagesAnalyticsInformation(msg);
            }
         }
            //uncomment the following 6 lines to enable clicking error msg
		 /*
	  else{
            var msg = jQuery("input:hidden[name=select_size_color_msg]").val();
	    jQuery("#addToCartErrMsg").html(msg);
            jQuery("#addToCartErrMsg").show();
            publishErrorMessagesAnalyticsInformation(msg);
	  }
	  */
      }

   , updateImage : function() {
          if(typeof(productImageItem)!='undefined'){
              productImageItem.imageUpdater(this.colorValue.attr('selectedValue'));
          }
     }
   , updateEMWA : function(asinVal) {
        var emwaSelectorButton=jQuery(".emwaSelectorButton");
        if(asinVal != "") {
            if(this.emwaElement.length > 0) {
                this.emwaElement.attr("class", "sEMWA");
                this.emwaElement.attr("title","");
			    emwaSelectorButton.attr("class","emwaSelectorButton emwaEnabledButton");
                emwaSelectorButton.removeAttr("disabled");
                jQuery("#dColorVal").text(this.colorValue.attr('selectedValue'));
                jQuery("#dSizeVal").text(this.sizeElement.val());
                this.cartElement.hide();
                if(this.signIn.length > 0)
                    this.signIn.val(this.getRedirectURLString());
            }
        }
        else {
            if(this.emwaElement.length > 0) {
                if((typeof(emwaVariables) != 'undefined')&&emwaVariables.isAllVariationsEMWA)
                {
                    var emwaTitle=emwaVariables.selectColorAndSizeForEMWAMessage;
                    if(this.sizeElement.val() != "") {
                        emwaTitle = emwaVariables.selectColorForEMWAMessage;
                    }
                    else if(this.colorValue.attr('selectedValue') != "") {
                        emwaTitle = emwaVariables.selectSizeForEMWAMessage;
                    }
        	        emwaSelectorButton.attr("class","emwaSelectorButton emwaDisabledButton ");
                    emwaSelectorButton.attr("disabled","disabled");
                    this.emwaElement.attr("title",emwaTitle);
                }
                else {
                    this.emwaElement.attr("class", "hEMWA");
                    this.cartElement.show();
        }
            }
        }
    },
        
    getRedirectURLString : function()
    {
        var redirectUrl = this.signIn.val();
        redirectUrl=redirectUrl.substring(0,redirectUrl.indexOf('?')+1);
        redirectUrl = redirectUrl + "&childAsin=" + this.asin.val()+"&emwaSubscribe=true";
        return redirectUrl;
    },

    restoreState:function()
    {
        if(typeof(emwaData)!='undefined')
        {
            EmwaPopoverItem.showEMWAPopUp(emwaData.emwaSubscribeChildAsin,null);
            this.restoreSizeColorData(emwaData.emwaSubscribeChildAsin);
            
        }

        if(tafData.showTAFPopupOnLoad)
        {
            this.restoreSizeColorData(tafData.tafAsin);
        }
    },
        
    restoreSizeColorData:function(asin)
    {
        var colorValue="";
        var sizeValue=""
        if((this.colorValue.length>0)&&(this.sizeElement.length>0)&&(typeof this.jsonData.asinData[asin] != 'undefined'))
        {
            colorValue=this.jsonData.asinData[asin].color;
            sizeValue=this.jsonData.asinData[asin].size;
            this.colorValue.attr('selectedValue',colorValue);
            this.sizeElement.val(sizeValue);
            var colorSwatchToSelect=this.colorSwatchElement.find("input:hidden[value="+colorValue+"]").parent();
            this.changeColorSwatchClick(colorSwatchToSelect);
        }
    },
    

    showJsVersion:function(){
        jQuery('.javascriptEnabled').show();
        jQuery('#colorSwatches').show();
    },

    hideNonJsVersion:function()
    {
           jQuery('.javascriptDisabled').hide();
    },
  
    addToCart: function (formId){
    var parentThis = this;
      jQuery.ajax({
           type     : "POST",
           url      : addToCartPageSecureURL,
           data     : "layoutDomain=Starter&layoutId=Expandable2&activeItemThumbImageHeight=103&activeItemThumbImageWidth=82&enableAnalytics=true&cartAddMethod=" + parentThis.cartAddMethodString + "&" + jQuery('#'+formId).serialize(),
           error    : function(html, status) {alert(status);},
           success  : function(html, status) {

                       if(html) {                       
                       
                        jQuery('html, body', parent.document).animate({
                            scrollTop: 0
                        }, 0);
                        
                        jQuery('#top-8',parent.document).html(html);

                        var cartPop = jQuery('.dynamicMiniCart',parent.document);

                        cartPop.css({
                            "display": "block"
                        });

                        // swap images when cart starts to slide down
                        jQuery("#miniCartBottomContracted", parent.document).css("display","none");
                        jQuery("#miniCartBottomExpanded", parent.document).css("display","block");
                        
                        cartPop.hide();

                        cartPop.slideDown(1500);

                        window.setTimeout(function(){
                         // swap images back after callback
                         cartPop.slideUp(2500,function(){
                           jQuery("#miniCartBottomContracted", parent.document).css("display","block");
                           jQuery("#miniCartBottomExpanded", parent.document).css("display","none");
                        });

                        }, 8000);
                        if(typeof(miniDetailTabs) != 'undefined'){
                            miniDetailTabs.closePopup();
                        }
                       }

                     },
           timeout  : 100000
         });
   },

   updateTAFRedirectURLString : function()
   {
        var asin=this.asin.val();
        if(asin==null||asin=="")
            asin=this.initialAsin;
        var redirectUrl = this.tafSigninLink.attr("href");
        var startIndex = redirectUrl.indexOf("childAsin",redirectUrl.indexOf("redirectUrl"));
        startIndex = redirectUrl.indexOf("%3D",startIndex)+3;
        var endIndex = redirectUrl.indexOf("%26",startIndex);
        redirectUrl = redirectUrl.substring(0,startIndex) + asin + redirectUrl.substring(endIndex);
        this.tafSigninLink.attr("href",redirectUrl);
   },

   changeAmazoom:function()
   {
       var parentThis=this;

       if(this.imageFrame==null) {
           this.imageFrame = jQuery("<iframe id='ImageFrame' scrolling='no' frameborder='0' allowtransparency='false' ></iframe>");
           var imageBodyParent = jQuery('#imageBody').parent();
           imageBodyParent.append(this.imageFrame);
       }

       var color=this.colorValue.attr('selectedValue');
       if(color!=null&&color!=""&&color!='null'){
           var asin=this.jsonData.colorData[color].Asin;
           this.imageFrame.attr("src","/richContent/"+asin);
           jQuery("#imageBody").hide();
           jQuery("#alternateImagesBody").hide();
           parentThis.imageFrame.show();           
       }
       else {
           this.imageFrame.hide();
           jQuery("#imageBody").show();
           jQuery("#alternateImagesBody").show();
       }
   }
}; 


