function bjmenu(el, trigger, options) {
    this.clickorhover='0'; 
    this.open=false;
    this.el = el;
    this.hide();
    this.trigger = trigger;
    this.options = options;
    this.trigger.style.zIndex='9999';
    this.getChildren();
    Event.observe(this.trigger, 'click', this.show_click.bindAsEventListener(this));
    Event.observe(document, 'click', this.hide_click.bindAsEventListener(this));
    Event.observe(this.trigger, 'mouseover', this.show_hover.bindAsEventListener(this));
    Event.observe(this.trigger, 'mouseout', this.hide_hover.bindAsEventListener(this));
}

bjmenu.prototype = {
    getChildren: function() {
        var items = this.el.getElementsByTagName("li");
        for (var i=0; i < items.length; i++) {
            this._initSubmenus(items[i]); 
        }
    },

    show: function(ev) {
    /*
        $(this.el).setStyle({cssFloat: 'left',
            display: 'block',
            cursor: 'pointer',
            position:'absolute'});
            */
        this.showhook();
        this.el.style.display = 'block';
        this.el.style.cursor = 'pointer';
        //this.el.parentNode.position='relative';
        //this.el.style.position='absolute';
        //console.log(this.clickorhover);
        //
        //commented out event prop block for Dave @ 15/02/2010
        //if (ev) Event.stop(ev);
        //
        //this.el.style.marginLeft="100px";
    },

    show_hover: function(ev) {
        if (this.options && this.options.onhover) {
            this.options.onhover.apply(this);
        }
        if (this.clickorhover == true) return false;
        this.clickorhover = false;
        this.show(ev);
    },

    show_click: function(ev) {
        if (this.open) { 
            this.hide(ev);
            return false;
        } else {
            this.open=true;
        }
//        this.clickorhover = true;
        this.clickorhover = false;
        this.show(ev);
    },

    hide_click: function(ev) {
        if (Event.element(ev).nodeName == "A") return;
        this.hide(ev);
    },

    hide_hover: function(ev) {
        if (!this.clickorhover) {
            this.hide(ev);
        }
    },

    hide: function(ev) {
        this.el.style.display = "none";
        this.clickorhover = null;
        this.open=false;
        if (this.options) {
            if (this.options.dissapearonclose) {
                if (this.trigger) {
                    this.trigger.style.display = 'none';
                }
            }
        }
    },

    showhook: function() {
        this.trigger.style.display = 'block';
    },

    hidehook: function() {
        this.trigger.style.display = 'none';
        this.hide();
    },

    _initSubmenus: function(p_el) {
        var subs = p_el.getElementsByTagName("ul"); 
        for (var i=0; i < subs.length; i++) {
            new bjmenu(subs[i], p_el);
        }
    }
};
