// 
//  main.js
//  safelightimages.cutpastecreate.net
//  
//  Created by Chris Mytton on 2009-08-26.
//  Copyright 2009 Cutpastecreate. All rights reserved.
// 
jQuery(function ($) {
	var Safelight = {
		emptyCart: function () {
			$.get(this.href, null, function () {
				$("ul#sliding_cart").empty().html('<li class="empty">Your shopping cart is empty.</li><li class="visitshop"><a href="' + Config.base_url + 'events" class="button"><span>View Event Photos</span></a></li>');
			}, 'html');

			return false;
		}
	};
	
	$("div.club-promotions").cycle();
	
	$("form.single_form select[name='variation']").change(function () {
		$("form.single_form p.wpsc_product_price span").text($('option:selected', this).attr('title'));
	}).change();
	
	$("#add_to_cart").replaceWith('<a class="button submit" href="#"><span>Add To Cart</span></a>');
	
	$("form.single_form a.button.submit").click(function () {
		$(this).parents('form.single_form').submit();
		
		return false;
	});
	
	$("form.single_form").submit(function () {
		$("#fancy_notification_content").hide();
		$("#loading_animation").show();
		$("#sliding_panel").show();
		$.post(this.action, $(this).serialize(), function (data) {
			$("#loading_animation").hide();
			$("#fancy_notification_content").slideDown();
			
			$("ul#sliding_cart").html(data);
			$("#empty_cart").click(Safelight.emptyCart);
		}, 'html');
		
		return false;
	});
	
	$("#checkout_submit").replaceWith('<a class="button checkout" href="#"><span>Make Purchase</span></a>');
	
	$("form#checkout a.button.checkout").click(function () {
		$(this).parents('form#checkout').submit();
		
		return false;
	});
	
	$("#continue_shopping").click(function () {
		$("#sliding_panel").slideUp();
		
		return false;
	});
	
	
	
	$("#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({
		onfocusout: false,
		showErrors: function(errorMap, errorList) {
			$(".status").html("<p>" + this.numberOfInvalids() + "  fields are awaiting your details.</p>");
			this.defaultShowErrors();
		},
		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();
			});
		}
	});
	
	$("#contactform input[type='submit']").replaceWith('<a href="#" class="button submit"><span>Submit</span></a>');
	
	$("#contactform .button.submit").click(function() {
			$("#contactform").submit();
			return false;
		});

		//2 enable $.validation on the specified form
		//2.1 Check all fields are completed and
		//2.2 Check the data in the fields is valid
		$("#contactform").validate({
		//3 Use AJAXsubmit to pass this data to the PHP file
			submitHandler: function(form) {
				$.post(form.action, $(form).serialize(), function () {
					$(form).after("<p>Thankyou for your enquiry</p>");
					$(form).get()[0].reset();
				});
			},
			showErrors: function(errorMap, errorList) {
				$(".status").html("<p>" + this.numberOfInvalids() + "  fields are awaiting your details.</p>");
				this.defaultShowErrors();
			},
			success: function(label) {
	     		label.parent('p').removeClass('error');
	   		}
	  		/*
	errorPlacement: function(error, element) {
	  			$(".status").addClass("fail");
	     		error.appendTo( element.parent("p") );
	     		$(element).parent().addClass("error");
	   		}
	*/
		});
	

	$("ul.club-contact li.club-address a").click(function () {
		Shadowbox.open({
			player: 'html',
			content: '',
			height: 300,
			width: 500,
			options: {
				onFinish: function (item) {
					if(GBrowserIsCompatible()){
						var latlngarray = $("ul.club-contact li.club-address a").attr("rel").split(',');
						
						var body = document.getElementById(Shadowbox.contentId());
						var map = new GMap2(body);
						map.setCenter(new GLatLng(latlngarray[0], latlngarray[1]), 13);

						var point = new GLatLng(latlngarray[0], latlngarray[1]);
						map.addOverlay(new GMarker(point));
						
						// add some simple controls
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
					}
				}
			}
		});
		
		return false;
	});
	
	
	$("#empty_cart").click(Safelight.emptyCart);
});
