/*=============================================================================

			 	 TITLE:	NetMediaOne - Core Library
		  MODIFIED:	2011.02.07
				AUTHOR: Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:	jQuery 1.5+

=============================================================================*/


/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


var NMO = {	version: "2.1.1" };
NMO.console = {
	outputBuffer: [],
	log: function(text) {
		this.outputBuffer.push(text);
		if (this.outputBuffer.length > 4096) {
			// Discard older entries 
			this.outputBuffer.shift();
		}
	},
	print: function(text) {
		if (text) {
			this.outputBuffer.push(text);
		}
		while( this.outputBuffer.length > 0 ) {
			try {
				window.console.log( this.outputBuffer.shift() );
			} catch(ex) { break; }
		}
	},
	toString: function(separator) {
		var sep = separator || "\n";
		return this.outputBuffer.join(sep);
	},
	clear: function() { this.outputBuffer = []; }
};
NMO.queryStringParams = {};
NMO.queryString = jQuery('script[src*="netmediaone.core.js"]').attr("src");
NMO.queryString = NMO.queryString.substring( NMO.queryString.indexOf("?") + 1 );
jQuery.each( NMO.queryString.split("&"), function(index, p){
	try {
		var kv = p.split('=');
		NMO.queryStringParams[kv[0]] = kv[1];
	} catch(ex) {}
});
NMO.rootPath = NMO.queryStringParams['rootPath'] || "/";
NMO.debug = NMO.queryStringParams['debug'] || true;
NMO.mouse = { x: 0, y: 0 };
NMO.select = jQuery;

NMO.Plugins = {
	
	Config: [],
	Add: function(plugin) {
		this[plugin.name] = plugin;
		if ( NMO.debug ) { NMO.console.print("Detected Plugin: "+plugin.name ); }
		if ( plugin.attachToEvents !== undefined ) {
			NMO.select.each( plugin.attachToEvents.split(","), function(index, ev) {
				switch (ev) {
					case "WINDOW_LOAD": {
						NMO.select(window).load( function() {
							try {
								if ( NMO.debug ) { NMO.console.print(plugin.name+" applied to selector: 'body'" ); }
								plugin.init( NMO.select('body') );
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if ( NMO.debug ) { NMO.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")" ); }
							}
						});
						break;
					}
					case "PAGE_LOAD": {
						NMO.select(document).ready( function() {
							try {
								if ( NMO.debug ) { NMO.console.print(plugin.name+" applied to selector: 'body'" ); }
								plugin.init( NMO.select('body') );
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if ( NMO.debug ) { NMO.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")" ); }
							}
						});
						break;
					}
					default: {
						NMO.select(document).bind(ev, function(e, scope) {
							try {
								if ( NMO.debug ) { NMO.console.print(plugin.name+" applied to selector: '"+(scope.selector||scope)+"'" ); }
								plugin.init( NMO.select(scope) );
								jQuery.event.trigger("PLUGIN_"+plugin.name+"_INIT");
							} catch (ex) {
								if ( NMO.debug ) { NMO.console.print("Unable to initialize Plugin: "+plugin.name+" ("+ex+")" ); }
							}
						});
						break;
					}
				}
			});
		}
	}
	
};
	
//	NMO.require()
//	Injects a resource reference (such as a script or stylesheet) into the document
NMO.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("Invalid file type specified.");
			}
		}

		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("Reached the unreachable code.");
				break;
			}
			
		}
		
	} catch(ex) {

		NMO.console.print("Error in NMO.require(): "+ex.message );
		
	}

};


NMO.findNearest = function(selector, seekFrom) {
	var target = NMO.select(seekFrom).nextAll(selector+":first");
	var seekparent = NMO.select(seekFrom).parent();
	if ( target.length == 0 && seekparent.length > 0 ) {
		target = NMO.findNearest( selector, seekparent.get(0) );
	}
	return target;
};


NMO.Plugins.Add({

	name: "ToggleNearestCollapsible",
	attachToEvents: "PAGE_LOAD,DOM_UPDATED",
	init: function(scope) {
		NMO.select(".toggle-nearest-collapsible", scope).live("click", function() {
			var self = NMO.select(this);
			self.parent().toggleClass("expanded");
			var col = NMO.findNearest(".collapsible", this);
			col.toggle("normal").toggleClass("collapsed");
			if ( col.hasClass("collapsed") ) {
				col.prev("dt").addClass("expanded");
			} else {
				col.prev("dt").removeClass("expanded");
			}
			if ( self.hasClass("remove-on-reveal") ) {
				self.remove();
			}
			NMO.select(scope).trigger('REFLOW_CONTENT', [scope]);
		});
	}

});


// Finds all immediate children of target elements and sets an equal height (taking padding into account)
NMO.Plugins.Add({
	
	name: "EqualizeHeight",
	attachToEvents: "WINDOW_LOAD,DOM_UPDATED,REFLOW_CONTENT",
	init: function(scope) {
		var maxHeight=0;
		var maxElement=null;
		NMO.select(".equal-height", scope).each( function() {
			var targets = NMO.select(this).children();
			targets.css('min-height', '')
				.each( function(i) {
					var el = NMO.select(this);
					if((el.height()+parseInt(el.css("padding-bottom"))+parseInt(el.css("padding-top")))>maxHeight) {
						maxHeight=el.height()+parseInt(el.css("padding-top"))+parseInt(el.css("padding-bottom"));
						maxElement=this;
					}
				})
				.not($(maxElement)).each( function() {
					var el = NMO.select(this);																												
					el.css('min-height', maxHeight-parseInt(el.css("padding-top"))-parseInt(el.css("padding-bottom")));
				});
		});
	}

});


NMO.Plugins.Add({

	name: "FirstLast",
	attachToEvents: "PAGE_LOAD,DOM_UPDATED",
	init: function(scope) {
		NMO.select("*:first-child", scope ).addClass("first-child");
		NMO.select("*:last-child", scope ).addClass("last-child");
	}

});



/************************************************************\
*
*		HTML 5 Compatability for IE
*
\************************************************************/
(function(){
	var e = ("abbr,article,aside,audio,canvas,datalist,details,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video").split(',');
	for (var i = 0; i < e.length; i++) { document.createElement(e[i]); }
})();



/************************************************************\
*
*		Page Load Event - Main Script Init
*
\************************************************************/
NMO.select( function() {
										 
	NMO.select("html").addClass("js-enabled");
	
	NMO.select(document).mousemove( function(e) {
		NMO.mouse.x = e.pageX || e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		NMO.mouse.y = e.pageY || e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	});
	
	NMO.select(window).resize( function() {
		NMO.select(document).trigger("VIEWPORT_CHANGED", [document]);
	});
	
	NMO.select(document).scroll( function() {
		$(document).trigger("VIEWPORT_CHANGED", [document]);
	});
	
	$(document).trigger("VIEWPORT_CHANGED", [document]);
	
	$(".nav-tabs a").each( function() {
		if( location.href.indexOf(this.href) != -1 ) {
			$(this.parentNode).addClass("Active");
		}
	});

});

NMO.select(window).load( function() {
	
	NMO.console.print();
															
});


