// preload images
(function($) {
var cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function() {
var args_len = arguments.length;
for (var i = args_len; i--;) {
var cacheImage = document.createElement('img');	
cacheImage.src = arguments[i];
cache.push(cacheImage);
}
}
})(jQuery)
jQuery.preLoadImages("/images/added.gif","/images/but_add_to_cart.gif","/images/but_roastsblends.gif", "/images/close.gif");

// price arrays, used for display only
price = Array();
price[1] = 8;
price[2] = 18.5;
price[3] = 34.5;

decaf = Array();
decaf[1] = 8.5;
decaf[2] = 18.5;
decaf[3] = 36;


decaf_product_id = 19;

current_product_id = '';

Number.prototype.toDecimals=function(n){
n=(isNaN(n))?
2:
n;
var
nT=Math.pow(10,n);
function pad(s){
s=s||'.';
return (s.length>n)?
s:
pad(s+'0');
}
return (isNaN(this))?
this:
(new String(
Math.round(this*nT)/nT
)).replace(/(\.\d*)?$/,pad);
}

function update_price(product_id,unit_id,qty){
	
	if(product_id == decaf_product_id){
		var unit_price = decaf[unit_id];
	} else {
		var unit_price = price[unit_id];
	}
	var total = (unit_price * qty);
	
	var newtotal = total.toDecimals(2);

	
	$("#price").text("$" + newtotal);
	
	
}


// when buy clicked
function buy(name,product_id){
	
	current_product_id = product_id;
	update_price(current_product_id, 1, 1);	

	$.post("index.php", { getprod: current_product_id }, function (data){ 
		newdata = data.split("||");

		$("#prodname").text(newdata[0]);
		$("#grind").html(newdata[1]);

		$('#buypanel').jqm().jqmShow();
		$('#buyconfirm').hide();
		$('#buyoptions').show();
		$('#unit_id').attr('selectedIndex', 0);
		$("#qty").val(1);
	});	

}



$(document).ready(function(){ 
		
	$("#buypanel #qty").live("keyup", function(){
		if((!isNaN($(this).val())) && ($(this).val().indexOf(".") == -1)){
			update_price(current_product_id, $("#unit_id").val(), $(this).val());	
		}
	});
	
	$("#buypanel #unit_id").live("change", function(){
		update_price(current_product_id, $("#unit_id").val(), $("#qty").val());	
	});
	
	$("#buypanel #unit_id").live("change", function(){
		update_price(current_product_id, $("#unit_id").val(), $("#qty").val());	
	});
	
	// when add to cart clicked
	$("#buypanel .add").live("click", function(){
	
		var grind = $("#grind").val();
		var unitid = $("#unit_id").val();
		var quantity = $("#qty").val();
	
		$.post("/coffee/add_item.php", { action: "addtocart", qty: quantity, grind: grind, product_id: current_product_id, unit_id: unitid }, function (data){ 
			if(data == 'added'){
				$('#buyoptions').hide();
				$('#buyconfirm').show();
				var prod_added = $("#prodname").text();
				var unit_added = $("#unit_id :selected").text();
				$("p#summary").text(quantity+"x "+prod_added+" ("+grind+" "+unit_added+")");
				
		   		$("img#but"+current_product_id).attr("src","/images/added.gif");
		      //  $("img#but"+current_product_id).parent("a").attr("href","/shop/");				

			}
		});	
		
	});

	$("#buypanel a.roastsblends").live("click",function(){
		$('#buypanel').jqmHide();
	});
});





