/**
 * @file dhtml.menu_new.js
 * @date 2005.04.13
 * @auth tmv
 * @desc 
 */


function DHTMLMenu(id, d_id, l0_html, l0_item, l1_html, l1_left, l1_top, l1_time, l1_item, l2_html, l2_left, l2_top, l2_time, l2_item, icon, icon_w_sub, icon_sub, icon_sub_w_sub, icon_sub2, show_up) {
    this.dhtml_id  = id;
    this.div_id = d_id;

    this.level0_item = l0_item;
    this.level0_html = l0_html;

    this.level1_html = l1_html;
    this.level1_left = l1_left;
    this.level1_top = l1_top;
    this.level1_mtime = l1_time;
    this.level1_item = l1_item;

    this.level2_html = l2_html;
    this.level2_left = l2_left;
    this.level2_top = l2_top;
    this.level2_mtime = l2_time;
    this.level2_item = l2_item;

    this.icon = icon;
    this.icon_w_sub = icon_w_sub;
    this.icon_sub = icon_sub;
    this.icon_sub_w_sub = icon_sub_w_sub;
    this.icon_sub2 = icon_sub2;

    this.dhtml_nodes_array = new Array();
    this.DHTML_timeout = null;
    this.DHTML_timeout1 = null;
    this.DHTML_active_level1 = null;
    this.DHTML_active_level2 = null;
    this.dhtml_level0_item = '';

    this.ihtml = ''; 
    this.submenu_divs = ''; 
    this.dhtml_levels_arr = new Array();

    this.DHTML_is_horizontal = '';
    this.DHTML_right_position = '';
    this.DHTML_show_menu_up = show_up;

    return this; 
}
/*
DHTMLMenu.prototype.DHTMLBuild = function () {
    this.dhtml_main_node = this.DHTMLCreateNode(0);
    this.MakeDHTMLLayer(this.dhtml_main_node, '');
}
*/

DHTMLMenu.prototype.MakeDHTMLLayer = function (parent_node, pm_id, pm_level) {
    //alert(1);
    this.dhtml_levels_arr[0] = new Array();
    for(var c = 1; c < parent_node.length; c++) {
        var dhtml_child_node = parent_node[c];
        var child_lng = dhtml_child_node.length;
        var onm = '';
        var dhtml_icon = this.icon;
        dhtml_link = '<img src=\"'+px_src+'\" height=\"0\" border=\"3\" width=\"0\" alt=\"\"  id=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"top\" style=\"height:0px; width:1px;border:0px solid #000000;\">';

        dhtml_link += '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'onmouseover=\"'+this.dhtml_id+'.ShowSubMenu('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+');\">'+dhtml_child_node[0]['text']+'</a>';
        dhtml_link2 = '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'onmouseover=\"'+this.dhtml_id+'.ShowSubMenu('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+');\">';
        dhtml_link += '<img src=\"'+px_src+'\" height=\"0\" border=\"0\" width=\"0\" alt=\"\"  id=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"middle\" style=\"height:0px;width:1px;border:0px solid #000000;\">';
        if(child_lng > 1) {
            dhtml_icon = this.icon_w_sub;
        }
        var add_level0_item = this.level0_item.replace('_dhtml_icon_', dhtml_link2 + dhtml_icon + '</a>');
//alert(add_level0_item);
        this.dhtml_level0_item += add_level0_item.replace('_item_list_', dhtml_link);

        this.dhtml_levels_arr[0][this.dhtml_levels_arr[0].length] = dhtml_child_node[0]['menu_id'];
        if(child_lng > 1) {
            this.submenu_divs += this.MakeDHTMLSubLayer(dhtml_child_node, dhtml_child_node[0]['menu_id'], dhtml_child_node[0]['level']); 
        }
    }
    //alert(document.getElementById(this.div_id));
    //document.getElementById(this.div_id).innerText = this.submenu_divs;
    //alert(this.dhtml_levels_arr[0])
    //alert(2);
}

DHTMLMenu.prototype.MakeDHTMLSubLayer = function (parent_node, pm_id, pm_level) {
    var div_item = '';
    var level2_divs = '';
    this.dhtml_levels_arr[pm_id] = new Array();
    for(var i = 1; i < parent_node.length; i++) {
        var dhtml_child_node = parent_node[i];
        var dhtml_icon = this.icon_sub;
        dhtml_link = '<img src=\"'+px_src+'\" height=\"0\" border=\"3\" width=\"0\" alt=\"\" id=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"top\" style=\"height:0px; width:1px;border:0px solid #000000;\">';
        
        dhtml_link += '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'  onmouseover=\"'+this.dhtml_id+'.ShowSubMenu2('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu2('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\">'+dhtml_child_node[0]['text']+'</a>';
        dhtml_link2 = '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'  onmouseover=\"'+this.dhtml_id+'.ShowSubMenu2('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu2('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\">';

        dhtml_link += '<img src=\"'+px_src+'\" height=\"0\" border=\"0\" width=\"0\" alt=\"\"  id=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"middle\" style=\"height:1px; width:1px;border:0px solid #000000;\">';

        if(dhtml_child_node.length > 1) {
            dhtml_icon = this.icon_sub_w_sub;
        }
        var add_level1_item = this.level1_item.replace('_dhtml_icon_', dhtml_link2 + dhtml_icon + '</a>');

       	div_item +=  add_level1_item.replace('_item_list_', dhtml_link);
        this.dhtml_levels_arr[pm_id][this.dhtml_levels_arr[pm_id].length] =  dhtml_child_node[0]['menu_id'];

        if(dhtml_child_node.length > 1) {
            level2_divs += this.MakeDHTMLSub2Layer(dhtml_child_node, dhtml_child_node[0]['menu_id'], dhtml_child_node[0]['level']); 
        }
    }
    div_item = '<div style=\"display:none;position:absolute;\" id=\"dhtml_div_'+pm_id+'\">'+this.level1_html.replace('_dhtml_item_', div_item)+'</div>';
    return div_item+level2_divs;
}

DHTMLMenu.prototype.MakeDHTMLSub2Layer = function (parent_node, pm_id, pm_level) {
    var div_item = '';
    var dhtml_icon = ''
    for(var z = 1; z < parent_node.length; z++) {
        var dhtml_child_node = parent_node[z];
        dhtml_link = '<img src=\"'+px_src+'\" height=\"0\" border=\"3\" width=\"0\" alt=\"\" id=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_l_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"top\" style=\"height:0px; width:1px;border:0px solid #000000;\">';
        
        dhtml_link += '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'  onmouseover=\"'+this.dhtml_id+'.ShowSubMenu3( '+pm_id+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu3('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\">'+dhtml_child_node[0]['text']+'</a>';
        dhtml_link2 = '<a href=\"'+dhtml_child_node[0]['url']+'\" '+dhtml_child_node[0]['m_class']+' '+dhtml_child_node[0]['target']+'  onmouseover=\"'+this.dhtml_id+'.ShowSubMenu3( '+pm_id+');\" onmouseout=\"'+this.dhtml_id+'.HideSubMenu3('+dhtml_child_node[0]['menu_id']+', '+dhtml_child_node[0]['level']+', '+pm_id+');\">';

        dhtml_link += '<img src=\"/'+px_src+'\" height=\"0\" border=\"0\" width=\"0\" alt=\"\"  id=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" name=\"dhtml_r_'+dhtml_child_node[0]['menu_id']+'\" hspace=\"0\" vspace=\"0\" align=\"middle\" style=\"height:0px; width:1px;\">';

        dhtml_icon = this.icon_sub2;
        var add_level2_item = this.level2_item.replace('_dhtml_icon_', dhtml_link2 + dhtml_icon + '</a>');
       	div_item +=  add_level2_item.replace('_item_list_', dhtml_link);
    }
    div_item = '<div style=\"display:none;position:absolute;\" id=\"dhtml_div_'+pm_id+'\">'+this.level2_html.replace('_dhtml_item_', div_item)+'</div>';
    return div_item;
}

DHTMLMenu.prototype.WriteSubMenu = function () {
    document.write(this.submenu_divs);
    this.DHTML_is_horizontal = this.CheckIsHorizontalMenu(this.dhtml_levels_arr[0]);
    this.DHTML_right_position = this.GetRightPosition(this.dhtml_levels_arr[0]);
}

DHTMLMenu.prototype.ShowSubMenu = function (div_id, level) {
    //alert(1);
    clearTimeout(this.DHTML_timeout);
   
    this.DHTMLHide(''); //uzdarom visus 

    clearTimeout(select_timeout);
    ShowSelectTag();
    if(document.getElementById('dhtml_div_'+div_id) != null) {
        document.getElementById('dhtml_div_'+div_id).style.display = 'block';
        document.getElementById('dhtml_div_'+div_id).style.left = this.GetLeftPosition(div_id, this.DHTML_right_position, 1, this.DHTML_is_horizontal, this.level1_left) + this.level1_left;
        document.getElementById('dhtml_div_'+div_id).style.top = this.GetTopPosition(div_id, this.DHTML_is_horizontal) + this.level1_top;
        var sel_obj = document.getElementById('dhtml_div_'+div_id);
        HideSelectTag(sel_obj);
    }
    this.DHTML_active_level1 = div_id;
}

DHTMLMenu.prototype.DHTMLWriteLayers = function () {
    //alert(this.dhtml_level0_item);
    document.getElementById(this.div_id).innerHTML = this.level0_html.replace('_dhtml_item_', this.dhtml_level0_item);
}

DHTMLMenu.prototype.ShowSubMenu2 = function (div_id, level, p_id) {
    clearTimeout(this.DHTML_timeout);
    clearTimeout(this.DHTML_timeout1);
    //paslepiam paskutini atidaryta
    this.DHTMLHide(this.DHTML_active_level2);
    clearTimeout(select_timeout); 
	//2007.02.19 Stepas: uzkomentavau, kadangi atidarant nauja meniu lygi vis tiek nereikia,
	//kad selectai IE6 matytusi (uzlipa ant dinaminio meniu virsaus)
    //ShowSelectTag(); //isorine f-ja --> ???

    //alert(div_id);
    //alert(GetRightPosition(dhtml_levels_arr[1]));
    if(document.getElementById('dhtml_div_'+div_id) != null) {
        document.getElementById('dhtml_div_'+div_id).style.display = 'block';
        document.getElementById('dhtml_div_'+div_id).style.left = this.GetLeftPosition(div_id, this.GetRightPosition(this.dhtml_levels_arr[p_id]), 2, this.DHTML_is_horizontal, this.level2_left) + this.level2_left;
        document.getElementById('dhtml_div_'+div_id).style.top = this.GetTopPosition(div_id, this.DHTML_is_horizontal) + this.level2_top;
        var sel_obj = document.getElementById('dhtml_div_'+div_id);
        HideSelectTag(sel_obj);//slepiam select tagus
    }
    else {
        clearTimeout(select_timeout);
        var sel_obj = document.getElementById('dhtml_div_'+p_id);
        HideSelectTag(sel_obj);
    }
    //DHTML_active_menu[DHTML_active_menu.length] = div_id;
    this.DHTML_active_level2 = div_id;
}

DHTMLMenu.prototype.DHTMLTryToHide = function () {
    clearTimeout(this.DHTML_timeout);
    this.DHTML_timeout = setTimeout(this.dhtml_id+'.DHTMLHide('+this.DHTML_active_level1+')', this.level1_mtime);
    clearTimeout(this.DHTML_timeout1);
    this.DHTML_timeout1 = setTimeout(this.dhtml_id+'.DHTMLHide('+this.DHTML_active_level2+')', this.level2_mtime);
    select_timeout = setTimeout('ShowSelectTag()', this.level1_mtime);
}

DHTMLMenu.prototype.DHTMLNodeData = function (index, level, p_id, id, text, url, m_class, target) {
    this.index = index;
    this.level = level;
    this.parent_id = p_id;
    this.menu_id = id;
    this.text = text;
    this.url = url;
    this.target = target;
    this.m_class = m_class;
}

DHTMLMenu.prototype.DHTMLCreateNode = function (level, p_id, id, text, url, m_class, target) {
    var dhtml_new_node = new Array();
    var nodes_index = this.dhtml_nodes_array.length;
    this.dhtml_nodes_array[nodes_index] = dhtml_new_node;
    dhtml_new_node[0] = new this.DHTMLNodeData(nodes_index, level, p_id, id, text, url, m_class, target);
    return dhtml_new_node;
}

DHTMLMenu.prototype.DHTMLAddChild = function (parent_node, child_node) {
    parent_node[parent_node.length] = child_node;
    return child_node;
}

DHTMLMenu.prototype.ns = function (element1, element2) {
    return (element2 - element1);
}

DHTMLMenu.prototype.DHTMLHide = function (id) {
    if(id == '') {
        if(document.getElementById('dhtml_div_'+this.DHTML_active_level1) != null)
            document.getElementById('dhtml_div_'+this.DHTML_active_level1).style.display = 'none';
        if(document.getElementById('dhtml_div_'+this.DHTML_active_level2) != null)
            document.getElementById('dhtml_div_'+this.DHTML_active_level2).style.display = 'none';
    }
    else {
        if(document.getElementById('dhtml_div_'+id) != null)
            document.getElementById('dhtml_div_'+id).style.display = 'none';
    }
}

DHTMLMenu.prototype.GetTopPosition = function (id, is_horizontal) {
    //jeigu reikia kad tilptu i ekrana 
    //var clienth = document.body.clientHeight + document.body.scrollTop;
    //var dh = document.getElementById('dhtml_div_'+id).offsetHeight; 
    if(is_horizontal == 0) {
        //top pozicija imu is left paveiksliuko nes jis tiksliau nustato
        var top_pos = ImT('dhtml_l_'+id);
    }
    else {
        if(this.DHTML_show_menu_up == 'yes') { //rodom meniu i virsu 
            var top_pos = ImT('dhtml_r_'+id) - document.getElementById('dhtml_div_'+id).offsetHeight;
        }
        else {
            var top_pos = ImT('dhtml_r_'+id);
        }
    }
    //if((dh + top_pos) > clienth) {
        //alert('('+dh+' + '+top_pos+')');
        //alert(document.body.scrollTop);
    //    top_pos = clienth - dh;//(dh + top_pos);
        //alert(top_pos
    //}
    //return ImT('dhtml_r_'+id);
    return top_pos;
}

DHTMLMenu.prototype.GetLeftPosition = function (id, right_pos, level, is_horizontal, u_l) {
    //alert(DHTML_is_horizontal);
    if(is_horizontal == 0 || level == 2) {
        var clientw = document.body.clientWidth + document.body.scrollLeft;
        var dw = document.getElementById('dhtml_div_'+id).offsetWidth;
        if((dw + right_pos+u_l) > clientw) {
            right_pos = clientw - dw - u_l - 10;
        }
    }
    else {
        right_pos = ImL('dhtml_l_'+id);
    }
    return right_pos;    
}

DHTMLMenu.prototype.HideSubMenu = function () {
    this.DHTMLTryToHide();
}

DHTMLMenu.prototype.HideSubMenu2 = function () {
    this.DHTMLTryToHide();
    //DHTMLTryToHide(DHTML_active_menu);
}

DHTMLMenu.prototype.ShowSubMenu3 = function (p_id) {
    clearTimeout(this.DHTML_timeout);
    clearTimeout(this.DHTML_timeout1);
    clearTimeout(select_timeout);
    var sel_obj = document.getElementById('dhtml_div_'+p_id);
    HideSelectTag(sel_obj);
}

DHTMLMenu.prototype.HideSubMenu3 = function() {
    //alert(1);
    this.DHTMLTryToHide();
}

DHTMLMenu.prototype.GetRightPosition = function (arr) {
    if(typeof arr != 'undefined') {
        /*var l = arr.length;
    //    alert(arr);
        var pos_arr = new Array();
        for(i=0; i<l; i++) {
            //alert(arr[i]);
            pos_arr[i] = ImL('dhtml_l_'+arr[i]);
        }
        pos_arr.sort(ns);
        //alert(pos_arr);
        return pos_arr[0];
        */
        return ImL('dhtml_l_'+arr[0]);
    }
    else 
        return 0;
}

DHTMLMenu.prototype.CheckIsHorizontalMenu = function (arr) {
    var l = arr.length;
//    alert(arr);
    var pos_arr = new Array();
    var is_top = 0;
    for(i=0; i<l; i++) {
        //alert(arr[i]);
      	pos_arr[i] = ImT('dhtml_r_'+arr[i]);
        if(i > 0 && ImT('dhtml_r_'+arr[i]) == ImT('dhtml_r_'+arr[(i - 1)]))
            is_top++;
    }
//    alert(is_top);
    return is_top;
}
