function layerWrite(id,nestref,text) {
   document.getElementById(id).innerHTML = text;
}

function changeLayerColor(id,nestref,color){
   document.getElementById(id).style.backgroundColor = color;
}

function changeImg(objName,active){
   start_index = 7 + document.location.hostname.length;
   obj = document.getElementById(objName);
   m_imgName=obj.src;
   m_imgName=m_imgName.substring(start_index,obj.src.lastIndexOf('_'));
   if(active==1){
//      alert()
      obj.src=m_imgName+activeImage;
      }else{
         obj.src=m_imgName+inactiveImage;
         }
  }

function handlemouseover(nestref,name,optionIndex,arrayName){ 
  HideAllFlag=0;
  var act=eval(arrayName+"ActiveItem");
  if (act==optionIndex){
     return;
  }
  if (act!=-1){
     if(imageMenu==0){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>";
     if(imageMenu==0){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>";
     if(imageMenu==0){layerWrite(name,nestref,newText);}
     if(imageMenu==0){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(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{
     if(imageMenu==0){changeLayerColor(name,nestref,activeColor);}
  }
  eval(arrayName+"ActiveItem="+optionIndex);
}


function createLayer(id,nestref,left,top,width,height,bgColor,visibility,zIndex,optionIndex,arrayName){
    window.status="Creating menu: "+id;
    var str = '\n<DIV id='+id+' style="position:absolute; left:'+left+'; top:'+top+'; width:'+width
    if (height!=null) {
        str += '; height:'+height
        str += '; clip:rect(0,'+width+','+height+',0)'
    }
    if (bgColor!=null) str += '; background-color:'+bgColor		
    if (zIndex!=null) str += '; z-index:'+zIndex
    if (visibility) str += '; visibility:'+visibility
    str += '; cursor:hand;">'+'</DIV>'
    if (nestref) {
       index = nestref.lastIndexOf(".")
       var nestlyr = (index != -1)? nestref.substr(index+1) : nestref
       document.getElementById(nestlyr).insertAdjacentHTML("BeforeEnd",str);
    }else{
       document.body.insertAdjacentHTML("BeforeEnd",str)
    }
    if (nestref){
        document.getElementById(id).onmouseover=new Function('handlemouseover("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');  
        document.getElementById(id).onmouseout=new Function('handlemouseout("'+nestref+'","'+id+'",'+optionIndex+',"'+arrayName+'"); return false');
        document.getElementById(id).onmousedown=new Function('handlemousedown("'+arrayName+'",'+optionIndex+'); return false');  
        document.getElementById(id).onmouseup=new Function('handlemouseup("'+arrayName+'",'+optionIndex+'); return false');
    }
}

function destroyLayer(id,nestref) {
   document.getElementById(id).innerHTML = ""
   document.getElementById(id).outerHTML = ""
}

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>";
      if(imageMenu==0){layerWrite(name+"Item"+act,name,newText);}
      hideLayer(nm);
   }
   if(imageMenu==0){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.clientX+document.body.scrollLeft+eval(name+"[1]"));
  var newLeft=eval(name+"[1]")+document.getElementById("headtable").offsetLeft;
  document.getElementById(name).style.left=newLeft;
}

function setYCoordinateToM(name,e){
  var newTop = parseInt(e.clientY+document.body.scrollTop+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%'>";

       if(imageMenu==0){
          inHTML+="<font face='"+fontFace+"' size='"+fontSize+"' color='"+inactiveFontColor+"'><span style='font-face:"+fontFace+"; font-size:"+fontStyleSize+"; color:"+inactiveFontColor+"'>";
          }else{
         src=menuName[cnt*5]+inactiveImage;
         inHTML+="<img id=img"+itmName+" src='"+src+"' border='0' onMouseOver='JavaScript:changeImg(\"img"+itmName+"\",1);' onMouseOut='JavaScript:changeImg(\"img"+itmName+"\",0);'>";}

       if (menuName[cnt*5+2]==""){
           if(imageMenu==0){
             inHTML+=menuName[cnt*5]+"</span></font>";
           }
           inHTML+="</td></tr></table>";
        }else{
           if(imageMenu==0){
             inHTML+=menuName[cnt*5]+"</span></font>";
           }
           inHTML+="</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=document.body.clientWidth;
  var screenHeight=document.body.clientHeight;
  document.body.onclick=hideAll;
  document.onscroll=hideAll;
  centerX=eval(screenWidth/2);
  centerY=eval(screenHeight/2);
  centerX=parseInt(centerX);
  centerY=parseInt(centerY);
  createMenuLayer("PrimarySoup");
  window.status="Done";
  menusCreated=1;
  regenerate2();
  hideAll();
  if (Unreg==1){WSForLoad();}
  HideTimer();
}

function regenerate(){
  var screenWidth=document.body.clientWidth;
  var screenHeight=document.body.clientHeight;
  centerX=eval(screenWidth/2);
  centerY=eval(screenHeight/2);
  centerX=parseInt(centerX);
  centerY=parseInt(centerY);
}

function regenerate2(){
  setTimeout("window.onresize=regenerate",400)
}

/////////////////////////////
/////////////////////////////
function WSmoveLayerTo(layerName,x,y){
  document.getElementById(layerName).style.top=y;
  document.getElementById(layerName).style.left=x;
}

function WScreateButton(){
  var WSbuttonHTML="<div id='WSButtonLayer' style='position:absolute; visibility:visible; top:"+WSY+"px; left:"+WSX+"px; width:"+WSImageWidth+"px'>";
  WSbuttonHTML+="<a href='"+WSURL+"' onMouseOver='WSStop();' onMouseOut='WSMove();' target='_blank'><img src='"+WSImage+"' border='0'></a>";
  WSbuttonHTML+="</div>";
  document.body.insertAdjacentHTML("BeforeEnd",WSbuttonHTML);
}

//// DEBUG
function log(m){
 alert(m);
  }
