function handlemouseover(nestref,name,optionIndex,arrayName){ 
  HideAllFlag=0;
  var act=eval(arrayName+"ActiveItem");
  if (act==optionIndex){
     return;
  }
  if (act!=-1){
     changeLayerColor(arrayName+"Item"+act,nestref,inactiveColor);
  } 
  var prevnm=eval(arrayName+"["+parseInt(act*5+2)+"]");
  if ((prevnm!="")&&(act!=-1)){ //previus active item had submenu
     hideLayer(prevnm);     
     var newText="<table cellpadding='0' cellspacing='0' width='"+parseInt(eval(arrayName+"[0]")-leftBorder-rightBorder)+"' height='"+eval(arrayName+"["+parseInt(act*5-1)+"]")+"'><tr><td width='100%'>"+"<font face='"+fontFace+"' size='"+fontSize+"' color='"+inactiveFontColor+"'><span style='font-face:"+fontFace+"; font-size:"+fontStyleSize+"; color:"+inactiveFontColor+";'>"+eval(arrayName+"["+act*5+"]")+"</span></font>"+"</td><td><img src='"+nextImage+"' width='"+nextImageW+"' height='"+nextImageH+"'></td></tr></table>";
     layerWrite(arrayName+"Item"+act,nestref,newText);
  }
  var nm=eval(arrayName+"["+parseInt(optionIndex*5+2)+"]");
  if (nm!=""){
     var newText="<table cellpadding='0' cellspacing='0' width='"+parseInt(eval(arrayName+"[0]")-leftBorder-rightBorder)+"' height='"+eval(arrayName+"["+parseInt(optionIndex*5-1)+"]")+"'><tr><td width='100%'>"+"<font face='"+fontFace+"' size='"+fontSize+"' color='"+activeFontColor+"'><span style='font-face:"+fontFace+"; font-size:"+fontStyleSize+"; color:"+activeFontColor+";'>"+eval(arrayName+"["+optionIndex*5+"]")+"</span></font>"+"</td><td><img src='"+nextImage+"' width='"+nextImageW+"' height='"+nextImageH+"'></td></tr></table>";
     layerWrite(name,nestref,newText);
     changeLayerColor(name,nestref,activeColorWithSub);
     var newLeft=parseInt(document.getElementById(arrayName).style.left)+parseInt(eval(arrayName+"[0]"))-rightBorder;
     var newTop=parseInt(document.getElementById(arrayName).style.top);
     var maxLeft=parseInt(parseInt(centerX*2)-parseInt(eval(arrayName+"[0]")));
     if (maxLeft<newLeft){
        newLeft=parseInt(document.getElementById(arrayName).style.left)-parseInt(eval(arrayName+"[0]"))+rightBorder;
     }
     var cnt=4;
     for (var i=1; i<optionIndex; i++){
         newTop+=parseInt(eval(arrayName+"["+cnt+"]"))+separatorBorder;
         cnt+=5;
     }
     var maxTop=parseInt(centerY*2-menuHeight(nm));
     if (newTop>maxTop){
         newTop=maxTop; 
     }
     layerMoveTo(nm,newLeft,newTop);
     showLayer(nm);
  }else{
     changeLayerColor(name,nestref,activeColor);
  }
  eval(arrayName+"ActiveItem="+optionIndex);
}

function createLayer(id,nestref,left,top,width,height,bgColor,visibility,zIndex,optionIndex,arrayName){

//alert('Creating ...'+'\nID='+id+'\nnestref='+nestref);

  window.status='Creating '+id+' layer!';
  var lyr = document.createElement('div');
  lyr.setAttribute('id',id);
  lyr.setAttribute('name',id);

  lyr.style.top=top;
  lyr.style.left=left;
  lyr.style.width=width;
  lyr.style.height=height;
  lyr.style.backgroundColor=bgColor;
  lyr.style.position="absolute";
  lyr.style.visibility=visibility;
  lyr.style.zIndex=zIndex;

//  lyr.style.color=foreColor;
//  lyr.innerHTML=HTML;

//  lyr.onmouseover=mouseOver;
//  lyr.onmouseout=mouseOut;

//alert('Creating done');


    if(document.getElementById(nestref)){
         lyr.onmouseover=new Function('handlemouseover("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');
         lyr.onmouseout=new Function('handlemouseout("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');
         lyr.onmousedown=new Function('handlemousedowne6("'+arrayName+'",'+optionIndex+'); return false');
         //lyr.onmouseup=new Function('handlemouseup("'+arrayName+'",'+optionIndex+'); return false');
//       lyr.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE | Event.MOUSEOVER | Event.MOUSEOUT)
         where = document.getElementById(nestref);
    }else{
         where = document.childNodes[0].childNodes[2];
    }



  where.appendChild(lyr);

 
/*    window.status="Creating menu: "+id;
    if(nestref){
       var lyr = eval("document."+nestref+".document."+id+" = new Layer(width, document."+nestref+")")
    }else{
       var lyr = document.layers[id] = new Layer(width)
       eval("document."+id+" = lyr")
    }
    lyr.name = id
    lyr.left = left
    lyr.top = top
    if (height!=null) lyr.clip.height = height
    if (bgColor!=null) lyr.bgColor = bgColor
    lyr.visibility = (visibility=='hidden')? 'hide' : 'show'
    if (visibility=='inherit'){
        lyr.visibility = 'inherit';
    }
    if (zIndex!=null) lyr.zIndex = zIndex
    if (nestref){       
          lyr.onmouseover=new Function('handlemouseover("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');
          lyr.onmouseout=new Function('handlemouseout("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');  
          lyr.onmousedown=new Function('handlemousedown("'+arrayName+'",'+optionIndex+'); return false');
          lyr.onmouseup=new Function('handlemouseup("'+arrayName+'",'+optionIndex+'); return false');
          lyr.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE | Event.MOUSEOVER | Event.MOUSEOUT)
    } */


}

function destroyLayer(id,nestref) {
//    if (nestref) eval("document."+nestref+".document."+id+".visibility = 'hide'")
//    else document.layers[id].visibility = "hide";


      document.getElementById(id).style.visibility="hidden";



}

function layerMoveTo(name,left,top){
   document.getElementById(name).style.left=left;
   document.getElementById(name).style.top=top;
}

function hideLayer(name){
var act=eval(name+"ActiveItem");
if (act!=-1){
   var nm=eval(name+"["+parseInt(act*5+2)+"]");
   if (nm!=""){
      var newText="<table cellpadding='0' cellspacing='0' width='"+parseInt(eval(name+"[0]")-leftBorder-rightBorder)+"' height='"+eval(name+"["+parseInt(act*5-1)+"]")+"'><tr><td width='100%'>"+"<font face='"+fontFace+"' size='"+fontSize+"' color='"+inactiveFontColor+"'><span style='font-face:"+fontFace+"; font-size:"+fontStyleSize+"; color:"+inactiveFontColor+";'>"+eval(name+"["+act*5+"]")+"</span></font>"+"</td><td><img src='"+nextImage+"' width='"+nextImageW+"' height='"+nextImageH+"'></td></tr></table>";
      layerWrite(name+"Item"+act,name,newText);
      hideLayer(nm);
   }
   changeLayerColor(name+"Item"+act,name,inactiveColor);
}
document.getElementById(name).style.visibility='hidden';
eval(name+"ActiveItem=-1");
}

function showLayer(name){
   document.getElementById(name).style.visibility='visible';
}


function setXCoordinateToM(name,e){
  var newLeft = parseInt(e.pageX+eval(name+"[1]"));
  document.getElementById(name).style.left=newLeft;
}

function setYCoordinateToM(name,e){
  var newTop = parseInt(e.pageY+eval(name+"[2]"));
  document.getElementById(name).style.top=newTop;
}

function setXCoordinateToC(name){
  var oldLeft=parseInt(eval(name+"["+1+"]"));
  oldLeft+=centerX;
  document.getElementById(name).style.left=oldLeft;

}

function setYCoordinateToC(name){
   var oldTop=parseInt(eval(name+"["+2+"]"));
   oldTop+=centerY;
   document.getElementById(name).style.top=oldTop;
}


function WMenu_Show(name,e){
   if (menusCreated==0){return;}
   hideAll();
   if (XCoordinatesCorrection=='C'){
      setXCoordinateToC(name);
   }
   if (YCoordinatesCorrection=='C'){
      setYCoordinateToC(name);
   }
   if (XCoordinatesCorrection=='M'){
      setXCoordinateToM(name,e);
   }
   if (YCoordinatesCorrection=='M'){
      setYCoordinateToM(name,e);
   }
   var oldLeft=parseInt(document.getElementById(name).style.left);
   var oldTop=parseInt(document.getElementById(name).style.top);
   var maxLeft=parseInt(parseInt(centerX*2)-parseInt(eval(name+"[0]")));
   var maxTop=parseInt(parseInt(centerY*2)-menuHeight(name));
   if (maxLeft<oldLeft){oldLeft=maxLeft;}
   if (maxTop<oldTop){oldTop=maxTop;}
   layerMoveTo(name,oldLeft,oldTop);
   showLayer(name);
}

function createMenuLayer(menuID){
   if (document.getElementById(menuID)){
      return;
   }
   var menuName=eval(menuID);
   var height=bottomBorder+topBorder;
   for (var i=4; i<=menuName.length-3; i++){
       height+=menuName[i]+separatorBorder;
       i+=4;
   }
   height-=separatorBorder;
   createLayer(menuID,null,menuName[1],menuName[2],menuName[0],height,borderColor,'hidden',1,0,menuID);
   var top=topBorder;
   var cnt=1;
   var itmName="";
   var inHTML="";
   for (var i=5; i<=menuName.length-2; i++){
       itmName=menuID+"Item"+cnt;
       inHTML="<table cellpadding='0' cellspacing='0' height='"+menuName[i-1]+"' width='"+parseInt(menuName[0]-leftBorder-rightBorder)+"'><tr><td width='100%'>";
       inHTML+="<font face='"+fontFace+"' size='"+fontSize+"' color='"+inactiveFontColor+"'><span style='font-face:"+fontFace+"; font-size:"+fontStyleSize+"; color:"+inactiveFontColor+"'>";
       if (menuName[cnt*5+2]==""){
       inHTML+=menuName[cnt*5]+"</span></font></td></tr></table>";
       }else{
            inHTML+=menuName[cnt*5]+"</span></font></td><td><img src='"+nextImage+"' width='"+nextImageW+"' height='"+nextImageH+"'></td></tr></table>";
       }
       createLayer(itmName,menuID,leftBorder,top,menuName[0]-leftBorder-rightBorder,menuName[i-1],inactiveColor,"inherit",1,cnt,menuID);
       layerWrite(itmName,menuID,inHTML);
       top+=parseInt(menuName[i-1]+separatorBorder);
       cnt++;
       if (menuName[i+2]!=""){
          createMenuLayer(menuName[i+2]); 
       }
       i+=4;
   }
   eval(menuID+"ActiveItem=-1");
}

function forLoad(){
  var screenWidth=pageXOffset+window.innerWidth;
  var screenHeight=pageYOffset+window.innerHeight;
//document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=hideAll;
  regenerate2();
  centerX=eval(screenWidth/2);
  centerY=eval(screenHeight/2);
  centerX=parseInt(centerX);
  centerY=parseInt(centerY);
  createMenuLayer("PrimarySoup");
  window.status="Done";
  menusCreated=1;
  hideAll();
//  if (Unreg==1){WSForLoad();}
  HideTimer();
}

function layerWrite(id,nestref,text) {
  lyr=document.getElementById(id);
  lyr.innerHTML=text;
/*
if (nestref){
      var lyr = eval('document.'+nestref+'.document.'+id+'.document');
  }else{
      var lyr = document.layers[id].document;
  }		
  lyr.open();
  lyr.write(text);
  lyr.close();
*/

}

function changeLayerColor(id,nestref,color){
/*if (nestref){
    var lyr = eval('document.'+nestref+'.document.'+id);
}else{
    var lyr = document.layers[id];
}*/
lyr=document.getElementById(id);
lyr.style.backgroundColor = color;
}

function regenerate(){
  window.location.reload()
}
function regenerate2(){
  setTimeout("window.onresize=regenerate",400)
}

//////////////////////////
//////////////////////////
function WSmoveLayerTo(layerName,x,y){
  eval(layerName+".top=y");     
  eval(layerName+".left=x");
}
function WScreateButton(){
/*  WSButtonLayer=new Layer(WSImageWidth);
  var WSbuttonHTML="<a href='"+WSURL+"' onMouseOver='WSStop();' onMouseOut='WSMove();' target='_blank'><img src='"+WSImage+"' border='0'></a>";
  WSButtonLayer.top=WSY;
  WSButtonLayer.left=WSX;
  WSButtonLayer.visibility="show";
  WSButtonLayer.document.open();
  WSButtonLayer.document.write(WSbuttonHTML);
  WSButtonLayer.document.close();     */
}
