// All praise be to jQuery
$(function() {

	$('#postageStandard').click(function()
	{
		d = "QLD City 2 days\nQLD Regional 2-4 days\nNSW City 2 days\nNSW Regional 2-4 days\nVIC City 2 days\nVIC Regional 2-4 days\nSA City 2 days\nSA Regional 3-8 days\nNT City 5 days\nNT Regional 5-8 days\nWA City 5 days\nWA Regional 5-8 days\nTAS City 5 days\nTAS Regional 5-8 days";
		alert(d);
	
	});
	
	$('#postageExpress').click(function()
	{
		d = "QLD City 24 hours\nQLD Regional 48 hours\nNSW City 24 hours\nNSW Regional 48 hours\nVIC City 24 hours\nVIC Regional 48 hours\nSA City 24 hours\nSA Regional 72 hours\nNT City 48 hours\nNT Regional 72 hours\nWA City 72 hours\nWA Regional 96 hours\nTAS City 72 hours\nTAS Regional 96 hours\n";
		alert(d);
	
	});

	$(".add-to-cart").click(function(e) {
		window.location = "/cart";
	});
	// Apply tablesorters
	$(".simplelist").each(function() {
		$(this).tablesorter();
	});

	$("#menu").find("a").each(function() {
		if ("/"+$(this).attr("href")==window.location.pathname || $(this).attr("href")==window.location.href) {
			$(this).addClass("active");
		}
	});

	$(".confirm").click(function(e) {
		return(confirm("Really?"));
	});

	$(".delete").click(attachAjaxDeleteEvent);

	function attachAjaxDeleteEvent(e) {
		e.preventDefault();
		var parentRow = $(this).parents("tr")[0];
		if (confirm("Really Delete?")) {
			$.get($(this).attr("href"), function(d) {
				if (d=="Success") {
					$(parentRow).hide();
				}
				else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
			});
		}
	}

	$("#loginForm").find("#user_name").focus();

	$("#timesheetForm").submit(function() {
		return(confirm("Submit Timesheet?"));
	});


	$("form").submit(function() {
		var alertMessage = "";
		$(this).find(".required").each(function() {
			if ($(this).val()=="") {
				alertMessage += $(this).attr("rel")+" is required\n";
			}
		});
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("input[name=paymentMethod]").length && $(this).find("input[name=paymentMethod]:checked").length==0) {
			alertMessage += "You must select a Payment Type\n";
		}
		if ($(this).find("input[name=postageMethod]").length && $(this).find("input[name=postageMethod]:checked").length==0) {
			alertMessage += "You must select a Postage Method\n";
		}
		if ($(this).find(".iAgree").length != $(this).find(".iAgree:checked").length) {
			alertMessage += "You must agree to our Terms and Conditions\n";
		}
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});



	$(".ajaxLink").click(function(e) {
		e.preventDefault();
		$.post($(this).attr("href"), function(d) {
			$("#ajaxDialog").html(d);
			$("#ajaxDialog").fadeIn(500);
			attachAjaxDialogEvents();
		});
	});

	$(".ajaxCartLink").click(function(e) {
		e.preventDefault();
		var quantity = $($(this).attr('rev')).val();
		var href = $(this).attr("href") + quantity;
		$.post(href, function(d) {
			$("#ajaxDialog").html(d);
			$("#ajaxDialog").fadeIn(500);
			attachAjaxDialogEvents();
		});
	});
		
	$(".productDetailsLink").click(function(e) {
		e.preventDefault();
		$.post($(this).attr("href"), function(d) {
			$('#lights_out').fadeIn(500);
			$("#productDialog").html(d);
			$("#productDialog").fadeIn(500);
			attachAjaxDialogEvents();
		});
	});


	// Update cart
	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cartForm").submit();
	});
	$(".removeItemFromCart").click(function(e) {
		e.preventDefault();
		$($(this).siblings("input")[0]).val(0);
		$("#cartForm").submit();
	});

	$(".imgChange").hover(
		function(){
			osrc = $(this).attr('src');
			nsrc = $(this).attr('rel');
			$(this).attr('src',nsrc);
			$(this).attr('rel', osrc);
		},
		function(){
			osrc = $(this).attr('src');
			nsrc = $(this).attr('rel');
			$(this).attr('src',nsrc);
			$(this).attr('rel', osrc);
		});

	// Order page
	$("#sameAsAbove").click(function() {
		if ($(this).attr("checked")==false) 
		{
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val("");
				}
			});
		}
		else {
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val($("input[name="+$(this).attr("rev")+"]").val());
				}
			});
		}
	});

}); 

function attachAjaxDialogEvents() {
	$(".closeDialog").click(function(e) {
		e.preventDefault();
		$("#ajaxDialog").fadeOut(500);
		$("#productDialog").fadeOut(500);
		$("#lights_out").fadeOut(500);
	});
}
