/**
 * GearSpin Javascript Menu
 *
 * @version 1.10
 * @webite http://www.gearspin.com/gsjmenu/
 *
 */


// init internal elements
var intarr = new Array();
for(var i = 0; i < chrarr.length; i++)
  intarr[chrarr[i]] = i;
var arrowXOff = 11 + menXOff;
var arrowYOff = 5;
var prevMenStr = "a";
var currMenStr = "a";

var isMS = (navigator.appName && navigator.appName.indexOf("Microsoft") >= 0);

//add the html
document.getElementById("menu").innerHTML = genHtml("a");

//draw the menu
createMenu(getRealTop(document.getElementById("placer")) + totYOff, 
           getRealLeft(document.getElementById("placer")) + totXOff, "a");

window.onresize = function(){createMenu(getRealTop(document.getElementById("placer")) 
                                               + totYOff, getRealLeft(document.getElementById("placer")) 
                                               + totXOff, "a");};

//FUNCTIONS -------------------------------------------------------------------------
//how to open the new url
function openURL(url, targ){
  window.open(url, targ);
}

//to get position of placer
function getRealLeft(imgElem) {
  xPos = eval(imgElem).offsetLeft;
  tempEl = eval(imgElem).offsetParent;
  while (tempEl != null) {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}
function getRealTop(imgElem) {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

// to generate the html that holds the new divs
function genHtml(myName){
  var childString = "";
  for(var j = 0; j < menuArr[myName].length; j++){
    childString += "<div id='menuArr" + myName + chrarr[j] + "'></div>" 
      + "<img src='" + arrowGif + "' id='img" + myName + chrarr[j] + "'></img>";
    if(menuArr[myName][j][1] == "-")
      childString += genHtml(myName + chrarr[j]);
  }
  return childString;
}

//launch a url
function launch(bStr){
  var myCell = menuArr[bStr.substring(0,bStr.length-1)][intarr[bStr.charAt(bStr.length-1)]];
  if(myCell == null || (myCell[2] != null && myCell[2].indexOf("g") != -1))
    return;
  if(myCell[1] != "-" && myCell[1] != ""){
    if(myCell[2] != null && myCell[2].indexOf("b") != -1)
      openURL(myCell[1], "_blank");
    else
      openURL(myCell[1], "_self");
  }
}

//clear the menu tree
function clearMenu(withDelay, thisPrev){
  if(withDelay && (currMenStr!="a" || prevMenStr != thisPrev))
      return;
  var bS;
  var startInd = 1;

  while(startInd <= currMenStr.length && startInd <= prevMenStr.length
        && (currMenStr.charAt(startInd-1) == prevMenStr.charAt(startInd-1)))
    startInd++;
  
  for(var i = startInd; i < prevMenStr.length + 1; i++){
    bS = prevMenStr.substring(0,i);
    if(menuArr[bS] != null)
      for(var j = 0; j < menuArr[bS].length; j++){
        document.getElementById("menuArr" + bS + chrarr[j]).style.visibility = "hidden";    
        if(menuArr[bS][j][1] == "-")
          document.getElementById("img" + bS + chrarr[j]).style.visibility = "hidden";  
      }
  }
}

//show the menu tree
function showMenu(){
  clearMenu(false, "");
  var bS;
   
  for(var i = 2; i < currMenStr.length + 1; i++){
    bS = currMenStr.substring(0,i);
    for(var j = 0; j < menuArr[bS].length; j++){
      document.getElementById("menuArr" + bS + chrarr[j]).style.visibility = "visible";
      if(menuArr[bS][j][1] == "-")
        document.getElementById("img" + bS + chrarr[j]).style.visibility = "visible";    
    }
  }
}

//on a mouse over
function overMouse(bStr, showFlag){
  currMenStr = bStr;
  var thisBut = document.getElementById("menuArr" + bStr);
  //alert(thisBut.offsetHeight);
  //if(bStr.length > 2 && shadowFlag)
  //thisBut.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color='" 
  //+ bgHiColor + "', strength=" + shadowOff + ", direction=135)";
  thisBut.style.backgroundColor = bgHiColor;
  thisBut.style.color = textHiColor;
  thisBut.style.border= borderHi;

  clearMenu(false, "");
  if(showFlag)
    showMenu();  
}


//on a mouse out
function outMouse(bStr){
  prevMenStr = bStr;
  currMenStr = "a";
  var thisBut = document.getElementById("menuArr" + bStr);

  //if(bStr.length > 2 && shadowFlag)
  //thisBut.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color='" 
  //+ shadowColor + "', strength=" + shadowOff + ", direction=135)";
  thisBut.style.backgroundColor = bgLowColor;
  thisBut.style.color = textLowColor;
  thisBut.style.border= borderLow;
  window.setTimeout("clearMenu(true, '" + bStr + "')", delayVal);
}

//internal helper function
function px(str){
  var numStr = str.substring(0, str.length - 2);
  return parseInt(numStr);
}

//actually draw the menu
function createMenu(myTop, myLeft, basal){
  var SoFar = 0;
  var hVal;
  var wVal;
  var loop = 0;

  for(var j = 0; j < menuArr[basal].length; j++){
    var Tj = chrarr[j];
    var bNstr = basal + Tj;
    var currBut = document.getElementById("menuArr" + bNstr);
    var currImg = document.getElementById("img" + bNstr);
    
    //draw button
    currBut.style.position = "absolute";
    currBut.style.border= borderLow;
    currBut.style.backgroundColor = bgLowColor;
    currBut.style.color = textLowColor;
    currBut.style.zIndex = basal.length + 1;
    currBut.nama = bNstr;
    currImg.style.visibility = "hidden"; 
    if(isMS)
      if(alpha < 100)
        currBut.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + alpha + ")"; 

    //"register" events
    var moreFlag = (menuArr[basal][j][1] == "-");
    currBut.onmouseover = (moreFlag?function(){overMouse(this.nama,true)}:function(){overMouse(this.nama,false)});
    currBut.onmouseout = function(){outMouse(this.nama)};
    currBut.onclick = function(){launch(this.nama)};

    //base, menu centric settings
    if(basal == "a"){
      currBut.style.width = baseWVal + "px";
      currBut.style.left = myLeft + SoFar + "px";
      currBut.style.top = myTop + "px";
      currBut.style.visibility = "visible";
      currBut.style.font = baseText;
      currBut.style.textAlign = "center";
    }
    else{
      if(!isMS)
        currBut.style.width = (menuWVal - 25) + "px";
      else{
        if(shadowFlag)
          currBut.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color='" 
            + shadowColor + "', strength=" + shadowOff + ", direction=135)";
        currBut.style.width = menuWVal + "px";
      }
      currBut.style.left = myLeft + "px";
      currBut.style.top = myTop + SoFar + "px";
      currBut.style.visibility = "hidden";
      currBut.style.font = menuText; 
      currBut.style.paddingLeft = textLeftOff;
      currBut.style.paddingRight = arrowXOff + 4;     
    }

    //some text stuff
    currBut.innerHTML = menuArr[basal][j][0];
    currBut.unselectable = "on";
    currBut.style.paddingTop = textTopOff;    
    currBut.style.paddingBottom = textTopOff;    

    //offset calculation
    wVal = px(currBut.style.width) - (px(currBut.style.borderWidth));    

    if(!isMS)
      hVal = currBut.offsetHeight - 1;
    else
      hVal = currBut.clientHeight + (px(currBut.style.borderWidth));

    if(basal == "a")
      SoFar += wVal;       
    else
      SoFar += hVal;           

    //if it has a child menu group
    if(menuArr[basal][j][1] == "-"){
      currBut.style.cursor = "default";
      //if base menu item
      if(basal == "a"){
        //create the child menu
        createMenu(px(currBut.style.top) + hVal, px(currBut.style.left), bNstr);            
      }   
      //if derived menu items
      else {
        //arrow stuff        
        currImg.style.position = "absolute";
        currImg.style.top = (myTop + SoFar - (hVal/2) - arrowYOff) + "px";
        if(!isMS)
          currImg.style.left = (myLeft  + wVal - arrowXOff + 25) + "px";
        else
          currImg.style.left = (myLeft  + wVal - arrowXOff) + "px";
        currImg.style.zIndex = basal.length + 1;

        //create the child menu  
        if(!isMS)
          createMenu(px(currBut.style.top) + menYOff, px(currBut.style.left) + wVal - menXOff + 25, bNstr);
        else
          createMenu(px(currBut.style.top) + menYOff, px(currBut.style.left) + wVal - menXOff , bNstr);
      }
    }
    //no child menu group
    else{
      currBut.style.cursor = "pointer";
    }    
  }    
}







