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.5);
	$('#buttons #basket_cover2').css('opacity',0.5);
	$('#view_cart_cover').css('opacity',1);

	
	$('.your_choice select').blur(function(){		
				
		var qty = document.addtocartform.add_qty.value;
		var price = document.addtocartform.add_price.value;
		var spec_id = document.addtocartform.spec_id.value;
							
		if(spec_id && qty >= 1)
			{	
				var price1 = price * qty;
				var total_price = Math.round(price1*100)/100;
				$(".your_price #total_price").html("&pound; " + total_price);
			}
		else
			{
				if(!spec_id)
					{
						alert("Please select a size/colour");
					}
				else
					{
						$(".your_choice #text-box-div").addClass('error-border');
						alert("Please select a size/colour");
					}
			}
		
	});
	
	$('.your_choice input').click(function(){		
				
		$('.your_choice div').removeClass('error-border');
	});
	
	$('#buttons #basket_cover').click(function(){
		alert("Please select a size/colour");
	});		
	$('#buttons #basket_cover2').click(function(){
		alert("To increase your required quantity of this product please vivit your shopping basket.");
	});		
		
		
	
	$('.select_id div div div.ins').click(function(){
		
		var details = ($(this).attr('id'));
		
		var qty_av = ($(this).find('img').attr('id'));
		
		// div id is in thye form id_size_price
		
		var myArray = details.split('_'); 
		
		var the_id = myArray[0];
		var colour = myArray[1];
		var the_size = myArray[2];
		var the_price = myArray[3];
		

		// set all divs as normally unclicked
		$('.select_id div div div').removeClass('red-border');
		$('#choices #colour_size').removeClass('error');
		$('#buttons #basket_cover').fadeOut(300);
		
		//$('#success #success_result').hide();
		
		// add value of clicked div to hidden form field
		document.addtocartform.spec_id.value = the_id;
		document.addtocartform.add_price.value = the_price;
		document.addtocartform.add_qty.value = 1;
		
		// clear options from dropdown select
		$('SELECT#add_qty').html('');
		
		// fill drop down qty box
		for(select_no=1; select_no<=qty_av;select_no=select_no+1)
			{
				// add an option to the drop down select
				$('#add_qty').append('<option value='+select_no+'>'+select_no+'</option>');
			}

		
		
		
		if(the_size && the_size != 'One Size Only')
			{
				var colour_size_details = the_size;
			}
		else
			{
				var colour_size_details = "";
			}
			
		if(colour && colour != 'None')
			{
				if(the_size && the_size != 'One Size Only')
					{
						colour_size_details += " | "+colour;
					}
				else
					{
						var colour_size_details = colour ;
					}
			}
				
		$("#colour_size").html("<span class='uppercase black-text'>" + colour_size_details + "</span>");
		
		$("#total_price").html("&pound; " + the_price);

		// give the clicked div a different background colour
		$(this).addClass('red-border');
		
	});
	
	
	
	$("img#submit").click(function(){		
		
		var hasError = false;
		
		var idVal = document.addtocartform.add_id.value;
		var specidVal = document.addtocartform.spec_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='error-border p10 m10b green-text uppercase bold f10 center'><img src='"+ROOT+"/images/loader.gif' alt='Loading' id='loading'></div>");
			
			$.post(ROOT + "/shop/add_to_cart.php",
   				{ add_id: idVal, spec_id: specidVal, add_qty: qtyVal, add_price: priceVal },
   					function(data){
	   					
	   					
	   					
	   					if(data)
							{
								var extra_message = "<div id='error_result' style='width: 283px; float: left' class='grey-box error normal uppercase'>"+data+"</div>";
							}
						else
							{
								var extra_message = "";
							}
							
						// do the rest
						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 colour_size_details2 = $("#colour_size").html();
						
						if(colour_size_details2)
							{
								var color_size_report = "Size/colour: "+colour_size_details2+", ";
							}
						else
							{
								var color_size_report = "";
							}
						
						$("#success").html(extra_message+"<div id='success_result' style='width: 283px; float: left' class='grey-box m10b uppercase bold'><div id='float-left' style='width: 36px'><img src='" + ROOT + "/shop/images/icon_tick.gif'></div><div id='float-left' class='normal f10' style='width: 247px'>Product successfully added to your cart!<br><span class='error f12'>"+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.spec_id.value = "";
								$('.select_id div div div').removeClass('red-bg');
								$('#colour_size').html("&nbsp;");
							}
							
						
						$('#buttons #basket_cover').fadeIn(300);
						$('#buttons #basket_cover2').fadeIn(300);
						
						$('#view_cart_cover').fadeOut(300);
						
							  
   					}
				 );
		}
		
		return false;
	});					
	

		
	
	
	
		   
});






