var height = 32; // height of the menu headers
var iheight = 15; // height of the menu_items

var speed = 0;
var timerID = "";
var N = (document.all) ? 0 : 1;
var width = 152
var self_menu = new Array();
var image_list = new Array();
var CPanelHeight = 0;

function openPopup(){
    window.open("../Shared/Release/_PopUp/PopUp.html","GL4FREE","toolbar=no, menubar=no, resizable=no, width=300, height=504");
}

function write_menu() {
  smc = 0; // count the position of the self_menu
  document.write("<div style='position:absolute'>");
  mn = 0;
  mni = 1;
  start = -1;

  for(i=0;i<Link.length;i++) {
   la = Link[i].split("|");
   if (la[0] == "0") {
    if(start == 0) {
      document.write("</div>");
      h =  csmc * iheight;
      tmn = mn; //-h
      self_menu[smc] = new Array(tmn,h,0,-2);
      smc++;
      mn--;
     }
     csmc = 0;
    if (start == -1) {document.write("<div class='menu' style='top:"+mn+";height:"+height+"' id='down"+smc+"'><img src="+ la[1] + "></div>");} else {
    image_list[smc] = new Array(la[1],la[2]);
    document.write("<div class='menu' style='top:"+mn+";height:"+height+"' id='down"+smc+"' onclick='pull_down("+smc+","+mni+");'><img id='pic"+smc+"' src="+ la[1] + "></div>");}
    pic1= new Image(100,25); 
    pic1.src=la[2]; 
    self_menu[smc] = new Array(mn,height,0,mni);
    smc++;
    mni++;
    mn+=height;
    start = 1;
   } else {
    if(start == 1) {
      if(N)mn+=2;
       document.write("<div class='item_panel' id='down"+smc+"' style='top:"+mn+"'>");
       start = 0;
     }

    document.write("<a href='"+la[2]+"'");
    if (la[3] != "") {document.write(" target='" + la[3] + "' ");}
    document.write("><div class='item' id='d"+i+"' style='height:"+iheight);
    if (N) document.write(";width:150");
    document.write("'>  "+ la[1] + "</div></a>");
    csmc++;
   }
  }
  if (start == 0) {
     document.write("</div>");
     h =  csmc * iheight;
     tmn = mn + 5; //-h
     self_menu[smc] = new Array(tmn,h,0);
     name = "down" + (self_menu.length-1);
   }
  document.write("</div>");
}

function swap_image(elementNo) {
  objName = "pic"+elementNo;
  if (self_menu[elementNo+1][2] == 1) {
    document.getElementById(objName).src = image_list[elementNo][1];
  } else {
    document.getElementById(objName).src = image_list[elementNo][0];
  }
}

function pull_down(nr,c) {
 if (timerID == "") {
 swap_image(nr);
 to = self_menu[nr+1][1];
 begin = nr + 2;
 if (timerID != "") clearTimeout(timerID);
 if (self_menu[nr+1][2] == 0) {
  self_menu[nr+1][2] = 1;
  if(nr == self_menu.length-2) {to++;}
  epull_down(begin,to,0);
 } else {
  to = 0;
  self_menu[nr+1][2] = 0;
  name = "down"+(nr+2);
  open_item = 0;
  for(i=0;i<nr;i++) {
   if(self_menu[i][2] == 1)
    {open_item += self_menu[i][1];
    }
  }
  if (N == false) {open_item-= (c*1)};
  if (nr== self_menu.length-2) {val = self_menu[self_menu.length-1][1];to=-1;}
  else  val = parseInt(document.getElementById(name).style.top) -(open_item)-(c*height);
  epull_up(begin,to,val);
  }
  }
}

function epull_down(nr,to,nowv) {
 name = "down" + (nr-1);
// CPanelHeight=CPanelHeight+2;
// document.getElementById("ControlPanel").style.height = CPanelHeight.toString()+"px";
 obj = document.getElementById(name).style.clip = "rect(0,"+width+","+(nowv+1)+",0)";
 for (i=nr;i<self_menu.length;i++) {
  name = "down" + i;
  obj = document.getElementById(name);
  obj.style.top = parseInt(obj.style.top)+2;
 }
 nowv=nowv+2;
 if(nowv < to) timerID = setTimeout("epull_down("+nr+","+to+","+nowv+")",speed);
 else timerID = ""; 
}

function epull_up(nr,to,nowv) {
 name = "down" + (nr-1);
// CPanelHeight=CPanelHeight-2;
// document.getElementById("ControlPanel").style.height = CPanelHeight.toString()+"px";
 obj = document.getElementById(name).style.clip = "rect(0,"+width+","+nowv+",0)";
 for (i=nr;i<self_menu.length;i++) {
  name = "down" + i;
  obj = document.getElementById(name);
  obj.style.top = parseInt(obj.style.top)-2;
 }
 nowv= nowv-2;
 if(nowv > to) timerID = setTimeout("epull_up("+nr+","+to+","+nowv+")",speed);
 else timerID = "";
}

function startup(nr) {
 write_menu();
// rawHeight = document.getElementById("ControlPanel").style.height;
// CPanelHeight = parseInt(rawHeight.replace("px", ""));
 for(i=0;i<self_menu.length;i++)
 {
  if (self_menu[i][3]>0) {
    name= "down"+(i+1);
    to = self_menu[i+1][1];
    self_menu[i+1][2] = 1;
    document.getElementById(name).style.clip = "rect(0,"+width+","+to+",0)";
    for (x=i+2;x<self_menu.length;x++) {
      name = "down" + x;
      obj = document.getElementById(name);
      obj.style.top = parseInt(obj.style.top)+to;
    }
  }
 }
}