// All praise be to jQuery
$(function() {
	
	// Highlight current menu item
	$("#menu").find("a").each(function() {
		if ("/"+$(this).attr("href")==window.location.pathname || $(this).attr("href")==window.location.href) {
			$(this).addClass("active");
		}
	});

	// Confirm certain link clicks
	$(".confirm").click(function(e) {
		return(confirm("Really?"));
	});

	// Date Inputs
	$('.date_input').each(function() {
		$(this).date_input();
	});

	// Apply delete links
	$(".delete").click(attachAjaxDeleteEvent);

	// Focus on username in login form
	$("#loginForm").find("#user_name").focus();

	// Form validation
	$("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=postageOption]").length && $(this).find("input[name=postageOption]: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 ($(this).find("input[name=paymentMethod]:checked").val()=="Credit Card") {
			if ($("#cardType").val()=="") alertMessage += "Card Type is required\n";
			if ($("#cardNumber").val()=="") alertMessage += "Card Number is required\n";
			if ($("#cardExpiryMonth").val()=="") alertMessage += "Card Expiry Month is required\n";
			if ($("#cardExpiryYear").val()=="") alertMessage += "Card Expiry Year is required\n";
			if ($("#cardName").val()=="") alertMessage += "Name on Card is required\n";
		}
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});

	$("#loginLink").click(function(e) {
		e.preventDefault();
		$("#loginForm").submit();
	});
	$("#newsletterSubscribeLink").click(function(e) {
		e.preventDefault();
		alert("Subscribe to newsletter here....");
	});

	// Cart quantity update
	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cartForm").submit();
	});

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

	//Select Registered Post as default
	if ($("#postageOptions #option1")) {
		$("#postageOptions #option1").attr("checked", true).parent().siblings("div").slideDown(200);
	}
	
	
	// Checkout, accordian information display for Postage Options
	$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
		$(this).click(function() {
			
			var standardPost = "Are you sure you want Standard Post?\n\nSelecting Standard Post means that Giggle Buttons is not responsible in the event that your package is lost by Australia Post and the order will not be replaced.\n\nRegistered Post is recommended as it offers additional buyer protection.";
			var internationalPost = "The international postage charge of $25 is so that your order is registered and trackable.";
			
			$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
				$(this).parent().siblings("div").slideUp(200);		

			});			
			$(this).parent().siblings("div").slideDown(200);
							
				if ($(this).val() == '2' || $(this).val() == '3') {
					if ($(this).val() == '3') {
						var confirmation = alert(internationalPost);
						return true;
					} else {
						var confirmation = confirm(standardPost);
					}
					
					
					if (confirmation==true) {
						$(this).attr("checked", true); 
					} else {
						$(this).parent().siblings("div").slideUp(200);	
						// Select other radio button
						$(this).attr("checked", false); 
						$("#postageOptions #option1").attr("checked", true).parent().siblings("div").slideDown(200); 
				
					}		
				} 

		
		});
		

		

	});
	
	$("#sameAsAbove").click(function() {
		if ($(this).attr('checked')) {
			//$("#deliveryAddressDetails").slideUp(200);
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val($("input[name="+$(this).attr("rev")+"]").val());
				}
			});
		}
		else {
			//$("#deliveryAddressDetails").slideDown(200);
		}
	});

}); 


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

function attachAjaxReactivateEvent(e) {
	e.preventDefault();
	var parentRow;
	//TODO: tr/li branch untested!
	if ($(this).parents("tr")[0]) {
		parentRow = $(this).parents("tr")[0];
	}
	else if ($(this).parents("li")[0]) {
		parentRow = $(this).parents("li")[0];
	}
	if (confirm("Reactivate?")) {
		$.get($(this).attr("href"), function(d) {
			if (d=="Success") {
				$(parentRow).hide();
			}
			else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
		});
	}
}

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