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

	$("a[rel^='prettyPhoto']").prettyPhoto();

	
	$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 12, arrowSize: 14});

	if($("#weDoMarker").val() == 'true'){
		$("#subMenus").addClass('showSubMenu');
		$('#whatWeDoSub').toggle('fast',function(){
			if(navigator.appName == 'Microsoft Internet Explorer')
			{
				this.style.removeAttribute("filter");
			}
		});
		var weDo = true;
	}else{
		var weDo = false;
	}
	
	if($("#folioSubMarker").val() == 'true'){
		$("#subMenus").addClass('showSubMenu');
			$('#ourFolioSub').toggle('fast', function(){
				if(navigator.appName == 'Microsoft Internet Explorer')
				{
					this.style.removeAttribute("filter");
				}
			});

			folioSub = true;
	}else{
		var folioSub = false;
	}

	$("#ourFolio").click(function(e) {
		e.preventDefault();
		if(folioSub)
		{
			$('#ourFolioSub').fadeOut('fast', function(){
				$("#subMenus").removeClass('showSubMenu');
			});
			folioSub = false;
		}else{
			$("#subMenus").addClass('showSubMenu');
			if(weDo)
			{
				$('#whatWeDoSub').fadeOut('slow');
				weDo = false;
			}
			$('#ourFolioSub').fadeIn('slow',function(){
				if(navigator.appName == 'Microsoft Internet Explorer')
				{
					this.style.removeAttribute("filter");
				}
			});

			folioSub = true;
		}	
	});

	

	$("#whatWeDo").click(function(e) {
		e.preventDefault();
		if(weDo)
		{
			$('#whatWeDoSub').fadeOut('slow', function(){
				$("#subMenus").removeClass('showSubMenu');
			});
			weDo = false;
		}else{
			$("#subMenus").addClass('showSubMenu');
			if(folioSub)
			{
				$('#ourFolioSub').fadeOut('slow');
				folioSub = false;
			}
			$('#whatWeDoSub').fadeIn('slow',function(){
				if(navigator.appName == 'Microsoft Internet Explorer')
				{
					this.style.removeAttribute("filter");
				}
			});
	
			weDo = true;
		}	
	});
	
	$(".websiteLink a").click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});

	// 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 (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();
	});

}); 


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);
	});
}