jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
		
	$.preloadImages(ROOT + "/library/pezz/images/loader.gif");
	$('#buttons #basket_cover').css('opacity',0.6);

	
	$('.your_choice input').blur(function(){		
				
		var qty = document.addtocartform.add_qty.value;
		var price = document.addtocartform.add_price.value;
							
				if(qty >= 1)
					{	
						var price1 = price * qty;
						var total_price = Math.round(price1*100)/100;
						$(".your_price #total_price").html("£" + total_price);
					}
				else
					{
						$(".your_choice #text-box-div").addClass('error-border');
						alert("Please enter a number");
					}
		
	});
	
	$('.your_choice input').click(function(){		
				
		$('.your_choice div').removeClass('error-border');
	});
	
	$('#buttons #basket_cover').click(function(){
		alert("Please select a size/colour.");
	});		
		
		
	
	$('.select_id div div div').click(function(){		
		
		
		var colour = ($(this).parent('div').attr('id'));
		
		var size = this.id.substr(10,25);
		var the_id = this.id.substr(0,10);

		// set all divs as normally unclicked
		$('.select_id div div div').removeClass('red-bg');
		$('#choices #colour_size').removeClass('error');
		$('#buttons #basket_cover').stop().fadeOut(300);
		
		//$('#success #success_result').hide();
		
		if(this)
			{
				// add value of clicked div to hidden form field
				document.addtocartform.add_id.value = the_id;
				
				
				if(size != '' && size != 'One_Size_Only')
					{
						$("#colour_size").html("<span class='uppercase'>" + size + " - " + colour + "</span>");
					}
				else
					{
						$("#colour_size").html("<span class='uppercase'>" + colour + "</span>");
					}

				// give the clicked div a different background colour
				$(this).addClass('red-bg');
			}
		
	});
	
	
	
	$("img#submit").click(function(){		
		
		var hasError = false;
		
		var idVal = document.addtocartform.add_id.value;
		
		if(idVal == '') {
			$("#choices #colour_size").html('Select above...');
			$('#choices #colour_size').addClass('error');
			$('#choices #colour_size').addClass('error-border');
			hasError = true;
		}
		else
			{
				var qtyVal = document.addtocartform.add_qty.value;
				
				if(qtyVal == '') {
					
					qtyVal = 1;
				}
				
				var priceVal = document.addtocartform.add_price.value;
				
				//if(priceVal == '') {
				//	$(".choices #total_price").html("");
				//	$('.choices #total_price').addClass('error');
				//	hasError = true;
				//}	
			}
		
		if(hasError == false) {
			
			$('#success').html("");
	
			//$(this).hide();
			$("#success").html("<div id='loading_box' style='float: left; width: 303px' class='border-dotted p10 m10b odd dark-text uppercase bold f10 center'><img src='"+ROOT+"/library/pezz/images/loader.gif' alt='Loading' id='loading'></div>");
			
			$.post(ROOT + "/shop/add_to_cart.php",
   				{ add_id: idVal, add_qty: qtyVal, add_price: priceVal },
   					function(data){			
							  
	   						// hide loading box
		
							// find current total in html span
							var current_cart_total = $('#subheader_cart span#subheader_cart_total').html(); 
						
							var current_cart_qty = (+$("#subheader_cart #subheader_cart_qty").html()) || 0; 
	
	
							var total_qty_current = Number(current_cart_qty) + Number(qtyVal);
							
							// find the total price of qty/prive for this product add
							var total_price = Number(qtyVal) * Number(priceVal);
							//add the current cart total to this subtotal
							var total_price_current = Number(current_cart_total) + Number(total_price);
		
							$('span#subheader_cart_qty').html(total_qty_current);  
							
							$('span#subheader_cart_total').html(total_price_current);
							
							var color_size_report = $("#colour_size").html();
							$("#success").html("<div id='success_result' style='width: 313px; float: left' class='border-dotted p10 odd dark-text uppercase bold f10'><div id='float-left' style='width: 36px'><img src='" + ROOT + "/shop/images/icon_tick.gif'></div><div id='float-left' class='f10 dark-text normal' style='width: 267px'>Product successfully added to your cart!<br><span class='f11 error'>Size/colour: "+color_size_report+", Qty: "+qtyVal+"</span></div></div>");
							
							
							// reset form values so that it'll chuck errors if they add another straight away
							document.addtocartform.add_price.value = priceVal;
							document.addtocartform.add_qty.value = "1";
							$('#total_price').html("£"+priceVal);
							
							// if its one size and one colour only then don't reset the ID field
							// otherwise wipe it so they have to select a size/colour
							// it checks the existance of a hidden div only coded in the OSO display
							var check_if_its_oso = $('#oso').html();
							
							if(!check_if_its_oso)
								{
									document.addtocartform.add_id.value = "";
									$('.select_id div div div').removeClass('red-bg');
									$('#colour_size').html("&nbsp;");
								}
							
   					}
				 );
		}
		
		return false;
	});						   
});