/*=============================================================================

			 	 TITLE:		NetMediaOne - Core Library
		  MODIFIED:		2008.12.30
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		jQuery 1.2.6

=============================================================================*/

var NMO = {
	
	version: "2.0",
	rootPath: "",
	devMode: false,
	resizeTimer: null,
	documentMouseX: 0,
	documentMouseY: 0,
	windowMouseX: 0,
	windowMouseY: 0,
 
	newInstance: function(obj) {
		function F() {};
		F.prototype = obj;
		return new F();
	},
	
	exceptionTypes: {
		
		INVALID_FILE_TYPE: new Error("The specified file type is unknown or invalid!"),
		DON_QUIXOTE: new Error("JS interpreter appears to be operating in Quixotic Mode (reached the unreachable code)")
		
	},
		
	// Inter-Context Communicator - Holds objects addressable across IFRAME boundaries
	ICC: (parent.NMO != null)?parent.NMO.ICC:{},

	log: function( msg ) {

		if ( console != undefined ) {
			console.log(msg);
		} else if (devMode) {
			alert(msg);
		}
		
	},

	//
	// getPageScroll()
	// Returns array with x,y page scroll values.
	// Core code from - quirksmode.org
	//
	getPageScroll: function() {
	
		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	
	pointerWithin: function(el) {
		
		if ( NMO.documentMouseX < ( el.offset().left ) || 
				NMO.documentMouseX > ( el.offset().left + el.outerWidth() ) ||
				NMO.documentMouseY < ( el.offset().top ) || 
				NMO.documentMouseY > ( el.offset().top + el.outerHeight() ) ) {
			
			return false; 
		
		} else {
		
			return true;
			
		}
		
	},
	
	// Injects a resource reference (such as a script or stylesheet) into the document
	require: function( fileName, fileType ) {
		
		try {

			if ( fileType == undefined ) {
				if ( fileName.indexOf(".css") != -1 ) { fileType = "CSS"; }
				else if ( fileName.indexOf(".js") != -1 ) { fileType = "JS"; }
				else {
					throw( this.exceptionTypes.INVALID_FILE_TYPE );
				}
			}

			var isRemote = ( fileName.indexOf("http") == 0 );
	
			switch ( fileType ) {
	
				case "CSS": {
					
					if ( $("link[href*="+fileName+"]").length > 0 ) {
						return;
					} else {
						$("head").append('<link rel="stylesheet" type="text/css" href="'+((isRemote)?'':NMO.rootPath)+fileName+'">');
					}
					break;
					
				} 
				
				case "JS": {
		
					if ( $("script[src*="+fileName+"]").length > 0 ) {
						return;
					} else {
						$("head").append('<script type="text/javascript" src="'+((isRemote)?'':NMO.rootPath)+fileName+'"></script>');
					}
					break;
					
				} 
				
				default: {
					throw( this.exceptionTypes.DON_QUIXOTE );
					break;
				}
				
			}
			
		} catch(ex) {

			this.log("Error in NMO.require(): "+ex.message );
			
		}

	},	

	init: function() {

		$(document).mousemove( function(e) {
			NMO.documentMouseX = e.pageX;
			NMO.documentMouseY = e.pageY;
			NMO.windowMouseX = e.clientX;
			NMO.windowMouseY = e.clientY;
		} );
		
		$("#navBar a").each( function() {
			var page = this.href.replace(".html","");
			if( (location.href.indexOf(".html") == -1 && page.replace(location.href,"") == "index") || location.href.indexOf(page) != -1 ) {
				$(this.parentNode).addClass("Active");
			}
		} );
		
		$(".NavTabs a").each( function() {
			if( location.href.indexOf(this.href) != -1 ) {
				$(this.parentNode).addClass("Active");
			}
		} );
		
		NMO.PageEnhancements.apply( [ "FirstLast", "ZebraStripes", "InitialPageSetup", "AjaxifyLinks" ], document.body );

	}
	
};

NMO.PageEnhancements = {

	ruleSets: {

		FirstLast: function(scope) {
			$("*:first-child", scope ).addClass("FirstChild");
			$("*:last-child", scope ).addClass("LastChild");
		},
		
		AjaxifyLinks: function(scope) {
			$("a.Ajax, .AjaxLinks a", scope ).each( function() {
				this.href = this.href + ( (this.href.indexOf("?")==-1) ? "?" : "&" ) + "renderMode=ajax";
			} );
		},
		
		InitialPageSetup: function(scope) {

			$("body").addClass("HasJS");
	
			$("#contentSection h1:first, #contentSection h2:first, #contentSection h3:first").addClass("FirstChild");

		},

		ZebraStripes: function(scope) {
			$( "table.Striped tbody > tr:visible:even", scope ).addClass("Even").removeClass("Odd");
			$( "table.Striped tbody > tr:visible:odd", scope ).addClass("Odd").removeClass("Even");
			$( "ul.Striped > li:visible:even", scope ).addClass("Even").removeClass("Odd");
			$( "ul.Striped > li:visible:odd", scope ).addClass("Odd").removeClass("Even");
		}
		
	},
	
	apply: function( rules, scope ) {		
		
		for ( var i = 0; i < rules.length; i++ ) {
			this.ruleSets[rules[i]](scope);
		}
		
	}		
									 
};

$( function() { NMO.init(); } );
