/***********************************
giscard girard
giscardg@hotmail.com

Browsers tested: NS4.7, IE5.5, NS6

***********************************/
var ns = document.layers;
var ie = document.all;
var w3c = document.getElementById;


function Menu(label,mBGColor,iOverColor,mBorderColor){
 this.items = new Array();
 this.mLinks = new Array();
 this.mBGColor = mBGColor || "#666666";
 this.mBorderColor = mBorderColor || "#CCCCCC";
 this.iOverColor = iOverColor || "#336699";
 this.mSeperatorColor = "#CCCCCC";
 this.itemHeight = 20;
 this.menuWidth = 260;
 this.menuHeight = null;//calculated
 this.mBorderWidth = 1;
 this.mSeperatorHeight = 1;
 this.onMenuOut = "";
 this.onMenuOver = "";
 this.mParent = null;
 this.horzOffset = 0;
 this.vertOffset = 0;
 this.fntColor = "white";
 this.fntSize = "8pt";
 this.fntFamily = "Verdana, Arial, Helvetica, sans-serif";

 if(!window.menus)
   window.menus = new Array();
 
 this.containerIndex = window.menus.length;
 var mLabel = label || "_mLabel" + window.menus.length;
 window.menus[mLabel] = this;
 window.menus[window.menus.length] = this;
 
}

Menu.prototype.addItem = function(Caption,lnk)
{
 this.items[this.items.length] = Caption;
 this.mLinks[this.mLinks.length] = lnk;
}

Menu.prototype.writeMenu = function(container)
{
 wndMenus = window.menus;
 if(!container){
    if(ns){
      window.menuContainer = container = new Layer(1024);
      container.clip.width=0;
      container.clip.height=0;
    }
    else if(w3c && !document.getElementById("mcMain")){
      window.menuContainer = container = document.createElement("SPAN");
      container.id="mcMain";
      document.body.appendChild(container);
    }
    else if(ie && !document.all["mcMain"]){
      document.body.innerHTML+='<SPAN ID="mcMain"></SPAN>';
      window.menuContainer = container = document.all["mcMain"];
    }
    container = window.menuContainer;    
 }
 window.menus = wndMenus;
 
 content="";
 for(var j=0; j < window.menus.length; j++)
 {
	menu = window.menus[j];
	
	if(!menu.menuHeight) 
	  menu.menuHeight = (menu.itemHeight * menu.items.length) + (menu.mBorderWidth * (menu.items.length-1));
 	var menuStyle = "position:absolute;";
	var w = menu.menuWidth?"width:"+(parseInt(menu.menuWidth)+(menu.mBorderWidth*2))+"px;":"";
	var h = menu.menuHeight?"height:"+(parseInt(menu.menuHeight)+(menu.mBorderWidth*2))+"px;":"";
 	var bgc = (ns)?"layer-background-color:"+menu.mBorderColor+";":"background:"+menu.mBorderColor+";";
	var clipRect = "clip:rect(0 " + (parseInt(menu.menuWidth)+(menu.mBorderWidth*2)) + " " + (parseInt(menu.menuHeight)+(menu.mBorderWidth*2)) + " 0);";
 	menuStyle += w + h + bgc + clipRect;
 
 	content +='<DIV ID="mm'+menu.containerIndex+'" STYLE="'+menuStyle+'">\n';
 
 	menuStyle = "position:absolute;";
 	w = menu.menuWidth?"width:"+menu.menuWidth+"px;":"";
	h = menu.menuHeight?"height:"+menu.menuHeight+"px;":"";
 	bgc = (ns)?"layer-background-color:"+menu.mBGColor+";":"background:"+menu.mBGColor+";";
 	clipRect = "clip:rect(0 " + menu.menuWidth + " " + menu.menuHeight + " 0);";
 	var t = "top:"+menu.mBorderWidth+"px;";
 	var l = "left:"+menu.mBorderWidth+"px;";
 	menuStyle += l + t + w + h + bgc + clipRect;
 
 	content +='<DIV ID="mm'+menu.containerIndex+'Main" STYLE="'+menuStyle+'">\n';
 
 	for(var i=0; i<menu.items.length; i++){   
 	  content += menu.writeItem(i);
 	}
 	
	content +='</DIV></DIV>\n';
 }
 
 if(ns){
   container.document.write(content);
   container.document.close();
 } 
 else 
   container.innerHTML=content;
    
 for(var j=0; j < window.menus.length; j++){
	menu = window.menus[j];
	for(var i=0; i< menu.items.length; i++){
	  if(ns){
	    mParent = container.document.layers["mm"+menu.containerIndex];
	    mItem = mParent.document.layers[0].document.layers["mmsub"+menu.containerIndex+""+i];
	  }
	  else if(w3c){
	   mParent = document.getElementById("mm"+menu.containerIndex);
	   mItem = document.getElementById("mmsub"+menu.containerIndex+""+i);
	  }
	  else if(ie){
	    mParent = document.all["mm"+menu.containerIndex];
	    mItem = document.all["mmsub"+menu.containerIndex+""+i];
	  }
	  toggleVisibility(mParent,false);
	  menu.mParent = mParent;
	  mItem.menu = menu;
	  mItem.mParent = mParent;
	  mItem.itemLink = menu.mLinks[i];
	  mItem.iOverColor = menu.iOverColor;
	  mItem.mBGColor = menu.mBGColor;
	  mItem.action = menu.mLinks[i];
 	  if(ns){
 	    mParent.captureEvents(Event.MOUSEOUT);
 	    mItem.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.MOUSEDOWN);
 	  }
	  mItem.onmouseout=function(e){  	   
	   if(ns)
	    e.target.bgColor=this.mBGColor;
	   else if(w3c)
	    this.style.backgroundColor=this.mBGColor;
	   else if(ie){
	     event.srcElement.style.backgroundColor=this.mBGColor;
	   }
	    return true;
	  }
	  mItem.onmouseover = function(e){
	   if(ns)
	    e.target.bgColor=this.iOverColor;
	   else if(ie)
	    event.srcElement.style.backgroundColor=this.iOverColor;
	   else {
	    this.style.background=this.iOverColor;
	   }
	   return true;	    
	  }	  
	  mItem.onmousedown = function()   
	  {
		 window.location  = this.itemLink;
	  }
	  mParent.menu = menu;
	  mParent.onMenuOut = menu.onMenuOut;
	  mParent.onmouseout  = menuOut;
   	}
 }

 toggleVisibility(container,true);
 
} // end of Menu.prototype.writeMenu

Menu.prototype.writeItem = function writeItem(itemIndex)
{
 var menuStyle = "position:absolute;";
 var w = this.menuWidth?"width:"+this.menuWidth+"px;":"";
 var h = this.itemHeight?"height:"+this.itemHeight+"px;":"";
 sh = itemIndex==0?0:parseInt(this.mSeperatorHeight);
 var yPos = (parseInt(this.itemHeight) + sh)*itemIndex;
 var T = 'top:'+yPos+';';
 var bgc = (ns)?"layer-background-color:"+this.mBGColor+";":"background:"+this.mBGColor+";";
 var fnt = "color:" + this.fntColor + ";font-family:" + this.fntFamily + ";font-size:" + this.fntSize + ";"; 
 var clipRect = "clip:rect(0 " + this.menuWidth + " " + this.itemHeight + " 0);";
 
 menuStyle += T + w + h + bgc + fnt + clipRect;
 var caption = this.items[itemIndex];
 var itemStr = '<DIV ID="mmsub'+this.containerIndex+''+itemIndex+'" STYLE="'+menuStyle+'">'+caption+'</DIV>\n'
 
 if(itemIndex != this.items.length-1){
  menuStyle = "position:absolute;";
  w = this.menuWidth?"width:"+this.menuWidth+"px;":"";
  h = this.itemHeight?"height:"+this.mSeperatorHeight+"px;":"";
  yPos = yPos + this.itemHeight;
  T = 'top:'+yPos+';';
  bgc = (ns)?"layer-background-color:"+this.mSeperatorColor+";":"background:"+this.mSeperatorColor+";";
  clipRect = "clip:rect(0 " + this.menuWidth + " " + this.mSeperatorHeight + " 0);";
 
  menuStyle += T + w + h + bgc + clipRect;
  itemStr += '<DIV ID="subseperator'+this.containerIndex+itemIndex+'" STYLE="'+menuStyle+'"></DIV>\n'  
 }
 return itemStr;
}
Menu.prototype.hide = function()
{
 eval(this.onMenuOut);
 toggleVisibility(this.mParent,false);
}

Menu.prototype.show = function(xPos,yPos)
{
 xpos = xPos || window.currentX;
 ypos = yPos || window.currentY;
 xpos += this.horzOffset;
 ypos += this.vertOffset;
 eval(this.onMenuOver);
 if(ns){
   this.mParent.parentLayer.left=xpos;
   this.mParent.parentLayer.top=ypos;
   this.mParent.parentLayer.clip.width = this.mParent.clip.width;
   this.mParent.parentLayer.clip.height = this.mParent.clip.height;
 }
 else if(w3c) {
   this.mParent.style.left = xpos + "px";
   this.mParent.style.top = ypos + "px";
 }
 else {
   this.mParent.style.posLeft = xpos;
   this.mParent.style.posTop = ypos;
 }
 toggleVisibility(this.mParent,true);
}
function toggleVisibility(obj,vis){
 if(ns){
  show = "show";
  hide = "hide";
  obj.style = obj;
 }
 else {
  show = "visible";
  hide = "hidden";
 }
 obj.style.visibility = (vis)?show:hide;
}

function menuOut(e){
 var ev = (ie)?event:e; 
 if(!mouseInMenu(this,ev)){
    toggleVisibility(this,false);
    eval(this.menu.onMenuOut);
 }
 if(ns)routeEvent(e);
 return true;
}
function mouseInMenu(m,ev) {
 o = ev.relatedTarget || ev.toElement || ev.target;  
 if(ns)
  if(o.id == m.id)
   return false;
  else
   return true;  
 while(o) {  
  if(o.id==m.id)
   return true;
  if(w3c)
   o = o.parentNode;
  else
   o = o.parentElement;  
 }
 return false; 
}

function itemOver(e){
 if(ns)
  e.target.bgColor=this.iOverColor;
 else if(w3c)
  this.style.backgroundColor=this.iOverColor;
 else if(ie)
  event.srcElement.style.backgroundColor=this.iOverColor;
}

function trackCursor(e)
{
 if(ie){
   e = new Object();
   e.pageX = event.clientX;
   e.pageY = event.clientY;
 }
 window.currentY = e.pageY;
 window.currentX = e.pageX;
 return true;
}

if(ns){
   window.captureEvents(Event.MOUSEMOVE);
   window.onmousemove = trackCursor;
}
else
   document.onmousemove = trackCursor;
