var NS4=(document.layers);
var NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
var DOM=(document.getElementById);
var IE=(document.all);
var IE4=(IE && !DOM);
var Mac = (navigator.appVersion.indexOf("Mac") != -1);
var IEMac=(IE && Mac);
var IE4Mac=(IE4 && Mac);
var NS = (NS4 || NS6);
var ver4 = (NS4 || IE4);

function MenuGenerator(initMenuObject, ident){
	
  this.id = ident;
	
  this.activeMenu = "";

  this.timeOut = this.timeOut || 1000;

  this.menuTimer = setTimeout(this.id + ".clearOpen()", this.timeOut);

  this.changeClassBack = "";

  this.menuObject = new Object();

  this.stopTimer();
	
  this.menuObject = initMenuObject;
  // Setup default values

  // CSS Classes
  this.menuItemCSSC = this.menuObject.menuItemCSSC || 'JSmenuItem';
  this.menuItemHoverCSSC = this.menuObject.menuItemHoverCSSC || 'JSmenuItemHover';
  this.subItemCSSC = this.menuObject.subItemCSSC || 'JSmenuSubItem';
  this.subItemHoverCSSC = this.menuObject.subItemHoverCSSC || 'JSmenuSubItemHover';
  this.disableCSSC = this.menuObject.disableCSSC || 'JSdisabledItem';
  // Paths
  this.imageDir = this.menuObject.imageDir || '/menu/images/';
  this.cssFile = this.menuObject.cssFile || '/menu/menu.css';
  // Options
  this.layout = this.menuObject.layout || 'vertical';
  this.openMethod = this.menuObject.openMethod || 'hover';
  this.timeOut = this.menuObject.timeOut || timeOut;
  this.trans = this.menuObject.transparent || 0;
  this.shadow = this.menuObject.shadow || 0;
  this.overrideCSSC = this.menuObject.overrideCSSC || 0;

  this.mainDelm = this.menuObject.mainDelm || '';

  this.submenus = this.menuObject.submenu;
}

MenuGenerator.prototype.generateMenu = function(){
  document.write('<link rel="stylesheet" href="'+this.cssFile+'" type="text/css" />');
  if(this.layout == 'horizontal'){
    document.write('<table cellpadding="0" cellspacing="0" border="0"><tr>');
  }

  for(var i = 0; i < this.submenus.length; ++i){
    var submenuitems = this.submenus[i].items;
    if(this.layout == 'horizontal'){
      document.write('<td>');
    }
    document.write('<div name="item'+ i + this.id + '" id="item'+ i + this.id + '" class="JSmenuItem" ');

    if(this.openMethod == 'click'){
      document.write('onClick');
    }else{
      document.write('onMouseOver');
    }
    document.write('="');
    if(submenuitems){
      document.write(this.id +'.displayMenu(\'item'+ i + this.id + '\',\''+this.layout+'\',\''+this.menuObject.transition.mode+'\');');
    }
	if(this.submenus[i].imageoff){
	  document.write(this.id +'.swapImage(\'item'+ i + this.id + '\', \'imageon\');');
	}
    document.write(this.id +'.changeClass(\'item'+ i + this.id + '\', \'JSmenuItemHover\')"');
	document.write(' onMouseOut="');
	//if submenuitems let the close menu hideMenu handle doing the image swap out
	//else we have to do it
    if(submenuitems){
      document.write(this.id + '.hideActiveTimeout();"');
    }else{
	  if(this.submenus[i].imageoff){
	  	document.write(this.id +'.swapImage(\'item'+ i + this.id + '\', \'imageoff\');');
	  }
      document.write(this.id + '.resetClass();"');
    }
    document.write('>');
    if((this.submenus[i].action) && (this.openMethod == 'hover')){
      document.write('<a href="'+ this.submenus[i].action + '">');
    }
    if(this.submenus[i].imageoff){
		document.write('<img src="' + this.submenus[i].imageoff + '" id=\'image_item'+i+this.id+'\' border=0>');
		//write out images so they are pre-cached
		//write out the hiddens so we know src for swapout
		if(this.submenus[i].imageon){
			imageOn = new Image();
			imageOn.src = this.submenus[i].imageon;
			document.write('<input type=\'hidden\' id=\'imageon_item'+i+this.id+'\' value=\''+this.submenus[i].imageon+'\'>');
		}
		imageOff = new Image();
		imageOff.src = this.submenus[i].imageoff;
		document.write('<input type=\'hidden\' id=\'imageoff_item'+i+this.id+'\' value=\''+this.submenus[i].imageoff+'\'>');
	}else{
	    document.write(this.submenus[i].name);

	}
	
    if((this.submenus[i].action) && (this.openMethod == 'hover')){
      document.write('</a>');
    }

    document.write('</div>');
    if((this.mainDelm) && (i + 1 < this.submenus.length)){
      document.write('</td><td><img src="' + this.mainDelm + '" width="9" height="24" align="absmiddle"></td><td>');
    }
    if(this.overrideCSSC){
      document.write('<div id="overrideCSSC" class="' + this.overrideCSSC + '" style="position: absolute;">');
    }
    document.write('<div name="menu_item'+ i + this.id +'" id="menu_item'+ i + this.id +'" class="JSmenu" onMouseOver="'+ this.id +'.stopTimer()" onMouseOut="'+ this.id +'.hideActiveTimeout()" style="position:absolute; ');
    if(IE){
      var filters = '';
      if(this.shadow > 0){
        filters += 'progid:DXImageTransform.Microsoft.Shadow(direction=135, color=#000000,strength='+this.shadow+') ';
      }
      if(this.trans > 0){
        filters += 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+this.trans+') ';
      }
	  if(this.menuObject.transition){
        if(this.menuObject.transition.mode == 'Fade'){
          filters += 'progid:DXImageTransform.Microsoft.Fade(duration=' + this.menuObject.transition.value + ') ';
        }else if(this.menuObject.transition.mode == 'Pixelate'){
          filters += 'progid:DXImageTransform.Microsoft.Pixelate(MaxSquare=' + this.menuObject.transition.value + ') ';
        }else if(this.menuObject.transition.mode == 'GradientWipe'){
          filters += 'progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=' + this.menuObject.transition.value + ', wipestyle=1, motion=forward) ';
        }
	  }
      
      if(filters != ''){
        document.write('FILTER: ' + filters + ';');
      }
    }
    
    document.write('">');

    document.write('<table cellpadding="0" cellspacing="0" border="0"><tr>');
    if(this.shadow > 0){
      if(IE){
        document.write('<td nowrap="true">');
      }else{
        document.write('<td height="1" bgcolor="#000000" colspan="2"><img src="' + this.imageDir + 'spacer.gif" width="1" height="1" /></td><td height="1" bgcolor="#FFFFFF"><img src="' + this.imageDir + 'spacer.gif" width="1" height="1" /></td></tr><tr><td width="1" bgcolor="#000000"><img src="' + this.imageDir + 'spacer.gif" width="1" height="1" /></td><td nowrap="true">');
      }
      
    }else{
      document.write('<td nowrap="true">');
    }

    if(submenuitems){
      for(var j = 0; j < submenuitems.length; ++j){
        if(submenuitems[j].divider){
          document.write('<img src="'+this.imageDir+'dividing_line.gif" width="100%" height="6">');
        }else if(submenuitems[j].disable){
          document.write('<div class="'+this.disableCSSC+'" onMouseOver="this.className=\''+this.disableCSSC+'Hover\'" onMouseOut="this.className=\''+this.disableCSSC+'\'">' + submenuitems[j].name + '<img src="' + this.imageDir + 'spacer.gif" width="10" height="5" border="0" />');
          document.write('</div>');
        }else{
          document.write('<div class="JSmenuSubItem" onMouseOver="this.className=\'JSmenuSubItemHover\'" onMouseOut="this.className=\'JSmenuSubItem\'"><a href="'+submenuitems[j].action+'"');
          if(submenuitems[j].target){
            document.write(' target="'+submenuitems[j].target+'"');
          }
          document.write('>' + submenuitems[j].name + '<img src="' + this.imageDir + 'spacer.gif" width="10" height="5" border="0" /></a>');
          document.write('</div>');
        }
      }
    }

    if(this.shadow > 0){
      if(IE){
        document.write('</td>');
      }else{
        document.write('</td><td background="' + this.imageDir + 'shadow-right.gif" width="7" valign="top"><img src="' + this.imageDir + 'shadow-corner_top_right.gif" width="7" height="6" /></td></tr><tr><td width="1" align="left" bgcolor="#FFFFFF"><img src="' + this.imageDir + 'spacer.gif" width="1" height="1" /></td><td background="' + this.imageDir + 'shadow-bottom.gif" height="7" align="left"><img src="' + this.imageDir + 'shadow-corner_bottom_left.gif" width="8" height="7" /></td><td width="7" height="7"><img src="' + this.imageDir + 'shadow-bottom_right.gif" width="7" height="7" /></td>');
      }
    }else{
      document.write('</td>');
    }

    document.write('</tr></table>');

    document.write('</div>');
    if(this.overrideCSSC){
      document.write('</div>');
    }
    if(this.layout == 'horizontal'){
      document.write('</td>');
    }
  }
  if(this.layout == 'horizontal'){
    document.write('</tr></table>');
  }
}

MenuGenerator.prototype.clearOpen = function(){
  if(this.activeMenu){
    this.hideMenu(this.activeMenu);
    this.activeMenu = "";
  }
}

MenuGenerator.prototype.stopTimer = function(){
  clearTimeout(this.menuTimer);
}

MenuGenerator.prototype.changeClass = function(elementName, className){
  //alert('|' + elementName + '|');
  var thisElement = getElementObject(elementName);
  var oldClass = thisElement.className;
  thisElement.className = className;
  this.changeClassBack = this.id + ".changeClass('"+elementName+"', '"+oldClass+"')";
}

MenuGenerator.prototype.displayMenu = function(elementName, layout, transition){
  this.clearOpen();
  var thisMenu = getElementObject('menu_'+elementName);
  var thisMenuTitle = getElementObject(elementName);
  attr = getElementAttribs(thisMenuTitle);
  
  
  if(this.menuObject.fixedPosition == 'topRight'){
    newXCoord = this.menuObject.fixedPositionCoord.x - thisMenu.offsetWidth;
    newYCoord = this.menuObject.fixedPositionCoord.y;
  }else if(this.menuObject.fixedPosition == 'topLeft'){
    newXCoord = this.menuObject.fixedPositionCoord.x;
    newYCoord = this.menuObject.fixedPositionCoord.y;
  }else{
    if(layout == 'horizontal'){
      newXCoord = attr.x;
      newYCoord = attr.y + attr.h - 1;
    }else{
      newXCoord = attr.x + attr.w - 1;
      newYCoord = attr.y - 1;
    }
  }
  
  if(IE){
    thisMenu.style.pixelLeft = newXCoord;
    thisMenu.style.pixelTop  = newYCoord;
  }else if(NS6){
    thisMenu.style.left = newXCoord + '';
    thisMenu.style.top  = newYCoord + '';
  }else if(NS4){
    thisMenu.moveBy(newXCoord, newYCoord);
  }

  this.activeMenu = elementName;

  if((IE) && (transition != "none")){
    thisMenu.filters.item("DXImageTransform.Microsoft." + transition).Apply();
  }
  thisMenu.style.visibility = "visible";
  if((IE) && (transition != "none")){
    thisMenu.filters.item("DXImageTransform.Microsoft." + transition).Play();
  }

}

MenuGenerator.prototype.swapImage = function(elementName, imageSrc){
  var image = getElementObject('image_' + elementName);
  if(image){
  	 var newImageSrc = getElementObject(imageSrc + '_' + elementName);
	 //alert(imageSrc + '_' + elementName);
	 if(newImageSrc)
		 image.src = newImageSrc.value;
  }
}

MenuGenerator.prototype.hideMenu = function(elementName){
  var thisMenu = getElementObject('menu_' + elementName);
  thisMenu.style.visibility = "hidden";
  this.stopTimer();
  this.activeMenu = "";
  eval(this.changeClassBack);
  //image swap out
  this.swapImage(elementName, 'imageoff');
}

MenuGenerator.prototype.resetClass = function(){
  eval(this.changeClassBack);
}

MenuGenerator.prototype.hideActiveTimeout = function(){
  this.menuTimer = setTimeout(this.id + ".clearOpen()", this.timeOut);
}

// Static helper functions
function getElementObject(elementName){
  if(DOM){
    thisElement = eval("document.getElementById('"+elementName+"')");
  }else{
    thisElement = eval("window['"+elementName+"']");
  }
  return thisElement;
}

function getElementAttribs(Elem){
  var attribs = {x:0, y:0, w:0, h:0};
  attribs.w = Elem.offsetWidth;
  attribs.h = Elem.offsetHeight;
  var currentElement = Elem;
  while(currentElement!=window.document.body) {
    attribs.y += currentElement.offsetTop;
    attribs.x += currentElement.offsetLeft;
    attribs.y -= currentElement.scrollTop;
    attribs.x -= currentElement.scrollLeft;

    currentElement = currentElement.offsetParent;
    // Adjust for body border/margin
    if(currentElement==window.document.body){
      attribs.y += currentElement.offsetTop;
      attribs.x += currentElement.offsetLeft;
      attribs.y -= currentElement.scrollTop;
      attribs.x -= currentElement.scrollLeft;    
    }
  }
  
  return attribs;
}