// Author: Benoit Marsot benoit@alapi.com
// Copyright 1999 ALAPI, Inc. All rights reserved.
// Contact us for usage: +1(415) 512 5464, info@alapi.com
// More objects see www.alapi.com 

function hlite(e) {
	//fix for sticky hilite
	if(this.parentMenuObj.actEntry!=null) {
		this.parentMenuObj.actEntry.document.layers[0].visibility=true;
		this.parentMenuObj.actEntry.document.layers[1].visibility=false;
	}

	this.document.layers[0].visibility=false;
	this.document.layers[1].visibility=true;
	this.parentMenuObj.actEntry=this;

	if(this.parentMenuObj.popUpParent.visibleMenu[this.parentMenuObj.popUpParent.visibleMenu.length-1]!=this.parentMenuObj.index) {
		this.parentMenuObj.popUpParent.pop();
	}
	return true;
}

var isClicked = false;
function doSel(e) {
	//fix for sticky hilite
	if(this.parentMenuObj.actEntry!=null) {
		this.parentMenuObj.actEntry.document.layers[0].visibility=true;
		this.parentMenuObj.actEntry.document.layers[1].visibility=false;
	}

	this.document.layers[0].visibility=false;
	this.document.layers[1].visibility=true;
	this.parentMenuObj.actEntry=this;

	if(this.parentMenuObj.popUpParent.visibleMenu[this.parentMenuObj.popUpParent.visibleMenu.length-1]!=this.parentMenuObj.index) {
		this.parentMenuObj.popUpParent.pop();
	}
	var showSub = (this.subMenu!=null)?true:false;
	if (showSub && isClicked) {
		this.subMenu.show();
	}
	return true;
}

function choose(e) {
	//the menu entry have a sub menu
	if(this.subMenu!=null) {
		this.subMenu.show();
		isClicked = (!isClicked)?true:false;
		if (!isClicked)
			this.subMenu.hide();
	}
	if(this.command!=null) {
		eval(this.command);
	}
	return true;
}

function new_menu(e) {
	while(window[window.__ALAPI_menu_name].visibleMenu.length>0) {
		window[window.__ALAPI_menu_name].pop();
	}
	return true;
}

function alapi_quit_menu(e) {
	if(this.routeEvent(e)==null) {
		while(window[window.__ALAPI_menu_name].visibleMenu.length>1) {
			window[window.__ALAPI_menu_name].pop();
		}
	}
	return true;
}

function ALAPI_PopUp_menu(name) {
//Properties
	this.menus=new Array();
	window.__ALAPI_menu_name=name;
	this.visibleMenu=new Array();  //ARRAY OF VISIBLE MENUS
//Methods
	this.pop=alapi_last_hide;
	this.addMenu= ALAPI_addMenu;
	this.closeAll=alapi_close_all;
	
//capture the window event
	window.captureEvents(Event.MOUSEDOWN)
	window.onmousedown=alapi_quit_menu;
}

function ALAPI_addMenu(lContainer, pMenu, pEntryIndex, posX, posY) {
	this.menus[this.menus.length]=new ALAPI_menu(lContainer, pMenu, pEntryIndex, posX, posY, this, this.menus.length);
}

function ALAPI_menu(lContainer, pMenu, pEntryIndex, posX, posY, popUpParent, index) {
	this.popUpParent=popUpParent;
	this.index=index;
	this.posY=posY;
	//fix
	this.actEntry=null;
	this.lContainer=lContainer;
	this.pEntryIndex=pEntryIndex;
	var i=0;

//todo:
	this.lContainer.document.layers[i].captureEvents(Event.MOUSEOVER|Event.MOUSEDOWN);
	this.lContainer.document.layers[i].onmouseover=doSel;
	this.lContainer.document.layers[i].onmousedown=choose; 
	this.lContainer.document.layers[i].parentMenuObj=this;
	for(i=1;i< this.lContainer.document.layers.length;i++) {
		this.lContainer.document.layers[i].top=this.lContainer.document.layers[i-1].clip.height+this.lContainer.document.layers[i-1].top-1;

		this.lContainer.document.layers[i].captureEvents(Event.MOUSEOVER|Event.MOUSEDOWN);
		this.lContainer.document.layers[i].onmouseover=doSel; 
		this.lContainer.document.layers[i].onmousedown=choose; 
		this.lContainer.document.layers[i].parentMenuObj=this;
	}
	this.lContainer.document.layers[i-1].clip.height=this.lContainer.document.layers[i-1].document.layers[0].clip.height+2;

	this.lContainer.clip.height=this.lContainer.document.layers[i-1].top+this.lContainer.document.layers[i-1].clip.height+1;
	//setting the position of the menu
	this.lContainer.left=(pMenu==null)?posX:pMenu.lContainer.document.layers[pEntryIndex].pageX+posX;
	this.lContainer.top=(pMenu==null)?posY:pMenu.lContainer.document.layers[pEntryIndex].pageY+posY;

	//creating a reference to the parent menu
	if(pMenu!=null) {
		pMenu.lContainer.document.layers[pEntryIndex].subMenu=this;
		this.pMenu=pMenu;
	}

	this.show=alapi_menu_show;
	this.slide=menu_slide;
	this.doS=doSlide;
	this.nMenu=new_menu;
	this.hide=alapi_menu_hide;
	this.command=alapi_menu_addCommand;
	//pre open method
	this.open=ALAPI_PreOpenMenu;	
}
function alapi_menu_addCommand(index,command) {
	this.lContainer.document.layers[index].command=command;
}

function alapi_menu_hide() {
	this.lContainer.visibility=false;
}

function alapi_last_hide() {
	var menuIndex;
	this.menus[menuIndex=this.visibleMenu.pop()].lContainer.visibility=false;
	//fix for sticky hilite
	if(this.menus[menuIndex].actEntry!=null) {
		this.menus[menuIndex].actEntry.document.layers[0].visibility=true;
		this.menus[menuIndex].actEntry.document.layers[1].visibility=false;
		this.menus[menuIndex].actEntry=null;
	}

}

var sliceY = 0;
var timerID = null;
var currMenu;
function doSlide(el) {
	currMenu = el;
	obj = eval("document.layers[" + currMenu + "]");
	sliceY += 12;
	obj.clip.bottom = sliceY;
	
	if (sliceY < 200)
	{
		timerID = setTimeout('doSlide(currMenu)',1);
	}
	else
	{
		obj.clip.bottom = 200;
		clearTimeout(timerID);
		sliceY = 0;
	}
}

function menu_slide() {
	this.popUpParent.visibleMenu[this.popUpParent.visibleMenu.length]=this.index;
	//setting the position of the menu
	this.lContainer.top=(this.pMenu==null)?this.posY:this.pMenu.lContainer.document.layers[this.pEntryIndex].pageY+this.posY;

	// New left position added by TS 000814 
	//var pLeft = (parseInt(window.innerWidth + 12) - 751);

	var iFrameDiff = parseInt(window.outerWidth)-parseInt(window.innerWidth);
	var pLeft = ((parseInt(window.innerWidth)+parseInt(iFrameDiff)) -751);
	pLeft = pLeft / 2;
	//pLeft = pLeft - 29.5;
	//pLeft = pLeft - 31;
	//MRY 000817
	pLeft = pLeft - 14;

	this.lContainer.left = pLeft;
	this.lContainer.visibility=true;
	
	//set the offset of menu
	if(parseInt(this.lContainer.top)+ this.lContainer.clip.height>=window.innerHeight) {
		this.lContainer.top = window.innerHeight-this.lContainer.clip.height-5;
	}	
	this.doS(this.index);
	//alert(document.images);
	return true;
}

function alapi_menu_show() {
	this.lContainer.visibility=true;
	this.popUpParent.visibleMenu[this.popUpParent.visibleMenu.length]=this.index;
	//setting the position of the menu
	this.lContainer.top=(this.pMenu==null)?this.posY:this.pMenu.lContainer.document.layers[this.pEntryIndex].pageY+this.posY-4;
	//this.lContainer.left=this.pMenu.lContainer.left+90;
	this.lContainer.left=this.pMenu.lContainer.left+104;
	
	//set the offset of menu
	if(parseInt(this.lContainer.top)+ this.lContainer.clip.height>=window.innerHeight) {
		this.lContainer.top = window.innerHeight-this.lContainer.clip.height-5;
	}	
}

function alapi_close_all() {
	while(this.visibleMenu.length>0) {
		this.pop();
	}
}

/*=================*
 *  Pre open Menu  *
 *=================*/

function ALAPI_PreOpenMenu(index) {
	//fix for sticky hilite
	if(this.actEntry!=null) {
		this.actEntry.document.layers[0].visibility=true;
		this.actEntry.document.layers[1].visibility=false;
	}

	this.lContainer.document.layers[index].document.layers[0].visibility=false;
	this.lContainer.document.layers[index].document.layers[1].visibility=true;
	this.actEntry=this.lContainer.document.layers[index];

	if(this.popUpParent.visibleMenu[this.popUpParent.visibleMenu.length-1]!=this.index) {
		this.popUpParent.pop();
	}
	if(this.lContainer.document.layers[index].subMenu!=null) {
		this.lContainer.document.layers[index].subMenu.show();
	}
	return true;
}


