jQuery.fn.toggleVal=function(focusClass){this.each(function(){$(this).focus(function()
{if($(this).val()==this.defaultValue){$(this).val("")}if(focusClass)
{$(this).addClass(focusClass)}}).blur(function(){if($(this).val()=="")
{$(this).val(this.defaultValue)}if(focusClass){$(this).removeClass(focusClass)}})})}

$(document).ready(function(){

$(".toggle").toggleVal("active");

	//initialise the slider by hiding all the child divs
	$('div.slide_content').hide();
		
	//Toggle the divs
	$('h3.slide_header').toggle (function() {
		$(this).next('.slide_up').css('display', 'block'); 
		$(this).next().slideDown('fast');
		$('a',this).text('[-]');
	}, function() {
		$(this).next().slideUp('fast');
		$('a',this).text('[+]');
	});

	$("a[rel^='prettyPhoto']").prettyPhoto(
	{
	padding: 40,
	theme: 'dark_rounded'
	}
	);

	$(".other_input").hide();
	
	$(".check_other").change(function () {
		if ($(this).val() == 'other') {
			$(this).next(".other_input").show().addClass('validate[required]');
		} else {
			$(this).next(".other_input").hide().removeClass('validate[required]');
		}
	});
	

	// Add Discount coupon
	$("#add_discount").click(function(e) {
		e.preventDefault();
	
		$.post('/order/coupon', {
            field: 'code',
            code: $('#coupon').val()
            },
               function(response) {
					if (response != 'Invalid') {
						//alert(response);
						window.location.href=window.location.href;
					} else {
						alert('That coupon code was invalid');
					}
			});
	});

	
	$(".hidden").hide();
	
	
    $(".change_size").change(function () {
		  link = $(this).parents('.product').find('a.cart_btn').attr('href');
		  baseLink = link.split('/').slice(0,5).join('/');
		  $(this).parents('.product').find('a.cart_btn').attr('href',baseLink+'/'+$("option:selected", this).text()) 
    });

	
	
	// Used to add a postage price to the order if the country is not Australia *Depreciated*
	/*
	$(".check_country").change(function () {
	
		if ($(this).val() != '14') {
			//alert('postage');
			$(".visible").hide();
			$(".hidden").show();
			$('#postage').val((5.50).toFixed(2));
			$('#txtAmount').val((parseInt($('#subtotal').val())+5.50).toFixed(2));
			$('#aust_post').attr('checked', true);
		} else {
			$(".hidden").hide();
			$(".visible").show();	
			$('#postage').val(0.00);
			$('#txtAmount').val(parseInt($('#subtotal').val()));
		}
	});	

	$("#aust_post").click(function() {
		$('#postage').val((5.50).toFixed(2));
		$('#txtAmount').val((parseInt($('#subtotal').val())+5.50).toFixed(2));
	});
	
	$("#express_post").click(function(e) {
		$('#postage').val((11.00).toFixed(2));
		$('#txtAmount').val((parseInt($('#subtotal').val())+11.00).toFixed(2));
	});	
	*/
	
	$(".check_country").change(function () {
		var option = $(this).val();
		if (option != '14') {
			$(".visible").hide();
			$(".contact").show();
			$('#shipping_header').html('Step 3. <small>Shipping</small>');
			$('#postage').val('contact us');
			var country = $(".check_country option[value='"+option+"']").html();
			alert('Please contact us for the shipping rates to '+country+' for this order. You may continue with your order but it will not be shipped until you have contacted Zzipit.');
			
		} else {
			$(".hidden").hide();
			$(".contact").hide();
			$(".visible").show();	
			$('#postage').val(0.00);
			$('#txtAmount').val(parseInt($('#subtotal').val()));
		}
	});		

	// Cart quantity update
	//$("#addCartInput, #checkout").bind("blur",  function(e){
	//	$("#cart_form").submit();	
	//});

	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cart_form").submit();
	});

	// Cart item removal
	$(".removeItemFromCart").click(function(e) {
		e.preventDefault();
		$($(this).siblings("input")[0]).val(0);
		$("#cart_form").submit();
	});

	
	
	//$(".removeItem").click(function(e) {
	//	e.preventDefault();
		//$($(this).siblings("input")[0]).val(0);
	//	$("#cart_content_form").submit();
	//});
	
	
	$(".removeItemFromCart").click(function(e) {
		e.preventDefault();
		$($(this).siblings("input")[0]).val(0);
		$("#cartForm").submit();
	});
	
	
	// Cart quantity update
	$("#addCartInput, #checkout").bind("keyup",  function(e){
		$("#cartForm").submit();	
	});	
	 
});
