// 
//  main.js
//  Distress to Impress
//  
//  Created by Chris Mytton on 2009-07-17.
//  Copyright 2009 Cutpastecreate. All rights reserved.
// 

jQuery(function ($) {
	
	// make sure external links open in a new window
	$("a[rel='external']").attr('target', '_blank');
	
	function getParams ( name, url ) {
		url = url || window.location.href;
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( url );
		if( results == null ) {
			return "";
		} else {
			return results[1];
		}
	}

	// embed any youtube clips that are on the page.
	$("a[href*='youtube.com']").each(function () {
		var videosrc = getParams('v', $(this).attr('href')),
		videosrc = 'http://www.youtube.com/v/' + videosrc + '&hl=en&fs=1&rel=0&color1=0x402061&color2=0x9461ca',
		embed = $.flash.create({
			width: 425,
			height: 344,
			swf: videosrc,
			params: {
				movie: videosrc,
				allowFullScreen: 'true',
				allowscriptaccess: 'always'
			}
		});
		$(this).after(embed).remove();
	});
	
	function emptyCart () {
		$.get(this.href, null, function () {
			$("ul#sliding_cart").empty().html('<li class="empty">Your basket is empty.</li><li class="visitshop"><h3><a href="' + Config.base_url + 'store">Visit the Store</a></h3></li>');
		}, 'html');

		return false;
	}
	
	// setup the cycle animation
	$("#advertise ul").cycle();
	
	$("form.single_form select[name='variation']").change(function () {
		$("form.single_form p.wpsc_product_price span").text($('option:selected', this).attr('title'));
	}).change();
	
	// make some snazzy animations happen upon submit
	$("form.single_form").submit(function () {
		$("#sliding_panel").show();
		$("#fancy_notification_content").hide();
		$("#loading_animation").slideDown();
		
		// post request to add item to the cart
		$.post(this.action, $(this).serialize(), function (data) {
			$("#loading_animation").slideUp();
			$("#fancy_notification_content").slideDown();

			$("ul#sliding_cart").html(data);
			$("#empty_cart").click(emptyCart);
			
		}, 'html');
		
		return false;
	});
	
	$("#checkout_submit").replaceWith('<a class="submit" href="#"><span>I\'m done, take me to Paypal</span></a>');
	
	$("#submit a.submit").click(function () {
		// trigger the checkout submit event (and validate)
		$(this).parents('form#checkout').submit();
		return false;
	});
	
	$("#continue_shopping").click(function () {
		// continue shopping
		$("#sliding_panel").slideUp();
		
		return false;
	});
	
	
	// collapse / expand sliding cart...
	$("#fancy_collapser_link").click(function () {
		var $cart = $("ul#sliding_cart");
		if (!$cart.is(':visible')) {
			$("#fancy_collapser").attr('src', Config.base_url + 'images/minus.png');
		} else {
			$("#fancy_collapser").attr('src', Config.base_url + 'images/plus.png');
		}
		
		$cart.slideToggle('slow');
		
		return false;
	});

	$.validator.messages.required = "Please complete this field.";
	
	$("#checkout").validate({
		rules: {
			first_name: 'required',
			last_name: 'required',
			address1: 'required',
			city: 'required',
			postcode: 'required',
			country: 'required',
			email: 'required email',
			shipping_first_name: {
				required: "#same_details:unchecked"
			},
			shipping_last_name: {
				required: "#same_details:unchecked"
			},
			shipping_address: {
				required: "#same_details:unchecked"
			},
			shipping_city: {
				required: "#same_details:unchecked"
			},
			shipping_postcode: {
				required: "#same_details:unchecked"
			},
			shipping_country: {
				required: "#same_details:unchecked"
			}
		},
		onfocusout: false,
		success: function(label) {
			label.parent('p').removeClass('error');
		},
		submitHandler: function(form) {
			$.post(Config.base_url + 'shop/front/create_order', $(form).serialize(), function (data) {
				form.submit();
			});
		},
		errorPlacement: function (error, element) {
			element.parent('p').append(error);
		}
	});
	
	$("#contactform input[type='submit']").replaceWith('<a href="#" class="button submit"><span>Submit</span></a>');
	
	$("#contactform .submit").click(function() {
		// trigger the contact form submit event (will validate)
		$("#contactform").submit();
		return false;
	});

	$("#contactform").validate({
		submitHandler: function(form) {
			$.post(form.action, $(form).serialize(), function (data) {
				$(".status").text(data.message);
				form.reset();
			}, 'json');
		}
	});
	
	$("#empty_cart").click(emptyCart);
});