

// Global variables
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;

// Initialize upon load to let all browsers establish content objects
function initDHTMLAPI( ) {
    if (document.images) {
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? 
            true : false;
    }
}



// Return the available content width space in browser window
function getInsideWindowWidth( ) {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (isIE6CSS) {
        // measure the html element's clientWidth
        return document.body.parentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
    return 0;
}
   
// Return the available content height space in browser window
function getInsideWindowHeight( ) {
	//alert("");
	return 500;
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (isIE6CSS) {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}




function findPos(coord, obj)
{
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			if (coord == 'x') {curleft += obj.offsetLeft}
			else {curleft += obj.offsetTop}
			obj = obj.offsetParent;
		}
	}
	else if (obj.coord)
		curleft += obj.coord;
	return curleft;
}


function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}


function playSound(id) {
	if (document.getElementById(id))
    if (document.all && document.getElementById(id).FileName) {
        document.getElementById(id).Play( );
    }
}


function setDivClass(div, className){
	try	{ if (div.attributes['collapsed'].value != "false") div.className = className;}	
	catch (e)	{div.className = className;	}
}


function setParentMenuVisibility(allowHide, menuID){
	var menus = document.getElementsByTagName('menu');
	for(var j=0; j<menus.length; j++) {
		spans = menus[j].getElementsByTagName('DIV');
		for(var s=0; s<spans.length; s++)
			if (spans[s].attributes['menu']) 
				if (spans[s].attributes['menu'].value == menuID){
				menus[j].setAttribute('allowHide', allowHide);
				setParentMenuVisibility(allowHide, menus[j].id);
			}
	}
}


function parentMenu(obj){
	var parent = obj.parentNode;
	while (parent.tagName != 'MENU')	{
		parent = parent.parentNode; 
		if (parent.tagName == 'BODY') return null;
	}
	return parent;
}

function createSoundObject(sound) {
	if (!document.getElementById(sound)){
		return '<object id="'+sound+'" width="0" height="0" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="#Version=6,0,0,0" style="display: none;"><param id="soundFile" name="FileName" value="'+sound+'"><param name="AutoStart" value="false"></object>';
	}else{return ''};
}

	function initMenu(){
		//alert("init");
		initDHTMLAPI( );

		var zIndex = 0;
		menus = document.getElementsByTagName('menu');
		for(var i=0; i<menus.length; i++) {
			menu = menus[i];
			menu.style.display = 'none';
			menu.setAttribute('allowHide', true);

			menu.onmouseover = function(){
				setParentMenuVisibility(false, this.id);
		//		this.style.display = 'none';
				this.setAttribute('allowHide', false);
		//		this.style.display = '';
				hideAll();
			}
			menu.onmouseout = function(){
				setParentMenuVisibility(true, this.id);
		//		this.style.display = 'none';
				this.setAttribute('allowHide', true);
		//		this.style.display = '';
				hideAll();
			};
		}

		for (var m=0; m<menus.length; m++){
			menu = menus[m];
			try {if (menu.attributes['sound']) menu.innerHTML = createSoundObject(menu.attributes['sound'].value) + menu.innerHTML;}
			catch (e){}

			spans = menu.getElementsByTagName('SPAN');
			if (spans.length == 0)	spans = menu.getElementsByTagName('DIV');
			for(var i=0; i<spans.length; i++){
				if ( menu.attributes['open'])
					if ( menu.attributes['close']) 
						spans[i].setAttribute('collapsed', true);

				if (spans[i].attributes['sound']) 
					spans[i].innerHTML = spans[i].innerHTML + createSoundObject(spans[i].attributes['sound'].value);
				if (spans[i].attributes['menu']) {setDivClass(spans[i],'EXPANDABLE');}

				if (spans[i].getElementsByTagName('A')) {
					spans[i].onclick = function(){
						aHrefs = this.getElementsByTagName('A');
						if (aHrefs.length > 0) {aHrefs[0].click();}
					}
				}

				if (spans[i].attributes['menu']) {
					var atr = parentMenu(spans[i]).attributes['close'];
					if (atr) 
						if ((atr.value == 'onclick') || (atr.value == 'onclickout')){
							document.getElementById(spans[i].attributes['menu'].value).onmouseout = function(){
								this.setAttribute('allowHide', false);
								hideAll();
							};
				
						}
				}


				spans[i].onmouseover = function(){
					try {	playSound(this.attributes['sound'].value);}
					catch(e){
						if (parentMenu(this).attributes['sound']) playSound(parentMenu(this).attributes['sound'].value);
					};
					setDivClass(this,'HOVER'); 
					if (this.attributes['menu']) {
						var menu = document.getElementById(this.attributes['menu'].value);
					//	menu.style.display = 'none';
						menu.setAttribute('allowHide', false);
					//	menu.style.display = '';
						setDivClass(this,'EXPANDABLEHOVER'); 
						if (getElementStyle(this, 'display', 'display') == 'inline') {
							x = findPos('x',this);
							if (x+menu.clientWidth > getInsideWindowWidth()){
								x = x + this.offsetWidth - menu.clientWidth;
							}
							if (x < 0 ) x = findPos('x',this);
							y = findPos('y',this) + this.offsetHeight;
							if (y+menu.clientHeight > getInsideWindowHeight()){
								y = y - this.offsetHeight - menu.clientHeight;
							}
							if (y < 0) y = findPos('y',this) + this.offsetHeight;
						}else {
							x = findPos('x',this) + this.offsetWidth;
							if (x+menu.clientWidth > getInsideWindowWidth()){
								x = x - this.offsetWidth - menu.clientWidth;
							}
							if (x < 0 ) x = findPos('x',this) + this.offsetWidth;

							y = findPos('y',this);
							if (y+menu.clientHeight > getInsideWindowHeight()){
								y = y  + this.offsetHeight - menu.clientHeight;
							}
							if (y < 0) y = findPos('y',this);

						}
						var style = menu.style;
						style.top = y;
						style.left = x;
						if (!parentMenu(this).attributes['open']){
							setSRC(this);
							show(menu);
						}else{
							if (parentMenu(this).attributes['open'].value != 'onclick'){
								setSRC(this);
								show(menu);
							}
						}
					}
				};

				if (parentMenu(spans[i]).attributes['open'])
					if (parentMenu(spans[i]).attributes['open'].value == 'onclick')
						if (spans[i].attributes['menu'])
							spans[i].onclick = function(){
								var menu = document.getElementById(this.attributes['menu'].value);

								if (!this.attributes['collapsed']){
									setSRC(this);
									zIndex++;
									this.style.zIndex = zIndex;
									show(menu); 
								}else{
									if (this.attributes['collapsed'].value == "true"){
										if (parentMenu(this).attributes['close'])
											if (parentMenu(this).attributes['close'].value == "onclickout"){
											var	spans = parentMenu(this).getElementsByTagName('SPAN');
											if (spans.length == 0)	spans = parentMenu(this).getElementsByTagName('DIV');
											for (var s=0;s<spans.length ;s++ ){
												spans[s].setAttribute('collapsed', true);
												try{
													hide(document.getElementById(spans[s].attributes['menu'].value));
													setDivClass(spans[s],'EXPANDABLE');
												//	alert(spans[s].attributes['menu'].value);
												}
												catch (e){setDivClass(spans[s],'');}
											}
										//	hideAll();
										}
										setDivClass(this, 'EXPAND');
										this.setAttribute('collapsed', false);
										setSRC(this);
																			zIndex++;
									this.style.zIndex = zIndex;

										show(menu); 
									}else{
										this.setAttribute('collapsed', true);
										setDivClass(this,'EXPANDABLE');
																			zIndex++;
									this.style.zIndex = zIndex;

										hide(menu);
									}
									
								}
							}


				spans[i].onmouseout = function(){
					var allowHide = true;
					var art = parentMenu(this).attributes['close'];
					if (art) 
						if ((art.value == 'onclick') || (art.value == 'onclickout')) allowHide = false;
					if (this.attributes['menu']) {
							var menu = this.attributes['menu'].value;
							document.getElementById(menu).setAttribute('allowHide', allowHide);
							setDivClass(this,'EXPANDABLE');
						} else {setDivClass(this,'');}
						if (!parentMenu(this).attributes['collapsed'])	hideAll();
				};	
			}




			function hide(obj){
				obj.style.display = 'none';
			}


			function setSRC(menuItem){
				try{document.getElementById(menuItem.getAttribute('targetID')).setAttribute(menuItem.getAttribute('targetAttribute'), menuItem.getAttribute('targetValue'));}
				catch(e){}
			}

			function show(obj){
					zIndex++;
					obj.style.zIndex = zIndex;
					obj.style.display = '';
					obj.style.visibility = 'hidden';
					try{
						obj.filters[0].Apply();
						obj.style.visibility = 'visible';
						obj.filters[0].Play(); 
					} catch(e){obj.style.visibility = 'visible'; }
			}




			function hideAll() {
				timer = setTimeout( function(){
							menus = document.getElementsByTagName('menu');
							for(var i=0; i<menus.length; i++)  
								if (menus[i].attributes['allowHide']) 
									if(menus[i].attributes['allowHide'].value == 'true')
										menus[i].style.display = 'none';
						}, 150);
			}

		}
	}