////////////////////////////////////////////////////////////////////////////////////////////////////
//dd_h v2.1
//Author: Alexey V. Golovkov, golovkov@artics.ru, 2003
////////////////////////////////////////////////////////////////////////////////////////////////////

//dropdown objects array
var ddArr = new Array();
var ddRoot = new Array();
//check browser object
////////////////////////////////////////////////////////////////////////////////////////////////////
function b_check()
{
    this.app = navigator.userAgent.toLowerCase();
    this.ie = (this.app.indexOf("msie") > -1) ? true : false;
    this.opera = window.opera;
    this.moz = (!this.ie && document.getElementById) ? true : false;
    this.nn4 = (!this.nn6 && document.layers) ? true : false;
    return this;
}
//making check browser object
var bros = new b_check();
////////////////////////////////////////////////////////////////////////////////////////////////////

//creating virtual table of objects
////////////////////////////////////////////////////////////////////////////////////////////////////
//making virtual table of child objects
var vchild = new Object();
//making virtual table of parent objects
var vparent = new Object();
//making object of opened dropdowns
var opened = new Object();
var allClosed = false;
////////////////////////////////////////////////////////////////////////////////////////////////////

//common global functions
////////////////////////////////////////////////////////////////////////////////////////////////////
//getting left coord
function dd_getRelLeft(nnlwhich, lwhich, lmodifier)
{
    liw=lwhich;
    if (bros.nn4)
    {
        if (nnlwhich != "") return eval(document.layers[nnlwhich].document.layers[liw].pageX+lmodifier);
        else return eval(document.layers[liw].pageX+lmodifier);
    }
    if (bros.moz || bros.opera || bros.ie)
    {
        if (bros.ie) el = document.all[liw];
        else el = document.getElementById(liw);
        var t = el.offsetLeft;
        while (el.offsetParent != null)
        {
            t += el.offsetParent.offsetLeft;
            if (el.offsetParent.tagName == 'BODY') break;
            el = el.offsetParent;
        }
        
        return eval(t+lmodifier);
    }
}

//getting top coord
function dd_getRelTop(nnrwhich, rwhich, rmodifier)
{
    riw=rwhich;
    if (bros.nn4)
    {
        if (nnrwhich != "") return eval(document.layers[nnrwhich].document.layers[riw].pageY+rmodifier);
        else return eval(document.layers[riw].pageY+rmodifier);
    }
    if (bros.moz || bros.opera || bros.ie)
    {
        if (bros.ie) el = document.all[riw];
        else  el = document.getElementById(riw);
        var t = el.offsetTop;
        while (el.offsetParent != null)
        {
            t += el.offsetParent.offsetTop;
            if (el.offsetParent.tagName == 'BODY') break;
            el = el.offsetParent;
        }
        return eval(t+rmodifier);
    }
}

//moveing item
function moveObj(item_id, item_top, item_left)
{
    if (bros.nn4)
    {
        document.layers[item_id].top  = item_top;
        document.layers[item_id].left  = item_left;
    }
    if (bros.moz || bros.opera)
    {
        document.getElementById(item_id).style.top = item_top;
        document.getElementById(item_id).style.left = item_left;
    }
    if (bros.ie)
    {
        document.all[item_id].style.top = item_top;
        document.all[item_id].style.left = item_left;
    }
}


//writing all dropdown divs
function init_dd()
{
    if (ddArr.length > 0)
    {
        alls = "";
        for (i=0; i<ddArr.length; i++)
        {
            rep_string = eval(ddArr[i]+".point_string");
            tpl_string = eval(ddArr[i]+".global_tpl");
            alls += fillTemplate(tpl_string, "%POINT_STRING%", rep_string);
        }
        document.write(alls);
    }
}

//get all parents
function getParents(obj_id, root_id)
{
    parents = new Array();
    pid = "";
    while (vparent[obj_id] && vparent[obj_id] != "")
    {
        pid = vparent[obj_id];
        if (pid != root_id) parents[parents.length] = vparent[pid];
        obj_id = vparent[pid];
    }
    return parents;
}

//get all childs
function getChilds(obj_id)
{
    childs = new Array();
    getL(obj_id);
    return childs;
}

//get child branching
function getL(obj_id)
{
    for (var pid in vparent)
    {
        if (vparent[pid] == obj_id)
        {
            if (vchild[pid]) getC(pid);
        }
    }
}

//addig childs
function getC(point_id)
{
    childs[childs.length] = vchild[point_id];
    getL(vchild[point_id]);
}



//addig childs


//showing child dropdown
function showChild(obj_id, point_id)
{
    hidePrevChild(point_id)
    if (vchild[point_id])
    {
        eval(vchild[point_id]+".show('"+obj_id+"')");
    }
}

//hiding child dropdown
function hideChild(point_id)
{
    if (vchild[point_id])
    {
        eval(vchild[point_id]+".hide_delayed(1,0)");
    }
}

//hiding previous opened trees
function hidePrevChild(point_id)
{
    is_glob = false;
    curr_root = eval(vparent[point_id]+".root");
    if (curr_root == "")
    {
        curr_root = point_id;
        for (var root in opened)
        {
            if (root != curr_root)
            {
                for (var c_obj in opened[root])
                {
                    is_op = eval(opened[root][c_obj]);
                    if (is_op) eval(c_obj+".hide()");
                }
            }
        }
    } else
    {
        for (var c_par in vparent)
        {
            if (vparent[c_par] == vparent[point_id])
            {
                if (vchild[c_par] && opened[curr_root][vchild[c_par]])
                {
                    childArr = getChilds(vchild[c_par]);
                    if (childArr.length > 0)
                    {
                        for (k=0; k<childArr.length; k++)
                        {
                            if (opened[curr_root][childArr[k]]) eval(childArr[k]+".hide()");
                        }
                    }
                    eval(vchild[c_par]+".hide()");
                }
            }
        }
    }
}

//get points of one level
function getPoints(point_id)
{
    points = new Array();

    return points;
}

//fill template
function fillTemplate(tpl_string, pattern, rep_string)
{
    tpl = tpl_string.split(pattern);
    return tpl[0]+rep_string+tpl[1];
}
////////////////////////////////////////////////////////////////////////////////////////////////////

//root menu object
////////////////////////////////////////////////////////////////////////////////////////////////////
function DdMenu(id, point_width, global_template)
{
    this.id = id;
    this.root = "";
    this.dd_width = point_width;
    this.global_tpl = global_template;
    this.point_string = "";
    return this;
}

//adding points to root menu
DdMenu.prototype.add_point = function(point_id, point_title, point_link, point_target, point_width, point_class, point_template)
{
    pwid = (point_width != 0) ? point_width : this.dd_width;
    point_link = (point_link != "") ? point_link : "javascript:void(null)";
    point_target = (point_target != "") ? " target=\""+point_target+"\"" : "";
    point_class =  (point_class != "") ? " class=\""+point_class+"\"" : "";
    if (bros.nn4)
    {
        div1 = "<td"+((pwid != "") ? " width=\""+pwid+"\"" : "")+"><ilayer name=\""+point_id+"\">";
        div2 = "</ilayer></td>";
        rep_string = div1+"<a href=\""+point_link+"\""+point_target+point_class+" onMouseOver=\"showChild('','"+point_id+"')\" onMouseOut=\"hideChild('"+point_id+"')\">"+point_title+"</a>"+div2;
    } else
    {
        div1 = "<td"+((pwid != "") ? " width=\""+pwid+"\"" : "")+" id=\""+point_id+"\" onMouseOver=\"showChild('','"+point_id+"')\" onMouseOut=\"hideChild('"+point_id+"')\">";
        div2 = "</td>";
        rep_string = div1+"<a href=\""+point_link+"\""+point_target+point_class+">"+point_title+"</a>"+div2;
    }
    this.point_string += fillTemplate(point_template, "%LINK_STRING%", rep_string);
    vparent[point_id] = this.id;
    opened[point_id] = new Object();
}

//writing root menu
DdMenu.prototype.init = function()
{
    document.write(fillTemplate(this.global_tpl, "%POINT_STRING%", this.point_string));
}
////////////////////////////////////////////////////////////////////////////////////////////////////


//dropdown object
////////////////////////////////////////////////////////////////////////////////////////////////////
function dd(id, parent_id, parent_align, dd_width,  hide_delay, top_mod, left_mod, roll_img, on_img, off_img, global_template)
{
    this.id = id;
    this.dd_id = "dd_"+this.id;
    r2 = eval(vparent[parent_id]+".root");
    if (r2 != "") this.root = r2;
    else this.root = parent_id;
    this.do_hide = true;
    this.parent_id = parent_id;
    this.parent_align = parent_align;
    this.dd_width = dd_width;
    this.hide_delay = hide_delay;
    this.top_mod = top_mod;
    this.left_mod = left_mod;
    this.roll_img = roll_img;
    this.on_img = on_img;
    this.off_img = off_img;
    this.global_tpl = global_template;
    this.point_string = "";
    this.timeout = null;
    ddArr[ddArr.length] = this.id;
    ddRoot[ddRoot.length] = this.root;
    vchild[this.parent_id] = this.id;
    vparent[this.id] = this.parent_id;
    opened[this.root][this.id] = false;
    this.dd_parent = (this.parent_id != this.root) ? vparent[this.parent_id] : "";
    if (bros.nn4)
    {
        d1 = "<layer name=\""+this.dd_id+"\" top=\"-1000\" left=\"0\" width=\""+this.dd_width+"\" z-index=\""+(ddArr.length+2)+"\" onMouseOver=\""+this.id+".clear_tm();\" onMouseOut=\""+this.id+".unlock_dd() ;"+this.id+".hide_delayed(2,0)\">";
        d2 = "</layer>";
    } else
    {
        d1 = "<div id=\""+this.dd_id+"\" style=\"position:absolute; top:-1000;  left:0; width:"+this.dd_width+"; z-index:"+(ddArr.length+2)+";\" onMouseOver=\""+this.id+".clear_tm();allClosed=false;\" onMouseOut=\""+this.id+".unlock_dd(); allClosed=true;"+this.id+".hide_delayed(2,0)\">";
        d2 = "</div>";
    }
    this.global_tpl = d1+this.global_tpl+d2;
    return this;
}

//adding dropdown point
dd.prototype.add_point = function(point_id, point_title, point_link, point_target, point_class, point_width, point_template)
{
    point_link = (point_link != "") ? point_link : "javascript:void(null)";
    point_target = (point_target != "") ? " target=\""+point_target+"\"" : "";
    point_class =  (point_class != "") ? " class=\""+point_class+"\"" : "";
    point_width = (point_width != "") ? " width=\""+point_width+"\"" : "";
    if (bros.nn4)
    {
        div1 = "<td"+point_width+"><ilayer name=\""+point_id+"\">";
        div2 = "</ilayer></td>";
        rep_string = "<a href=\""+point_link+"\""+point_target+point_class+" onMouseOver=\"showChild('"+this.id+"','"+point_id+"'); "+this.id+".clear_tm()\" onMouseOut=\"hideChild('"+point_id+"')\">"+point_title+"</a>";
    } else
    {
        div1 = "<td"+point_width+" id=\""+point_id+"\" onMouseOver=\"showChild('"+this.id+"','"+point_id+"'); "+this.id+".clear_tm();\" onMouseOut=\"hideChild('"+point_id+"')\">";
        div2 = "</td>";
        rep_string = "<a href=\""+point_link+"\""+point_target+point_class+">"+point_title+"</a>";
    }

    this.point_string += fillTemplate(point_template, "%LINK_STRING%", rep_string);
    vparent[point_id] = this.id;
}

//showing dropdown
dd.prototype.show = function(parent_obj)
{
    if (this.timeout) clearTimeout(this.timeout);
    if (!opened[this.root][this.id])
    {
        if (parent_obj != "") pobj = eval(parent_obj+".dd_id");
        else pobj = "";
        if (this.parent_align == "bottom")
        {
            top_pos = dd_getRelTop(pobj, "tm1", this.top_mod);
            left_pos = dd_getRelLeft(pobj, "tm1", this.left_mod);
        } else if (this.parent_align == "right")
        {
            wid = parseInt(eval(parent_obj+".dd_width"));
            top_pos = dd_getRelTop(pobj, "tm1", this.top_mod);
            left_pos = dd_getRelLeft(pobj, "tm1", this.left_mod) + wid;
        } else if (this.parent_align == "left")
        {
            top_pos = dd_getRelTop(pobj, "tm1", this.top_mod);
            left_pos = parseInt(dd_getRelLeft(pobj, "tm1", this.left_mod)) - this.dd_width;
        }
        if (this.roll_img != "")
        {
            //if(bros.nn4) document.layers[tm1].document.images[this.roll_img].src = eval(this.on_img+".src");
            //else
            if (!bros.nn4) document.images[this.roll_img].src = eval(this.on_img+".src");
        }
        // lleft  =  this.getLeft('mainmenu');
        // ltop   =  this.getTop('mainmenu');
        
        moveObj(this.dd_id, top_pos+9, left_pos);
        opened[this.root][this.id] = true;
    }
}

//clearing timeout for avoiding delayed hide
dd.prototype.clear_tm = function()
{
    if (this.dd_parent != "") eval(this.dd_parent+".clear_tm()");
    clearTimeout(this.timeout);
    
    this.do_hide = false;
    
}

//unlocking dropdown for hiding
dd.prototype.unlock_dd = function()
{
    if (this.dd_parent != "") eval(this.dd_parent+".unlock_dd()");
    this.do_hide = true;
    
}

//hidiing all dropdown tree
dd.prototype.hide_tree = function()
{
    if (this.dd_parent != "")
    {
        eval(this.dd_parent+".hide_tree()");
    }
    if (opened[this.root][this.id] && this.do_hide)
    {
        if (this.roll_img != "")
        {
            //if(bros.nn4) document.layers[this.parent].document.images[this.roll_img].src = eval(this.off_img+".src");
            //else document.images[this.roll_img].src = eval(this.off_img+".src");
            if (!bros.nn4) document.images[this.roll_img].src = eval(this.off_img+".src");
        }
        moveObj(this.dd_id, "-1000", "0");
        opened[this.root][this.id] = false;
        
    }
}

//hiding current dropdown
dd.prototype.hide = function()
{
    if (opened[this.root][this.id] && this.do_hide)
    {
        if (!bros.nn4) document.images[this.roll_img].src = eval(this.off_img+".src");
        moveObj(this.dd_id, "-1000", "0");
        opened[this.root][this.id] = false;
        
        
    }
}

//hiding ddropdown with some delay
dd.prototype.hide_delayed = function(h_flag, not_show_default)
{
    if (opened[this.root][this.id] && this.do_hide)
    {
        if (this.timeout) {clearTimeout(this.timeout); } 
        if (h_flag != 1) {this.timeout = setTimeout(this.id+".hide_tree()", this.hide_delay);    }
        else {this.timeout = setTimeout(this.id+".hide()", this.hide_delay);
        //window.alert(allClosed);
          setTimeout("showDefault()", this.hide_delay);
           
        }
        
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////////

