// JavaScript Document

$(function(){ 
	if($('#gallery').length > 0){
		$("#gallery a").fancybox();		
	}
	// Find a stockist
	if($('#findStockists').length > 0){
		$('#address').focus(function(){
			if($('#address').val() == 'Enter your town or postcode'){
				$('#address').val('')
			}
		});
		$('#address').blur(function(){
			if($('#address').val() == ''){
				$('#address').val('Enter your town or postcode')
			}
		});
		
		$('#findStockists').submit(function(){
			if($('#address').val() == 'Enter your town or postcode' || $('#address').val() == ''){
				
			} else {
				$('#mapContainer').focus();
				setCenter($('#address').val());			
			}
			return false;
		});
		
		$('#stockists').change(function(){
			if($('option:selected', this).val() != ''){
				setCenter($('option:selected', this).val());
				var id = $('option:selected', this).attr('id').split('_');
				$.get('/stockists/get/'+id[1]+'/'+Math.random(), {}, function(data){
					$('#result').html(data);
				});		
			}
		});
	}
	if($("ul#scroller li").length > 0){
		$("ul#scroller").simplyScroll({
			className: 'vert',
			horizontal: false,
			frameRate: 20,
			speed: 5
		});
	}

	if($(".accordian").length > 0){
		$(".accordian").hrzAccordion({openOnLoad:"1",fixedWidth: 829, handlePosition: "left", openEaseAction: "easeOutCubic", closeEaseAction: "easeInCubic"});
		setTimeout("$('.handle').eq(1).click();",1000);
		setTimeout("$('.handle').eq(2).click();",3000);
		setTimeout("$('.handle').eq(0).click();",5000);
	}
	
	if($("ul#bottles").length > 0){
		if(($('#quantity').val()*1) > 1){
			$("ul#bottles li").not('ul#bottles li#bottle_'+$('#quantity').val()).addClass('active').animate({opacity: 0},5);
			updatePrice(qty);
		} else {
			$("ul#bottles li").not('ul#bottles li#bottle_1').addClass('active').animate({opacity: 0},5);
		}
		setTimeout("$('ul#bottles li').css('display', 'block');",5);
		$('#qtyUp').click(function(){		
			var li = $("ul#bottles li.active").attr('id').split('_');
			qty = (li[1]*1)+1;
			if($("ul#bottles li#bottle_"+qty).length > 0){
				$("ul#bottles li.active").removeClass('active');
				$("ul#bottles li#bottle_"+qty).addClass('active').animate({opacity: 1},500);
			}
			updatePrice(qty);
			return false;
		});
		$('#qtyDown').click(function(){		
			var li = $("ul#bottles li.active").attr('id').split('_');
			qty = (li[1]*1)-1;
			if($("ul#bottles li#bottle_"+qty).length > 0){
				$("ul#bottles li.active").removeClass('active').animate({opacity: 0},500);
				$("ul#bottles li#bottle_"+qty).addClass('active');
			} else {
				qty = 1;
			}
			updatePrice(qty);
			return false;
		});
		
		
		if(($('#tin_quantity').val()*1) > 1){
			$("ul#tins li").not('ul#tins li#tin_'+$('#tin_quantity').val()).addClass('active').animate({opacity: 0},5);
			updateTinPrice(qty);
		} else {
			$("ul#tins li").not('ul#tins li#tin_1').addClass('active').animate({opacity: 0},5);
		}
		setTimeout("$('ul#tins li').css('display', 'block');",5);
		$('#tin_qtyUp').click(function(){		
			var li = $("ul#tins li.active").attr('id').split('_');
			qty = (li[1]*1)+1;
			if(qty < 4){
				if($("ul#tins li#tin_"+qty).length > 0){
					$("ul#tins li.active").removeClass('active');
					$("ul#tins li#tin_"+qty).addClass('active').animate({opacity: 1},500);
				}
			} else {
				qty = 3;
			}
			updateTinPrice(qty);
			return false;
		});
		$('#tin_qtyDown').click(function(){		
			var li = $("ul#tins li.active").attr('id').split('_');
			qty = (li[1]*1)-1;
			if($("ul#tins li#tin_"+qty).length > 0){
				$("ul#tins li.active").removeClass('active').animate({opacity: 0},500);
				$("ul#tins li#tin_"+qty).addClass('active');
			} else {
				qty = 1;
			}
			updateTinPrice(qty);
			return false;
		});
		
			
		if(($('#jar_quantity').val()*1) > 1){
			$("ul#jars li").not('ul#jars li#jar_'+$('#jar_quantity').val()).addClass('active').animate({opacity: 0},5);
			updateJarPrice(qty);
		} else {
			$("ul#jars li").not('ul#jars li#jar_1').addClass('active').animate({opacity: 0},5);
		}
		setTimeout("$('ul#jars li').css('display', 'block');",5);
		$('#jar_qtyUp').click(function(){		
			var li = $("ul#jars li.active").attr('id').split('_');
			qty = (li[1]*1)+1;
			if(qty < 6){
				if($("ul#jars li#jar_"+qty).length > 0){
					$("ul#jars li.active").removeClass('active');
					$("ul#jars li#jar_"+qty).addClass('active').animate({opacity: 1},500);
				}
			} else {
				qty = 5;
			}
			updateJarPrice(qty);
			return false;
		});
		$('#jar_qtyDown').click(function(){		
			var li = $("ul#jars li.active").attr('id').split('_');
			qty = (li[1]*1)-1;
			if($("ul#jars li#jar_"+qty).length > 0){
				$("ul#jars li.active").removeClass('active').animate({opacity: 0},500);
				$("ul#jars li#jar_"+qty).addClass('active');
			} else {
				qty = 1;
			}
			updateJarPrice(qty);
			return false;
		});
		
		
		
		if(($('#tube_quantity').val()*1) > 1){
			$("ul#tubes li").not('ul#tubes li#tube_'+$('#tube_quantity').val()).addClass('active').animate({opacity: 0},5);
			updateTubePrice(qty);
		} else {
			$("ul#tubes li").not('ul#tubes li#tube_1').addClass('active').animate({opacity: 0},5);
		}
		setTimeout("$('ul#tubes li').css('display', 'block');",5);
		$('#tube_qtyUp').click(function(){		
			var li = $("ul#tubes li.active").attr('id').split('_');
			qty = (li[1]*1)+1;
			if(qty < 6){
				if($("ul#tubes li#tube_"+qty).length > 0){
					$("ul#tubes li.active").removeClass('active');
					$("ul#tubes li#tube_"+qty).addClass('active').animate({opacity: 1},500);
				}
			} else {
				qty = 5;
			}
			updateTubePrice(qty);
			return false;
		});
		$('#tube_qtyDown').click(function(){		
			var li = $("ul#tubes li.active").attr('id').split('_');
			qty = (li[1]*1)-1;
			if($("ul#tubes li#tube_"+qty).length > 0){
				$("ul#tubes li.active").removeClass('active').animate({opacity: 0},500);
				$("ul#tubes li#tube_"+qty).addClass('active');
			} else {
				qty = 1;
			}
			updateTubePrice(qty);
			return false;
		});
		
		
	}
});

function updateTubePrice(qty){
	var subTotal = '0.90';
	var postage = 0;
	var quantity = 1;
	var unitPrice = '0.90';
	quantity = qty;
	switch(qty){
		case 1: case 0:
			quantity = 1;
		break;
		case 2:
			unitPrice = '0.75';
			subTotal = '1.50';
			quantity = 2;
		break;
		case 3:
			unitPrice = '0.56';
			subTotal = '1.68';
			quantity = 3;
		break;
		case 4:
			unitPrice = '0.47';
			subTotal = '1.86';
			quantity = 4;
		break;
		case 5:
			unitPrice = '0.41';
			subTotal = '2.05';
			quantity = 5;
			$('#body_shop #tubeForm .buyNow').show();
		break;
		default:
			unitPrice = '1.80';
			subTotal = '9.00';
			postage = '7.42';
			quantity = 5;
			$('#body_shop #tubForm .buyNow').hide();
			$('#body_shop #tubeForm #contactUs').show();
		break;
	}
	$('#body_shop #tubeArea .buyNow').show();
	$('#body_shop #tubeArea #contactUs').hide();
	$('#tubePrice').html("&pound; "+subTotal);
	//$('#postagePrice').html("&pound; "+postage);
	$('#tube_amount').val(unitPrice);
	$('#tube_shipping').val(postage);
	$('#tube_quantity').html(quantity);
	$('#tube_actual_quantity').val(quantity);
}

function updateTinPrice(qty){
	var subTotal = 9.99;
	var postage = 4;
	var quantity = 1;
	var unitPrice = 9.99;
	postage = postage*qty;
	quantity = qty;
	subTotal = unitPrice*qty;
	$('#body_shop #tinArea .buyNow').show();
	$('#body_shop #tinArea #contactUs').hide();
	$('#tinPrice').html("&pound; "+subTotal);
	//$('#postagePrice').html("&pound; "+postage);
	$('#tin_amount').val(unitPrice);
	$('#tin_shipping').val(postage);
	$('#tin_quantity').html(quantity);
	$('#tin_actual_quantity').val(quantity);
}

function updatePrice(qty){
	var subTotal = '2.00';
	var postage = '2.59';
	var quantity = 1;
	var unitPrice = '2.00';
	$('#body_shop #bottleForm .buyNow').show();
	$('#body_shop #bottleForm #contactUs').hide();
	var sauceName = "Mr Singh's Chilli Sauce";
	switch(qty){
		case 1: case 0:
			quantity = 1;
		break;
		case 2:
			unitPrice = '1.95';
			subTotal = '3.90';
			postage = '4.80';
			quantity = 2;
		break;
		case 3:
			unitPrice = '5';
			subTotal = '5.00';
			postage = '4.80';
			quantity = 1;
			sauceName = "Mr Singh's Chilli Sauce 3 for &pound;5";
		break;
		case 4:
			unitPrice = '1.85';
			subTotal = '7.40';
			postage = '7.42';
			quantity = 4;
		break;
		case 5:
			unitPrice = '1.80';
			subTotal = '9.00';
			postage = '7.42';
			quantity = 5;
			$('#body_shop #bottleForm .buyNow').show();
		break;
		default:
			unitPrice = '1.80';
			subTotal = '9.00';
			postage = '7.42';
			quantity = 5;
			$('#body_shop #bottleForm .buyNow').hide();
			$('#body_shop #bottleForm #contactUs').show();
		break;
	}
	$('#sauce_name').val(sauceName);
	$('#bottlePrice').html("&pound; "+subTotal);
	//$('#postagePrice').html("&pound; "+postage);
	$('#amount').val(unitPrice);
	$('#shipping').val(postage);
	$('#quantity').html(quantity);
	$('#actual_quantity').val(quantity);
}

function updateJarPrice(qty){
	var subTotal = '2.00';
	var postage = '2.59';
	var quantity = 1;
	var unitPrice = '2.00';
	$('#body_shop #bottleForm .buyNow').show();
	$('#body_shop #bottleForm #contactUs').hide();
	var jarName = "Mr Singh'sPunjabi Pesto";
	switch(qty){
		case 1: case 0:
			quantity = 1;
		break;
		case 2:
			unitPrice = '1.95';
			subTotal = '3.90';
			postage = '4.80';
			quantity = 2;
		break;
		case 3:
			unitPrice = '5';
			subTotal = '5.00';
			postage = '4.80';
			quantity = 1;
			jarName = "Mr Singh'sPunjabi Pesto 3 for &pound;5";
		break;
		case 4:
			unitPrice = '1.85';
			subTotal = '7.40';
			postage = '7.42';
			quantity = 4;
		break;
		case 5:
			unitPrice = '1.80';
			subTotal = '9.00';
			postage = '7.42';
			quantity = 5;
			$('#body_shop #jarForm .buyNow').show();
		break;
		default:
			unitPrice = '1.80';
			subTotal = '9.00';
			postage = '7.42';
			quantity = 5;
			$('#body_shop #jarForm .buyNow').hide();
			$('#body_shop #jarForm #contactUs').show();
		break;
	}
	$('#jar_name').val(jarName);
	$('#jarPrice').html("&pound; "+subTotal);
	//$('#postagePrice').html("&pound; "+postage);
	$('#jar_amount').val(unitPrice);
	$('#jar_shipping').val(postage);
	$('#jar_quantity').html(quantity);
	$('#jar_actual_quantity').val(quantity);
}
