jQuery(document).ready(function() {
	// Initialize showcase on home page
	jQuery('div.showcase-item:first-child').show();
	jQuery('ul#showcase-nav li:first-child a').addClass('current');

	// Make entire showcase-item clickable
	jQuery('div.showcase-item').click(function(){
		window.location.href=this.getElementsByTagName('a')[0].href; return false;
	});

	// Setup showcase nav click events
	jQuery('ul#showcase-nav a').click(function(event){
		event.preventDefault();
		
		// set showcase-nav state
		jQuery('ul#showcase-nav li a').removeClass('current');

		// Set showcase-item state
		jQuery('div.showcase-item').hide();
		jQuery('div.showcase-item.' + jQuery(this).attr('class')).fadeIn('slow');
		jQuery(this).addClass('current');
	});

	// Start automatic clicks to act like an auto slideshow for showcase
	var showcaseIntervalId = setInterval( "showcaseSwitch()", 7000 );
	
	// Stop automatic clicks when the user mousedowns on a showcase-nav link container (we can't use the same click event as the actual link)
	jQuery('ul#showcase-nav li').mousedown(function(event){
		event.preventDefault();
		clearInterval(showcaseIntervalId);
	});


	// Contact form
	jQuery('form#tons-contact-form').submit(function(event){
		event.preventDefault();
		jQuery.post("/wp-content/themes/tasteofnovascotia/forms/contact.php", { fullname: jQuery('#fullname').val(), email: jQuery('#email').val(), comments: jQuery('#comments').val(), submitted: jQuery('#submitted').val(), recaptcha_challenge_field: jQuery('#recaptcha_challenge_field').val(), recaptcha_response_field: jQuery('#recaptcha_response_field').val() },  function(response){
			jQuery('p.alert').fadeOut('slow');
			if(response.result == 0) {
				jQuery('form#tons-contact-form').before(response.message);
				jQuery('form#tons-contact-form').fadeOut('slow');
			} else if (response.result == 1 || response.result == 2) {
				jQuery('form#tons-contact-form').before(response.message);
			}
			jQuery('html, body').scrollTop(0);			
		}, "json");
	});
	
	// Gift basket order form
	jQuery('form#tons-gift-basket-order-form').submit(function(event){
		event.preventDefault();
		jQuery.post("/wp-content/themes/tasteofnovascotia/forms/order.php", { 
				billing_name: jQuery('#billing_name').val(), 
				billing_address: jQuery('#billing_address').val(), 
				billing_city: jQuery('#billing_city').val(), 
				billing_province: jQuery('#billing_province').val(), 
				billing_country: jQuery('#billing_country').val(), 
				billing_postal: jQuery('#billing_postal').val(), 
				billing_phone: jQuery('#billing_phone').val(), 
				billing_fax: jQuery('#billing_fax').val(), 
				billing_email: jQuery('#billing_email').val(), 
				delivery_date: jQuery('#delivery_date').val(), 
				same: jQuery('#same').val(), 
				shipping_name: jQuery('#shipping_name').val(), 
				shipping_address: jQuery('#shipping_address').val(), 
				shipping_city: jQuery('#shipping_city').val(), 
				shipping_province: jQuery('#shipping_province').val(), 
				shipping_country: jQuery('#shipping_country').val(), 
				shipping_postal: jQuery('#shipping_postal').val(),
				shipping_phone: jQuery('#shipping_phone').val(),
				shipping_fax: jQuery('#shipping_fax').val(),
				shipping_email: jQuery('#shipping_email').val(),
				basket_1: jQuery('#basket_1').val(),
				basket_2: jQuery('#basket_2').val(),
				basket_3: jQuery('#basket_3').val(),
				basket_4: jQuery('#basket_4').val(),
				submitted: jQuery('#submitted').val()
		},  function(response){
			jQuery('p.alert').fadeOut('slow');
			if(response.result == 0) {
				jQuery('form#tons-gift-basket-order-form').before(response.message);
				jQuery('form#tons-gift-basket-order-form').fadeOut('slow');
			} else if (response.result == 1 || response.result == 2) {
				jQuery('form#tons-gift-basket-order-form').before(response.message);
			}
			jQuery('html, body').scrollTop(0);
		}, "json");
	});	
	jQuery('input#same').click(function(event) {
		//alert(jQuery('input#same').attr("checked"));
		if (jQuery('input#same').attr("checked")) {
			jQuery('#shipping-info input').attr("disabled", true);
		} else {
			jQuery('#shipping-info input').attr("disabled", false);			
		}
	});

	// PopUp links
	jQuery('a#newsletter_link').click(function(event){
		event.preventDefault();
		popUp(jQuery(this).attr('href'));
	})
	
});

// Automatically clicks the next showcase-nav link
function showcaseSwitch() {
	if(jQuery('ul#showcase-nav li a.current').parent().next().is('li')) {
		jQuery('ul#showcase-nav li a.current').parent().next().find('a').trigger('click');
	} else {
		jQuery('ul#showcase-nav li:first-child').find('a').trigger('click');
	}
}

function popUp(uri, x, y, mode) {
	if(!x) {
		x=800;
	}

	if(!y) {
		y=600;
	}

	var opts;
	if(!mode || mode == 'dialog') {
		var opts = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	} else if(mode == 'normal') {
		var opts = 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	}

	if(window.open(uri,'PopUpWindow',opts)) {
		return false;
	} else {
		return true;
	}
}