/**
 * Color/Size picker implementation.
 */

dojo.provide("atg.b2cblueprint.picker");

atg.b2cblueprint.picker={
  
  //called when a user clicks on a color
  clickColor: function(color,productId){
    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 str=color.toUpperCase()
		// No longer changing spaces to underscores
		//		var str1=str.replace(/ /g, "_")

    var picker=atg.b2cblueprint.picker;
		picker.changeColor(str,productId);
    picker.setQuantity();
    picker.setGiftlistId();
    picker.submitRefreshForm(); 
  },  

	//switch out the product image when a new swatch is selected
	changeColor: function(str,productId) {
		dojo.byId("productLink").href='javascript:atg.b2cblueprint.util.detailsPopup("/sportinglife/browse/popupProductDetails.jsp?productId='+productId+'&color='+str+'")';
		dojo.byId("myId").src='/images/products/170/'+productId+'_'+str+'_2.JPG';
		return false;		
	},

  //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;
  },
  
  changeTab: function(tabname)
  {	
	var tabToDisplay = document.getElementById(tabname);
	
	if (tabToDisplay.style.display == "block") {}
	else {
		
		document.getElementById('detail_features').style.display = 'none';
		document.getElementById('detail_contents').style.display = 'none';
		document.getElementById('detail_productTips').style.display = 'none';
		document.getElementById('detail_usageInstructions').style.display = 'none';
		document.getElementById('detail_review').style.display = 'none';
		
		document.getElementById('detail_features_tab').style.fontWeight = 'normal';
	
		document.getElementById('detail_productTips_tab').style.fontWeight = 'normal';
		document.getElementById('detail_usageInstructions_tab').style.fontWeight = 'normal';
		document.getElementById('detail_review_tab').style.fontWeight = 'normal';
		
		document.getElementById(tabname).style.display = 'block';
		document.getElementById(tabname + '_tab').style.fontWeight = '900';
	}
  }
};

