if (!_DMENU_)
{
var _DMENU_ = true;
var list_indicator = unescape('%BB%A0');
var menu_id_prefix = 'm_';
var node_id_prefix = 'n_';
var pic_id_prefix  = 'p_';
var pic_img_id_prefix = 'p_img_';
var img_id_prefix = 'img_';
var tm_id_prefix = 'tm_';
var pm_id_prefix = 'pm_';
var sm_id_prefix = 'submenu_';
var br_id_prefix = 'bridge_';
var trs_id_prefix = 'trans_';
var lyr_id_prefix = 'layer_';

if (typeof(dMenu_eventListener) == 'undefined')
	var dMenu_eventListener = null;


function isDigit(value)
{
	var re = /^[0-9]*$/;
	return (!isNaN(value) && re.test(value));
}

Object.prototype.count = function(){
	var c = 0;
	for (var i in this)
		if (isDigit(i))
			c++;
	return c;
};

if (!Function.prototype.bind == null) {
	Function.prototype.bind = function (object)
	{
		var method = this;
		return function ()
		{
			return method.apply(object, arguments);
		};
	};
}

if (PDEvent == null) {
  var fni = 1;
	var PDEvent = {
		add: function(obj,type,fn) {
			if (obj.attachEvent) {
        var fns = Number(fni++).toString();
				obj['e'+type+fns] = fn;
				obj[type+fns] = function() { obj['e'+type+fns](); }
				obj.attachEvent('on'+type,obj[type+fns]);
			} else
			obj.addEventListener(type,fn,false);
		},
		remove: function(obj,type,fn) {
			if (obj.detachEvent) {
				obj.detachEvent('on'+type,obj[type+fn]);
				obj[type+fn] = null;
			} else
			obj.removeEventListener(type,fn,false);
		}
	};
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}


function MenuNode(id, pid, menu_obj)
{
	this.id = id;
	this.pid = pid;
	this.menu = menu_obj;
	this.level = 0;
	this.parent = null;
	this.subNodes = {};

	this.m_ele = null;
	this.ele = null;
	this.pic = null;
	this.p_img = null;
	this.img = null;
	this.img_init = false;
  
  
  this.init = function()
  {
    if (this.prefix != '') {
      this.m_ele = $(menu_id_prefix + this.id);
      this.ele = $(node_id_prefix + this.id);
      if (this.ele != null) {
        this.ele.dMenuNode = this;
        this.ele.onclick = function() {
          this.dMenuNode.menu.showMenu(this.dMenuNode.id);
        };
        this.ele.onmouseover = function() {
          this.dMenuNode.menu.showPic(this.dMenuNode);
        };
      }
      this.pic = $(pic_id_prefix + this.id);
      if (this.pic != null) {
        this.p_img = $(pic_img_id_prefix + this.id);
        if (this.p_img == null || this.p_img.value == '') {
          this.pic.parentNode.removeChild(this.pic);
          this.pic = null;
        }
        else {
          this.img = $(img_id_prefix + this.id);
        }
      }
    }
  };

  this.hasImg = function()
  {
    return (this.pic != null);
  };

	this.init();
}

function isIE()
{
  var reg_str = "MSIE ";
  var appver = navigator.appVersion;
  var offset = appver.indexOf(reg_str);
  if (offset != -1)
  {
    offset += reg_str.length;
    IEVersion = parseFloat(
	  appver.substring( offset, appver.indexOf(';', offset) ));
    return true;
  }
  else
    return false;
}

var is_IE = isIE();

function $(id) {
	return document.getElementById(id);
}

function init_levels(node, level)
{
	if (node != null) {
		node.level = level;
		for (var nid in node.subNodes) {
			if (!isNaN(nid) && node.subNodes[nid] != null) {
				node.subNodes[nid].level = level;
				init_levels(node.subNodes[nid], level + 1);
			}
		}
	}
}

function debug_var(obj)
{
  var temp = "";
  for (x in obj)
    temp += x + ": " + obj[x] + "\n";
  return (temp);
}

function dMenu(mid)
{
	this.mid = mid;
	this.parent = null;
	this.submenu = null;
	this.bridge = null;
	this.trans = null;
	this.layer = null;
	this.nodesByID = {};
	this.visMenus = {};

  this.init = function()
  {
    if (!isNaN(this.mid)) {
      this.parent  = $(tm_id_prefix  + this.mid);
      this.submenu = $(sm_id_prefix  + this.mid);
      this.bridge  = $(br_id_prefix  + this.mid);
      this.trans   = $(trs_id_prefix + this.mid);
      this.layer   = $(lyr_id_prefix + this.mid);

      this.parent.dMenu = this;
      this.bridge.dMenu = this;
      this.layer.dMenu  = this;

      //this.addNode(this.mid, -1);

      var divs = this.layer.getElementsByTagName('div');
      if (divs.length > 0) {
        var did = null;
        var nid = 0;
        var pm = null;
        for (var i = 0; i < divs.length; i++) {
          did = divs[i].id.split('_');
          if (did.length == 2) {
            if (did[0] == menu_id_prefix.charAt(0)) {
              pm = $(pm_id_prefix + did[1]);
              if (pm != null) {
                if (!isNaN(pm.value)) {
                  this.addNode(did[1], pm.value);
                  var lis = this.nodesByID[did[1]].m_ele.getElementsByTagName('li');
                  if (lis.length > 0) {
                    for (var j = 0; j < lis.length; j++) {
                      nid = lis[j].id.split('_');
                      if (nid.length == 2) {
                        if (nid[0] == node_id_prefix.charAt(0) && !isNaN(nid[1])) {
                          this.addNode(nid[1], did[1]);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
        if (this.nodesByID.count()) {
          this.initLevels();
          this.setMouseHandler();
        }
      }
    }
  };

  this.emitHideMenu = function()
  {
    var btn = this.parent.getElementsByTagName('div')[0];
    if (btn != null && btn.btncfg) {
      btn.btncfg.b = false;
      btn.onmouseout(btn);
    }
    this.hideMenu(this.mid);
    dMenu_eventListener.fireEvent(null, dMenu_eventListener, 'showMenu', false);
  };

  this.setMouseHandler = function()
  {
    if (this.parent != null) {
      this.parent.onmouseover = function() {
        var btn = this.getElementsByTagName('div')[0];
        if (btn != null && btn.btncfg)
          btn.btncfg.b = true;
        this.dMenu.showMainMenu();
        dMenu_eventListener.fireEvent(null, dMenu_eventListener, 'showMenu', true);
      };
      this.parent.onmouseout = function(e) {
        if (!e) e = window.event;
        var mx = e.pageX;
        var my = e.pageY;
        if (is_IE) {
          mx = e.clientX + document.body.scrollLeft;
          my = e.clientY + document.body.scrollTop;
        }
        var w = this.clientWidth;
        //var h = this.clientHeight;
        var x = findPosX(this);
        var y = findPosY(this);
        if ((mx - 2) < x || (mx + 2) > (x + w) || (my - 2) < y) {
          this.dMenu.emitHideMenu();
        }
      };
      this.bridge.onmouseout = function(e) {
        if (!e) e = window.event;
        var mx = e.pageX;
        if (is_IE) {
          mx = e.clientX + document.body.scrollLeft;
        }
        var w = this.clientWidth;
        var x = findPosX(this);
        var y = findPosY(this);
        if ((mx - 2) < x || (mx + 2) > (x + w)) {
          this.dMenu.emitHideMenu();
        }
      };
      this.layer.onmouseout = function(e) {
        if (!e) e = window.event;
        var mx = e.pageX;
        var my = e.pageY;
        if (is_IE) {
          mx = e.clientX + document.body.scrollLeft;
          my = e.clientY + document.body.scrollTop;
        }
        var w = this.clientWidth;
        var h = this.clientHeight;
        var x = findPosX(this);
        var y = findPosY(this);
        var bx = findPosX(this.dMenu.bridge);
        var bw = this.dMenu.bridge.clientWidth;
        if ((mx - 2) < x || (mx + 2) > (x + w) || (my + 2) > (y + h)
          || ((my - 2) < y) && mx > (bx + bw)) {
          this.dMenu.emitHideMenu();
        }
      };
    }
    
  };

  this.addNode = function(id, pid)
  {
    if (this.nodesByID[id] == null) {
      var node = new MenuNode(id, pid, this);
      this.nodesByID[node.id] = node;
      if (this.nodesByID[pid] != null) {
        node.parent = this.nodesByID[pid];
        node.parent.subNodes[node.id] = node;
      }
    }
  };

  this.loadImg = function(node)
  {
    if (node != null && node.hasImg() && node.img != null && node.img_init == false) {
      node.img.src = node.p_img.value;
      node.img_init = true;
    }
  };

  this.showMainMenu = function()
  {
    this.showMenu(this.mid);
  };

  this.showMenu = function(mid)
  {
    if (this.nodesByID[mid] != null) {
      var node = this.nodesByID[mid];
      if (node.ele != null) {
        node.ele.className = 'act_entry';
      }
      if ((this.visMenus[node.level] != null
         && this.visMenus[node.level].id != node.id)
         || this.visMenus[node.level] == null) {
        if (this.visMenus[node.level] != null && Number(this.visMenus[node.level].id) > 0) {
          this.hideMenu( this.visMenus[node.level].id );
        }
        if (Number(mid) == this.mid) {
          this.submenu.style.display =
            this.bridge.style.display =
            this.layer.style.display = '';
        }
        if (node.m_ele != null) {
          node.m_ele.style.display = '';
        }
        this.visMenus[node.level] = node;
      }

      if (node != null && node.hasImg()) {
        var m_pic = node.pic;
        if (m_pic != null) {
          this.loadImg(node);
          m_pic.style.display = '';
          m_pic.parentNode.removeChild(m_pic);
          this.layer.appendChild(m_pic);
          this.visMenus[node.level] = node;
        }
      }
      this.updateTransSize();
    }
  };

  this.showPic = function(node)
  {
    if (node != null) {
      if ((this.visMenus[node.level] != null
       && this.visMenus[node.level].id != node.id)
       || this.visMenus[node.level] == null) {
        var resize = false;
        if (this.visMenus[node.level] != null && Number(this.visMenus[node.level].id) > 0) {
          this.hidePic( node.level );
          resize = true;
        }

        if (node.hasImg()) {
          node.ele.className = 'act_entry';

          var m_pic = node.pic;
          if (m_pic != null) {
            this.loadImg(node);
            m_pic.style.display = '';
            m_pic.parentNode.removeChild(m_pic);
            this.layer.appendChild(m_pic);
            this.visMenus[node.level] = node;
            resize = true;
          }
        }
        
        if (resize == true) {
          this.updateTransSize();
        }
      }
    }
  };

  this.hidePic = function(mlvl)
  {
    if (this.visMenus[mlvl] != null) {
      var node = this.visMenus[mlvl];
      if (node.subNodes.count() == 0)
        node.ele.className = '';
      if (node.pic != null) {
        node.pic.style.display = 'none';
        this.visMenus[node.level] = null;
      }
      return true;
    }
    return false;
  };

  this.hideMenu = function(mid)
  {
    if (this.nodesByID[mid] != null) {
      if (Number(mid) == this.mid) {
        this.submenu.style.display =
          this.bridge.style.display =
          this.layer.style.display = 'none';
      }
      var node = this.nodesByID[mid];
      if (node.ele != null) {
        node.ele.className = '';
      }
      for (var i = node.level; this.hideMenuLevel(i); i++){}
      this.updateTransSize();
    }
  };

  this.hideMenuLevel = function(mlvl)
  {
    if (this.visMenus[mlvl] != null) {
      var node = this.visMenus[mlvl];
      if (node.ele != null) {
        node.ele.className = '';
      }
      if (node.pic != null) {
        node.pic.style.display = 'none';
        this.visMenus[node.level] = null;
      }
      if (node.m_ele != null) {
        node.m_ele.style.display = 'none';
        this.visMenus[node.level] = null;
      }
      return true;
    }
    return false;
  };

  this.updateTransSize = function()
  {
    this.trans.style.width = this.layer.offsetWidth;
    this.trans.style.height = this.layer.offsetHeight;
  };

  this.initLevels = function()
  {
    if (this.nodesByID != null) {
      var node = this.nodesByID[this.mid];
      if (node != null) {
        init_levels(node, 1);
      }
    }
  };
  
  this.init();
}

}
