// Options pricing script by Erik Daniel http://landmine-design.com
//Created: Oct7th, 2008
//Version 1

// This script gets the pricing for a configurable product from the option SKU based on the other option
// chosen by the user
// It also forces the user to choose option from top to bottom
// and resets the price if the user changes option after the price has been set

$(document).ready(function(){
		
		// is this a configurable product?
		var config = $("div.wrapperAttribsOptions:last h4 label.attribsSelect").text();
		if( config == "SKU" ){
			var $_GET = {};

			document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
			    function decode(s) {
			        return decodeURIComponent(s).replace(/\+/g, " ");
			    }

			    $_GET[decode(arguments[1])] = decode(arguments[2]);
			});

			var action = $_GET["action"];
	

		
		
		
		
			$("div.wrapperAttribsOptions:last").css("display","none");
			if($("div.wrapperAttribsOptions input[type='checkbox']").length > 1){
				$("div.wrapperAttribsOptions input[type='checkbox']").attr("disabled","disabled");
			}
			var nId = 0;
			
			var last = $("div.wrapperAttribsOptions").length  - ($("div.wrapperAttribsOptions").length - $("div.wrapperAttribsOptions:has(select)").length) - 2;
	

			var originalPrice = $('#productPrices').text();
			
			// Array to carry the configurable product options
			var aOptions = new Array(
									"Size",
									"Paper",
									"Pages",
									"Panels",
									"Color",
									"Folding",
									"Binding",
									"Style",
									"Quantity"
									);
			
			// this can be removed by the jQuery inArray builtin
			function oc(a)
			{
			  var o = {};
			  for(var i=0;i<a.length;i++)
			  {
				o[a[i]]='';
			  }
			  return o;
			}
			
			function resetOptions(x)
			{	
				$("div.wrapperAttribsOptions:gt(" + x + ") select").attr("disabled","disabled");	
				$("div.wrapperAttribsOptions:gt(" + x + ") select option:contains(Select)").attr("selected", true);
				
			}
			
			function nextOption(x)
			{
			
				$("div.wrapperAttribsOptions:gt(" +  x  + ") select option:contains(Select)").attr("selected", true);
				$("div.wrapperAttribsOptions:gt(" + x  + ") select").attr("disabled","disabled");	
	
				var myVal = $('div.wrapperAttribsOptions:eq(' + x + ') select option:selected').text();	
				
				if(myVal.indexOf("Select") < 0 ){
					$("div.wrapperAttribsOptions:eq(" + (x + 1)+ ") select").removeAttr("disabled");
				}
				$('#productPrices').html(originalPrice);
			
			}
			
			function updatePrice(x)
			{
				var sku = '';
				//get the price from the selected SKU option and update the price listing
				for(var y = 0; y <= last; y++)
				{
					option = $('div.wrapperAttribsOptions:eq(' + y + ') h4 label.attribsSelect').text();
					nextoption = $('div.wrapperAttribsOptions:eq(' + (y +1) + ') h4 label.attribsSelect').text();
					
					if(option in oc(aOptions))
					{
						sku += $('div.wrapperAttribsOptions:eq(' + y + ') select option:selected').attr("title");
						if(nextoption in oc(aOptions))
						{
							sku += '-';
						}
					 }	
				}
				
				//change rhe pricing attribute to the correct sku which will update the pricing and weight of the product
				$("div.wrapperAttribsOptions:last select").removeAttr("disabled");
				$("div.wrapperAttribsOptions:last select option:contains(" + sku + ")").attr("selected", true);
				
				// show the customer the price with the selected options
				var sPrice = $("div.wrapperAttribsOptions:last select option:contains(" + sku + ")").text();
				
				var updatedPrice = sPrice.substring(sPrice.indexOf("+")+1, sPrice.length - 1 );
				$('#productPrices').html(updatedPrice);
				
			}
			
			// handle the checkboxes 
			// some products have variable design rates
			// enable/disable the rates based on what the customer has chosen
	
			function checkboxHandler(){
				
				if($("input[type='checkbox']").length > 1)
				{
					$('div.wrapperAttribsOptions select option:selected').each(function(){
						var z = $(this).text();
						$("div.wrapperAttribsOptions label.attribsCheckbox").each(function(){
							var zz = ($(this).text()).split(" Design");	
							if(z.indexOf(zz[0]) >= 0)
							{
								var x = $(this).attr("for");
								$("div.wrapperAttribsOptions input[id!=" + x +"][type!=file]").removeAttr("checked").attr("disabled","disabled");
								$("div.wrapperAttribsOptions input[id=" + x +"]").removeAttr("disabled");
							//	console.log(zz[0] + " match");
							}
						});
					});

				}
				
				//if it was pertinent get rid of the 
			
			}
			
			if(action != "add_product") {
				resetOptions(nId);
			} else {
				updatePrice();
			}
			
			//Handle the changes in the select inputs
			$("div.wrapperAttribsOptions select").change(function() {
				// gets the value of the selected item
				var index = $('div.wrapperAttribsOptions select').index(this); 
				checkboxHandler();
				
				nId++;
				if(index < last){
					nextOption(index);
				}
				else
				{
					//calculate price
					updatePrice();
				}
			
			});// eo select change function
		
		}//eo if
			
	});

