var SkuCost = Class.create();
SkuCost.prototype = {
	
	 initialize : function(element) {
		this.element = $(element);
		this.item_id = this.element.item_id.value;
		this.colourFld = this.element.getElementsByClassName('colour_id')[0];
		this.sizeFld = this.element.getElementsByClassName('size_id')[0];
		this.typeFld = this.element.getElementsByClassName('type_id')[0];
	
		this.typeid = 0;
		this.sizeid = 0;
			callCost = this.activate.bindAsEventListener(this);
			
			if(this.sizeFld != null){
				this.sizeid = this.sizeFld.options[this.sizeFld.selectedIndex].value;
				this.sizeFld.observe('change',callCost)
				}
				
				if(this.colourFld!=null) { 
				this.colourid = this.colourFld.options[this.colourFld.selectedIndex].value;
					this.colourFld.observe('change',callCost);
				}
					
		if(this.typeFld != null){
			this.typeid = this.typeFld.options[this.typeFld.selectedIndex].value;
			this.typeFld.observe('change',callCost)
			
			}	
		
	
	
		
	
		
			Event.observe(this.colourFld,'change',callCost);
							
					this.getSkuCost()
						
					
		},
		
	activate : function(ev){
	Event.stop(ev);
	var elm = Event.findElement(ev, "select");
	
	this.getSkuCost();
	},
		
		getSkuCost : function(){
		//	alert("get cost")
		

		var form = this.element;
		var itemid = form.item_id.value;
		
		if(this.sizeid>0){
		this.sizeid = this.sizeFld.options[this.sizeFld.selectedIndex].value;
		}
		if(this.colourFld!=null) var colourid = this.colourFld.options[this.colourFld.selectedIndex].value;
		if(this.typeFld!=null) var typeid = this.typeFld.options[this.typeFld.selectedIndex].value
		if(this.typeid>0){
		this.typeid = this.typeFld.options[this.typeFld.selectedIndex].value;
						}			
		params="item_id="+itemid+"&colour_id="+colourid+"&size_id="+this.sizeid+"&type_id="+typeid;
	//	console.log(params)
		var url = root+'/ajax/ajax.cost.php';
		//alert(url);
//console.log(url)
			new Ajax.Request(url,{
							method: 'get',
							parameters: params,
							onSuccess: this.skuCostResponse.bind(this)

						});

			},
		

		
			
			skuCostResponse : function(t){
				//alert(t.responseText)
				respArray = t.responseText.split('|');
			
			
				costElm = this.element.getElementsByClassName("price")[0];
				skuElm = this.element.getElementsByClassName("sku_id")[0];
				//console.log(skuElm)
			$(skuElm).value = respArray[3];
				var html = '<table cellspacing="0" cellpadding="0" border="0"><tr><td>'+respArray[0]+'</td><td>'+respArray[1]+'</td></tr><tr><td colspan="2">Delivery: '+respArray[2]+'</td></tr>';
				if(respArray[4]>1){
				html+= '<tr><td colspan="2"><b>Minimum Order: '+respArray[4]+' units</b></td></tr>';
			}
				html+='</table>';
				$(costElm).innerHTML = html;
				
			}
			
}