var sub_close = 0;
var show_timer = 0;
var sub_window_xy = {"width": 0, "height":0};
var first_sub_element = "";
function prepareLeftMenu(){
    var ul_obj = document.getElementById("nav");
    if(ul_obj){
        var found_all_li = ul_obj.getElementsByTagName("LI");
        if(found_all_li.length > 0){
            for(i=0; i<found_all_li.length; i++){
                found_all_li[i].onmouseout = function(){
                    destroyWindow();
                }
            }
        }
        var found_sub_divs = ul_obj.getElementsByTagName("DIV");
        if(found_sub_divs.length > 0){
            for(i=0; i<found_sub_divs.length; i++){
                if(trimString(found_sub_divs[i].className) == "sub"){
                    found_sub_divs[i].parentNode.onmouseover = function(){
                        createSubWindow(this);
                    }
                    found_sub_divs[i].parentNode.onmouseout = function(){
                        closeSubWindow(this);
                    }                    
                }
            }
        } 
    }   
}
function closeSubWindow(obj){
    if(obj){
        sub_close = setTimeout("destroyWindow()",200);
    }
}
function destroyWindow(){
    clearTimeout(sub_close);
    clearInterval(show_timer);
    var dest_window = document.getElementById("left_sub_menu_holder");
    if(dest_window){
        document.body.removeChild(dest_window); 
    }
}
function createSubWindow(obj){
    clearTimeout(sub_close);
    var hold_window = document.getElementById("left_sub_menu_holder");
    if(hold_window && first_sub_element != obj){destroyWindow();}
    first_sub_element = obj;
    if(obj && !hold_window){
        var pos = getElementPosition(obj);
        if(pos){
            var free_sub_div = document.createElement("DIV");
            free_sub_div.id = "left_sub_menu_holder";
            free_sub_div.style.left = pos.left + (pos.width + 7) + "px";
            free_sub_div.style.top = (pos.top - 1) + "px";
            free_sub_div.onmouseover = function(){
                clearTimeout(sub_close);
            } 
            free_sub_div.onmouseout = function(){
                closeSubWindow(this);
            }                        
            free_sub_div.innerHTML = "<div class='separator'>&nbsp;</div>" + returnSubContent(obj);
            document.body.appendChild(free_sub_div);
            sub_window_xy.width = free_sub_div.offsetWidth;
            sub_window_xy.height = free_sub_div.offsetHeight;
            free_sub_div.style.width = "0px";
            free_sub_div.style.height = "0px";            
            free_sub_div.style.visibility = "visible"; 
            show_timer = setInterval("subWindowScroll()",5);
        }
    }
}
function subWindowScroll(){
    var hold_sub_window = document.getElementById("left_sub_menu_holder");
    if(hold_sub_window){
        if(hold_sub_window.offsetWidth + 9 < sub_window_xy.width){
            hold_sub_window.style.width = hold_sub_window.offsetWidth + 9 + "px";  
        }else{
            hold_sub_window.style.width = sub_window_xy.width + "px";
        }
        if(hold_sub_window.offsetHeight + 8 < sub_window_xy.height){
            hold_sub_window.style.height = hold_sub_window.offsetHeight + 8 + "px";   
        }else{
            hold_sub_window.style.height = sub_window_xy.height + "px";
        }
        if(parseInt(hold_sub_window.style.width) == sub_window_xy.width && parseInt(hold_sub_window.style.height) == sub_window_xy.height){
            clearInterval(show_timer);
            hold_sub_window.style.overflow = "visible";
        }          
    }
}
function returnSubContent(obj){
    var content = "";
    if(obj){
        var sub_div = obj.getElementsByTagName("DIV");
        if(sub_div.length > 0){
            for(i=0; i<sub_div.length; i++){
                if(trimString(sub_div[i].className) == "sub"){
                    content = sub_div[i].innerHTML;
                }
            }
        }         
    }
    return content;
}
function getElementPosition(elem){
    if(elem){
        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 trimString(sInString){
    return sInString.replace(/(^\s+)|(\s+$)/g, '');
}
prepareLeftMenu();
function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";  
 }  
}  
