$(document).ready(function()
{
	$('#searchString').click(function(event)
	{
		$(this).val('');
	});

	$('div.productDisplay input.submitButtonAsText').hover(function () {
		$(this).css({'text-decoration':'underline','cursor':'pointer'});
	}, function () {
		$(this).css({'text-decoration':'none','cursor':'default'});
	});

	if(pageName != favouritesPage)
	{

		$('div.productDisplay input.submitButtonAsText').click(function(event){
			var mode = $(this).parent().find('input[name="mode"]').val();
			var productID = $(this).parent().find('input[name="productID"]').val();

			$.get(rootPath+'toggleFavourites.php',{'mode':mode,'productID':productID});

			favCount = parseInt($('#favouriteCounter').text());

			if(mode == 'addToFavourites')
			{
				$(this).parent().find('input[name="mode"]').val('removeFromFavourites');
				$(this).val('Remove From Favourites');
				$('#favouriteCounter').text(favCount+1);

			}
			else
			{
				$(this).parent().find('input[name="mode"]').val('addToFavourites');
				$(this).val('Add to Favourites');
				$('#favouriteCounter').text(favCount-1);
			}
			return false;
		});
	}

	//checkout
	if(pageName == checkoutPage)
	{
		$('input#submitDetailsButton').click(function(){

			if($('input#discountCode').val() != '')
			{
				alert('Please apply your discount code before continuing');
				return false;
			}

		});
	}

	//v3 image styling
	//REMOVED - 16/11/2009 to use image resize class instead
	/*$('div.productDisplay3 img.productImage').load(function(){
	if($(this).width()>80){
	$(this).css("width","80px");
	}

	if($(this).height()>150){
	$(this).css("height","150px");
	}
	});*/

	//CSS Menu Styling
	//Removed 16/11/2009 - v3style menu no longer drops down
	/*$('div.subnav').css('opacity',0.90);
	$("ul#mainNav > li > a").hover(function () {
		//remove all subnavs
		$("div.subnav").fadeOut(200);
		$(this).next().slideDown(500);
	},null);

	$("div.subnav").mouseleave(function () {
		$(this).slideUp(500);
	});

	//when leaving header collapes
	$("div#topArea").mouseleave(function () {
		$("div.subnav").slideUp(500);
	});*/

	//Ajax handling of cart
	$("input.addToCart").click(function(){
		var priceID = $(this).parents().find('input[name="priceID"]').val();
		var quantity = $(this).parents().find('input[name="quantity"]').val();
		var page = $(this).parents().find('input[name="page"]').val();
		var isinteger = /^\d+$/
		$(this).parents().find('input[name="quantity"]').val("1");
		$(this).parents().find(".cartFeedback").html("&nbsp;");

		if(isinteger.test(quantity))
		{
			$.post(rootPath+'index.php',{'cartOperation':'true','addItem':'true','priceID':priceID,'quantity':quantity},function(){

				itemCount = parseInt($('#cartItems').text());

				if(!isNaN(itemCount))
				{
					quantity = parseInt(quantity);
					$('#cartItems').html(itemCount + quantity);
					$('#cartText').html(" items in basket");
				}
				else
				{
					$('#cartItems').html("1");
					$('#cartText').html(" item in basket");
				}

				//show the cart link
				$("#cartLink").css("display","block");
			});

				if(page=='prod'){
					$(this).parent().parent().parent().parent().parent().parent().find(".cartFeedback").html("Item added to cart").fadeIn(300);
				}
				else{
					$(this).parents().find("#"+priceID).html("Item added to cart").fadeIn(300);
				}
		}
		else
		{
			if(page=='prod'){
				$(this).parent().parent().parent().parent().parent().parent().find(".cartFeedback").html("Item added to cart").fadeIn(300);
			}
			else{
				$(this).parents().find("#"+priceID).html("Item added to cart").fadeIn(300);
			}
		}
		return false;
	});
});