function clearText(field)
{
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;
}
		
$(document).ready(function() {
	$("#product_tabs").tabs();
	$("#admin_tabs").tabs();
	$("#account_tabs").tabs();


	// Datepicker
	$(".datepicker").live("click", function(){
		 $(this).datepicker({
			 showOn:'focus',
			dateFormat: 'yy-mm-dd'
		}).focus();
	});

	// Product gallery lightbox
	$('ul.product_thumbnails li a').not('a.nolightbox').lightBox();

	$('.lightbox').lightBox();

	$('select.submit-on-change').change(function(){
		var parentForm = $(this).parents('form').submit();
	});

	$('.message').click(function(){
		$(this).fadeOut();
	});

	$('#do-product-display-order').change(function(){
		window.location.href = $(this).val();
	});


	// Show the gallery tab on a product page
	$('.goto-producttabs-gallery').click(function(){
		$('#product_tabs').tabs('select', 'tab_gallery');
		return false;
	});

	// Add product to basket
	$('a.product-price-check').live("click", function(){
		// Get the product ID
		var productID = $(this).attr('data-productID');
		if(!productID)
			return common_error();

		// Display facebox popup
		$.facebox({
			ajax: '/ajax/product_price_check/' + productID + '/'
		});

		return false;
	});

	$('a.product-price-check-guest').live("click", function(){
		var href = $(this).attr('href');
		apprise('Sorry, you must be logged in to do this!<br/>Would you like to login now?', {'verify':true, 'textYes':'Yes', 'textNo':'No thanks!'}, function(response)
		{
			if(response)
				window.location.href = href;
		});

		return false;
	});

	// Add product to basket
	$('a.add-product-to-basket').live("click", function(){
		// Get the product ID
		var productID = $(this).attr('data-productID');
		if(!productID)
			return common_error();

		// Display facebox popup
		$.facebox({
			ajax: '/ajax/add_product_popup/' + productID + '/'
		});

		return false;
	});



	$('a.productdeliverypopup').live("click", function(){
		// Get the product ID
		var productID = $(this).attr('data-productID');
		if(!productID)
			return common_error();

		// Display facebox popup
		$.facebox({
			ajax: '/ajax/product_delivery_popup/' + productID + '/'
		});

		return false;
	});

	$('.close-facebox').live("click", function(){
		$.facebox.close();
		return false;
	});


	// Add product to compare
	$('a.add-product-to-compare').live("click", function(){
		// Get the product ID
		var productID = $(this).attr('data-productID');
		if(!productID)
			return common_error();

		// Submit request to add the product (can use href)
		$.post($(this).attr('href'));

		refresh_sidebar_basket();

		apprise('The product has been added to your comparison list!', {'verify':true, 'textYes':'Compare products now!', 'textNo':'Continue shopping...'}, function(response)
		{
			// Go to basket or continue shopping?
			if(response)
				window.location.href = '/compare/';
		});
		return false;
	});


	// Add bundle to basket
	$('a.add-bundle-to-basket').live("click", function(){
		// Get the bundle ID
		var bundleID = $(this).attr('data-bundleID');
		if(!bundleID)
			return common_error();

		// Submit request to add the product (can use href)
		$.post($(this).attr('href'));

		refresh_sidebar_basket();

		apprise('The bundle has been added to your basket!', {'verify':true, 'textYes':'View basket', 'textNo':'Continue shopping...'}, function(response)
		{
			// Go to basket or continue shopping?
			if(response)
				window.location.href = '/basket/';
		});
		return false;
	});


	// Keep nav hover state on for dropdown
	$('#navigation ul ul').hover(function(){
		$(this).parents('li').find('a:first').addClass('hover');
	}, function(){
		$(this).parents('li').find('a').removeClass('hover');
	});


	function common_error()
	{
		apprise('Something went wrong - please refresh the page and try again!');
		return false;
	}

	function refresh_sidebar_basket()
	{
		// Send request for the partial
		$.ajax({
			url: '/ajax/get_partial/sidebar-basket',
			success: function(serverResponse)
			{
				// Set the content
				$('#sidebar div.box.partial-basket').html(serverResponse);

				// Refresh cufon
				Cufon.refresh();
			}
		});
	}


	// Search from header
	$('#search a.go').click(function(){
		$(this).parents('form').submit();
		return false;
	});



	$('.do-update-basket').click(function(){
		$('#basketform').submit();
		return false;
	});



	$('.do-save-basket').click(function(){
		apprise('Saving this basket will mean you\'ll be able to retrieve it at a later date.<br/>Once saved your current shopping cart will be emptied.', {'verify':true, 'textYes':'Save basket now!', 'textNo':'Continue shopping...'}, function(response)
		{
			if(response)
				window.location.href = '/basket/savebasket/';
		});
		return false;
	});



	$('.do-restore-basket').click(function(){
		var goToOnYes = $(this).attr('href');

		apprise('Are you sure you want to restore this basket?<br/>By doing so your current basket will be emptied and the saved basket will be deleted.', {'verify':true, 'textYes':'Restore this basket!', 'textNo':'Cancel'}, function(response)
		{
			if(response)
				window.location.href = goToOnYes;
		});
		return false;
	});



	$('.do-delete-basket').click(function(){
		var goToOnYes = $(this).attr('href');

		apprise('Are you sure you want to delete this basket?<br/>This cannot be undone.', {'verify':true, 'textYes':'Delete this basket!', 'textNo':'Cancel'}, function(response)
		{
			if(response)
				window.location.href = goToOnYes;
		});
		return false;
	});
});
