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

// Declare a global var for tab navigation
var currTabName;

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 currentVar1 = form.elements.selectedVar1.value;

    //if the color is not changing, dont do anything
    if(currentVar1 === color){
      return;
    }

    //set the new selected color in the refresh form and submit it
    form.elements.selectedVar1.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 currentVar2 = form.elements.selectedVar2.value;
    if(currentVar2 === size){
      return;
    }

    //set the new selected size in the refresh form and submit it
    form.elements.selectedVar2.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.selectedVar1.value = "";
    form.elements.selectedVar2.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;
  }
};

dojo.provide("atg.qcsupply.productTabs");

atg.qcsupply.productTabs={

    //called when a user clicks a tab
    clickTab: function(tabname) {
    var form = dojo.byId("tab_switchRefreshForm");    
    var currentTab = form.elements.tabname.value;

	// Set current tab to global var for use later
	currTabName = tabname;
	
    //If the same tab was clicked as is already showing, do nothing
    
	if(currentTab === tabname) {
        return;
    }
    
    //set the new selected tab in the refresh form and submit it
    form.elements.tabname.value = tabname;
    var thisInstance = atg.qcsupply.productTabs;
    thisInstance.submitRefreshForm();
    },


    //This method is copied straight from picker.js in the blueprint.  I have no idea how it works, 
    //but it does!
    submitRefreshForm: function()
    {
      dojo.io.bind({
      load: function(type, data,evt){
        var divProductTabs = dojo.byId("atg_b2cblueprint_moreDetailsTabs");
		divProductTabs.innerHTML = data;
		
		// Set up references to powerReviews data and tab display area
		var divReviewDiv = dojo.byId("powerReviews");
		var divProductTabDetailArea = dojo.byId("atg_b2cblueprint_tabContent");
		
		
		// If current Tab is Reviews- display the stored review data
		if(currTabName == "reviews") displayReviews();
        
		//Not sure what this line does, but it was in the picker.js file, so I'm leaving it for now...
        dojo.widget.byId('richCart').hijackAllAddToCartNodes();
        },
      formNode: dojo.byId("tab_switchRefreshForm")
      });  
    }

};

var holder = false;
function displayReviews(){
	var divReviewDiv = dojo.byId("powerReviews");
	var divProductTabDetailArea = dojo.byId("atg_b2cblueprint_tabContent");
	if (!holder) {
		divProductTabDetailArea.innerHTML = divReviewDiv.innerHTML;
		holder = divReviewDiv.innerHTML;
		divReviewDiv.innerHTML = "";
	}
	else {
		divProductTabDetailArea.innerHTML = holder;
	}
}