var BLOCKUI_MESSAGE = '<img src="http://www.washingtonpost.com/wp-adv/style/loader.gif" />';

jQuery.extend({
	postJSON : function(url, data, callback) {
		$.post(url, data, callback, "json");
	}
});

var WPNI = window.WPNI || {};

WPNI.namespace = function(ns) {
	if(!ns || !ns.length) {
		return null;
	}

	var n_2 = ns.split(".");
	var n_3 = WPNI;

	for(var i=(n_2[0]=="WPNI")?1:0;i<n_2.length;++i) {
		n_3[n_2[i]] = n_3[n_2[i]] || {};
		n_3 = n_3[n_2[i]];
	}
	return n_3;
};

// Used for module JavaScript
WPNI.namespace("Module");
// Used for Utility JavaScript
WPNI.namespace("Util");
// Used for Global activites
WPNI.namespace("Global");
// Used for Global Map Functions
WPNI.namespace("Map");
// Used for Global hover behaviors
WPNI.namespace("Behavior");
// Used for Global Modal setup
WPNI.namespace("Modal");

$(document).ready(function() {
	$("body").addClass("javascript");
	
	// Bind a hover behavior for any selected image.
	// If that image is wrapped in a link, hovering the link will trigger the image behavior
	WPNI.Behavior.imageHover($(".see-more a img"));
	
	// set hover behavior for ie6 for application text buttons
	// also set bind the click event of the container to trigger the child a href click event
	WPNI.Behavior.buttonHover($(".button"));
	
	// fix some ie and ie6 specific issues
	// png fix and global navigation hover
	WPNI.Behavior.fixIE();

	// bind submit action to form text buttons
	// assumes all text buttons will be children of their submitted forms
	$("a.submit-button").bind("click", function(event){
	   $(this).parents('form').submit();
	   return false;
 	});					
	
	// bind submit action to form text buttons
	// assumes all text buttons will be children of their submitted forms
	$("a.cancel-button").bind("click", function(event){
	   $(this).parents('form').reset();
	   return false;
 	});		
});
/*** Behavior ***/
WPNI.Behavior = {
	// takes a jQuery object as an argument
	// binds an image swap behavior on an image
	// if the image is wrapped in an href tag, hovering the linked text will also trigger the image swap
	imageHover : function (jqObjs){
		$(jqObjs).each(function(){
			$(this).unbind("mouseover.imageHover");
			$(this).bind("mouseover.imageHover", function(){
				var imageSrc = $(this).attr('src');
				if (typeof imageSrc !== 'undefined') {
					if (imageSrc.indexOf("-on") == -1){
						imageSrc = imageSrc.replace(/\.png/, "-on.png");
						imageSrc = imageSrc.replace(/\.gif/, "-on.gif");
					}
					$(this).attr('src', imageSrc);
				}
			});
			$(this).unbind("mouseout.imageHover");
			$(this).bind("mouseout.imageHover", function(){
				var imageSrc = $(this).attr('src');
				
				imageSrc = imageSrc.replace(/-on/, "");
				$(this).attr('src', imageSrc);
			});
			// bind an action to any link wrapping this image to trigger the hover behavior
			$(this).parent("a").unbind("mouseover.imageHover");
			$(this).parent("a").bind("mouseover.imageHover", function(){$(this).children('img').trigger("mouseover.imageHover");});
			$(this).parents("a:eq(0)").unbind("mouseout.imageHover");
			$(this).parents("a:eq(0)").bind("mouseout.imageHover", function(){$(this).children('img').trigger("mouseout.imageHover");});
		});
	},
	buttonHover : function(jqObjs){
		/* gobal button hover actions */
		$(jqObjs).each(function(){
			// text button hover for ie6
			if ( $.browser.msie && $.browser.version=="6.0" ){
				$(this).hover(
					function() {
						$(this).addClass("hover");
					},
					function() {
						$(this).removeClass("hover");
					}
				);
			}
			/*
			// Needs more work.  This worked at one time but is no longer effective
			// fire button click when parent container is clicked
			//$(this).unbind("click.buttonHover");
			$(this).bind("click",
				function() {
					$(this).children().find("a").trigger("click");
				}
			);
			*/
		});	
	},
	fixIE : function() {
		if ($.browser.msie) {
			// apply the png fix if this is ie
			//$('img[@src$=.png]').ifixpng();

			//ie6 fix for global header
			if ( $.browser.version=="6.0" ){
				$("#navigation>ul>li").hover(function() {
					$(this).addClass("hover");
					$(this).height(31);
					$(this).find('div').show();
				},function(){
					$(this).removeClass("hover");
					$(this).find('div').hide();
				});
			}
		}
	}
};

WPNI.Util.Utility = {
	logEvent : function(project, name) {
		if (name != undefined){
			var prop = project + " - " + name;
			if (sendEventToOmniture) {
				sendEventToOmniture(prop, null, {prop19: prop});
			}
		}
	},
    blockUI: function(modalID){
		$("#" + modalID).block({
			message: BLOCKUI_MESSAGE
		});
	},
	// use this utility to unblock all modal forms to ensure standard behavior.
	unblockUI: function(modalID){
		setTimeout("$(\"#" + modalID + "\").unblock()",50);
	},
    resetForm: function(modalId, formId) {
		if ($("#" + formId).length > 0) {
			// hide jquery.validator messages
			$("#" + formId).validate().resetForm( );
			// reset the form back to defaults.
			document.forms[formId].reset();
		}
		// remove any json error messages
		$("#" + modalId + " .json-error").remove();
	}
};