// All praise be to jQuery
$(function() {
	
	//$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$("input[type=text]").focus(function(){ this.select(); });
	
	$('img[src*=images/icons/]').addClass('lineup_icon');
	
	$(".formError").live("click",function(){
		$(this).fadeOut(150,function(){
			$(this).remove();
		}) 
	})
	
	// 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");
		}
	});
	
	$("span.hidden").hide();
	
	$(".showPayment").bind('click', function() {
		$(".hidden").hide().find('input').removeClass('validate[required]');
		div = '#'+$(this).attr('id')+'_form';
		$(div).show().find('input').addClass('validate[required]');
	});	

	// 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("#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
	$("#addCartInput, #checkout").bind("keyup",  function(e){
		$("#cart_form").submit();	
	});
	
	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cart_form").submit();
	});

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

	// Checkout, accordian information display for Postage Options
	$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
		$(this).click(function() {
			$("#postageOptions,#paymentOptions").find("input[type=radio]").each(function() {
				$(this).parent().siblings("div").slideUp(200);
			});			
			$(this).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);
	});
}