//////////////////////////////////////////////////////////////////////////////////
// Layers 
//////////////////////////////////////////////////////////////////////////////////

var layerQueue=new Array()
var layerIndex=-1

/* hides <select> and <applet> objects (for IE only) */
hideElement=function(elmID, overDiv){
  if(ie){
	  for(var i=0;i<document.getElementsByTagName(elmID).length; i++){
	    obj = document.getElementsByTagName( elmID )[i];
	    if(!obj || !obj.offsetParent){continue;}
    
	    // Find the element's offsetTop and offsetLeft relative to the BODY tag.
	    objLeft   = obj.offsetLeft;
	    objTop    = obj.offsetTop;
	    objParent = obj.offsetParent;
  	  
	    while(objParent.tagName.toUpperCase()!="BODY"){
		    objLeft  += objParent.offsetLeft;
		    objTop   += objParent.offsetTop;
  		  objParent = objParent.offsetParent;
	    };
    
	    objHeight = obj.offsetHeight;
	    objWidth = obj.offsetWidth;
    
	    if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
	    else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
	    else if( overDiv.offsetTop >= ( objTop + objHeight ));
	    else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
	    //else{obj.style.visibility = "hidden";};
	    else{obj.style.display='none';};
	  }
  }
}
 
/*
* unhides <select> and <applet> objects (for IE only)
*/
showElement=function(elmID){
  if(ie){
	  for(var i=0;i<document.getElementsByTagName(elmID).length;i++){
	    obj = document.getElementsByTagName(elmID)[i];  	  
	    if(!obj || !obj.offsetParent){continue;};  	
	    //obj.style.visibility = "";
	    obj.style.display='';
	  }
  }
}

lw_createLayer=function(layerName, top_pos, left_pos, width, height, bgcolor, bordercolor, z_index) {
	//return "<div ONCLICK='event.cancelBubble=true' id='"+layerName+"' style='z-index:" + z_index + ";position:absolute;top:"+top_pos+";left:"+left_pos+";visibility:hidden;'><table bgcolor='"+bgcolor+"' style='border-width:1px;border-style:solid;border-color:" + bordercolor + "' cellpadding=2 cellspacing=0 width=0><tr><td valign=top width='"+width+"' height='"+height+"'><span id='"+layerName+"_content'></span></td></tr></table></div>";
	/*
	document.write('<div ONCLICK="event.cancelBubble=true" id="'+layerName+'" style="z-index:' + z_index + ';position:absolute;top:'+top_pos+';left:'+left_pos+';visibility:hidden;">'+
	               '<table bgcolor="'+bgcolor+'" style="border-width:1px;border-style:solid;border-color:' + bordercolor + '" cellpadding="2" cellspacing="0" width="0"><tr><td valign=top width="'+width+'" height="'+height+'"><span id="'+layerName+'_content"></span></td></tr></table></div>');
  */
	/*
	document.write('<div ONCLICK="event.cancelBubble=true" id="'+layerName+'" style="z-index:' + z_index + ';position:absolute;top:'+top_pos+';left:'+left_pos+';visibility:hidden;">'+
	               '<table bgcolor="'+bgcolor+'"'+
	               ' style="border:solid 1px ' + bordercolor + ';"'+
	               ' cellpadding="2" cellspacing="0" width="0"><tr>'+
	               '<td valign=top style="width:'+width+'px;height:'+height+'px;"><span id="'+layerName+'_content"></span></td></tr></table></div>');
  */
	document.write('<div ONCLICK="event.cancelBubble=true" id="'+layerName+'" style="z-index:' + z_index + ';position:absolute;top:'+top_pos+';left:'+left_pos+';display:none;">'+
	               '<table bgcolor="'+bgcolor+'"'+
	               ' style="border:solid 1px ' + bordercolor + ';"'+
	               ' cellpadding="2" cellspacing="0" width="0"><tr>'+
	               '<td valign=top style="width:'+width+'px;height:'+height+'px;"><span id="'+layerName+'_content"></span></td></tr></table></div>');
};

lw_getObj=function(objName) {
	return (dom)?document.getElementById(objName).style:ie?eval("document.all."+objName) :eval("document."+objName)
};

lw_showLayer=function(layerName) {
	found=false
	for (i=0;i<=layerIndex;i++){
		if (layerQueue[i]==layerName){found=true};
	};

	//if ((lw_getObj(layerName).visibility!="visible")&&(lw_getObj(layerName).visibility!="show")){
	if(lw_getObj(layerName).display!='inline'){
		//lw_getObj(layerName).visibility = (dom||ie)?"visible":"show";
		lw_getObj(layerName).display='inline';
		layerQueue[++layerIndex]=layerName;

		hideElement( 'SELECT', document.getElementById(layerName) );
		hideElement( 'APPLET', document.getElementById(layerName) );
	};
};

lw_hideLayer=function(){
	showElement( 'SELECT', document.getElementById(layerQueue[layerIndex]) );
	showElement( 'APPLET', document.getElementById(layerQueue[layerIndex]) );

	//lw_getObj(layerQueue[layerIndex--]).visibility = "hidden";
	lw_getObj(layerQueue[layerIndex--]).display="none";
};

lw_hideLayerName=function(layerName) {
	var i;
	var tmpQueue=new Array();
	var newIndex=-1;

	showElement( 'SELECT', document.getElementById(layerName) );
	showElement( 'APPLET', document.getElementById(layerName) );

	//lw_getObj(layerName).visibility = "hidden";
	lw_getObj(layerName).display='none';

	for (i=0;i<=layerIndex;i++){
		if((layerQueue[i]!="")&&(layerQueue[i]!=layerName)){
			tmpQueue [++newIndex] = layerQueue[i];
			hideElement( 'SELECT', document.getElementById(layerQueue[i]) );
			hideElement( 'APPLET', document.getElementById(layerQueue[i]) );
		};		
	};
	layerQueue = tmpQueue;
	layerIndex = newIndex;
};

lw_closeAllLayers=function(){
	while (layerIndex >= 0){lw_hideLayer();};
};

lw_closeLastLayer=function() {
	if(layerIndex >= 0){
		//while ((lw_getObj(layerQueue[layerIndex]).visibility!="visible") && (layerIndex>0)){
		while ((lw_getObj(layerQueue[layerIndex]).display!='inline') && (layerIndex>0)){
			layerIndex--;
		};
		lw_hideLayer();
	};
};

lw_escLayer=function(e){
	if (navigator.appName=='Netscape'){
		var keyCode = e.keyCode?e.keyCode:e.which?e.which:e.charCode;
		if ((keyCode==27)||(keyCode==1)){
			lw_closeLastLayer();
		};
	}
	else{	
	  if((event.keyCode==0)||(event.keyCode==27)){lw_closeLastLayer();};
  };
};

var lw_leftpos      = 0;
var lw_toppos       = 0;
var lw_width        = 0;
var lw_height       = 0;
var lw_bgcolor      = "#ffffff";
var	lw_bordercolor  = "#d0d0d0";

document.onkeypress = lw_escLayer;
document.onclick = lw_closeAllLayers;
    
var orientation=0;	// 0-horizontal 1-vertical
var imgPath = "sparowMenu/";

var mainItemForeground      = "#000000";
var mainItemBorder          = "#f0f0f0";
var mainItemBackground      = "#ffd600";

var mainItemHoverForeground = "#0000ff";
var mainItemHoverBorder     = "#f0f0f0";
var mainItemHoverBackground = "#ffd600";
var mainItemAlign           = "center"

var subItemForeground       = "#000000";
var subItemBorder           = "#ffffff";
var subItemBackground       = "#ffffff";

var subItemHoverForeground  = "#000000";
var subItemHoverBorder      = "#a0a0a0";
var subItemHoverBackground  = "#d0d0d0";
var subItemAlign            = "left"

var menuFont = "verdana";
var menuSize = "11px";


lw_calcpos=function(obj) {
	lw_leftpos=0
	lw_toppos=0
	lw_width = obj.offsetWidth
	lw_height = obj.offsetHeight

	var aTag = obj
	do{
		lw_leftpos += aTag.offsetLeft;
		lw_toppos += aTag.offsetTop;
		aTag = aTag.offsetParent;
	}
	while(aTag.tagName!="BODY");
}
/////////////////////////////////////////////////////////////////////////////

var	ie=(navigator.appName=='Microsoft Internet Explorer');
var	ns=(navigator.appName=='Netscape');
var	dom=document.getElementById;
var lw_menuId, lw_trigger, to1;
var s1;

/////////////////////////////////////////////////////////////////////////////

replaceAll=function(AString, ASearch, AReplace){
  var st = AString;
  if(ASearch.length == 0){return st;}
  var idx = st.indexOf(ASearch);
  while (idx >= 0){
    st  = st.substring(0,idx) + AReplace + st.substr(idx+ASearch.length);
    idx = st.indexOf(ASearch);
  }
  return st;
};
// Removes leading whitespaces
LTrim=function(value){
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
};
// Removes ending whitespaces
RTrim=function(value){
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
};
// Removes leading and ending whitespaces
trim=function(value){
  return FormatFunc.LTrim(FormatFunc.RTrim(value));
};

Tmenu=function(id, parentId, func, description, img, width, height, align) {
	this.id = id;
	this.parentId = parentId;
	this.func = func;
	this.description = description;
	this.numChild = 0;
	this.levelId = 0;
	this.img = img;
	this.width = width;
	this.height = height;
	this.align = align;
};

/////////////////////////////////////////////////////////////////////////////

var menu = new Array();
var menuCounter = 0;
var numLevel = 0;
var prevMenuId = -1;
var nNowAt = 0;

displayMenuItem=function(menuId){
	var sHTML
	
	if (menu[menuId].parentId>0){
    menuFont        = subItemMenuFont;
    menuSize        = subItemMenuSize;
    menuFontWeight  = subItemMenuFontWeight;
	
		ItemForeground  = subItemForeground;
		Border          = subItemBorder;
		Background      = subItemBackground; 

		HoverForeground = subItemHoverForeground; 
		HoverBorder     = subItemHoverBorder; 
		HoverBackground = subItemHoverBackground; 
	}
	else{
    menuFont        = mainItemMenuFont;
    menuSize        = mainItemMenuSize;
    menuFontWeight  = mainItemMenuFontWeight;

		ItemForeground  = mainItemForeground; 
		Border          = mainItemBorder; 
		Background      = mainItemBackground; 

		HoverForeground = mainItemHoverForeground; 
		HoverBorder     = mainItemHoverBorder; 
		HoverBackground = mainItemHoverBackground; 
	};

	if ((orientation==1)||(menu[menuId].levelId>0)){sHTML += "<tr>";};

  // replace the description of the thirth level and higher
  if(menu[menuId].levelId>1){  
    menu[menuId].description=replaceAll(menu[menuId].description,' ','&nbsp;');
  };

	if (menu[menuId].description!=='-'){
		//sHTML = "<td id='menu_"+menuId+"' style='cursor:pointer;cursor:hand;border-style:solid;border-width:1px;background-color:"+ Background +";color=\""+ ItemForeground +"\";border-color:"+ Border +"' onmouseover='clearInterval(s1);this.style.backgroundColor=\"" + HoverBackground + "\";this.style.color=\"" + HoverForeground + "\";this.style.borderColor=\"" + HoverBorder + "\";"
		/*
		sHTML = '<td id="menu_'+menuId+'" '+
		        'style="cursor:pointer;cursor:hand;border-style:solid;border-width:1px;'+
		        'background-color:'+Background+';'+
		        'align=\''+menu[menuId].align+'\;'+
		        'width=\''+menu[menuId].width+'\;'+
		        'color=\''+ ItemForeground +'\;'+
		        'border-color:'+ Border +'"'+
		        ' onmouseover="clearInterval(s1);this.style.backgroundColor=\'' + HoverBackground + '\';this.style.color=\'' + HoverForeground + '\';this.style.borderColor=\'' + HoverBorder + '\';';
    */
		sHTML = '<td id="menu_'+menuId+'" '+
		        'style="cursor:pointer;'+
		        'height:'+menu[menuId].height+';'+
		        'background-color:'+Background+';'+
		        'color:'+ ItemForeground +';'+
		        'font-weight:'+menuFontWeight+';'+
		        'border-bottom:solid 1px '+Border+';';//+
		        //'border-top:solid 1px '+Border+';';//+
		        //'border-bottom:solid 1px '+Border+';';

		if (menu[menuId].parentId==0){
      sHTML +='border-left:solid 1px '+Border+';';
    };
    
    sHTML +='"';

		sHTML +='onmouseover="clearInterval(s1);'+
		        ' this.style.backgroundColor=\'' + HoverBackground + '\';'+
		        ' this.style.color=\'' + HoverForeground + '\';'
		        //'this.style.borderColor=\'' + HoverBorder + '\';';

    
		if (menu[menuId].numChild!=0){
			sHTML +='showSubmenu('+menu[menuId].id+',this);hideSubmenu(menu['+menuId+'].levelId+1);';
		}
		else
		{
			sHTML +='prevMenuId = '+menuId+';hideSubmenu(menu['+menuId+'].levelId);';
		};

		/*
		sHTML +=';nNowAt='+menuId+';clearAll('+menu[menuId].levelId+','+menu[menuId].parentId+')" '+
		        ' onclick="document.location.href=\''+menu[menuId].url+'\'" '+
		        ' onmouseout="s1=setInterval(\'resetMenu();hideSubmenu(0)\',1000);">'+
		        '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
    */
		sHTML +=';nNowAt='+menuId+';clearAll('+menu[menuId].levelId+','+menu[menuId].parentId+')" ';
		sHTML +=' onclick="'+menu[menuId].func+'"';
		if(menu[menuId].func!=''){
  		//sHTML +=' onclick="\''+menu[menuId].func+'\'"';
		};
		sHTML +=' onmouseout="s1=setInterval(\'resetMenu();hideSubmenu(0)\',1000);">'+
		        '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';

		if (menu[menuId].levelId>0){
			sHTML += '<td style="padding-left:3px" width="10px"><img src="';
			if (menu[menuId].img==""){
				sHTML += imgPath + 'trans.gif';
			}
			else
			{
				sHTML += imgPath + menu[menuId].img;
			}
			sHTML += '" width="6px" height="16px"></td>';
		};

		if ((orientation==0) && (menu[menuId].levelId==0))
		{
			nArrowWidth = 0;
		}
		else
		{
			nArrowWidth = 25;
		};

		sHTML +='<td style="padding-left:5px;padding-right:5px;'+
		        'font-family:'+menuFont+';'+
		        'font-size:'+menuSize+';'+
		        'color:'+ItemForeground+';'+
		        'font-weight:'+menuFontWeight+';'+
		        'width:'+menu[menuId].width+';" '+
		        'align="'+menu[menuId].align+'"'+
		        'onmouseover="clearInterval(s1);'+
		        'this.style.color=\'' + HoverForeground + '\';" '+
		        'onmouseout="this.style.color=\'' + ItemForeground + '\';"'+
		        '>'+
		        menu[menuId].description.replace(' ','&nbsp;') + 
		        '<input style="width:25px" type="hidden" id="menu_'+menu[menuId].id+'_childs" value="'+menu[menuId].numChild+'" />'+
		        '</td><td style="padding-right:2px" align="right" width="'+nArrowWidth+'px">';

		if (menu[menuId].numChild>0){
			if ((orientation==0)&&(menu[menuId].levelId==0)){
				sHTML += "<img src='" + imgPath + "arrow_down.gif'>";
			}
			else
			{
				sHTML +='<img src="' + imgPath + 'arrow_right.gif">';
			};
		}
		else{
			sHTML +='&nbsp;';
		};
		sHTML +='</td></tr></table></td>';
	}
	else
	{
		sHTML='<td><img src="'+imgPath+'trans.gif" height="2px"></td></tr><tr><td onmouseover="" bgcolor="#d0d0d0"><img src="'+imgPath+'trans.gif" height="1px"></td></tr><tr><td><img src="'+imgPath+'trans.gif" height="2px"></td>';
	};

	if ((orientation==1)||(menu[menuId].levelId>0)){
		sHTML +='</tr>';
	};
	return sHTML;
};

clearAll=function(levelId, parentId){
	if (levelId>0){
		Border = subItemBorder; 
		Background = subItemBackground; 
	}
	else{
		Border = mainItemBorder; 
		Background = mainItemBackground; 
	};

	for (i=0;i<menuCounter;i++){
		if (menu[i].levelId==levelId){
			if ((i!=nNowAt) && (menu[i].parentId==parentId) && (menu[i].description!="-")) {
				lw_getObj("menu_"+i).backgroundColor=Background;
				lw_getObj("menu_"+i).borderColor=Border;
			};
		};
	};
};

mapID=function(id) {
	for (var i=0;i<menuCounter;i++){
		if (menu[i].id==id){
			return i;
		};
	};
	return -1;
};

showSubmenu=function(menuId,trigger){
	lw_menuId = menuId;
	lw_trigger = trigger;
	if (ns){
		to1 = setTimeout("showActualSubmenu(lw_menuId,lw_trigger)",50);			
	}
	else{
		showActualSubmenu(lw_menuId,lw_trigger);
	};
};

showActualSubmenu=function(menuId,trigger){
	var nLevel = 0;
	var leftpos = 0; 
	var nIndex=0;

	if (menuId>0){
		lw_calcpos(trigger);

		for (var i=0;i<menuCounter;i++){
			if (menu[i].id==menuId){	
				nLevel = menu[i].levelId;
				nIndex = i;
			};
		};

		if ((orientation==1)||(menu[nIndex].parentId>0)){
			lw_getObj("menu_level_"+nLevel).top=lw_toppos;
			leftpos = lw_leftpos + lw_width + 5;

			if (nLevel==0) {
				leftpos -= 3;
			};
		}
		else {
			lw_getObj("menu_level_"+nLevel).top = lw_toppos + lw_height + 2;
			leftpos = lw_leftpos;
		};

		lw_getObj("menu_level_"+nLevel).left=leftpos

		sHTML = "<table cellpadding=0 cellspacing=0 border=0>";
		for (var i=0;i<menuCounter;i++){
			if (menu[i].parentId==menuId){
				sHTML += displayMenuItem(i);
			};
		};
		sHTML += "</table>";

		document.getElementById("menu_level_"+nLevel+"_content").innerHTML=sHTML;
		lw_showLayer('menu_level_'+nLevel);
	}; 
};

hideSubmenu=function(levelId){
	for (var cnt=levelId; cnt<numLevel; cnt++){
		lw_hideLayerName("menu_level_"+cnt);
	};
};

DrawMenu=function(){
	for(var i=0;i<numLevel;i++){
		//lw_createLayer("menu_level_"+i,0,0,300,0,"#ffffff","#d0d0d0",100);
		lw_createLayer('menu_level_'+i,lw_toppos,lw_leftpos,lw_width,lw_height,lw_bgcolor,lw_bordercolor,100);
	};
	
	var sHTML='<table border="0" width="100%" cellpadding="2" cellspacing="0">';
	for(var i=0; i<menuCounter; i++){
		if (menu[i].parentId==0){		
			sHTML += displayMenuItem(i);
		}
		else if(menu[i].parentId==-1){		
			sHTML += '<tr><td><img src="trans" height="2px"></td></tr>'+
			'<tr bgcolor="'+mainItemBackground+'">'+
			'<td style="padding:5px;border:solid 1px white;"><b>' + menu[i].description.replace(' ','&nbsp;') + '</b></td></tr>';
		};
	};
	sHTML += '</table>';
	return sHTML;
	//document.writeln(sHTML);
};

getLevel=function(menuId){
	var pId=menuId;
	var nLevel=0;

	while(pId!=0){
		nLevel++;
		for (var i=0;i<menuCounter;i++){
			if (menu[i].id==pId){	
				pId = menu[i].parentId;
			};
		};
	};
	return nLevel;
};

AddMenuItem=function(id, parentId, func, description, img, width, height, align){
	menu[menuCounter++] = new Tmenu (id, parentId, func, description, img, width, height, align);
	if (parentId>0){
		for (i=0;i<menuCounter;i++){
			if (menu[i].id==parentId){
				menu[i].numChild++;
			};
		};
		menu[menuCounter-1].levelId = getLevel(parentId);

		if ( numLevel < menu[menuCounter-1].levelId){	
			numLevel = menu[menuCounter-1].levelId;
		};
	}	 
	else if (parentId==0){
		menu[menuCounter-1].levelId = 0;
	}
	else{
		menu[menuCounter-1].levelId = -1;
	};
};

handleonclick=function(){
	if (ns){lw_closeAllLayers();}
	else{lw_closeAllLayers(event);};
	resetMenu();
};

handlekeypress=function(e){
	if (ns){
		var keyCode = e.keyCode?e.keyCode:e.which?e.which:e.charCode;
		if ((keyCode==27)||(keyCode==1)){handleonclick();}
	}
	else{
	  if((event.keyCode==0)||(event.keyCode==27)){handleonclick();};
  };
	resetMenu();
};

resetMenu=function(){
	for (i=0;i<menuCounter;i++){
		if (menu[i].levelId==0){
			lw_getObj('menu_'+i).backgroundColor=mainItemBackground;
			lw_getObj('menu_'+i).borderColor=mainItemBorder;
		};
	};
};

document.onkeypress = handlekeypress;
document.onclick = handleonclick;
