/* **********************************************************************
 * Menu for ekonomika.sk
 * **********************************************************************/

if (!versions_loaded) {
 // Determine Browser Version
 var bV = parseInt(navigator.appVersion);
 var NS4 = (document.layers)         ? true : false;
 var NS3 = (document.images)         ? true : false;
 var IE4 = ((document.all)&&(bV>=4)) ? true : false;
 var ver4 = (NS4 || IE4)             ? true : false;

 // Determine Browser Platform
 var MAC   = (navigator.userAgent.indexOf("Mac")      != -1) ? true : false;
 var Opera = (navigator.userAgent.indexOf("Opera")    != -1) ? true : false;
 var IE5   = (navigator.userAgent.indexOf("MSIE 5.0") != -1) ? true : false;

 var versions_loaded = true;
}

/* **********************************************************************
 * WINDOW METHODS
 * **********************************************************************/

function getInsideWindowWidth() {
 var width = -1;
 if (NS4) width = window.innerWidth;
 else if (IE4) width = document.body.clientWidth;
 return(width);
}

function getInsideWindowHeight() {
 var height = -1;
 if (NS4) height = window.innerHeight;
 else if (IE4) height = document.body.clientHeight;
 return(height);
}

function getWindowLeft() {
 var x = -1;
 if (NS4) x = window.pageXOffset;
 else if (IE4) x = document.body.scrollLeft;
 return(x);
}

function getWindowTop() {
 var y = -1;
 if (NS4) y = window.pageYOffset;
 else if (IE4) y = document.body.scrollTop;
 return(y);
}

/* **********************************************************************
 * GENERIC OBJECT LOCATOR METHODS
 * **********************************************************************/

function getObject(objectName, container) {
 var object = null;
 if (typeof(objectName) != "string") { object = objectName; }
 else if (ver4) {
  if (!container) container = document;
  if (IE4) {
     if (container.all[objectName]) object = container.all[objectName]; }
  else if (NS4) {
     if (container.anchors[objectName]) object = container.anchors[objectName];
     else if (container.forms[objectName]) object = container.forms[objectName];
     else if (container.images[objectName]) object = container.images[objectName];
     else if (container.layers[objectName]) object = container.layers[objectName];
  }
 }
 return(object);
}

function findObjectContainer(objectName, container) {
 var objectContainer = null;
 if (NS4) {
    if (!container) container = window.document;
    if (getObject(objectName,container)) { objectContainer = container; }
    else {
     var layers = getLayers(new RegExp(".*"), container);
     for (var i=0; i < layers.length; i++) {
         objectContainer = findObjectContainer(objectName, layers[i].document);
         if (objectContainer) break;
     }
    }
 }
 else if (IE4) { objectContainer = getObject(objectName,document) ? document : null; }
 return(objectContainer);
}

function findObject(objectName) {
 var object = null;
 if (ver4) {
    var container = findObjectContainer(objectName);
    if (container) object = getObject(objectName, container);
 }
 return(object);
}

/* **********************************************************************
 * LAYER METHODS
 * **********************************************************************/

function getLayer(name) {
 var obj = null;
 if (IE4) { obj = document.all[name]; }
 else if (NS4) { obj = document.layers[name]; }
 return(obj);
}

function getLayers(regex, doc) {
 var layers = new Array();
 layers.length = 0;
 if (ver4) {
    if (!doc) doc = document;
    if (NS4) { for (var i=0; i < doc.layers.length; i++) {
       var obj = doc.layers[i];
       if (!regex || regex.exec(obj.id)) { layers[layers.length] = obj; }
    }} else if (IE4) {
       var divColl = doc.all.tags("DIV");
       for (i=0; i < divColl.length; i++) {
           if (!regex || regex.exec(divColl[i].id)) { layers[layers.length] = divColl[i]; }
       }
    }
 }
 return(layers);
}

function isHidden(obj) {
 if (IE4) { return(obj.style.visibility == "hidden"); }
 else if (NS4) { return(obj.visibility == "hide"); }
 else { return(false); }
}

function hideLayer(obj) {
 if (IE4) { obj.style.visibility = "hidden"; }
 else if (NS4) { obj.visibility = "hide"; }
}

function showLayer(obj) {
 if (IE4) { obj.style.visibility = "visible"; }
 else if (NS4) { obj.visibility = "show"; }
}

function getObjTop(obj) {
 if (ver4) return(IE4 ? obj.style.pixelTop : obj.top);
 else return(0);
}

function getObjLeft(obj) {
 if (ver4) return(IE4 ? obj.style.pixelLeft : obj.left);
 else return(0);
}

function getObjHeight(obj) {
 if (ver4) return(IE4 ? obj.clientHeight : (obj.clip ? obj.clip.height : -1));
 else return(0);
}

function getObjWidth(obj) {
 if (ver4) return(IE4 ? obj.clientWidth : (obj.clip ? obj.clip.width : -1));
 else return(0);
}

function moveObjTo(obj, x, y, yesIfHidden) {
 if (!isHidden(obj) || yesIfHidden) {
    if (IE4) {
       if (x >= 0) obj.style.pixelLeft = x;
       if (y >= 0) obj.style.pixelTop  = y;
     } else if (NS4) {
       if (x < 0) x = getObjLeft(obj);
       if (y < 0) y = getObjTop(obj);
       obj.moveTo(x, y);
     }
 }
 return(1);
}

/* **********************************************************************
 * IMAGE METHODS
 * **********************************************************************/

function getImage(imgNm) {
 return(findObject(imgNm));
}

function getImageContainer(imgNm) {
 return(findObjectContainer(imgNm));
}

function getParentObject(obj) {
 var parent = null;
 if (IE4) { if (obj.offsetParent) parent = obj.offsetParent; }
 else if (NS4) {
  // If we're looking at a layer object (or a window), get
  // the parentWindow value, otherwise search for the container
  if (obj.parentLayer) { parent = obj.parentLayer || null; }
  else {
    parent = findObjectContainer(obj);
    if (parent) parent = parent.parentWindow;
  }
 }
 return(parent);
}

function findOffsetValue(obj, value) {
 var offset = 0;
 if (ver4) {
    // Requested value is specified as an MSIE offset value.
    // Perform any translations necessary for other browsers.
    if (NS4) {
       if (value == "offsetLeft") value = "pageX";
       else if (value == "offsetTop") value = "pageY";
    }
    var parent = getParentObject(obj);
    while (parent) {
      offset += (parent[value] || 0);
      parent = getParentObject(parent);
    }
 }
 return(offset || 0);
}

function getImageLeft(img) {
 if (typeof(img) == "string") img = getImage(img);
 var left   = 0;
 var offset = findOffsetValue(img, "offsetLeft");
 if (NS4) { left = img.x; }
 else if (IE4) { left = img.offsetLeft; }
 return(left + offset);
}

function getImageTop(img) {
 if (typeof(img) == "string") img = getImage(img);
 var top    = 0;
 var offset = findOffsetValue(img, "offsetTop");
 if (NS4) { top = img.y; }
 else if (IE4) { top = img.offsetTop; }
 return(top + offset);
}

function getImageHeight(img) {
 if (typeof(img) == "string") img = getImage(img);
 return(img.height);
}

function getImageWidth(img) {
if (typeof(img) == "string") img = getImage(img);
return(img.width);
}

/* **********************************************************************
 * Create a MenuItem object.
 * **********************************************************************/

function MenuItem(miname, mivalue, target) {
 if (!Opera) {
    this.name  = miname;
    this.value = mivalue;
    this.bold  = false;
    this.target = target;
    return(this);
 }
}

/* **********************************************************************
 * Create the Menu object.
 * **********************************************************************/

var allmenus	= new Array(); // tracks all created menus for later printing.
allmenus.length	= 0;

function Menu(mname, _imagename) {
 // Set up properties for this instance
 this.name = mname;
 this.imagename = _imagename;
 this.items = new Array();
 this.items.length = 0;
 // If the _width parameter isn't supplied, make an attempt
 // to determine the menu width based on the image's width.
 var imgWidth = null;
 if (_imagename) {
    var img  = getImage(_imagename);
    if (img) { imgWidth = getImageWidth(img); }
 }
 if (imgWidth) this.width = imgWidth;
 // If borderstyle is "image", make sure leftimage, leftimagewidth and
 // leftimagecolor are specified
 if (this.borderstyle && this.borderstyle == "image") {
    if (!this.leftimagewidth || !this.leftimage) {
       this.borderstyle = "solid";
       this.leftimage = null;
    } else if (!this.leftimagecolor) { this.leftimagecolor = "#ffffff"; }
 }
 // Add the menu to the list of menus if it's not the prototype
 if (mname != "discard") {
    allmenus[allmenus.length++] = this;
    allmenus[mname] = this;
 }
 return(this);
}

function Menu_createMenuItem(miname, mivalue, target) {
 var newitem;
 newitem = new MenuItem(miname, mivalue, target);
 newitem.index = this.items.length;
 newitem.menuname = this.name;
 this.items[this.items.length++] = newitem;
 return(newitem);
}

function Menu_addMenuItem(miname, mivalue, target) {
 var newitem;
 if (!this.maxitems || this.spilltype == "columns" ||
      this.items.length < this.maxitems || mivalue == "---submenu") {
	newitem = this.createMenuItem(miname, mivalue, target);
 } else {
   if (!this.childmenu) {
      var cname = this.name + "_child";
      var cimg  = this.name + "_child_img";
      var cm = new Menu(cname, cimg);
      this.childmenu = cm;
      this.addSubMenu(cm, "more.gif");
   }
   newitem = this.childmenu.addMenuItem(miname, mivalue, target);
 }
 return(newitem);
}

function Menu_addMenuItemHeader(miname, mivalue) {
 var newitem = this.addMenuItem(miname, mivalue, "");
 if (newitem) { newitem.bold = true; }
 return(newitem);
}

function Menu_addMenuSeparator(hidden) {
 var newitem = this.addMenuItem("separator", "---");
 if (newitem) { newitem.hidden = hidden; }
 return(newitem);
}

function Menu_addSubMenu(menu, mimage) {
 var newitem = this.addMenuItem(menu.name, "---submenu");
 if (newitem) {
    newitem.image = mimage;
    newitem.menu = menu;
    newitem.menu.parent = this;
 }
 return(newitem);
}

function Menu_setLocation() {
 var img = getImage(this.imagename);
 var x, y;
 if (this.parent) {
    var parent_layer = getLayer(this.parent.name);
    x = getObjLeft(parent_layer) + getObjWidth(parent_layer) + 1;
    y = getObjTop(parent_layer)  + getImageTop(img) + this.topOffset + 1;
 } else {
    x = getImageLeft(img) + this.leftOffset;
    y = getImageTop(img)  + getImageHeight(img) + this.topOffset + 1;
 }
 moveObjTo(getLayer(this.name), x, y, true);
 if (this.childmenu) { this.childmenu.setLocation(); }
}

function Menu_show() {
 if (NS4 || (IE4 && !MAC)) {
  if (this.parent) this.parent.show();
  var menuName = this.name;
  // Move the menu to a location near the image
  var layer = getLayer(menuName);
  if (this.imagename && isHidden(layer)) {
     this.setLocation();
     // Now, adjust the bottom of the menu with respect to the bottom
     // of the window to make sure the entire menu is displayed.
     var y = getObjTop(layer);
     var x = getObjLeft(layer);
     // adjust the top
     var lbottom = y + getObjHeight(layer);
     var wbottom = getInsideWindowHeight();
     var wtop    = getWindowTop();
     if (lbottom > (wbottom + wtop)) { y -= (lbottom - (wbottom + wtop)); }
     if (y < wtop) y = wtop;
     // adjust the left edge
     var lright  = getObjLeft(layer) + getObjWidth(layer);
     var wright  = getInsideWindowWidth();
     var wleft   = getWindowLeft();
     if (lright > (wright + wleft)) { x -= lright - (wright + wleft); }
     if (x < wleft) x = wleft;
     // move the object to the new location
     moveObjTo(layer, x, y, true);
  }
  // Display the menu
  showLayer(layer);
  clearTimeout(this.timeout);
 }
}

function Menu_hide(now) {
 if (NS4 || (IE4 && !MAC)) {
    if (now) { hideLayer(getLayer(this.name)); }
    else {
      var cmd      = 'allmenus["' + this.name + '"].hide(true)';
      var delay    = this.delay;
      this.timeout = setTimeout(cmd, delay);
      if (this.parent) { this.parent.hide(); }
    }
 }
}

function Menu_print() {
 var menu = this;
 // write the header
 var output = "";
 if (NS4 || (IE4 && !MAC)) {
    // Make sure we haven't already been printed
    if (getLayer(this.name)) return;

    if (this.childmenu) { this.childmenu.print(); }

    if (navigator.appName == "Microsoft Internet Explorer") {
       output += '<DIV id="' + menu.name + '" ';
       output += 'onMouseOver="showMenu(\'' + menu.name + '\')" ';
       output += 'onMouseOut="hideMenu(\'' + menu.name + '\')" ';
       output += 'style="POSITION: absolute; background-color: #ff9966; Z-INDEX: 10; VISIBILITY: hidden; ';
       output += ' WIDTH: ' + menu.width + 'px; HEIGHT: ' + menu.height + ';">';
    } else {
       output += '<LAYER name="' + menu.name + '" ';
       output += 'onMouseOver="showMenu(\'' + menu.name + '\')" ';
       output += 'onMouseOut="hideMenu(\'' + menu.name + '\')" ';
       output += 'width=' + menu.width + ' height=' + menu.height + ' ';
       output += 'visibility="hidden" zIndex=10>';
    }

    if (menu.borderstyle == "solid") {
       output += '<TABLE border=0 cellpadding=0 cellspacing=1';
       output += ' width=' + menu.width + ' bgcolor="' + menu.bgcolor + '">';
       output += '<TR height=1>';
       output += '<TD bgcolor="' + menu.bordercolor + '" height=' + menu.borderwidth + ' colspan=3>';
       output += '<IMG src="' + addr_prefix + 'img/13x13.gif" width=1 height=1></TD>';
       output += '</TR>';
       output += '<TD bgcolor="' + menu.bordercolor + '" width=' + menu.borderwidth + '>';
       output += '<IMG src="' + addr_prefix + 'img/13x13.gif" width=1 height=1></TD>';
       output += '<TD valign="top" bgcolor="' + menu.bgcolor + '">';
       output += '<TABLE border=0 cellspacing=0 cellpadding=' + menu.padding + '>';
       output += '<TR><TD>';
    } else {
       output += '<TABLE border=0 cellpadding=' + menu.padding + ' cellspacing=0';
       output += ' width=' + menu.width + ' bgcolor="' + menu.bgcolor + '">';
       output += '<TR><TD valign="top">';
    }

    // now add all the menu items
    var j, c;
    for (j=0, c=0; j < menu.items.length; j++, c++) {
      if (c > this.maxitems && this.spilltype == "columns") {
         output += '</TD><TD valign="top">';
         c = 0;
         if (menu.items[0].bold) { output += '&nbsp;<BR>'; c++; }
      }
      var mi = menu.items[j];
      if (mi.name == "separator") {
         if (mi.hidden) { output += "<BR>"; }
         else { output +=  '<DIV><IMG src="'+addr_prefix+'img/0.gif" ALT="" width=5 height=5></DIV>'; }
         //***else { output +=  '<font color="#ffffff"><HR size=2></font>'; }
      } else { output += this.makeMenuItem(mi, false); }
      output += '';
    }

    if (menu.borderstyle == 'solid') {
       output += '</TD></TR></TABLE></TD>';
       output += '<TD bgcolor="' + menu.bordercolor + '" width=' + menu.borderwidth + '>';
       output += '<IMG src="' + addr_prefix + 'img/13x13.gif" width=1 height=1></TD></TR>';
       output += '<TR height=1>';
       output += '<TD bgcolor="' + menu.bordercolor + '" height=' + menu.borderwidth + ' colspan=3>';
       output += '<IMG src="' + addr_prefix + 'img/13x13.gif" width=1 height=1></TD>';
       output += '</TR></TABLE>';
    } else { output += '</TD></TR></TABLE>'; }

    if (navigator.appName == "Microsoft Internet Explorer") { output += '</DIV>'; }
    else { output += '</LAYER>'; }
    document.write(output);
 }
}

function Menu_getHeight() {
 var height = 0;
 var i;
 for (i=0; i < this.items.length; i++) { height += getObjHeight(this.items[i].name); }
 return(height);
}

function Menu_makeMenuItem(mi) {
 var output = "";
 output += '<DIV>';
 if (mi.bold) output += '<B>';

 if (mi.value == "---submenu") {
    output += '<A href="#" onClick="return false"';
    output += ' onMouseOver="showMenu(' + "'" + mi.menu.name + "'" + ')"';
    output += ' onMouseOut="hideMenu('  + "'" + mi.menu.name + "'" + ')"';
    output += '><IMG name="' + mi.menu.imagename + '" src="' + mi.image + '" border=0></A><BR>';
 } else {
    var label = mi.name;
    if (this.nobreaks) {
       var t = "";
       var start, end;
       for (start=0, end=0; (end = label.indexOf(" ",start)) != -1;) {
           t += label.substring(start,end) + "&nbsp;";
           start = end + 1;
       }
       t += label.substring(start);
       label = t;
    }
    output += '<A href="' + mi.value + '" ' + mi.target + ' CLASS="menu"';
    output += '>';
    output += '<font color="#ffffff">';
    output += label + '</font></A><BR>' ;
 }
 if (mi.bold) output += '</B>';
 output += '</DIV>';
 return(output);
}


/* **********************************************************************
 * Initialize the Menu object prototype.
 * **********************************************************************/

if (!Opera) {
 new MenuItem("", "");
 new Menu('discard');
 // Properties
 Menu.prototype.maxitems		= 0;
 Menu.prototype.childmenu		= null;
 Menu.prototype.width			= 110;
 Menu.prototype.height			= 10;
 Menu.prototype.imagename		= null;
 Menu.prototype.borderstyle		= "solid";
 Menu.prototype.bordercolor		= "#000000";
 Menu.prototype.bgcolor			= "#ffffff";
 Menu.prototype.bottomshadow		= "#666666";
 Menu.prototype.topshadow		= "#cccccc";
 Menu.prototype.leftOffset		= 0;
 Menu.prototype.topOffset		= 0;
 Menu.prototype.padding			= 0;
 Menu.prototype.delay			= 50;
 Menu.prototype.rollover		= false;
 Menu.prototype.leftimage		= null;
 Menu.prototype.leftimagewidth		= 0;
 Menu.prototype.leftimagecolor		= '';
 Menu.prototype.spilltype		= "child";
 Menu.prototype.nobreaks		= 1;
 // Methods
 Menu.prototype.createMenuItem		= Menu_createMenuItem;
 Menu.prototype.addMenuItem		= Menu_addMenuItem;
 Menu.prototype.addMenuItemHeader	= Menu_addMenuItemHeader;
 Menu.prototype.addMenuSeparator	= Menu_addMenuSeparator;
 Menu.prototype.addSubMenu		= Menu_addSubMenu;
 Menu.prototype.makeMenuItem		= Menu_makeMenuItem;
 Menu.prototype.getHeight		= Menu_getHeight;
 Menu.prototype.show			= Menu_show;
 Menu.prototype.hide			= Menu_hide;
 Menu.prototype.print			= Menu_print;
 Menu.prototype.setLocation		= Menu_setLocation;
}

/* **********************************************************************
 * The following functions operate the menus.
 * **********************************************************************/

function printMenus() {
 var i = 0;
 for (; i < allmenus.length; i++) { allmenus[i].print(); }
 return(i);
}

function showMenu(menuName) {
 if (allmenus[menuName]) allmenus[menuName].show();
}

function hideMenu(menuName) {
 if (allmenus[menuName]) allmenus[menuName].hide();
}
