/**
 * Color/Size picker implementation.
 */

dojo.provide("atg.b2cblueprint.picker");
dojo.provide("atg.mff.pickerTypes.util");
var dataPT;
atg.b2cblueprint.picker={
  
   //called when a user changes a color
  selectColor: function(select){
    var picker = atg.b2cblueprint.picker;
    picker.clickColor(select.options[select.selectedIndex].value);
  },
  
  //called when a user changes a size
  selectSize: function(select){
    var picker = atg.b2cblueprint.picker;    
    picker.clickSize(select.options[select.selectedIndex].value);
  },
  
  selectStyle: function(select){
    var picker = atg.b2cblueprint.picker;    
    picker.clickStyle(select.options[select.selectedIndex].value);
  },
  //called when a user clicks on a color
  clickColor: function(color){
    var form = dojo.byId("colorsizerefreshform");
    var formPT = dojo.byId("pickertypesdatarefreshform");
    var currentColor = form.elements.selectedColor.value;
    var currentSize = form.elements.selectedSize.value;
    //if the color is not changing, dont do anything
    if(currentColor === color){
        return;
    }
    //set the new selected color in the refresh form and submit it
    form.elements.selectedColor.value = color;
    formPT.elements.selectedColor.value = color;
    if (form.elements.selectedSize != null && typeof(form.elements.selectedSize) != 'undefined')
    {
      form.elements.selectedSize.value = 'empty';//for correction of add to cart button behavior
      formPT.elements.selectedSize.value = 'empty';
    }
    var picker = atg.b2cblueprint.picker;
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm();
    picker.submitRefreshPickerTypesForm("color");
    /*atg.mff.pickerTypes.util.setSizePickerContent();*/
  },

  //called when a user clicks on a size
  clickSize: function(size){
    var form = dojo.byId("colorsizerefreshform");
    var formPT = dojo.byId("pickertypesdatarefreshform");
    //if the user clicks the size that's already selected, don't do anything
    var currentSize = form.elements.selectedSize.value;
    var currentColor = form.elements.selectedColor.value;
    if(currentSize === size){
        return;
    }   

    //set the new selected size in the refresh form and submit it
    if (form.elements.selectedSize != null && typeof(form.elements.selectedSize) != 'undefined'){
    form.elements.selectedSize.value = size;
    formPT.elements.selectedSize.value = size;}
    if (form.elements.selectedStyle != null && typeof(form.elements.selectedStyle) != 'undefined')
    {
      form.elements.selectedStyle.value = 'empty';//for correction of add to cart button behavior
      formPT.elements.selectedStyle.value = 'empty';
    }
    var picker = atg.b2cblueprint.picker;    
    picker.setQuantity();
    picker.setGiftlistId();      
    picker.submitRefreshForm();
    picker.submitRefreshPickerTypesForm();
    atg.mff.pickerTypes.util.setStylePickerContent("size");
  },
  
    //called when a user clicks on a style
  clickStyle: function(style, addMultipleItems, productId){
    var form = dojo.byId("colorsizerefreshform");
    var formPT = dojo.byId("pickertypesdatarefreshform");
    //if the user clicks the style that's already selected, don't do anything
    var currentStyle = form.elements.selectedStyle.value;
    
    if(currentStyle === style){
      return;
    }

    //set the new selected style in the refresh form and submit it
    form.elements.selectedStyle.value = style;
    formPT.elements.selectedStyle.value = style;

    var picker=atg.b2cblueprint.picker;
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm();
    picker.submitRefreshPickerTypesForm("style"); 
  },

  //gets the quantity from the addtocart form and sets the refreshform quantity 
  //we do this so we can preserve the quantity between refreshes
  setQuantity: function()
  {
    var addtocartform = dojo.byId("addToCart"); 
    var currentQuantity = addtocartform.elements.atg_b2cblueprint_quantityField.value;
    var refreshform = dojo.byId("colorsizerefreshform");
    refreshform.elements.savedquantity.value = currentQuantity;
  },
  
  //gets the quantity from the addToGiftList form and sets the refreshform savedgiftlist 
  //we do this so we can preserve the giftlist selection between refreshes
  setGiftlistId: function()
  {
    var addToGiftListForm = dojo.byId("addToGiftList");
    if(!addToGiftListForm){
      return;  
    }
    
    var currentGiftList = addToGiftListForm.elements.atg_b2cblueprint_GiftListChoice.value;
    var refreshform = dojo.byId("colorsizerefreshform");
    refreshform.elements.savedgiftlist.value = currentGiftList;
  },
  
  //resets the color and size selected and submits the refresh form
  resetPicker: function(){
    var form = dojo.byId("colorsizerefreshform");
    //reset the new selected size and color in the refresh form and submit it
    form.elements.selectedSize.value = "";
    form.elements.selectedColor.value = "";

    var picker=atg.b2cblueprint.picker;
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm(); 
  },

  submitRefreshForm: function()
  {
    dojo.io.bind({
    load: function(type, data, evt){
    
      var divColorPicker = dojo.byId("atg_b2cblueprint_picker");
      divColorPicker.innerHTML = data;
      
      document.getElementById('mff_productDetailPriceValue').innerHTML = document.getElementById('mff_skuCurrentPrice').innerHTML;
      dojo.widget.byId('richCart').hijackAllAddToCartNodes();  
      var element = dojo.widget.byId("richCalculateShippingTrigger");
      var form = dojo.byId("colorsizerefreshform");
      var sizeValue=form.elements.selectedSize.value;
      var colorValue=form.elements.selectedColor.value;
      if((sizeValue=="")||(colorValue=="")||(sizeValue=="empty")||(colorValue=="empty")){
         element.setLinkUnavailable();
      }else{
         element.setLinkAvailable();
      }
      
      },
    formNode: dojo.byId("colorsizerefreshform")
    });
  },
  
  submitRefreshPickerTypesForm: function(selectionIdentifier)
  {
    dojo.io.bind({
    sync:true,
    headers: { "Accept" : "application/json" },
    mimetype: "application/json",
    load: function(type, data, evt){
      dataPT = data;
      if (selectionIdentifier == "color"){
      atg.mff.pickerTypes.util.setSizePickerContent();
      }
      if (selectionIdentifier == "size"){
      atg.mff.pickerTypes.util.setStylePickerContent();
      }
    },
    formNode: dojo.byId("pickertypesdatarefreshform")
    });
  },
  setQuantityOnGiftlistForm: function()
  {
    var addtocartform = dojo.byId("addToCart"); 
    var currentQuantity = addtocartform.elements.atg_b2cblueprint_quantityField.value;
    var addtogiftlistform = dojo.byId("addToGiftList");
    //set the quantity in the add to gift lsit form and submit it
    addtogiftlistform.elements.giftListAddQuantity.value = currentQuantity;
  }
};

atg.mff.pickerTypes.util={
  setSizePickerContent : function(){
  
    var colorSelect = dojo.byId('colorSelect');
    var sizeSelect = dojo.byId('sizeSelect');
    
    var styleSelect = dojo.byId('styleSelect');
    if (dataPT != null && typeof(dataPT) != 'undefined')
    {
      data = dataPT;
    }
    if (data.pickerTypesUseDefault != null &&
      data.pickerTypesUseDefault != 'undefined' &&
      (data.pickerTypesUseDefault == 'false' || data.pickerTypesUseDefault == false)){
	    if (colorSelect != null && typeof(colorSelect) != 'undefined'){
	      var selind0 = colorSelect.selectedIndex;
	      var selval0 = colorSelect.value;
	      
	      if (sizeSelect != null && typeof(sizeSelect) != 'undefined'){
	        if (selind0 == 0){
	          sizeSelect.setAttribute('disabled',true);
	          atg.mff.pickerTypes.util.clearSelect(sizeSelect);
	          if (styleSelect != null && typeof(styleSelect) != 'undefined'){
	           styleSelect.setAttribute('disabled',true);
	           atg.mff.pickerTypes.util.clearSelect(styleSelect);
	          }
	        }
	        else{
	          
	          sizeSelect.removeAttribute('disabled');
	          if (styleSelect != null && typeof(styleSelect) != 'undefined'){
	            styleSelect.setAttribute('disabled',true);
	          }
	          atg.mff.pickerTypes.util.clearSelect(sizeSelect);
	          if (data.sizesPT != null && typeof(data.sizesPT) != 'undefined'){
	           optionsNodes = data.sizesPT;
            
	           atg.mff.pickerTypes.util.addOptionsToSelect(sizeSelect,optionsNodes);
	          }
	        }
	      }
	    }
    }else{//for default pickers
      if (sizeSelect != null && typeof(sizeSelect) != 'undefined'){
	      if (colorSelect.selectedIndex != 0){
	        atg.mff.pickerTypes.util.clearSelect(sizeSelect);
	        selval = colorSelect.value;
	        if (data.sizes != null && typeof(data.sizes) != 'undefined'){
	          optionsNodes = data.sizes;
	        
	        atg.mff.pickerTypes.util.addOptionsToSelect(sizeSelect,optionsNodes);
	        sizeSelect.removeAttribute('disabled');
	        }
	      }else{
	        atg.mff.pickerTypes.util.clearSelect(sizeSelect);
	        sizeSelect.setAttribute('disabled',true);
	      }
	    }
    }
  },
  
  
  setStylePickerContent : function(){
    var colorSelect = dojo.byId('colorSelect');
    var sizeSelect = dojo.byId('sizeSelect');
    var styleSelect = dojo.byId('styleSelect');
    if (dataPT != null && typeof(dataPT) != 'undefined')
    {
      data = dataPT;
    }
    if (data.pickerTypesUseDefault != null &&
      data.pickerTypesUseDefault != 'undefined' &&
      (data.pickerTypesUseDefault == 'false' || data.pickerTypesUseDefault == false)){
	    if (colorSelect != null && typeof(colorSelect) != 'undefined' && sizeSelect != null && typeof(sizeSelect) != 'undefined'){
	      var selind0 = colorSelect.selectedIndex;
	      var selval0 = colorSelect.value;
	      var selind1 = sizeSelect.selectedIndex;
	      var selval1 = sizeSelect.value;
	      if (styleSelect != null && typeof(styleSelect) != 'undefined'){
	        if (selind1 == 0){
	          styleSelect.setAttribute('disabled',true);
	          atg.mff.pickerTypes.util.clearSelect(styleSelect);
	        }
	        else{
	          styleSelect.removeAttribute('disabled');
	          atg.mff.pickerTypes.util.clearSelect(styleSelect);
	          if (data.sizesPT != null && typeof(data.sizesPT) != 'undefined'){
             optionsNodes = data.stylesPT;
            }
	          atg.mff.pickerTypes.util.addOptionsToSelect(styleSelect,optionsNodes);
	       }
	     }
	   }
   }
  },

  addPicker : function () {
  if (data.pickerTypesUseDefault != null &&
      data.pickerTypesUseDefault != 'undefined' &&
      (data.pickerTypesUseDefault == 'false' || data.pickerTypesUseDefault == false))
      { 
        var colorSelect = dojo.byId("colorSelect");
        var sizeSelect = dojo.byId("sizeSelect");
        var styleSelect = dojo.byId("styleSelect");
        
        if (data.pickerTypeAmount >= 3){
          if (colorSelect != null && typeof(colorSelect) != 'undefined' &&
              sizeSelect != null && typeof(sizeSelect) != 'undefined' && 
              styleSelect != null && typeof(styleSelect) != 'undefined')
		        { 
		          sizeSelect.setAttribute('disabled', true);
		          styleSelect.setAttribute('disabled', true);
		        }
        }
        if (data.pickerTypeAmount == 2){
          if (colorSelect != null && typeof(colorSelect) != 'undefined' &&
              sizeSelect != null && typeof(sizeSelect) != 'undefined')
            { 
              sizeSelect.setAttribute('disabled', true);
            }
        }
        if (data.pickerTypeAmount == 1){
          
        }
        if (colorSelect != null && typeof(colorSelect) != 'undefined'){
           var pickerValues = data.colorsPT;
           if (pickerValues != null && typeof(pickerValues)!= 'undefined'){
            atg.mff.pickerTypes.util.addOptionsToSelect(colorSelect,pickerValues );
           }
        }
    }else{
      var sizeSelect = dojo.byId("sizeSelect");
      var colorSelect = dojo.byId("colorSelect");
      if (sizeSelect != null && typeof(sizeSelect) != 'undefined' && 
          colorSelect != null && typeof(colorSelect) != 'undefined')
      { 
        sizeSelect.setAttribute('disabled', true);
      }
    }
  },
  
  clearSelect : function (select) {
    if (select != null && typeof(select) != 'undefined'){
      var options = select.options;
      for (i=options.length-1; i>=1; i--)
      {
        select.removeChild(options[i]);
      }
    }
  },
  
  addOptionsToSelect : function(select,optionsNodesValues) {
     var option;
     if (select != null && typeof(select) != 'undefined' &&
        optionsNodesValues != null && typeof(optionsNodesValues) != 'undefined')
     for (var i = 0; i < optionsNodesValues.length; i++){
      option = dojo.doc().createElement("option");
      option.setAttribute('text',optionsNodesValues[i]);
      option.setAttribute('value',optionsNodesValues[i]);
      option.setAttribute('id',optionsNodesValues[i]);
      if (optionsNodesValues[i] != null && typeof(optionsNodesValues[i]) != 'undefined')
      {
        option.innerHTML = dojo.string.trim(optionsNodesValues[i]);
      }else{
        option.innerHTML = optionsNodesValues[i];
      }
      
      select.appendChild(option);
     }
  }
};



