var WM_TOP = "top";
var WM_BOTTOM = "bottom";
var WM_RIGHT = "right";
var WM_LEFT = "left";

var MARGIN_BOTTOM = 0;
var MARGIN_RIGHT = 20;

var currentItem = null;
var menuTrail = new Array();

var currentStyleOff = null;

function wmItemOn(item, level, styleOn, styleOff, submenuId, submenuPosition) {

  if (item.onmouseout == null) {
    item.onmouseout = startOffTimer;
  }

  stopOffTimer();

  if (currentItem != null) {
    if (styleOff != currentStyleOff && currentStyleOff != null) {
      currentItem.className = currentStyleOff;
    } else {
      currentItem.className = styleOff;
    }
  }

  currentItem = item;
  item.className = styleOn;
  currentStyleOff = styleOff;


  if (submenuId != null) {
    hide(level);

    var menu = document.getElementById(submenuId);

    if (submenuPosition == WM_TOP) {
      menu.style.top = findOffsetTop(item) - menu.offsetHeight + 10;
      menu.style.left = findOffsetLeft(item) + item.offsetWidth + MARGIN_RIGHT;
    }

    if (submenuPosition == WM_BOTTOM) {
      menu.style.top = findOffsetTop(item) + item.offsetHeight + MARGIN_BOTTOM;
      menu.style.left = findOffsetLeft(item);
    }

    if (submenuPosition == WM_RIGHT) {
      menu.style.top = findOffsetTop(item);
      menu.style.left = findOffsetLeft(item) + item.offsetWidth;
    }
    
    if (submenuPosition == WM_LEFT) {
      menu.style.top = findOffsetTop(item);
      menu.style.left = findOffsetLeft(item) - item.offsetWidth - MARGIN_RIGHT;
    }

    menu.style.visibility = "visible";

    menuTrail[level] = menu;
  } else {

    // item has no submenu attached, update the menuTrail
    
    hide(level);
  }

}


// Hide all submenus after the level passed in, inclusive.
function hide(level) {
  for (var i = level; i < menuTrail.length; i++) {
    menuTrail[i].style.visibility = "hidden";
  }
}


// *** timer ******************************************************************

// Timer is needed to turn off all the items and submenus, 
// once mouse moves away from the menu.

var timerID = null;
var timerOn = false;
var timecount = 250;

function startOffTimer() {
  if (timerOn == false) {
    timerID = setTimeout("offAll()", timecount);
    timerOn = true;
  }
}

function stopOffTimer() {
  if (timerOn) {
    clearTimeout(timerID);
    timerID = null;
    timerOn = false;
  }
}

// Hide all submenus and turn current item off.
function offAll() {
  hide(0);
  
  if (currentStyleOff != null) {
    currentItem.className = currentStyleOff;
  }
}

// *** position ***************************************************************

// Utility code to calculate element position.

// Find total left offset.
function findOffsetLeft(obj){
  var curleft = 0;
  if (obj.offsetParent){
    while (obj.offsetParent){
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    curleft += obj.x;
  }

  return curleft;
}

// Find total top offset.
function findOffsetTop(obj){
  var curtop = 0;
  if (obj.offsetParent)	{
    while (obj.offsetParent){
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }else if (obj.y){
    curtop += obj.y;
  }

  return curtop;
}

function flash (version, filename, name, width, height, wmode, bgcolor, menu, flashvars) {
	document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+version+"' width='"+width+"' height='"+height+"' id='"+name+"' align='middle'>");
	document.write("<param name='FlashVars' value='"+flashvars+"' />");
	document.write("<param name='allowScriptAccess' value='sameDomain' />");
	document.write("<param name='wmode' value='"+wmode+"' />");
	document.write("<param name='bgcolor' value='"+bgcolor+"' />");
	document.write("<param name='salign' value='lt' />");
	document.write("<param name='movie' value='"+filename+"' /><param name='menu' value='"+menu+"' /><param name='quality' value='high' /><embed src='"+filename+"' wmode='"+wmode+"' menu='"+menu+"' quality='high' FLashVars='"+flashvars+"' salign='lt' width='"+width+"' height='"+height+"' bgcolor='"+bgcolor+"' name='"+name+"' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
	document.write("</object>");
}