var Menuizer = Class.create();

Object.extend(Menuizer.prototype, {

    initialize: function(defaultTab) {
        this.DELAY = 300;
        this.theDefault = $$('.menu.on').length ? $$('.menu.on')[0].id : defaultTab;
        this.current = this.theDefault;
        this.lastEvent = 0;
//        console.log("default: " + this.theDefault);
        this.showTab(this.theDefault);

        this.currentEvent = null;

        this.initEvents();

    },

    initEvents: function() {
        $$('.menu').each(function(element, index) {
            Event.observe(element.id,'mouseover',this.queueEvent.bind(this));
            Event.observe(element.id,'mouseout',this.queueEvent.bind(this));
        }.bind(this));
        $$('.submenu1').each(function(element, index) {
            Event.observe(element.id,'mouseover',this.fireEvent.bind(this));
        }.bind(this));
        Event.observe('subNav','mouseout',this.queueEvent.bind(this));

    },

    getMenuId: function(element) {
        var parent = element;
        while (parent.nodeName != 'LI' && parent.nodeName != 'UL') {
            parent = parent.parentNode;
        }
        if (parent.className.indexOf('menu') == -1)
            parent = parent.parentNode;
        var id = parent.id;
        if (id.indexOf('submenu') >= 0)
            id = id.substring(3,id.length);
//        console.log('id: ' + id);
        return id;
    },

    showTab: function(tab) {
//        clearTimeout(this.currentEvent);
        this.hideCurrent();
        if ($(tab))
            $(tab).addClassName('on');
        if ($('sub' + tab))
            $('sub' + tab).show();
        this.current = tab;

    },

    queueEvent: function(event) {
        var tab = this.getTarget(event);
        clearTimeout(this.currentEvent);
//        console.log('queueEvent: ' + tab);
        this.currentEvent = setTimeout(this.showTab.bind(this,tab),this.DELAY);
    },

    fireEvent: function(event) {
        var tab = this.getTarget(event);
        clearTimeout(this.currentEvent);
//        console.log('fireEvent: ' + tab);
        this.showTab(tab);
    },

    getTarget: function(event) {
        var tab = this.theDefault;
        if (event && event.type == 'mouseover') {
            tab = this.getMenuId( ( event.target ? event.target : event.srcElement ) );
        }
        return tab;
    },

    hideTab: function(tabName) {
        var tab = tabName ? tabName : this.theDefault;
//        console.log('hide: ' + tab);
        $('sub' + tab).hide();
        $(tab).removeClassName('on');
    },

    hideCurrent: function() {
//        console.log('sub' + this.current);
        this.hideTab(this.current);
    }
});


function popUp(url,width,height) {
    width = (width == null) ? 830 : width;
    height = (height == null) ? 680 : height;
    var newwindow=window.open(url,'name','toolbar=0,scrollbars=0,location=0,statusbar=0, menubar=0,resizable=0,width='+width+',height='+height);
    if (window.focus) {newwindow.focus()}
}

function getOSFull() {
    if (navigator.appVersion.indexOf("Mac")!=-1)
        return "Mac OS X";
    if (navigator.appVersion.indexOf("X11")!=-1 || navigator.appVersion.indexOf("Linux")!=-1)
        return "Unix";
    return "Windows";
}