var timer_id=null;
var opened=null;

function cancel_timer() {
    if (timer_id != null) clearTimeout(timer_id);
    timer_id=null;
}

function get_menu_item_classname(num) {
    return "menu_item_"+num;
}

function get_submenu_classname(num) {
    return "submenu_"+num;
}

function force_redraw(el) {
    var old_display = $(el).css('display')
    $(el).css('display', 'none');
    var tmp = $(el).width();
    $(el).css('display', old_display);
}


function on_mouse_over(params, menu_num) {
   
   cancel_timer();
   if (opened != null && opened != menu_num) {
       $('.'+get_submenu_classname(opened)).hide();
   }

   $('.'+get_submenu_classname(menu_num)).show();
   opened = menu_num;
   
}

function on_mouse_out(params, menu_num) {
   cancel_timer();
   timer_id = setTimeout("$('."+get_submenu_classname(menu_num)+"').hide(); opened=null;", 1000);      
}


$.fn.init_dropmenu = function(settings, menu_num, top) {
    
    $(this).addClass(get_submenu_classname(menu_num));

    var parent_pos = $('.' + get_menu_item_classname(menu_num)).position();    
    var left = parent_pos.left + settings.adjust_horiz;
        
    $(this).css('left', left+'px');
    $(this).css('top', top+'px');
    
    if (jQuery.browser.msie && jQuery.browser.version < 7 || jQuery.browser.opera) {    
                  
        $(this).children().mouseover(function(){            
            $(this).addClass('hover');
            force_redraw(this);
        });
            
        $(this).children().mouseout(function(){
            $(this).removeClass('hover');
            force_redraw(this);
        });
    } 



    $('.' + get_menu_item_classname(menu_num)).mouseover(function() {      
        on_mouse_over(settings, menu_num);   
    });
    
    $('.' + get_menu_item_classname(menu_num)).mouseout(function() {      
        on_mouse_out(settings, menu_num);   
    });
}


$.fn.dropmenu = function(settings) {
   
   var pos=$(this).offset();
   var top=$(this).height()+pos.top+settings.adjust_vert;   
   
      
   var menu_num = 0;
   $(this).children().each(function(){       
       $(this).addClass(get_menu_item_classname(menu_num));       
       $(this).children('ul').init_dropmenu(settings, menu_num, top);
       
       menu_num++;
       
   }); 

};

