/*=============================================================================

			 	 TITLE:		NetMediaOne - Core JavaScript Classes & Utilities
		  MODIFIED:		2006.09.30
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		Prototype 1.5.0 ( www.conio.com )
									Scriptaculous 1.6.4
									YAHOO! Event Utility

=============================================================================*/

var NMO = window.NMO || {

	version: "1.0.0",
	isIE: /MSIE/.test( navigator.userAgent.toUpperCase() ),
	ddMenus: $A( new Array() ),
	mouseX: 0,
	mouseY: 0,

	init: function() {
		Event.observe( document, "mousemove", NMO.onMouseMove, false );
		
		$$(".DropDownMenu").each( function(m) {
			NMO.ddMenus.push( new NMO.DropDownMenu(m) );
		} );
	
	},
	
	onMouseMove: function(e) {
		NMO.mouseX = Event.pointerX(e);
		NMO.mouseY = Event.pointerY(e);
	},

	imgOn: function( img ) {
		$(img).src = $(img).src.replace( "_off", "_on" );
	},
	
	imgOff: function( img ) {
		$(img).src = $(img).src.replace( "_on", "_off" );
	},
	
	findParentByClassName: function( el, className ) {
		var foundEl = "NULL";
		if ( Element.hasClassName( el.parentNode, className ) ) {
			foundEl = el.parentNode;
		}	else {
			foundEl = NMO.findParentByClassName( el.parentNode, className );
		}
		return foundEl;
	},
	
	//
	// 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){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	
	// -----------------------------------------------------------------------------------
	
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

};

NMO.DropDownMenu = Class.create();
NMO.DropDownMenu.prototype = {

	initialize: function(elementRef) {
		this.HIDE_DELAY = 400;
		this.hideTimer = "";
		this.thinkTimer = "";
		this.hasFocus = false;
		this.container = $(elementRef);
		this.boundTo = $( this.container.getAttribute("bindto") );
		this.shadow = document.createElement("iframe");
		Element.addClassName( this.shadow, "DropDownMenuShadow");
		this.shadow.setAttribute( "frameborder", "0" );
		this.shadow.setAttribute( "scrolling", "no" );
		this.shadow.setAttribute( "src", "menushadow.html" );
		$("layoutWrapper").appendChild(this.shadow);
		Element.setOpacity( this.shadow, .10 );
		this.container.style.width = Number( this.container.getAttribute("menuwidth") || 160 ) + "px";
		this.leftOffset = Number( this.container.getAttribute("leftoffset") || 16 );
		this.topOffset = Number( this.container.getAttribute("topoffset") || 40 );		
		if ( this.boundTo != null ) {
			YAHOO.util.Event.addListener( this.boundTo, "mouseover", this.show, this, true );
		}
		this.group = this.container.getAttribute("menugroup") || "default";
	},
	
	think: function() {
		clearTimeout( this.thinkTimer );
		if ( Position.within( this.container, NMO.mouseX, NMO.mouseY ) || Position.within( this.boundTo, NMO.mouseX, NMO.mouseY ) ) {
			clearTimeout( this.hideTimer );
			this.hasFocus = true;
		} else {
			this.hasFocus = false;
			this.hideTimer = setTimeout( function() { this.hide(); }.bind(this), this.HIDE_DELAY );
		}
		this.thinkTimer = setTimeout( function() { this.think(); }.bind(this), 100 );
	},
	
	show: function() {
		clearTimeout( this.hideTimer );
		clearTimeout( this.thinkTimer );
		NMO.ddMenus.each( function(m) {
			if ( m.group == this.group && m.container.id != this.container.id ) {
				m.hasFocus = false;
				m.hide();
			}
		}.bind(this) );
		this.container.style.display = "block";
		this.shadow.style.display = "block";
		if ( this.boundTo != null ) {
			Position.clone( this.boundTo, this.container, { setWidth: false, setHeight: false, offsetTop: this.topOffset, offsetLeft: this.leftOffset } );
			Position.clone( this.container, this.shadow, { offsetTop: 3, offsetLeft: 3 } );
		}
		this.hasFocus = true;
		this.think();
	},
	
	hide: function() {
		clearTimeout( this.hideTimer );
		if ( !this.hasFocus ) {
			clearTimeout( this.thinkTimer );
			this.shadow.style.display = "none";
			this.container.style.display = "none";
		}
	}

};

Event.observe( window, "load", NMO.init, false );
