﻿function jsddm(panelID) {
    var timeout = 500;
    var closetimer = 0;
    var lastmenuitem = 0;
    var exceptionList = new Array();

    function init() {
        bindMouseEvents();
        bindDocumentClick();
    }

    function bindMouseEvents() {
        $('#' + panelID + ' > li').bind('mouseover', open);
        $('#' + panelID + ' > li').bind('mouseout', timer);
    }

    function bindDocumentClick() {
        $(document).bind('click', close);
    }

    function open() {
        canceltimer();

        $this = $(this);
        if (lastmenuitem == 0 || lastmenuitem.get(0) != $this.get(0)) {
            close();
            $this.find('ul').css('visibility', 'visible');
            lastmenuitem = $this;            
        }
    }

    function close(event) {
        if (event == null || event.target == null || $.inArray(event.target.id, exceptionList) == -1) {
        
            if (lastmenuitem) {
                lastmenuitem.find('ul').css('visibility', 'hidden');
                lastmenuitem = 0;
            }
        }
    }

    function timer() {
        closetimer = window.setTimeout(close, timeout);
    }

    function canceltimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }

    function addExceptionList(id) {
        exceptionList.push(id);
    }

    this.Init = init;
    this.BindMouseEvents = bindMouseEvents;
    this.AddExceptionList = addExceptionList;
    return this;
}
