
$(document).ready(function() {

	//Facebox
	$('a[rel*=facebox]').facebox();


	$('form').formtips({
		class_name: 'tipped'
	});

	mainmenu();
	offerCode();
	checkCart();

	$('#rightArrow').click(function() { $('#timelineContainer').stop().scrollTo( '+=540',  {duration:400,onAfter:checkScrollLimit});});
	$('#leftArrow').click(function() { $('#timelineContainer').stop().scrollTo( '-=540', {duration:400,onAfter:checkScrollLimit});});
	checkScrollLimit();
	
	//Products Scrolling
	$('#rightArrowWine').click(function() 
	{ 
		$('#wineInner').animate(
		{
			marginLeft: '-175px'
		})
	});
	
	$('#leftArrowWine').click(function() 
	{ 
		$('#wineInner').animate(
		{
			marginLeft: '5px'
		})
	});


});

function checkScrollLimit()
{
	if ($('#timelineContainer').scrollLeft() <= 100) { 
		$('#leftArrow').hide();
	}
	else{
		$('#leftArrow').show();	
	}

	if ($('#timelineContainer').scrollLeft() > 1500) { 
		$('#rightArrow').hide();
	}
	else{
		$('#rightArrow').show();	
	}

}


function offerCode()
{
	var validCode = 'BOOK10'
	
	$('#dvdOffer').blur(function()
	{
		if ($(this).val() == validCode)
		{
			$('#offerError').html('');
			$('#product-price').val('9.99');
			$('#productBook p strong').html('Kitchen Inspiration - <span style="color:#881B15;">&pound;9.99</span>');
		}
		else 
		{
			$('#offerError').html('That code was not recognised').css('color','#881B15');
		}
	});
}


function checkCart()
{
	
	var myInterval = window.setInterval(function () { 
		if ($('#googlecart-widget') == null)
		{
		}
		else
		{		
			clearInterval(myInterval);
			$('#googlecart-checkout-button').click(function() {
				
				var gcNumItems = 0; 
				var caseSize = 6; 
				var gcNumItems = googlecart.getTotalQuantity(); 

				if((gcNumItems % caseSize) != 0) 
				{
					alert('You must order products in multiples of 6. You have ' + gcNumItems + ' items in your basket. Please add or remove some items to proceed.');
					return false;
				}
				else 
				{ 
					return true;
				} 
			});

		}
	;},1000);

}

	function mainmenu(){
		$("#nav ul").css({display: "none"}); // Opera Fix
		$("#nav li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		},function(){
			$(this).find('ul:first').css({visibility: "hidden"});
		});
	}

