var AC_FL_RunContent = 0;
var zInd = 300;

//* loading js files *//
document.write('<script type="text/javascript" src="includes/js/life.js"></script>');
document.write('<script type="text/javascript" src="includes/js/scriptaculous/lib/prototype.js"></script>');
document.write('<script type="text/javascript" src="includes/js/scriptaculous/src/scriptaculous.js"></script>');
document.write('<script type="text/javascript" src="includes/js/AC_RunActiveContent.js"></script>');

function addDOMLoadEvent(func) {
	if (!window.__load_events) {
		var init = function () {
			// quit if this function has already been called
			if (arguments.callee.done) return;
			// flag this function so we don't do the same thing twice
			arguments.callee.done = true;
			// kill the timer
			if (window.__load_timer) {
				clearInterval(window.__load_timer);
				window.__load_timer = null;
			}
			
			// execute each function in the stack in the order they were added
			for (var i=0;i < window.__load_events.length;i++) {
				window.__load_events[i]();
			}
			window.__load_events = null;
		};
   
		// for Mozilla/Opera9
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", init, false);
		}

		// for Internet Explorer
		/*@cc_on @*/
		/*@if (@_win32)
		document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				init(); // call the onload handler
			}
		};
		/*@end @*/

		// for Safari
		if (/WebKit/i.test(navigator.userAgent)) { // sniff
			window.__load_timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					init(); // call the onload handler
				}
			}, 10);
		}

		// for other browsers
		window.onload = init;
		
		// create event function stack
		window.__load_events = [];
	}
	
	// add function to event stack
	window.__load_events.push(func);
}

// Initialize user interface
function initUI(){
	// Set navigation root
	var navRoot = document.getElementById("navigation");
	
	// If flash replacement script cannot be found
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		var flash = AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0',
			'width', '760',
			'height', '158',
			'src', 'includes/flash/header',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'opaque',
			'devicefont', 'false',
			'id', 'flesjheader',
			'bgcolor', '#ffffff',
			'name', 'header',
			'menu', 'false',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', 'includes/flash/header',
			'salign', ''
		); //end AC code
		
		if(document.getElementById("content-header")){
			document.getElementById("content-header").innerHTML = flash;
		}
	}
	
	// For each child element of navigation root
	for (i=0; i<navRoot.childNodes.length; i++) {
		// Set node
		node = navRoot.childNodes[i];
		// If node is LI element and not a bullet
		if (node.nodeName == "LI" && node.innerHTML.length > 2) {
			// Append classname and set temporary over state to compensate IE opacity bug
			node.className += " navItem over";
			// If element has style ;)
			if(node.childNodes[1].style){
				// Compensate for IE bug in De Tol website by setting fixed width
				node.childNodes[1].style.width = node.childNodes[1].offsetWidth;
			}
			// Clear temporary over state
			clearOverState(node);
			
			/*
			 * mouseOver event
			 */
			node.onmouseover = function() {
				// Clear mouse over state
				clearOverState(this);
				// Clear any potential timeout
				clearTimeout(this.toID);
				// Append classname
				this.className += " over";
				if(this.childNodes[1].style){
					// Set opacity to 1
					new Effect.Opacity(this.childNodes[1].id,{duration:0,from:0,to:1});
					// Move active subnav to top layer
					upEl(this.childNodes[1].id);
				}
			}
			/*
			 * mouseOut event
			 */
			node.onmouseout = function(event) {
				if(this.childNodes[1].id){
					// Set self
					var _self = this;
					// Set timeout to make subnav disappear
					this.toID = setTimeout(function(ms){_self.disappear();}, 1000);
				}
			}
			/*
			 * Function disappear
			 * This function fades the sub navigation to alpha = 0
			 */
			node.disappear = function () {
				// Start fade effect (Scriptaculous)
				new Effect.Opacity(this.childNodes[1].id,{duration:0.5,from:1.0,to:0,afterFinish:callBackOnFinish});
			}
		}
	}
}

/*
 * callBackOnFinish
 * Clears the over state when subnav has faded away
 */
function callBackOnFinish(obj){
	clearOverState(obj.element.parentNode);
}

/*
 * clearOverState
 * Clears the over state of an object
 */
function clearOverState(obj){
	obj.className = obj.className.replace(" over","");
}

function getRefToDiv(divID,oDoc) {
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) {
			return oDoc.layers[divID];
		} else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDiv(divID,oDoc.layers[x].document);
			}
			return y;
		}
	}
	return false;
}

function upEl(theDiv) {
	if( document.layers ) {
		theDiv = document.layers['myDiv6C'].document.layers[theDiv];
	} else {
		theDiv = getRefToDiv(theDiv);
	}
	if( !theDiv ) { return; }
	if( theDiv.style ) { theDiv = theDiv.style; }
	theDiv.zIndex = zInd; zInd++;
}

addDOMLoadEvent(initUI);