/*
 * Copyright (C) 2009 Ignition Commerce.
 * All Rights Reserved.  No use, copying or distribution of this
 * work may be made except in accordance with a valid license
 * agreement from IgnitionCommerce.  This notice must be
 * included on all copies, modifications and derivatives of this
 * work.
 *
 * @Author Brian Boyett
 * @Version 1.0
 * @Since 1.0
 * 
 * This javascript is to support the functionality of the proportions.jsp,
 * which displays the various proportion styles associated with a style.
 * 
 * The maps below contain information used in the changeProportion function.
 * This allows us to change the values at will.  
 * 
 * proportionAjaxDivs contains a key (a given name for a page) and a 
 * value (the div on the page that will be reloaded with the new product elements).  
 * This is required.
 * 
 * proportionMultiAjaxDivs simply has a true or false value for each page. 
 * True would mean that the page has multiple divs that will have different 
 * products and false would mean a single div on the page to contain a product. This
 * is required.
 * 
 * proportionLoadPages contains the url (minus the context root) to each page. This is
 * required.
 * 
 * proportionParameters contains comma separated list of parameters that will need
 * to be added to the page url.  The script assumes that you have an element with 
 * the same name of the parameter on the page that has been populated with the value.
 * This way, it can simply look for the element and retrieve the value. This is optional.
 * 
 * proportionAjaxCallbacks is a function that would be called after the ajax (load) 
 * is finished.  This is optional.
 */ 

var proportionAjaxDivs = {
  'outfit'      	: 'outfitItemContainer',
  'quick.view'		: 'thumbNailProductDetailContainer',
  'product.detail'	: 'bodyContainer'
};

var proportionMultiAjaxDivs = {
  'outfit'			: true,
  'quick.view' 		: false,
  'product.detail' 	: false
};

var proportionLoadPages = {
  'outfit'			: '/browse/content/outfitItem.jsp',
  'quick.view'		: '/browse/content/thumbNailProductDetail.jsp',
  'product.detail'	: '/browse/productDetailContainer.jsp'
};

var proportionParameters = {
  'outfit'			: 'oiProduct,idx',
  'quick.view'		: 'icProduct,icParent,icSort',
  'product.detail' 	: 'icProduct,icCategory'
};

var proportionAjaxCallbacks  = {
  'outfit'			: outfitAjaxCallback	
};

function outfitAjaxCallback(idx, productId) {
  outfitSetSizes(idx, productId);
}

function changeProportion(style, idx) {
  var page = jq('#icProportionPage').val();
  var multiAjax = proportionMultiAjaxDivs[page];
  var ctl = jq('#icProduct').get(0);
  if(ctl == null) { 
    ctl = jq('#oiProduct' + idx).get(0);
  }
  ctl.value = style;

  var contextRoot = jq('#contextroot').val();
  var divToLoad = proportionAjaxDivs[page];
  var pageToLoad = proportionLoadPages[page];
  var parameters = proportionParameters[page].split(',');

  var fullLoadPath = contextRoot + pageToLoad;

  if(multiAjax) {
    divToLoad += idx;
  }

  var productId;
  if(parameters.length > 0) {
    fullLoadPath += '?';
    for(var i = 0; i < parameters.length; i++) {
	  var nextParam = parameters[i];
      if(i != 0) {
	    fullLoadPath += '&';  
      }
	  fullLoadPath += nextParam + '=';

	  if(nextParam == 'oiProduct') {
		productId = jq('input#' + nextParam + idx).val();
      }
	  if(multiAjax) {
	    fullLoadPath += jq('input#' + nextParam + idx).val();
	  } else {
	    fullLoadPath += jq('input#' + nextParam).val();
	  }
    }
  }
  if(proportionAjaxCallbacks[page] != null) {
    jq('#' + divToLoad).load(fullLoadPath, {},  function() {eval(proportionAjaxCallbacks[page](idx, productId));}); 
  }
  else {
	jq('#' + divToLoad).load(fullLoadPath); 
  }
}  