 
var menuRendered=false,
	 menuCount=0,
 	 menuAnchor,
 	 menuTable,
	 menuArray,
	 activeMenu=0,
	 menuOpen=false,
	 tabSpacing=0,
	 menuBoxWidth=730,
	 activeColor='#6666cc',
	 menuItemTextColor='#ffffff',
	 inactiveColor='transparent',
	 category = '';

function Menu(id, image, imagePressed )
{
 this.id	= id;
 this.image	= image;
 this.imagePressed = imagePressed;
 this.items 	= new Array();
 this.images	= new Array();
 this.actions	= new Array();
 this.tabHeight = 20;
 this.tabWidth  = 75;

 this.addMenuItem = function (label, action)
	{
	 	 this.items[this.items.length] 		  = label;
	 	 this.images[this.images.length] 	  = "";
	 	 this.actions[this.actions.length] 	  = action;
	}

 this.setTabSize = function( width, height )
 {
   this.tabWidth = width;
   this.tabHeight = height;
 }
 this.swapImages = function()
 {
   var tmpimg = this.image;
   this.image = this.imagePressed;
   this.imagePressed = tmpimg;
 }
}

function setMenuBoxWidth( width )
{
 this.menuBoxWidth = width;
}

function setTabSpacing( width )
{
 this.tabSpacing = width;
}

function setPageCategory( category )
{
 this.category = category;
 if(!menuRendered) return;
 var i = findHighlightTab();
 if(i<=0) return;
 menus[i].swapImages();
 swapImage('imageTab'+i, menus[i].image);
}

menus = new Array();
p_img1s = new Array();
p_img2s = new Array();

function addMenu(menu)
{
 menuCount++;
 menus[menuCount] = menu;
 p_img1s[menuCount] = new Image();
 p_img1s[menuCount].src = menu.image;

 p_img2s[menuCount] = new Image();
 p_img2s[menuCount].src = menu.imagePressed;
}

function findHighlightTab()
{ 
 if(this.category.length<=0) return 0;
 for(i=1;i<=menuCount;i++)
 {
  if(category.toUpperCase()==menus[i].id.toUpperCase())
	{
		return i;
	}
 }
 
 for(i=1;i<=menuCount;i++)
 for(j=0;j<menus[i].items.length;j++)
 {
  if(category.toUpperCase()==menus[i].items[j].toUpperCase())
	{
		return i;
	}
 }
 return 0;
}

function renderMenu()
{
 renderStyle();

 for(i=1;i<=menuCount;i++)
 {
	document.write( '<div id="menuLabel'+i+'">' );
	var ml = 'href="javascript:void(0)"';

	if(menus[i].items.length<=0)
	{
		ml = 'href="javascript:void(0)"';
	}
	else
	if(menus[i].items.length<2)
	{
		ml = 'href="'+menus[i].actions[0]+'" onClick="swapImage(\'imageTab'+i+'\', menus['+i+'].imagePressed )"';
	}

	ml += ' onMouseOver="if(activeMenu!='+i+') swapImage(\'imageTab'+i+'\', menus['+i+'].imagePressed ); menuShow('+i+')" '+'" onMouseOut="if(activeMenu!='+i+') swapImage(\'imageTab'+i+'\', menus['+i+'].image )"';

 	if(menus[i].image.length<=0) {
 		document.writeln( '<a class="ml" '+ml+'>'+menus[i].id+'</a></div>' );
  	} else {
		var imgz = menus[i].image;
	  	document.writeln( '<a class="m1" '+ml+'><img id="imageTab'+i+'" name="imageTab'+i+'" src="'+imgz+'" border=0 alt="'+menus[i].id+'" /></a></div>' );
	}

	document.writeln( '<div id="menu'+i+'" class="menuBox">	' );
	
	for(j=0;j<menus[i].items.length;j++) {
		if(menus[i].images[j].length<=0) {
			if(menus[i].items[j]=='---') {
				document.writeln( '<HR WIDTH="100%" SIZE=1 NOSHADE COLOR="'+menuItemTextColor+'" />' );
			} else
				if (menus[i].id != 'Home') {
					document.write( '<a href="'+menus[i].actions[j]+'" onClick="closeMenu()">'+menus[i].items[j]+'</a>' );
				}
				if (j != menus[i].items.length-1) { 
					document.write(' <SPAN CLASS="menuTableDivider">|</SPAN> ');
				}
		} else {
			document.writeln( '<a href="'+menus[i].actions[j]+'"><img src=\"\" alt=\"'+menus[i].items[j]+'\" border="0" class="m" /></a><br />' );
		}	
	}
	
	document.writeln( '</div>' );
 }
 menuRendered=true;
 if (chan != "member") {
 setPageCategory(chan);
 }
}

function renderStyle()
{

document.writeln( "<style type='text/css'>" );
if (is.nav4)
{
 for(i=1;i<=menuCount;i++)
 {
 	tabWidth = menus[i].tabWidth;
 	tabHeight = menus[i].tabHeight;
	document.writeln("#menuLabel"+i+" { position:absolute; visibility:hidden; overflow:hidden; font-family: tahoma,verdana; font-size: 12px; font-weight:bold; background: transparent; layer-background-color: "+activeColor+"; width:"+tabWidth+"px; height: "+tabHeight+"px; margin:0px; padding:0px;}");
 }
}
else
{
 for(i=1;i<=menuCount;i++)
 {
 	var tabWidth = menus[i].tabWidth;
 	var tabHeight = menus[i].tabHeight;
  	document.writeln("#menuLabel"+i+" { position:absolute; visibility:hidden; overflow:hidden; font-family: tahoma,verdana; font-size: 12px; font-weight:bold; background: transparent; layer-background-color: "+activeColor+"; width: "+tabWidth+"px; height: "+tabHeight+"px; clip:rect(0px,"+tabWidth+"px,"+tabHeight+"px,0px); margin:0px; padding:0px; border:0px outset;}");
 }
}

document.writeln("a.m:link, a.m:visited, a.m:active { font-family: tahoma,verdana; text-decoration:none; font-size: 12px; color:"+menuItemTextColor+"; background:transparent; width:"+menuBoxWidth+"px; }" );

document.writeln("a.m:hover { font-family: tahoma,verdana; text-decoration:none; font-size: 12px; color:#000000; background:#ccfd30; }" );

document.writeln("a.ml:link, a.ml:visited, a.ml:active, a.ml:hover { font-family: tahoma,verdana; text-decoration:none; font-size: 12px; color:#000000; background:transparent; }" );

document.writeln(".menuBox { position:absolute; visibility:hidden; overflow:hidden; font-family: tahoma,verdana; font-size: 11px; color:#ffffff; width: 728px; text-align: center; }" );

document.writeln("#menuAnchor { position:relative; visibility:hidden; width:1em; height:1em; left:0px; top:0px; }" );

document.writeln("#menuTable { position:relative; visibility:hidden; width:728px; height:25px; left:0px; top:0px; text-align: center; border-width: 1px; border-style : solid;}" );

document.writeln("</style>");
}

function windowOnload()
{
  menuAnchor = cbeGetElementById('menuAnchor').cbe;
  menuTable = cbeGetElementById('menuTable').cbe;
  menuArray = new Array();
  window.cbe.addEventListener("resize", resizeListener);
  document.cbe.addEventListener("click", menuHide);
  setTimeout("init()", 250);
}

function resizeListener(e) {
  init();
}

function init() {
  var i, menuX, menuY, menuLabel;
  menuX=0;
  for(i=1;i<=menuCount;++i)
  {
	menuLabel = cbeGetElementById('menuLabel'+i).cbe;
	menuX +=menuLabel.width()+tabSpacing;
  }
  menuX=menuAnchor.pageX()+menuAnchor.width()-menuX;
  menuY=menuAnchor.pageY()+menuAnchor.height();
  menuTableX=menuTable.pageX();
  menuTableY=menuTable.pageY()+6;
  if(!menuRendered)
  {
  	alert('Menu not rendered' );
  	return;
  }

  var zindex = 99999999999999999999999;
  for (i = 1; i <= menuCount; ++i) {
    menuLabel = cbeGetElementById('menuLabel'+i).cbe;
    menuLabel.zIndex(zindex);
    menuLabel.resizeTo(menus[i].tabWidth,menus[i].tabHeight);
    menuLabel.moveTo(menuX, menuY-menuLabel.height());
    menuLabel.show();
    menuArray[i] = cbeGetElementById('menu'+i).cbe;
    menuArray[i].moveTo(menuTableX, menuTableY);
    menuArray[i].menuLabel = menuLabel;
    menuArray[i].zIndex(zindex+1);
	 menuX += (menuLabel.width()+tabSpacing);
  }
}

function menuRefreshTabs()
{
  closeMenu();

  for (i = 1; i <= menuCount; ++i) {
    menuArray[i].menuLabel.background(inactiveColor);
   if(menus[i].imagePressed)
   {
    var tag = 'imageTab'+i;
    swapImage(tag, menus[i].image);
   }
  }
}

function menuShow(mn)
{
  if (mn == activeMenu && menuOpen) {
    closeMenu();
    return;
  }
  menuRefreshTabs();
  menuArray[mn].show();
  menuArray[mn].menuLabel.background(activeColor);
  if(!(chan == menus[mn].id.toString())) {
  	if(menus[mn].imagePressed)
  	{
  	 var tag = 'imageTab'+mn;
  	 swapImage(tag, menus[mn].imagePressed);
  	}
  }
  menuOpen = true;
  activeMenu = mn;
}

function swapImage( tag, image_url )
{  
 var ih = document.images[tag];
 if(ih)
	{
	 ih.src = image_url;
	}
}

function menuHide(e)
{
  if (!menuOpen) return;
  if (!menuArray[activeMenu]) return;
  var x = e.pageX;
  var y = e.pageY;
  if (!menuArray[activeMenu].contains(x,y,-2,0,0,0)
      && !menuArray[activeMenu].menuLabel.contains(x,y,0,20,0,0)) {
    closeMenu();
  }
}

function closeMenu() {
  if(!menuArray[activeMenu]) {
	return;
  }
  menuArray[activeMenu].hide()  
  menuArray[activeMenu].menuLabel.background(inactiveColor)
  if(menus[activeMenu].imagePressed)
  {
   var tag = 'imageTab'+activeMenu
   swapImage(tag, menus[activeMenu].image)
  }
  menuOpen = false
  activeMenu = 0
}
