var MenuBuilder = newClass(Uni_XHandler, {

  totalMenuWidth: 915,

  totalMenuItemWidth: 180,

  inverted: false,

  selectedItem: false,

  timeout: null,

  vertInvMultiplier: 18,
  
  vertInvOffset: 3,

  hideDelay: 50,

  findCoordinates: function(obj) {
    var coords = new Array();
    coords["left"] = 0;
    coords["top"]  = 0;
  
    if (obj.offsetParent) {
			while (obj.offsetParent) {
				coords["left"] += obj.offsetLeft;
		  		coords["top"]  += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x) {
			coords["left"] += obj.x;
			coords["top"]  += obj.y;
		}
		
    return coords;
	},

  build: function (containerId, structure, _inv) {
    this.inverted = _inv == true;
    this.buildTree($(containerId), structure);
  },

  buildTree: function(root, struct, _skip) {
    var info, mroot = root.appendChild(this.doc.createElement('DIV'));
    mroot.className = "menu-section";

    for (var name in struct) {
      info = struct[name];
      mitem = this.buildItem(name, info, mroot, _skip);
      
      this.setUDStyle(mitem, info);
      if (info["childs"] && info["childs"] != null)
      {
        if (!_skip) this.setHandlers(mitem);
        this.buildTree(mitem, info["childs"], true);
      }
    }
  },

  buildItem: function(name, info, root) {
    var item = root.appendChild(this.doc.createElement('DIV'));
    item.className = "menu-item";

    var caption = item.appendChild(this.doc.createElement('DIV'));
    caption.className = "caption";

    var link = caption.appendChild(this.doc.createElement('A'));
    link.appendChild(this.doc.createTextNode(name));
    link.href = info["href"];

    return item;
  },

  show: function(item) {
    if (this.selectedItem != item)
      this.selectedItem = item;
    else 
      clearTimeout(this.timeout);

    var h_center = this.win.screen.width / 2;
    var r_limit  = h_center + (this.totalMenuWidth / 2);
    var coords = this.findCoordinates(item);
    item.className =  'active menu-item';

    var childSection = item.getElementsByTagName('DIV')[1];
    if ((coords['left'] + this.totalMenuItemWidth) > r_limit)
      childSection.style.marginLeft = '-' + (this.totalMenuItemWidth / 2) + 'px';

    if (this.inverted)
    {
      if (childSection && childSection.className == 'menu-section') {
        childSection.style.marginTop = 
          -1 * ((1 + childSection.childNodes.length) * this.vertInvMultiplier + this.vertInvOffset) + "px";
      }
    }
  },

  hide: function(item) {
    var obj = this;
    this.timeout = setTimeout(function() { item.className =  'menu-item'; }, obj.hideDelay);
    return true;
  },

  setHandlers: function(item) {
    var obj = this;
    this.addEventTo(item, 'mouseover', function() { obj.show(item); });
    this.addEventTo(item, 'mouseout',  function() { obj.hide(item); });
  },

  setUDStyle: function(node, nodeInfo) {
    if (nodeInfo["style"] && nodeInfo["style"] != '')
    {
      var style = nodeInfo["style"];
      for (var prop in style) {
        node.style[prop] = style[prop];
      }
    }
  }

});
