/************************************************************
    Drop-down Menu module
    copyright (c) 2003 by Andy V. Kasatkin (aka taker)
    http://www.taker.ru, taker@mail.ru
************************************************************/

var color_on  = '#505050';
var color_off = '#FFFFFF';
var color_div = '#E2E2E2';
var bg_color_on  = '#FFFFFF';
var bg_color_off = '#C31C1C';
var item_height = 27;
var deactivate_timeout = 1000;  // msec
var light_off_timeout = 50;     // msec

var roll_supported = (document.images)? true : false;
var subnav_supported = false;
var timeout = 0;
var light_timeout = 0;
var target;

var menu_counter = 0;
var item_counter = 0;
var active = null;
var active_item = null;
var menu_list = new Array;
var item_list = new Array;
var menu_over = false;
var menu_active = null;
var timer;
var current_path = '';

function Menu(xpos, ypos, width, items) {
    this.id = menu_list.length;
    this.xpos = xpos;
    this.ypos = ypos;
    this.width = width;
    this.height = items.length * item_height;
    this.items = items;
    this.skin = menu_skin;
    menu_list[this.id] = this;
}

function Item(name, url) {
    this.id = item_list.length;
    this.name = name;
    this.url = url;
    this.skin = item_skin;
    this.div = item_div;
    item_list[this.id] = this;
}

// ======================================

function item_skin() {
    var div_end = '</div>';
    re = new RegExp(this.url, 'gi');
    if (current_path.search(re) >= 0) {
        var div_start = (browser.nn4 || browser.opera5) ? '<div class="item">' : '<div class="item" id="item_div_' + this.id + '" style="background-color:' + bg_color_on + '">';
        var str =
            '<tr>' +
            '<td bgcolor="' + bg_color_on + '">' + div_start +
            '<table cellpadding="0" cellspacing="0" border="0"><tr valign="top">' +
            '<td class="submenu">&nbsp;' + 
            '<img src="/img/arrow_white.gif" width="9" height="6" border=0 alt=""></td>' +
            '<td class="submenu">' + this.name + '</td>' +
            '</tr></table>' + div_end +
            '</td>' +
            '</tr>\n';
    } else {
        var div_start = (browser.nn4 || browser.opera5) ? '<div class="item">' : '<div class="item" id="item_div_' + this.id + '" style="background-color:' + bg_color_off + '">';
        var str =
            '<tr>' +
            '<td bgcolor="white" style="cursor: hand;" onclick="location.href=\'' + this.url + '\'" onMouseOver="item_over(' + this.id + ');" onMouseOut="item_out(' + this.id + ');">' + div_start +
            '<table cellpadding="0" cellspacing="0" border="0"><tr valign="top">' +
            '<td class="submenu">&nbsp;<a href="' + this.url + '">' + 
            '<img src="/img/arrow_gray.gif" width="9" height="6" border=0 alt="" id="item_image_' + this.id + '"></a></td>' +
            '<td class="submenu"><a id="item_link_' + this.id + '" href="' + this.url + '">' + this.name + '</a></td>' +
            '</tr></table>' + div_end +
            '</td>' +
            '</tr>\n';
    }
    return str;
}

function item_div() {
    return '<tr><td bgcolor="' + color_div + '"><img src="/img/void.gif" alt="" border="0" width="1" height="1"></td></tr>\n';
}

function menu_skin() {
    var str = 
        '<div class="subnav" id="menu_' + this.id + '">' +
        '<table cellpadding="0" cellspacing="0" border="0" bgcolor="#FAFAFA" width="' + this.width + '">\n' +
        '<tr>\n' +
        '<td><img src="/img/void.gif" width="1" height="1" border="0" alt=""></td><td><img src="/img/void.gif" width="1" height="1" border="0" alt=""></td><td><img src="/img/void.gif" width="1" height="1" border="0" alt=""></td>\n' +
        '</tr>\n' +
        '<tr>\n' +
        '<td><img src="/img/void.gif" width="1" height="1" border="0" alt=""></td><td>\n' +
        '<table cellpadding="0" cellspacing="0" border="0" width="' + (this.width-2) + '">\n';
    for (var i = 0; i < this.items.length; i++) {
        str += this.items[i].skin();
        str += this.items[i].div();
    }
    str +=
        '</table>' +
        '</td><td><img src="/img/void.gif" width="1" height="1" border="0" alt=""></td></tr></table>' +
        '</div>';
    return str;
}

// ======================================

function deactivate() {
    timeout = 0;
    if (active != null) {
        active.layer.hide();
        active = null;
    }
}

function over(item) {
    menu_over = true;
    menu_active = item;
    if (subnav_supported) {
        if (timeout != 0) { clearTimeout(timeout); timeout = 0; }
        if (active == null || active.id != item) {
            if (active) {
                active.layer.hide();
                clearTimeout('timer');
            }
            if (menu_list[item]) {
                appearance(item);
                active = menu_list[item];
            } else {
                active = null;
            }
        }
    }
}

function appearance(item){
    var steps = (browser.explorer) ? 20 : 10;
    if ((browser.dom || browser.ie4) && (!window.opera || browser.opera7) && !browser.mlin) {
        step = parseInt(menu_list[item].items.length * item_height / steps);
        recMoveLayer(item, step, 0);
    }
    if(browser.mlin || (window.opera && !browser.opera7)) menu_list[item].layer.move(menu_list[item].xpos, menu_list[item].ypos);
    menu_list[item].layer.show();
}

function recMoveLayer(item, step, iter){
    var leer = menu_list[item].layer;
    var layerHeight = menu_list[item].items.length * item_height;
    var clipHeight = layerHeight - ++iter * step;
    if (clipHeight < 0) clipHeight = 0;
    if (browser.nn4) {
        leer.style.clip.top = clipHeight;
    } else {
        leer.style.clip = "rect(" + clipHeight + "px " + menu_list[item].width + "px " + layerHeight + "px 0px)";
    }
    leer.move(menu_list[item].xpos, menu_list[item].ypos - clipHeight);
    if (clipHeight > 0){
        timer = setTimeout('recMoveLayer("' + item + '", ' + step + ', ' + iter + ')', 10);
    }
}

function out(item) {
    menu_over = false;
    if (subnav_supported && (timeout == 0)) timeout = setTimeout("deactivate()", deactivate_timeout);
}

function item_light_on(item) {
    var id = item_list[item].id;
    if (browser.dom || browser.ie4) {
        layerRef('item_div_' + id).style.backgroundColor = bg_color_on;
        layerRef('item_link_' + id).style.color = color_on;
        document.images['item_image_'+item].src = '/img/arrow_white.gif';
    }
}

function item_light_off(item) {
    var id = item_list[item].id;
    if (browser.dom || browser.ie4) {
        layerRef('item_div_' + id).style.backgroundColor = bg_color_off;
        layerRef('item_link_' + id).style.color = color_off;
        document.images['item_image_'+item].src = '/img/arrow_gray.gif';
    }
}

function item_deactivate() {
    light_timeout = 0;
    if (active_item != null) {
        item_light_off(active_item);
        active_item = null;
    }   
}

function item_over(item) {
    if (light_timeout != 0) { clearTimeout(light_timeout); light_timeout = 0; }
    if (active_item != null && active_item != item) item_light_off(active_item);
    active_item = item;
    item_light_on(item);
}

function item_out(item) {
    light_timeout = setTimeout("item_deactivate()", light_off_timeout);
}

function inArea(checkX, checkY, areaX, areaY, areaW, areaH) {
    return ((checkX >= areaX) && (checkY >= areaY) && (checkX < areaX + areaW) && (checkY < areaY + areaH));
}

function mouseMove(e) {
    var x = (browser.netscape)? e.pageX : event.clientX;
    var y = (browser.netscape)? e.pageY : event.clientY;
    if ((browser.explorer) && (document.body.scrollTop)) y += document.body.scrollTop;
    if ((browser.explorer) && (document.body.scrollLeft)) x += document.body.scrollLeft;
    if (active) {
        if (inArea(x, y, active.xpos, active.ypos, active.width, active.height)) {
            if (timeout != 0) { clearTimeout(timeout); timeout = 0; }
        } else {
            if ((!menu_over) && (timeout == 0)) { timeout = setTimeout("deactivate()", deactivate_timeout); }
        }
    }
    return true;
}

function init() {
    for (var i = 0; i < menu_list.length; i++) document.writeln(menu_list[i].skin());
}

function activate() {
    for (var i = 0; i < menu_list.length; i++) {
        menu_list[i].layer = new Layer('menu_' + menu_list[i].id);
    }
    if (browser.netscape) document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = mouseMove;
    subnav_supported = (browser.dom || browser.nn4 || browser.ie4);
}

function changeBg(objName, color){
    if(document.getElementById){
        obj = document.getElementById(objName)
        obj.style.backgroundColor=color;
    }
}

function changeColor(objName, color){
    if(document.getElementById){
        obj = document.getElementById(objName)
        obj.style.color=color;
    }
}