//================================================
//
//  Slavon.net menu v0,7b "write" lib
//
//   menu_x - x of "place_menu"
//   menu_y - y of "place_menu"
//   temp_menu - menu array by id (ex: menu[i].pid = 2)
//   index_menu - indexes between menu[] and temp_menu
//   menu_gen - menu (ID) already generated
//
//////////////////////////////////////////////////
//
//   TO DO:
//   1. menu[id].ori have 0,2 ; need 1,3
//   2. menu[id].type - horizontal, vertical
//   3. cellpadding - ok; cellspacing - need check
//   4. Optimization :)
//
//
//================================================



function write_menu_l1(ox, oy){
	if (!isDOM) return true;
menu_x = ox;
menu_y = oy;
write_menu_custom(0);
}
function write_menu_other(){
	if (!isDOM) return true;
}
function write_menu_custom(id){
browserName = navigator.appName;
var vvv=0;
if (browserName == "Microsoft Internet Explorer") {
	vvv=15;
}
	if (!isDOM) return true;
	var temp_menu = new Array();
	var index_menu = new Array();
	var str = '';


	for(i=0; i<menu_gen.length; i++)
		if (menu_gen[i] == id)
			return true;
	
    
	for(i=1; i<menu.length; i++)
		if (menu[i].pid == id){
			temp_menu[(temp_menu.length?temp_menu.length:0)] = menu[i];
			index_menu[(index_menu.length?index_menu.length:0)] = i;
		}
	if (temp_menu.length){
		if (menu_gen.length && menu[id].pid == 0){
			xx = menu[id].x;
			yy = menu[id].y + menu[id].h;
		}
		if (menu_gen.length && menu[id].pid != 0){
			xx = menu[id].x + menu[id].w-vvv;
			yy = menu[id].y;
		}
		 
		
		                                                
if (!menu_gen.length){
    //<td><DIV style="width: 26px; height: 40px;"><SPACER type="block" width="26" height="40"></SPACER></DIV></td><td><DIV style="width: 26px; height: 40px;"><SPACER type="block" width="26" height="40"></SPACER></DIV></td>
    //<a href="#"><img src="/common/i/sitemap.png"></a>&nbsp;<a href="#"><img src="/common/i/sitemail.png"></a>
    //<ul><li class="icons"><a href="#"><img src="/common/i/sitemap.png"></a></li></ul>
    str = '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td></td><td><DIV style="width: 26px; height: 40px;"><SPACER type="block" width="26" height="40"></SPACER></DIV></td><td width="991"></td><td nowrap="1"><ul><li class="icons"><a href="#"><img src="/common/i/sitemap.png"></a></li><li class="icons"><a href="#"><img src="/common/i/sitemail.png"></a></li></ul></td>';
	for(i=0; i<temp_menu.length; i++){
    	if (i==0) {
			str +='<td onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" valign="bottom" align="right" nowrap class="table-o"><img src="/common/img/sp.gif" alt="" width="8" height="19" border="0" vspace="7" align="absmiddle"><a href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a><img src="/common/img/sp.gif" alt="" width="8" height="19" border="0" align="absmiddle" vspace="7"></td>';
		}else{
			str +='<td onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" valign="bottom" align="right" nowrap class="table-o"><img src="/common/img/line.gif" vspace="7" alt="" width="1" height="19" border="0" align="absmiddle" id="t_' + index_menu[i] + '" name="t_' + index_menu[i] + '"><img src="/common/img/sp.gif" alt="" width="8" vspace="7" height="19" border="0" align="absmiddle"><a href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a><img src="/common/img/sp.gif" alt="" width="8" height="1" border="0" align="absmiddle"></td>'; 
		}
	}
    str += '<td><DIV style="width: 14px;"><SPACER type="block" width="14"></SPACER></DIV></td><td></td></tr><tr><td width="50%"></td><td colspan="21" width="991"><table cellpadding="0" cellspacing="0" border="0" width="991"><tr><td><DIV style="width: 26px; height: 5px;"><SPACER type="block" width="26" height="5"></SPACER></DIV></td><td><DIV style="width: 965px; height: 5px;"><SPACER type="block" width="965" height="5"></SPACER></DIV></td></tr></table></td><td width="50%"></td></tr></table>';
}else{
	str = "<div id='a" + id + "' name='a" + id + "' style='width: " + (getRef("td_" + id).offsetWidth) + "px; position: absolute; left: " + xx + "px; top: " + yy + "px; visibility: visible; z-index: 999999;'>";

    str += '<table cellpadding="0" cellspacing="0" border="0" class="tab-b"><tr><td><DIV style="width: 0px; height: 15px;"><SPACER type="block" width="1" height="15"></SPACER></DIV></td></tr>';
	for(i=0; i<temp_menu.length; i++){
		str += '<tr><td style="padding: 0px 20px 5px 20px;" onclick="document.location=\'' + temp_menu[i].url + '\';" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" nowrap onmouseover=" td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();"><a href="'+ temp_menu[i].url + '" style="">'+ temp_menu[i].text +'</a></td></tr>';
	}
    str += '<tr><td><DIV style="width: 0px; height: 18px;"><SPACER type="block" width="1" height="18"></SPACER></DIV></td></tr></table>';
	str += "</div>";
}


		if(menu_gen.length){
			getRef('top-menu2').innerHTML += str;
		}else{
			getRef('top-menu').innerHTML = str;
		}
		for(i=0; i<temp_menu.length; i++){
			if (menu_gen.length){
				menu[index_menu[i]].x = getRef("a" + id).offsetLeft + getRef("td_" + index_menu[i]).offsetLeft;
				menu[index_menu[i]].y = getRef("a" + id).offsetTop + getRef("td_" + index_menu[i]).offsetTop;
			}else{
				//menu[index_menu[i]].x = menu_x + getRef("top-menu").offsetLeft + getRef("td_" + index_menu[i]).offsetLeft + 0;
				//menu[index_menu[i]].y = menu_y + getRef("top-menu").offsetTop + getRef("td_" + index_menu[i]).offsetTop + 0;
                menu[index_menu[i]].x = menu_x + getElementPosition("td_" + index_menu[i]).left;
                menu[index_menu[i]].y = menu_y + getElementPosition("td_" + index_menu[i]).top; 
			}
			menu[index_menu[i]].h = getRef("td_" + index_menu[i]).offsetHeight;
			menu[index_menu[i]].w = getRef("td_" + index_menu[i]).offsetWidth;
        }
		menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
	}

}

function clear_all(){
	if (!isDOM) return true;
menu_show = new Array();
menu_gen = new Array();
menu_id = "";
menu_status = 0;
getRef('top-menu2').innerHTML = "";
getRef('top-menu').innerHTML = "";
write_menu_l2(menu_x, menu_y);
}

function write_menu_l2(ox, oy){
    if (!isDOM) return true;
menu_x = ox;
menu_y = oy;
write_menu_custom2(0);
}
function write_menu_other(){
    if (!isDOM) return true;
}

function getElementPosition(elemId){
    var elem = document.getElementById(elemId);
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
    while (elem)    
    {        
        l += elem.offsetLeft;        
        t += elem.offsetTop;        
        elem = elem.offsetParent;    
    }    
return {"left":l, "top":t, "width": w, "height":h};
}

function write_menu_custom2(id){
browserName = navigator.appName;
var vvv=0;
if (browserName == "Microsoft Internet Explorer") {
    vvv=0;
}
    if (!isDOM) return true;
    var temp_menu = new Array();
    var index_menu = new Array();
    var str = '';


    for(i=0; i<menu_gen.length; i++)
        if (menu_gen[i] == id)
            return true;
    
    
    for(i=1; i<menu.length; i++)
        if (menu[i].pid == id){
            temp_menu[(temp_menu.length?temp_menu.length:0)] = menu[i];
            index_menu[(index_menu.length?index_menu.length:0)] = i;
        }
    if (temp_menu.length){
        if (menu_gen.length && menu[id].pid == 0){
            xx = menu[id].x;
            yy = menu[id].y + menu[id].h;
        }
        if (menu_gen.length && menu[id].pid != 0){
            xx = menu[id].x + menu[id].w-vvv;
            yy = menu[id].y;
        }
         

if (!menu_gen.length){
    str = '<ul id="dummy"><li class="icons"><a href="#"><img src="/common/i/sitemap.png"></a></li><li class="icons"><a href="#"><img src="/common/i/sitemail.png"></a></li>';
    for(i=0; i<temp_menu.length; i++){
        if (i==0) {
            //str +='<td onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" valign="bottom" align="right" nowrap class="table-o"><img src="/common/img/sp.gif" alt="" width="8" height="19" border="0" vspace="7" align="absmiddle"><a href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a><img src="/common/img/sp.gif" alt="" width="8" height="19" border="0" align="absmiddle" vspace="7"></td>';
            str +='<li onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '"><a style="text-decoration: underline;" href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a></li>';
        }else{
            str +='<span class="razd">|</span><li onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '"><a style="text-decoration: underline;" href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a></li>';
            //str +='<span class="razd">|</span><li><div onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" class="xxx"><a style="text-decoration: underline;" href="'+ temp_menu[i].url + '">'+ temp_menu[i].text +'</a></div></li>';
        }
    }
    str += '</ul>';
}else{
    str = "<div id='a" + id + "' name='a" + id + "' style='width: " + (getRef("td_" + id).offsetWidth) + "px; position: absolute; left: " + xx + "px; top: " + yy + "px; visibility: visible; z-index: 999999;'>";
    
    str += '<table cellpadding="0" cellspacing="0" border="0" class="tab-b"><tr><td><DIV style="width: 0px; height: 15px;"><SPACER type="block" width="1" height="15"></SPACER></DIV></td></tr>';
    for(i=0; i<temp_menu.length; i++){
        str += '<tr><td style="padding: 0px 20px 5px 20px; text-align: left;" onclick="document.location=\'' + temp_menu[i].url + '\';" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" nowrap onmouseover=" td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();"><a href="'+ temp_menu[i].url + '" style="">'+ temp_menu[i].text +'</a></td></tr>';
    }
    str += '<tr><td><DIV style="width: 0px; height: 18px;"><SPACER type="block" width="1" height="18"></SPACER></DIV></td></tr></table>';
    str += "</div>";
}


        if(menu_gen.length){
            getRef('top-menu2').innerHTML += str;
        }else{
            getRef('top-menu').innerHTML = str;
        }
        for(i=0; i<temp_menu.length; i++){
            if (menu_gen.length){
                menu[index_menu[i]].x = getRef("a" + id).offsetLeft + getRef("td_" + index_menu[i]).offsetLeft;
                menu[index_menu[i]].y = getRef("a" + id).offsetTop + getRef("td_" + index_menu[i]).offsetTop;
            }else{
                //alert(getElementPosition("td_" + index_menu[i]).left);
                //menu[index_menu[i]].x = menu_x + getRef("dummy").offsetLeft + getRef("td_" + index_menu[i]).offsetLeft + 0;
                menu[index_menu[i]].x = menu_x + getElementPosition("td_" + index_menu[i]).left;
                //menu[index_menu[i]].y = menu_y + getRef("dummy").offsetTop + getRef("td_" + index_menu[i]).offsetTop + 0;
                menu[index_menu[i]].y = menu_y + getElementPosition("td_" + index_menu[i]).top-1; 
                
            }
            menu[index_menu[i]].h = getRef("td_" + index_menu[i]).offsetHeight;
            menu[index_menu[i]].w = getRef("td_" + index_menu[i]).offsetWidth;
        }
        menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
    }
function clear_all(){
    if (!isDOM) return true;
menu_show = new Array();
menu_gen = new Array();
menu_id = "";
menu_status = 0;
getRef('top-menu2').innerHTML = "";
getRef('top-menu').innerHTML = "";
write_menu_l2(menu_x, menu_y);
}
}