/* Copyright (C) 2001-2002 Cyan
             (C) 2001 Bob Clary, Netscape Communications
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

var is_mozilla = false; // fix for Mozilla 0.9.8+ - navigator.family destructed =(
get_browser_id();

function get_browser_id() {
        navigator.OS            = '';
        navigator.version       = 0;
        navigator.org           = '';
        navigator.family        = '';

        var platform;
        if (typeof(window.navigator.platform) != 'undefined')
        {
                platform = window.navigator.platform.toLowerCase();
                if (platform.indexOf('win') != -1)
                        navigator.OS = 'win';
                else if (platform.indexOf('mac') != -1)
                        navigator.OS = 'mac';
                else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1)
                        navigator.OS = 'nix';
        }

        var i = 0;
        var ua = window.navigator.userAgent.toLowerCase();

        if (ua.indexOf('opera') != -1)
        {
                i = ua.indexOf('opera');
                navigator.family        = 'opera';
                navigator.org           = 'opera';
                navigator.version       = parseFloat('0' + ua.substr(i+6), 10);
        }
        else if ((i = ua.indexOf('msie')) != -1)
        {
                navigator.org           = 'microsoft';
                navigator.version       = parseFloat('0' + ua.substr(i+5), 10);

                if (navigator.version < 4)
                        navigator.family = 'ie3';
                else
                        navigator.family = 'ie4'
        }
        else if (typeof(window.controllers) != 'undefined' && typeof(window.locationbar) != 'undefined')
        {
                i = ua.lastIndexOf('/')
                navigator.version = parseFloat('0' + ua.substr(i+1), 10);
                navigator.family = 'gecko';

                if (ua.indexOf('netscape') != -1)
                        navigator.org = 'netscape';
                else if (ua.indexOf('compuserve') != -1)
                        navigator.org = 'compuserve';
                else
                        navigator.org = 'mozilla';
        }
        else if ((ua.indexOf('mozilla') !=-1) && (ua.indexOf('spoofer')==-1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera')==-1)&& (ua.indexOf('webtv')==-1) && (ua.indexOf('hotjava')==-1))
        {
                var is_major = parseFloat(navigator.appVersion);

                if (is_major < 4)
                        navigator.version = is_major;
                else
                {
                        i = ua.lastIndexOf('/')
                        navigator.version = parseFloat('0' + ua.substr(i+1), 10);
                }
                navigator.org = 'netscape';
                navigator.family = 'nn' + parseInt(navigator.appVersion);
        }
        else if ((i = ua.indexOf('aol')) != -1 )
        {
                // aol
                navigator.family        = 'aol';
                navigator.org           = 'aol';
                navigator.version       = parseFloat('0' + ua.substr(i+4), 10);
        }

        is_mozilla = (navigator.family == 'gecko');
		if (navigator.family == 'nn4') navigator.family = 'nn4off';
}

var is_loaded = 0;
var menu_total= 0;
var item2show = null;
var inside = 0; // cumulative

var timers = new Array(null, null, null);

function set_timer(id) {
  if ((loaded || (navigator.family == 'nn4off')) && timers[id] == null)
  {
    timers[id] = setTimeout(menu_delays[id * 2 + 1], menu_delays[id * 2]);
  }
}

function drop_timer(id) {
  if (timers[id] != null) {
    clearTimeout(timers[id]);
    timers[id] = null;
  }
}

function set_visible(id, mode) {
  var j, m;

  if (id == null)
    return;

  if (navigator.family == 'ie4') {
    document.all["menu"+id].style.visibility = mode ? 'visible' : 'hidden';
  } else if (navigator.family == 'nn4')
    document.layers["menu"+id].visibility = mode ? 'show' : 'hide';
  else if (is_mozilla)
    document.getElementById("menu"+id).style.visibility = mode ? 'visible' : 'hidden';

  if (!mode)
    hilite(id, 0);

  eval("m = window.menu" + id);

  if (navigator.family == 'ie4') {
    for (j = 0; j < m[8]; j++)
      document.all["item"+id+"_"+j].style.visibility = mode ? 'visible' : 'hidden';
  } else if (navigator.family == 'nn4') {
    for (j = 0; j < m[8]; j++)
      document.layers["item"+id+"_"+j].visibility = mode ? 'show' : 'hide';
  } else if (is_mozilla) {
      for (j = 0; j < m[8]; j++)
        document.getElementById("item"+id+"_"+j).style.visibility = mode ? 'visible' : 'hidden';
  }
}

function hide_all() {
  timers[1] = null;
  var i;

  for (i = 1; i <= menu_total; i++)
    if (!inside || (i != item2show))
      set_visible(i, false);

  if (!inside)
    item2show = null;
}

function show_item() {
  timers[0] = null;

  set_visible(item2show, true);
}

function show_sched(item) {
  if (!is_loaded)
    return;

  inside++;

  if (item == item2show)
    return;

  item2show = item;

  if (item != null)
    set_timer(0);

  hide_all();
}

function hide_sched() {
  if ((navigator.family == 'nn4off') && document.images)
  {
  	// explicitly hide pics
	var i;
	for (i = 1; document.images[pic_prefix + i]; i++)
		document.images[pic_prefix + i].src = img_cache[(i - 1)* 2].src;
	
	return;
  }

  if (!is_loaded)
    return;

  inside--;
  drop_timer(1);
  set_timer(1);
}

function hilite_item(menu, item, state) {
  if (!is_loaded)
    return true;

  var name = "item"+menu+"_"+item, m;
  if ((state != 1 && state != 0))
    return true;

  eval("m = window.menu"+menu);

  if (state) {
    if (navigator.family == 'ie4') {
      document.all[name].style.color = m[10];
      document.all[name].style.background = m[9];
    } else if (navigator.family == 'nn4') {
      document.layers[name].bgColor = m[9];
      document.layers[name].document.open();
      document.layers[name].document.write('<img src='+whitespace_pic+' border=0 width=1 height='+m[12]+'><br><img src='+whitespace_pic+' border=0 width='+m[11]+' height=3><font class=menu_item_text color="'+m[10]+'">'+m[13 + item * 2]);
      document.layers[name].document.close();
    } else if (is_mozilla) {
      document.getElementById(name).style.color = m[10];
      document.getElementById(name).style.background = m[9];
    }

    status = m[14 + item * 2];
  } else {
    if (navigator.family == 'ie4') {
      document.all[name].style.color = m[6];
      document.all[name].style.background = m[5];
    } else if (navigator.family == 'nn4') {
      document.layers[name].bgColor = m[5];
      document.layers[name].document.open();
      document.layers[name].document.write('<img src='+whitespace_pic+' border=0 width=1 height='+m[12]+'><br><img src='+whitespace_pic+' border=0 width='+m[11]+' height=3><font class=menu_item_text color="'+m[6]+'">'+m[13 + item * 2]);
      document.layers[name].document.close();
    } else {
      document.getElementById(name).style.color = m[6];
      document.getElementById(name).style.background = m[5];
    }

    status = '';
  }

  return true;

}

function ns_item_over(e) {
  show_sched(e.target.l_id);
  hilite_item(e.target.l_id, e.target.l_item, 1);
}

function ns_item_out(e) {
  hide_sched();
  hilite_item(e.target.l_id, e.target.l_item, 0);
}

function ns_item_click(e) {
  var proto;
  inside = 0;
  hide_all();

  eval("proto = window.menu" + e.target.l_id);
  location.href = proto[13 + e.target.l_item * 2];

  return true;
}

function create_menu_item(name, proto, left, top, width, height, bgcolor, attrs, text) {
  var out = "";

  if (navigator.family == 'ie4') {
    out = '<div class=menu_item_text id="'+name+((proto[8])?'" style="position: absolute; top: '+top+'px; left: '+left
          + 'px; width: '+width+'; background: '+bgcolor+'; color: '+proto[6]
          + '; height: '+height+'px; visibility: hidden; padding-top: ' + proto[12] + '; overflow: hidden; cursor: hand; padding-left: ' 
	      + proto[11] + 'px;"'+attrs+'>':'" style="width: 0px; height: 0px;">');
	out+=text+'</div>';
  } else if (navigator.family == 'nn4') {
    out = new Layer(width); 
    out.top = top;
    out.left= left;
    out.height = height;
    out.bgColor= bgcolor;
    out.visibility = 'hide';
    document.layers[name] = out;

    out.document.open();
    out.document.write('<img src='+whitespace_pic+' border=0 width=1 height='+proto[12]+'><br><img src='+whitespace_pic+' border=0 width='+proto[11]+' height=3><font class=menu_item_text color="'+proto[6]+'">'+text+'</font>');
    out.document.close();

    out.clip.right = width;
    out.clip.bottom= height;

    if (attrs.length) {
      out.onmouseover = attrs[0];
      out.onmouseout  = attrs[1];

      out.document.captureEvents(Event.MOUSEUP);
      out.document.onmouseup = attrs[2];

      out.l_id  = attrs[3];
      out.document.l_id  = attrs[3];
      out.l_item= attrs[4];
      out.document.l_item= attrs[4];
    }

    out = '';
  } else if (is_mozilla) {
    var zindex = 2;
    if (!name.indexOf("menu"))
      zindex = 1;

    var divel = document.createElement("DIV");
	if (proto[8])
	{
    	divel.setAttribute('style', 'position: absolute; top: '+top+'px; left: '+left+
        	               'px; width: '+width+'px; background: '+bgcolor+'; color: '+proto[6]+
            	           '; height: '+(height - proto[12])+'px; visibility: hidden; overflow: hidden; cursor: hand; padding-left: ' + proto[11] + 'px; padding-top: ' + proto[12] + 'px; z-index: ' + zindex + ';'); 
	    divel.setAttribute('class', 'menu_item_text');						   
	}
	
    divel.setAttribute('id', name);

    if (attrs.length) {
      divel.setAttribute('onmouseover', attrs[0]);
      divel.setAttribute('onmouseout', attrs[1]);
      divel.setAttribute('onmouseup', attrs[2]);
    }

    if (text.length)
      divel.appendChild(document.createTextNode(text));
    document.body.appendChild(divel);
  }

  return out;
}

function create_menu_dropdown(id, proto) {
  var out = "", i, j, attrs;

  out += create_menu_item("menu"+id, proto, proto[3], proto[4], proto[0] + proto[2] * 2,
                          proto[8] * (proto[1] + proto[2]) + proto[2], proto[7], '', "");

  for (i = 0; i < proto[8]; i++) {
    j = "item"+id+"_"+i;

    if (navigator.family == 'ie4')
      attrs = ' onmouseover="show_sched('+id+'); hilite_item('+id+','+i+', 1)"'+
              ' onmouseout="hide_sched(); hilite_item('+id+','+i+', 0);"'+
              ' onclick="item2show = null; hide_all(); location.href=\''+proto[14 + i * 2]+'\'; return true;"';
    else if (navigator.family == 'nn4')
      attrs = new Array(ns_item_over, ns_item_out, ns_item_click, id, i);
    else if (is_mozilla)
      attrs = new Array('show_sched('+id+'); hilite_item('+id+','+i+', 1)',
                        'hide_sched(); hilite_item('+id+','+i+', 0)',
                        'item2show = null; hide_all(); location.href=\''+proto[14 + i * 2]+'\'; return true;');

    out += create_menu_item(j, proto, proto[3] + 1, proto[4] + proto[2] + i *
                            (proto[1] + proto[2]), proto[0],
                            proto[1], proto[5],
                            attrs,
                            proto[13 + i * 2]);
  }

  return out;
}

function init_menu() {
  var m, menus = "";
  menu_total = 1;

  while (eval("m = window.menu"+menu_total))
    menus += create_menu_dropdown(menu_total++, m);
  menu_total--;

  if (navigator.family == 'ie4')
    document.all.container.innerHTML = menus;
}

function loaded() {
  var i, j;

  if ((navigator.family != 'nn4off') && (navigator.family != 'ie4') && (navigator.family != 'gecko'))
    return true;

  if (is_loaded)
    return true;

 // is_loaded = 1;

  for (i = 1, j = 0; i <= pics; i++) {
    img_cache[j] = new Image();
    img_cache[j++].src = pic_path + i + pic_ext_pass;

    img_cache[j] = new Image();
    img_cache[j++].src = pic_path + i + pic_ext_act;
  }

  if (navigator.family != 'nn4off') {
    init_menu();
	is_loaded = true;
  } else
    is_loaded = false;

  return true;
}

function hilite(img_no, state) {
  if ((! document.images) || (state != 1 && state != 0) || !(document.images[pic_prefix + img_no]))
    return true;

  if ((navigator.family == "nn4off") || is_loaded)
    document.images[pic_prefix + img_no].src = img_cache[(img_no - 1)* 2 + state].src;
  
  return true;
}
