/**
 * Color/Size picker implementation.
 */

dojo.provide("atg.b2cblueprint.picker");

atg.b2cblueprint.picker={
  
  //called when a user clicks on a color
  clickColor: function(color){
    var form = dojo.byId("colorsizerefreshform");
    var currentColor = form.elements.selectedColor.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;
    var picker=atg.b2cblueprint.picker;
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm(); 
  },  

  //called when a user clicks on a size
  clickSize: function(size){
    var form = dojo.byId("colorsizerefreshform");

    //if the user clicks the size that's already selected, don't do anything
    var currentSize = form.elements.selectedSize.value;
    if(currentSize === size){
      return;
    }

    //set the new selected size in the refresh form and submit it
    form.elements.selectedSize.value = size;

    var picker=atg.b2cblueprint.picker;
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm();       
  },

  //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;
      dojo.widget.byId('richCart').hijackAllAddToCartNodes();
      },
    formNode: dojo.byId("colorsizerefreshform")
    });  
  },
  
  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;
  }
};

