$(document).ready(function()
{
	//---------------------------------------------START CUSTOM JS//
	
	function removeConfirmation()
	{
		$('div.basket_confirmation').fadeOut('slow',function()
		{
			$('div.basket_confirmation').remove();
		});
	}
	
	function customBasketAdded(form_id)
	{
		if($('div#header span.basket').length > 0)
		{
			$('div#header span.basket').replaceWith('<a class="basket" href="'+_root+'checkout/" title="View Basket">View Basket</a>');
		}
		
		if($('form#'+form_id).length > 0)
		{
			if($('form#'+form_id+' div.basket_confirmation').length > 0)
			{
				if(confirmation != undefined)
				{
					clearTimeout(confirmation);
				}
				
				removeConfirmation();
			}
			else
			{
				if($('p.stock_error').length > 0)
				{
					$('form#'+form_id+' button').after('<div class="basket_confirmation">There was not enough stock <br />to add all items to your basket! <strong>What would you like to do?<br /></strong><a class="continue" href="'+_root+'" title="Browse">Browse</a><a class="checkout" href="'+_root+'checkout/" title="Checkout">Checkout</a></div>');
				}
				else
				{
					$('form#'+form_id+' button').after('<div class="basket_confirmation">Your item has been added <br />to your basket! <strong>What would you like to do?<br /></strong><a class="continue" href="'+_root+'" title="Browse">Browse</a><a class="checkout" href="'+_root+'checkout/" title="Checkout">Checkout</a></div>');
				}
				
				var confirmation = setTimeout(removeConfirmation,2500);
				
				$('div.basket_confirmation a').unbind().bind('click',function()
				{
					if($(this).hasClass('checkout'))
					{
						return true;
					}
					else
					{
						if(confirmation != undefined)
						{
							clearTimeout(confirmation);
						}
						
						removeConfirmation();
						
						return false;
					}
				});
			}
		}
	}
	
	function customBasketRemoved()
	{
	}
	
	
	if($('li.mobile.warning').length > 0)
	{
		$('li.mobile.warning').css('background-position','178px bottom');
	}
	
	//---------------------------------------------END CUSTOM JS//
	
	checkBasket();
	
	function checkBasket()
	{
		checkFields();
		checkAdd();
		updateBasket();
		checkRemove();
		emptyBasket();
	}
	
	function checkFields()
	{
		$('form.add_to_basket').each(function()
		{
			var form_id = $(this).find('a.form_anchor').attr('name');
			
			$(this).attr('id',form_id);
			
			if($('form#'+form_id+' select').length > 0)
			{
				var basket_fields	= new Array();
				var count			= 0;
				
				$('form#'+form_id+' select').each(function()
				{
					basket_fields[count] = new Array($(this).prev('label').html(),$(this).attr('name'),$(this).val().replace('"','&quot;'),$(this).attr('tabindex'));
					
					count++;
				});
				
				if($('form#'+form_id+' input[name="quantity"]').length > 0)
				{
					basket_fields[count] = new Array('Hidden','quantity',1,'null');
				}
				
				$('form#'+form_id+' select').each(function()
				{
					$(this).unbind().bind('change',function()
					{
						updateFields(form_id,$(this).attr('name'),$(this).val(),basket_fields);
					});
				});
			}
		});
	}
	
	function updateFields(form_id,field_name,field_value,basket_fields)
	{
		if($('form#'+form_id+' input[name="product_id"]').length > 0)
		{
			var product_id = $('form#'+form_id+' input[name="product_id"]').val();
			
			$('form#'+form_id+' input[name="product_id"]').remove();
		}
		else
		{
			var product_id = 'null';
		}
		
		$.ajax({
				async			: false,
				url				: _root+'check-basket/',
				type			: "POST",
				data			: ({
										field_name				: field_name,
										field_value				: field_value.replace('"','&quot;'),
										all_fields				: basket_fields,
										product_id				: product_id,
										reference_product_id	: $('form#'+form_id+' input[name="reference_product_id"]').val(),
										bundle_product_id		: $('form#'+form_id+' input[name="bundle_product_id"]').val(),
										required_products		: $('form#'+form_id+' input[name="all_products"]').val(),
										ajax					: 'true'
									}),
				success			: function(data)
				{
					data = data.split('~');
					
					if(data.length > 0)
					{
						for(count = 0; count < data.length; count++)
						{
							if(data[count].substr(0,8) == 'product=')
							{
								var updated_fields = data[count].split('||');
								
								$('form#'+form_id+' fieldset.'+updated_fields[0].replace('product=','')+' ol').html(updated_fields[1]);
							}
							else if(data[count].substr(0,9) == 'quantity=')
							{
								$('form#'+form_id+' li.quantity').html(data[count].replace('quantity=',''));
							}
							else if(data[count].substr(0,6) == 'price=')
							{
								$('form#'+form_id).parent().find('span.price').html(data[count].replace('price=',''));
							}
							else if(data[count].substr(0,10) == 'was_price=')
							{
								$('form#'+form_id).parent().find('span.was_price').html(data[count].replace('was_price=',''));
							}
							else if(data[count].substr(0,11) == 'product_id=')
							{
								$('form#'+form_id+' button').before('<input type="hidden" name="product_id" value="'+data[count].replace('product_id=','')+'" />');
							}
						}
					}
					
					checkFields();
				}
		});
	}
	
	function checkAdd()
	{
		$('form.add_to_basket').unbind().bind('submit',function()
		{
			var form_id = $(this).find('a.form_anchor').attr('name');
			
			$(this).attr('id',form_id);
			
			if($('form#'+form_id+' input[name="product_id"]').length > 0)
			{
				var product_id = $('form#'+form_id+' input[name="product_id"]').val();
				
				if($('form#'+form_id+' input[name="quantity"]').length > 0)
				{
					var quantity = $('form#'+form_id+' input[name="quantity"]').val();
				}
				else if($('form#'+form_id+' select[name="quantity"]').length > 0)
				{
					var quantity = $('form#'+form_id+' select[name="quantity"]').val();
				}
				else
				{
					var quantity = 1;
				}
				
				if($('form#'+form_id+' input[name="volume"]').length > 0)
				{
					var volume = $('form#'+form_id+' input[name="volume"]').val();
				}
				else
				{
					var volume = 1;
				}
				
				if(product_id != '' && product_id != undefined)
				{
					if($('body').attr('id') == 'orders')
					{
						addToBasket(product_id,quantity,volume,'true',form_id,'full');
					}
					else if($('div.basket').hasClass('mini'))
					{
						addToBasket(product_id,quantity,volume,'false',form_id,'mini');
					}
					else
					{
						addToBasket(product_id,quantity,volume,'false',form_id,'full');
					}
				}
				else
				{
					checkBasket();
				}
			}
			else
			{
				$('form#'+form_id+' select').each(function()
				{
					if($(this).val() == 'null')
					{
						$(this).prev('label').html($(this).prev('label').html().replace(' <span class="warning">Please make a selection</span>','')+' <span class="warning">Please make a selection</span>');
						$(this).parent().addClass('warning');
						
						$(this).unbind().bind('change',function()
						{
							if($(this).val() != 'null')
							{
								$(this).prev('label').html($(this).prev('label').html().replace(' <span class="warning">Please make a selection</span>',''));
								$(this).parent().removeClass('warning');
							}
						});
					}
					else if($(this).parent().hasClass('warning'))
					{
						$(this).prev('label').html($(this).prev('label').html().replace(' <span class="warning">Please make a selection</span>',''));
						$(this).parent().removeClass('warning');
					}
				});
				
				checkBasket();
			}
			
			return false;
		});
		
		$('a.increase').unbind('click').bind('click',function()
		{
			var product_id = $(this).attr('href').split('/');
			
			product_id = product_id[(product_id.length-2)];
			
			var quantity = 1;
			
			addToBasket(product_id,quantity,1,'true');
			
			return false;
		});
	}
	
	function addToBasket(product_id,quantity,volume,summary,form_id,basket_style)
	{
		var basket_title		= $('div.basket h4').html();
		
		if($('div.basket_container a.checkout').length > 0)
		{
			var basket_button	= $('div.basket_container a.checkout').html();
		}
		else
		{
			var basket_button	= $('div.basket_container span.checkout').html();
		}
		
		$.ajax({
				async			: false,
				url				: _root+'add-to-basket/'+product_id,
				type			: "POST",
				data			: ({
										product_id		: product_id,
										quantity		: quantity,
										volume			: volume,
										summary			: summary,
										basket_style	: basket_style,
										basket_title	: basket_title,
										basket_button	: basket_button,
										ajax			: 'true'
									}),
				success			: function(data)
				{
					data = data.split('~');
					
					$('.basket_container').html(data[0]);
					
					if(data[1] != undefined)
					{
						$('.basket_totals').html(data[1]);
					}
					
					if($('div#helper').length > 0)
					{
						$('p.error').remove();
						$('.hidden').removeClass('hidden');
						$('div.confirm_basket form').submit();
					}
					
					$('.basket_container div.product-'+product_id).addClass('added');
					
					customBasketAdded(form_id);
					
					checkBasket();
				}
		});
	}
	
	function updateBasket()
	{
		$('div.delivery_options select#delivery_country').unbind().bind('change',function()
		{
			$('div.delivery_options button').click();
		});
		
		$('div.confirm_basket form button').bind('click',function()
		{
			$(this).addClass('clicked');
		});
		
		$('div.confirm_basket form').unbind().bind('submit',function()
		{
			if($('button.clicked').attr('name') == 'addOrder')
			{
				return true;
			}
			else
			{
				$('button.clicked').removeClass('clicked');
				
				var quantity			= '';
				var delivery_country	= '';
				var delivery_postcode	= '';
				var delivery_option		= '';
				var voucher_code		= '';
				
				$(this).find('input').each(function()
				{
					if($(this).attr('name').substr(0,9) == 'quantity-')
					{
						quantity	   += $(this).attr('name').substr(9)+':'+$(this).attr('value')+'~';
					}
				});
				
				if($(this).find('select#delivery_country').length > 0)
				{
					delivery_country	= $(this).find('select#delivery_country').val();
				}
				
				if($(this).find('input#delivery_postcode').length > 0)
				{
					delivery_postcode	= $(this).find('input#delivery_postcode').val();
				}
				
				if($(this).find('input[name="delivery_option"]:checked').length > 0)
				{
					delivery_option		= $(this).find('input[name="delivery_option"]:checked').val();
				}
				
				if($(this).find('input#voucher_code').length > 0)
				{
					voucher_code		= $(this).find('input#voucher_code').val();
				}
				
				$.ajax({
						async			: false,
						url				: _root+'update-basket/',
						type			: "POST",
						data			: ({
												quantity			: quantity,
												delivery_country	: delivery_country,
												delivery_postcode	: delivery_postcode,
												delivery_option		: delivery_option,
												voucher_code		: voucher_code,
												summary				: 'true',
												ajax				: 'true'
											}),
						success			: function(data)
						{
							data = data.split('~');
							
							$('.basket_container').html(data[0]);
							
							if(data[4] != undefined)
							{
								$('.delivery_options').html(data[1]);
								$('.basket_totals').html(data[2]);
								$('.voucher_code').html(data[3]);
								$('.continue_checkout').html(data[4]);
							}
							else if(data[3] != undefined)
							{
								$('.delivery_options').html(data[1]);
								$('.basket_totals').html(data[2]);
								$('.continue_checkout').html(data[3]);
								
								if($('.voucher_code').length > 0)
								{
									$('.voucher_code').remove();
								}
							}
							else if(data[2] != undefined)
							{
								$('.delivery_options').html(data[1]);
								$('.basket_totals').html(data[2]);
							}
							else if(data[1] != undefined)
							{
								$('.basket_totals').html(data[1]);
							}
							
							if($('button.continue').length == 0 || $('input[name="no_payment"]').length > 0)
							{
								$('div.alternative').hide();
							}
							else
							{
								$('div.alternative').show();
							}
							
							if($('div#helper').length > 0)
							{
								if($('.basket_container p.error').length > 0)
								{
									$('.voucher_code').addClass('hidden');
									$('.continue').addClass('hidden');
								}
								else
								{
									$('.basket_container a').each(function()
									{
										$(this).removeAttr('href').attr('name','disabled');
									});
									
									$.ajax({
											async			: false,
											url				: _root+'update-basket/',
											type			: "POST",
											data			: ({
																	referrer				: $('select#referrer').val(),
																	other_referrer			: $('input#other_referrer').val(),
																	delivery_instructions	: $('input#delivery_instructions').val(),
																	gift_message			: $('input#gift_message').val(),
																	payment_method			: $('select#payment_method').val(),
																	fk_flag_id				: $('select#fk_flag_id').val(),
																	consignment				: $('input#consignment').val(),
																	contact_id				: $('input[name="fk_contact_id"]').val(),
																	summary					: 'admin',
																	ajax					: 'true'
																}),
											success			: function(data)
											{
												$('.basket_detail').html(data);
												
												$('li.hidden').hide();
												
												$.getScript(_adminRoot+'orders/media/js/basket.js');
											}
									});
								}
							}
							
							checkBasket();
						}
				});
				
				return false;
			}
		});
		
		if($('div.confirm_basket form input[name="delivery_option"]').length > 0)
		{
			$('div.confirm_basket form input[name="delivery_option"]').each(function()
			{
				$(this).unbind().bind('change',function()
				{
					$('div.confirm_basket form').submit();
				});
			});
		}
	}
	
	function checkRemove()
	{
		$('a.remove').unbind('click').bind('click',function()
		{
			var product_id = $(this).attr('href').split('/');
			
			product_id = product_id[(product_id.length-2)];
			
			if($(this).hasClass('summary'))
			{
				var summary = 'true';
			}
			else
			{
				var summary = 'false';
			}
			
			removeFromBasket(product_id,summary);
			
			return false;
		});
		
		$('a.decrease').unbind('click').bind('click',function()
		{
			var product_id = $(this).attr('href').split('/');
			
			product_id = product_id[(product_id.length-2)];
			
			removeFromBasket(product_id,'true');
			
			return false;
		});
	}
	
	function removeFromBasket(product_id,summary)
	{
		var delivery_country	= '';
		var delivery_postcode	= '';
		var delivery_option		= '';
		
		if($('div.confirm_basket form').find('select#delivery_country').length > 0)
		{
			delivery_country	= $('div.confirm_basket form').find('select#delivery_country').val();
		}
		
		if($('div.confirm_basket form').find('input#delivery_postcode').length > 0)
		{
			delivery_postcode	= $('div.confirm_basket form').find('input#delivery_postcode').val();
		}
		
		if($('div.confirm_basket form').find('input[name="delivery_option"]:checked').length > 0)
		{
			delivery_option		= $('div.confirm_basket form').find('input[name="delivery_option"]:checked').val();
		}
		
		$.ajax({
				async			: false,
				url				: _root+'remove-product/'+product_id+'/',
				type			: "POST",
				data			: ({
										product_id			: product_id,
										delivery_country	: delivery_country,
										delivery_postcode	: delivery_postcode,
										delivery_option		: delivery_option,
										summary				: summary,
										ajax				: 'true'
									}),
				success			: function(data)
				{
					data = data.split('~');
					
					$('.basket_container').html(data[0]);
					
					if(data[4] != undefined)
					{
						$('.delivery_options').html(data[1]);
						$('.basket_totals').html(data[2]);
						$('.voucher_code').html(data[3]);
						$('.continue_checkout').html(data[4]);
					}
					else if(data[3] != undefined)
					{
						$('.delivery_options').html(data[1]);
						$('.basket_totals').html(data[2]);
						$('.continue_checkout').html(data[3]);
						
						if($('.voucher_code').length > 0)
						{
							$('.voucher_code').remove();
						}
					}
					else if(data[2] != undefined)
					{
						$('.delivery_options').html(data[1]);
						$('.basket_totals').html(data[2]);
					}
					else if(data[1] != undefined)
					{
						$('.basket_totals').html(data[1]);
					}
					
					customBasketRemoved();
					
					checkBasket();
				}
		});
	}
	
	function emptyBasket()
	{
		$('a.empty').unbind('click').bind('click',function()
		{
			$.ajax({
					async			: false,
					url				: _root+'empty-basket/',
					type			: "POST",
					data			: ({
											ajax			: 'true'
										}),
					success			: function(data)
					{
						$('div.basket_container').html(data);
						
						checkBasket();
					}
			});
			
			return false;
		});
	}
});
