//=======================================================
// Stealthmenu v3.1
// A script by: Michael van Ouwerkerk - 13thparallel.org
//=======================================================

// push appends new elements to an array, and returns the new length
if( !Array.prototype.push ) {
	Array.prototype.push = function() {
		for( var i = 0; i < arguments.length; i++ ) this[ this.length ] = arguments[ i ];
		return this.length;
	}
}

// pop removes the last element from an array and returns it
if( !Array.prototype.pop ) {
	Array.prototype.pop = function() {
		var lastitem = this.length > 0 ? this[ this.length - 1 ] : undefined;
		if( this.length > 0 ) this.length--;
		return lastitem;
	}
}

// Netscape 4 must reload on resize to restore the css.
if( document.layers && !document.getElementById ) {
	var scrX = window.innerWidth;
	var scrY = window.innerHeight;
	window.onresize = function() {
		if( scrX != window.innerWidth || scrY != window.innerHeight ) history.go( 0 );
	}
}

var px = (document.layers && !document.getElementById) || window.opera ? 0 : "px";

var Stealthmenu = {
	stylestring : '<style type="text/css">.sub a:hover {color: #fff; font-weight: 700; font-style: italic; text-decoration: none;}</style>',
	rolloverSpace : 22,
	useLiquidLayout : true,
	solidWidth : 686,
	
	useFading : true,			// true for a fade-in effect in windows explorer 5+ and all platforms ns6, false for no fade-in effect.
	fadeDuration : 300,			// The duration of the fade-in effect, in milliseconds.
	maxOpacity : 100,			// A number between 0 and 100. This is the percentage where the fade-in stops.
	fadeTimer : null,
	fadeElm : null,
	
	floaters : [ "select" ],
	
	hideDelay : 250,			// The number of milliseconds the script must wait before hiding the submenu.
	hideTimer : null,
	sub : new Array(),
	currentSub : null,
	actionStorage : new Array(),
	loaded : false,
	
	init : function() {
		if( !this.loaded && ( document.getElementById || document.all || document.layers ) ) {
			var filterStr = "";
			if( this.useFading ) filterStr += "progid:DXImageTransform.Microsoft.Alpha( Opacity=100, Style=0 )";
			//if( this.useFading ) filterStr += " ";
			//filterStr += "progid:DXImageTransform.Microsoft.Shadow( color=#CE0000, direction=315, strength=5 )";
			if( this.useFading ) filterStr += " ";
			filterStr += "progid:DXImageTransform.Microsoft.dropshadow( OffX=5, OffY=5, Color=#006400, Positive=true )";

			var elm = true;
			var id = null;
			var i = 0;
			while( elm ) {	
				id = "divSub" + i;
				elm = document.getElementById ? document.getElementById( id ) : document.all ? document.all[ id ] : document.layers[ id ];
				if( elm ) {
					this.sub[ i ] = new this.makeSub( elm );
					if( document.getElementById && elm.offsetWidth ) {
						elm.style.width = elm.offsetWidth + this.rolloverSpace + "px";
					}
					if( elm.filters ) elm.style.filter = filterStr;
				}
				i++;
			}
			
			// Write down the style string.
			if( document.getElementById ) document.write( this.stylestring );
			
			this.loaded = true;
		}
	},
	
	// Positions a submenu.
	positionSub : function( sub ) {
		// Setting up variables.
		var viewport = this.getViewport();
		var setX = sub.originalLeft;
		var setY = sub.originalTop;
		
		// Adjusting horizontal position if a liquid layout is used.
		if( this.useLiquidLayout && viewport.width > this.solidWidth ) {
			setX += ( viewport.width - this.solidWidth ) / 2;
		}

		// Make sure the element is placed inside the viewport.
		if( setX + this.getWidth( sub.elm ) > viewport.width + viewport.scrollX ) {
			setX = viewport.width + viewport.scrollX - this.getWidth( sub.elm );
		}
		if( setY + this.getHeight( sub.elm ) > viewport.height + viewport.scrollY ) {
			setY = viewport.height + viewport.scrollY - this.getHeight( sub.elm );
		}
		if( setX < viewport.scrollX ) setX = viewport.scrollX;
		if( setY < viewport.scrollY ) setY = viewport.scrollY;
		
		// Move the submenu.
		this.setLeft( sub.elm, setX );
		this.setTop( sub.elm, setY );
	},
	
	show : function( num ) {
		if( this.loaded && this.sub[ num ] ) {
			this.stopDelayedHide();
			if( this.currentSub != this.sub[ num ] ) {
				this.hideCurrent();
				this.currentSub = this.sub[ num ];
				this.positionSub( this.currentSub );
				
				// Hide nasty elements.
				if( window.showModalDialog || window.opera ) {
					for( var i = 0; i < this.floaters.length; i++ ) {
						this.hideFloaters( this.currentSub.elm, this.floaters[ i ] );
					}
				}
				
				// Show the sub either by fading in setting its visibility.
				if( this.useFading && document.getElementById ) {
					this.startFade( this.currentSub.elm );
				}
				else this.currentSub.css.visibility = "visible";
			}
			
			// Netscape 4 workaround, the delayedHide must be stopped after a timeout.
			if( document.layers && !document.getElementById ) {
				setTimeout( "Stealthmenu.stopDelayedHide()", this.hideDelay / 2 );
			}
		}
	},
	
	startDelayedHide : function() {
		Stealthmenu.hideTimer = setTimeout( "Stealthmenu.hideCurrent()" , Stealthmenu.hideDelay );
	},
	
	stopDelayedHide : function() {
		clearTimeout( Stealthmenu.hideTimer );
	},

	hide : function() {
		if( this.loaded ) this.startDelayedHide();
	},
	
	hideCurrent : function() {
		if( this.currentSub ) {
			this.currentSub.css.visibility = "hidden";
			clearTimeout( Stealthmenu.fadeTimer );
			this.fadeElm = null;
			this.doActions();
			if( window.showModalDialog || window.opera ) {
				for( var i = 0; i < this.floaters.length; i++ ) {
					this.showFloaters( this.floaters[ i ] );
				}
			}
			this.currentSub = null;
		}
	},
	
	startFade : function( elm ) {
		this.fadeElm = elm;
		elm.style.visibility = "visible";
		if( typeof elm.style.MozOpacity != "undefined" || elm.filters ) {
			this.doFade();
		}
	},
	
	doFade : function( startTime ) {
		if( !startTime ) var startTime = new Date().valueOf();
		var currentTime = new Date().valueOf();
		var percent = ( currentTime - startTime ) / this.fadeDuration;
		if( percent >= 1 ) {
			this.setOpacity( this.fadeElm, this.maxOpacity );
		}
		else {
			var opacity = Math.round( percent * this.maxOpacity );
			this.setOpacity( this.fadeElm, opacity );
			this.fadeTimer = setTimeout( "Stealthmenu.doFade(" + startTime + ")", 10 );
		}
	},
	
	setOpacity : function( elm, opacity ) {
		if( elm && elm.style && typeof opacity == "number" ) {
			elm.opacity = opacity;
			if( typeof elm.style.MozOpacity != "undefined" ) elm.style.MozOpacity = opacity / 100;
			//if( elm.filters ) elm.style.filter = "alpha( opacity = " + opacity + " )";
			if( elm.filters && elm.filters[ "DXImageTransform.Microsoft.Alpha" ] ) {
				elm.filters[ "DXImageTransform.Microsoft.Alpha" ].opacity = opacity;
			}
		}
	},
	
	// Oject constructor.
	makeSub : function( elm ) {
		this.elm = elm;
		this.css = this.elm.style ? this.elm.style : this.elm;
		this.elm.onmouseout = Stealthmenu.startDelayedHide;
		this.elm.onmouseover = Stealthmenu.stopDelayedHide;
		this.originalLeft = Stealthmenu.getLeft( this.elm );
		this.originalTop = Stealthmenu.getTop( this.elm );
		return this;
	},
	
	// Stores a string that can later be executed.
	storeAction : function( action ) {
		this.actionStorage.push( action );
	},
	
	// Executes all stored actions.
	doActions : function() {
		while( this.actionStorage.length ) eval( this.actionStorage.pop() );
	},
	
	// Hides stuff like select boxes that happen to be under a submenu.
	hideFloaters : function( elm, tagName ) {
		if( document.getElementsByTagName && elm.offsetWidth && elm.offsetHeight ) {;
			var floaters = document.getElementsByTagName( tagName );
			var x = 0;
			var y = 0;
			for( var i = 0; i < floaters.length; i++ ) {
				x = this.getPageLeft( floaters[ i ] );
				y = this.getPageTop( floaters[ i ] );
				if( x + floaters[ i ].offsetWidth > elm.offsetLeft 
				&& x < elm.offsetLeft + elm.offsetWidth 
				&& y + floaters[ i ].offsetHeight > elm.offsetTop 
				&& y < elm.offsetTop + elm.offsetHeight ) {
					floaters[ i ].style.visibility = "hidden";
				}
			}
		}
	},
	
	// Shows all elements with a certain tagName.
	showFloaters : function( tagName ) {
		if( document.getElementsByTagName ) {
			var floaters = document.getElementsByTagName( tagName );
			for( var i = 0; i < floaters.length; i++ ) {
				floaters[i].style.visibility = "visible";
			}
		}
	},
	
	getWidth : function( node ) {
		var num = 0;
		if( node.offsetWidth ) num = node.offsetWidth;
		else if( node.pixelWidth ) num = node.pixelWidth;
		else if( node.style && node.style.width ) num = parseInt( node.style.width );
		else if( node.clip && node.clip.width ) num = node.clip.width;
		return num;
	},
	
	getHeight : function( node ) {
		var num = 0;
		if( node.offsetHeight ) num = node.offsetHeight;
		else if( node.pixelHeight ) num = node.pixelHeight;
		else if( node.style && node.style.height ) num = parseInt( node.style.height );
		else if( node.clip && node.clip.height ) num = node.clip.height;
		return num;
	},
	
	getLeft : function( node ) {
		var num = 0;
		if( node.offsetLeft ) num = node.offsetLeft;
		else if( node.left ) num = node.left;
		else if( node.style && node.style.left ) num = parseInt( node.style.left );
		return num;
	},
	
	getTop : function( node ) {
		var num = 0;
		if( node.offsetTop ) num = node.offsetTop;
		else if( node.top ) num = node.top;
		else if( node.style && node.style.top ) num = parseInt( node.style.top );
		return num;
	},
	
	setLeft : function( node, num ) {
		if( node.style ) node.style.left = num + "px";
		else node.left = num;
	},
	
	setTop : function( node, num ) {
		if( node.style ) node.style.top = num + "px";
		else node.top = num;
	},
	
	getPageLeft : function( elm ) {
		var x = 0;
		while( elm.offsetParent && typeof elm.offsetLeft != "undefined" ) {
			x += elm.offsetLeft;
			elm = elm.offsetParent;
		}
		return x;
	},
	
	getPageTop : function( elm ) {
		var y = 0;
		while( elm.offsetParent && typeof elm.offsetTop != "undefined" ) {
			y += elm.offsetTop;
			elm = elm.offsetParent;
		}
		return y;
	},

	getViewport : function() {
		var viewport = { width: 0, height: 0, scrollX: 0, scrollY: 0 };
		
		// measure the width
		if( document.documentElement && document.documentElement.clientWidth ) viewport.width = document.documentElement.clientWidth;
		else if( document.body && document.body.clientWidth ) viewport.width = document.body.clientWidth;
		else if( window.innerWidth ) viewport.width = window.innerWidth - 18;
		
		// measure the height
		if( document.documentElement && document.documentElement.clientHeight ) viewport.height = document.documentElement.clientHeight;
		else if( document.body && document.body.clientHeight ) viewport.height = document.body.clientHeight;
		else if( window.innerHeight ) viewport.height = window.innerHeight - 18;
		
		// measure the scrollX
		if( document.documentElement && document.documentElement.scrollLeft ) viewport.scrollX = document.documentElement.scrollLeft;
		else if( document.body && document.body.scrollLeft ) viewport.scrollX = document.body.scrollLeft;
		else if( window.pageXOffset ) viewport.scrollX = window.pageXOffset;
		else if( window.scrollX ) viewport.scrollX = window.scrollX;
		
		// measure the scrollY
		if( document.documentElement && document.documentElement.scrollTop ) viewport.scrollY = document.documentElement.scrollTop;
		else if( document.body && document.body.scrollTop ) viewport.scrollY = document.body.scrollTop;
		else if( window.pageYOffset ) viewport.scrollY = window.pageYOffset;
		else if( window.scrollY ) viewport.scrollY = window.scrollY;
		
		return viewport;
	}
}

Stealthmenu.init();

// end