jQuery().ready(function() {
	
	var showMega = function(ctx) {
		ctx.addClass("hover_state");
		ctx.find(".megaItems").show();
	};
	
	var hideMega = function(ctx) {
		ctx.removeClass("hover_state");
		ctx.find(".megaItems").hide();
	};
	
	var currentItem;
	jQuery(".primary_nav_item").hover(function(event) {
		currentItem = jQuery(this);
		showMega(currentItem);
	}, function(event) {
		hideMega(currentItem);
	});
	
	var loginContainer, container, containerWidth = 0, megaContainerWidth;
	/* 
	 * Calculate the containers total needed width based 
	 * on the number of <ul> elements contained inside it.
	 */
	var setLoginContainerWidth = function() {
		loginContainer.attr("id", "render_of_screen");
		loginContainer.find("ul").each(function() {
			containerWidth = containerWidth + jQuery(this).outerWidth(true);
		});

		loginContainer.attr("id", "login_container");
		loginContainer.width(containerWidth);
	};
	
	/* 
	 * Calculate the containers total needed width based 
	 * on the number of <ul> elements contained inside.
	 */
	var setContainerWidth = function(container) {
		megaContainerWidth = 0;
		var currentId = jQuery(container).attr("id");
		container.attr("id", "render_of_screen");
		
		jQuery(container).find(".megaItem:last").css("margin-right", "0");
		container.find(".megaItem").each(function() {
			megaContainerWidth = megaContainerWidth + jQuery(this).outerWidth(true);
		});		
		
		container.attr("id", currentId);		
		container.width(megaContainerWidth).css("left", "-95px");
	};
	
	var megaItemsLength = jQuery(".megaItems").length;
	if(megaItemsLength > 0) {
		jQuery(".megaItems").each(function() {
			container = jQuery(this);
			setContainerWidth(container);
		});		
	}
	
	if(jQuery("#login_container").length > 0) {
		loginContainer = jQuery("#login_container");
		setLoginContainerWidth();
	}	
	
	jQuery("#show_login").hover(function(event) {	
		loginContainer.slideDown(); 
	}, function(event) {
		loginContainer.slideUp();
	});
	
	jQuery("#login").submit(function(event) {
				
		var errors = "";
		
		jQuery(this).find(":input").attr("rel", "required").each(function() {
			if(jQuery.trim(jQuery(this).val()) === "") {
				errors = errors + "<p>Please supply a " + jQuery(this).attr("name") + "</p>";
			}
		});
		
		if(errors !== "") {
			showValidationResult(errors);
			return false;
		}
		jQuery("#validation_results").removeClass().empty();
		return true;
	});
	
	jQuery("#reminder").submit(function(event) {
		
		var errors = "";
		
		jQuery(this).find(":input").attr("rel", "required").each(function() {
			if(jQuery.trim(jQuery(this).val()) === "") {
				errors = errors + "<p>Please supply a " + jQuery(this).attr("name") + "</p>";
			}
		});
		
		if(errors !== "") {
			showValidationResult(errors);
			return false;
		}
		jQuery("#validation_results").removeClass().empty();
		return true;
	});
	
	var showValidationResult = function(errors) {
		jQuery("#validation_results").addClass("error").html(errors);
	};
	
	if(jQuery("input:text").length > 0) {
		jQuery("input").focus(function() {
			jQuery(this).css("background-color", "#fff");
		});
		
		jQuery("input").blur(function() {
			jQuery(this).css("background-color", "#f4f4f4");
		});
	}
	
	/* Search input interaction for focus and blur events */
	jQuery("#query").focus(function() {
		if(jQuery(this).val() === "Search") {
			jQuery(this).val("");
		}
	});
	
	jQuery("#query").blur(function() {
		if(jQuery(this).val() === "") {
			jQuery(this).val("Search");
		}
	});	
});
