var current_opened_main;
var current_opened_sub;

ERW_Menu = function(main_obj_id, submenu_obj_id) {

    //alert("Main:"+main_obj_id+"Sub:"+submenu_obj_id)

    var main_obj_id                         = main_obj_id;
    var submenu_obj_id                      = submenu_obj_id;

    var main_obj                            = document.getElementById(main_obj_id)
    var submenu_obj                         = document.getElementById(submenu_obj_id)

    var main_ahref                          = FindChildByTagname(main_obj,"A")



    document.body.appendChild(submenu_obj);

    submenu_obj.style.display               = 'none'
    submenu_obj.style.position              = 'absolute';
    submenu_obj.style.marginTop             = '3px';

    main_obj.onmouseover = function(){
        if(current_opened_main!=''){
            if(the_obj = document.getElementById(current_opened_main)){
                the_a = FindChildByTagname(the_obj,"A")
                the_a.className = ''
            }
        }
        if(current_opened_sub!=''){
            if(the_obj = document.getElementById(current_opened_sub)){
                the_obj.style.display = 'none'
            }
        }
        main_ahref.className = 'head_menu_A_hover'
        pos                                 = getAbsolutePos(main_ahref);
        submenu_obj.style.left              = pos.x + 'px'
        submenu_obj.style.top               = (pos.y +23) + 'px'
        submenu_obj.style.zIndex            = "10000";
        submenu_obj.style.display           = 'block'
        current_opened_main = main_obj.id;
        current_opened_sub = submenu_obj.id
    }
    main_obj.onmouseout = function(){
        main_ahref.className = ''
        submenu_obj.style.display = 'none'
        current_opened_main = ''
        current_opened_sub = ''
    }
    submenu_obj.onmouseover = function(){
        if(current_opened_main!=''){
            if(the_obj = document.getElementById(current_opened_main)){
                the_a = FindChildByTagname(the_obj,"A")
                the_a.className = ''
            }
        }
        if(current_opened_sub!=''){
            if(the_obj = document.getElementById(current_opened_sub)){
                the_obj.style.display = 'none'
            }
        }
        main_ahref.className = 'head_menu_A_hover'
        submenu_obj.style.display  = 'block'

        current_opened_main = main_obj.id;
        current_opened_sub = submenu_obj.id
    }
    submenu_obj.onmouseout = function(){
        main_ahref.className = ''
        submenu_obj.style.display  = 'none'

        current_opened_main = ''
        current_opened_sub = ''
    }
}


function Init(){
    if(menu = document.getElementById('ERW_DropDown')){
        i=0;
        current_item = menu.firstChild;
        while(current_item){
            //alert(current_item.id)
            if(current_item.nodeName=="LI"){
                // ciclo sugli LI principali
                if(the_ul   = FindChildByTagname(current_item,"UL")){
                    current_item.id = "erw_main_menu_"+i
                    the_ul.id = "erw_sub_menu_"+i
                    tmp_obj = new ERW_Menu(current_item.id, the_ul.id );
                    i++
                }
            }
            current_item = current_item.nextSibling
        }
    }else{
        alert("non trovo il menu")
    }
}

function FindChildByTagname(obj, tagname){
    my_item = obj.firstChild;
    while(my_item){
        if(my_item.nodeName==tagname){
            return my_item
        }
        my_item = my_item.nextSibling
    }
    return false;
}

function getAbsolutePos(el) {
    var scrollleft = 0, scrolltop = 0, tn = el.tagName.toUpperCase();

    if (Array_indexOf(['BODY', 'HTML'], tn) == -1) { // ?
        if (el.scrollLeft) {
            scrollleft = el.scrollLeft;
        }

        if (el.scrollTop) {
            scrolltop = el.scrollTop;
        }
    }

    var r = { x: el.offsetLeft - scrollleft, y: el.offsetTop - scrolltop };


    if (el.offsetParent && tn != 'BODY') {
        var tmp = getAbsolutePos(el.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }

    return r;
}

function Array_indexOf(_this, x) {
    for (var i=0; i<_this.length; i++) {
        if (_this[i] == x) {
            return i;
        }
    }
    return -1;
}
function getBox(el) {
    var box = {
        "x": 0, "y": 0,
        "width": 0, "height": 0,
        "scrollTop": 0, "scrollLeft": 0
    };

    if (el.ownerDocument.getBoxObjectFor) {
        var rect = el.ownerDocument.getBoxObjectFor(el);
        box.x = rect.x - el.parentNode.scrollLeft;
        box.y = rect.y - el.parentNode.scrollTop;
        box.width = rect.width;
        box.height = rect.height;
        box.scrollLeft = el.ownerDocument.body.scrollLeft;
        box.scrollTop = el.ownerDocument.body.scrollTop;
    } else {
        var rect = el.getBoundingClientRect();
        box.x = rect.left;
        box.y = rect.top;
        box.width = rect.right - rect.left;
        box.height = rect.bottom - rect.top;
        box.scrollLeft = 0; //el.document.body.scrollLeft;
        box.scrollTop = 0;  //el.document.body.scrollTop;
    }
    return box;
}
