
$(document).ready(function() {

	setupMailChimp();
	setupHallOfFame();
	
	
	// site top menu		
	$('#topMenu .sf-menu').supersubs({
		extraWidth:	1
	}).superfish();
	
	// fix the line separating product categories.		
	$('.store .outline .bar').each(function() {
		var len = $(this).parent().find('h2').width();
		var leftOver = $(this).parent().width() - len - 20;
		$(this).width(leftOver);
	});
	$("a.fancy").fancybox({
		'ajax' : {
			type	: "POST",
			data	: 'mydata=test',
			success : function() {
				// update the number shown at the top for shopping cart
				updateCartNumber();
			}
		}
	});
	
	$("a.fancyAddToCart").fancybox({
		'onStart' : function() {
			var quantity = $('input.flavourQuantity').val();
			var flavour_id = $('select.productflavours').val();
			var product_id = $('input.product_id').val();
			
			var root = location.protocol + '//' + location.host;
			var url = root + "/store.php?action=add&product_id=" + product_id + "&flavour_id=" + flavour_id + "&quantity=" + quantity;
			if(quantity != "") {
				$.post(url, function(data) {
					$("#shoppingcart").replaceWith(data);
				});	
			}
		}		,
		'ajax' : {
			type	: "POST",
			data	: "",
			success : function() {
				// update the number shown at the top for shopping cart
				updateCartNumber();
			}
		}
	});
	
	$('#shoppingcart .removeproduct a').live('click', function(e) {	
		var url = $(this).attr('href');
		e.preventDefault();
		$.post(url, function(data) {
			$("#shoppingcart").replaceWith(data);
			//$("#shoppingcart").show();
			updateCartNumber();
		});
		return false;
	});	
	$('select.destination').live('change', function() {
		var baseUrl = "store.php?action=show";
		var url = baseUrl + "&destination=" + $(this).val();
		$.post(url, function(data) {
			// remove the 
			$("#shoppingcart").replaceWith(data);
			$("#shoppingcart").show();
			updateCartNumber();
		});
	}); 
	$('select.productflavours').live('change', function() {
		var rrp = $('select.productflavours option:selected').attr('rrp');			
		if(rrp != "") {
			$(".providerprice .rrp").text("$" + rrp); 
		} else {
			$(".providerprice .rrp").text("");
		}
		var price = $('select.productflavours option:selected').attr('price');	
		$(".providerprice .flavourprice").text(" $" + price);
		// update the input box showing the quantity of this item
		var root = location.protocol + '//' + location.host;
		var flavour_id = $('select.productflavours option:selected').val();
		var url = root + "/store.php?action=show_quantity&flavour_id=" + flavour_id;
		updateProductQuantity(url)		
	}); 
	$('.keepshopping').live('click', function() {
		$.fancybox.close();
	});
	// update the total and prices when shopping cart quantity is changed
	$(".productdescr input.quantity").live("blur keyup", function(e) {
		 if (e.type == 'focusout' || e.keyCode == '13')  {
			var quantity =  $(this).val();
			var product_id = $(this).parent().find(".product_id").val();
			var flavour_id = $(this).parent().find(".flavour_id").val();
			
			var root = location.protocol + '//' + location.host;
			var url = root + "/store.php?action=add&product_id=" + product_id + "&flavour_id=" + flavour_id + "&quantity=" + quantity;
			if(quantity != "") {
				updateShoppingCart(url);
			}			
			$('input.flavourQuantity').val(quantity);
		}
		
	});
		
});

function setupMailChimp() {

	//update form url 
	var root = location.protocol + '//' + location.host;
	$('#mc_signup_form').attr('action', root + "#mc_signup");
	$(".signupbuton").fancybox({
		'onStart' : function() {
			$("#mc_mv_EMAIL").val($("input.signupinput").val());
			$("input.signupinput").val("");
		},
		'scrolling':'no',
		'showCloseButton':false
	});
	$("#mc_signup_submit").val("Signup");
	$('.mc_custom_border_hdr').text("Newsletter Subscription");
	// show thank you message
	$("#mailchimpthankyou").fancybox();
	// submit a form asychronously
	email = $("#mc_mv_EMAIL").val()
	$('#mc_signup_form').ajaxForm(function(data) { 
		// validate the data first		
		// show thank you message
		$("#mailchimpthankyou").click();		
	}); 	
}
function setupHallOfFame() {
	
	$(".rightbar .halloffame .famer").mouseenter(function() {
		var right = ($(this).offset().left - $("#showfamer").width() - 10) + "px";
		var top =  $(this).offset().top + "px";
		var after =  $(this).find("img.now").attr('src');
		var before = $(this).find("img.then").attr('src');
		
		$("#showfamer .beforebig img").attr('src', before);
		$("#showfamer .afterbig img").attr('src', after);
		
		$("#showfamer").css({
			'left' : right,
			'top'  : top 
		}).show();
		
		
	}).mouseleave(function() {
		$("#showfamer").hide();
	});
}

function updateCartNumber() {
	var url = $(".topcontent .shoppingcart a.fancy").attr("href") + "_number";
	$.post(url, function(data) {
		var str = ""
		if(data <= 0) {
			$("span.cartItems").text("");
		} else {
			$("span.cartItems").text(" (" + data + ")");
		}
	});
}
function updateShoppingCart(url) {
	$.post(url, function(data) {
		$("#shoppingcart").replaceWith(data);
		$("#shoppingcart").show();
		updateCartNumber();
	});
}

function updateProductQuantity(url) {
	$.post(url, function(data) {
		$('input.flavourQuantity').val(data);
	});	
}




