if (BASE_PORTAL_URL == null) BASE_PORTAL_URL = '';

var MENU_FILES = {
    'menu_home'         : [BASE_PORTAL_URL + '/files/imgs/btn_home', 0, 0, 0, 0],
    'menu_barcodes'     : [BASE_PORTAL_URL + '/files/imgs/btn_barcodes', 0, 0, 0, 0],
    'menu_clients'      : [BASE_PORTAL_URL + '/files/imgs/btn_clients', 0, 0, 0 ,0],
    'menu_blog'         : [BASE_PORTAL_URL + '/files/imgs/btn_blog', 0, 0, 0, 0],
    'menu_contact'      : [BASE_PORTAL_URL + '/files/imgs/btn_contact', 0, 0, 0, 0]
};

function initMenu()
{
    var menu = $('#menu')[0];
    for (var i = 0; i < menu.children.length; i++) {
        if (
            menu.children[i].children[1] != null &&
            menu.children[i].children[1].tagName.toLowerCase() == 'ul'
        ) {
            for (var k = 0; k < menu.children[i].children[1].children.length; k++) {
                var el = menu.children[i].children[1].children[k];
                el.onmouseover = function() {
                    this.style.backgroundColor = '#636363';
                };
                el.onmouseout = function () {
                    this.style.backgroundColor = '';
                };
                el.onmousedown = function (e) {
                    this.style.backgroundColor = '#363636';
                    if (!e) var e = window.event;
                    e.cancelBubble = true;
                    if (window.event.stopPropagation) window.event.stopPropagation();
                    window.event.cancelBubble = true;
                }
            }
        }

        menu.children[i].onmouseover = function (){
            if (
                MENU_FILES[this.className] != null && (
                    MENU_FILES[this.className][2] == 1 || (
                        MENU_FILES[this.className][3] && MENU_FILES[this.className][3][0].complete
                    )
                )
            ) {
                this.style.backgroundImage = 'url(' + MENU_FILES[this.className][0] + '_over.png)';
                for (var j = 0; j < this.children.length; j++) {
                    if (this.children[j].tagName.toLowerCase() == 'ul') {
                        this.children[j].style.display = 'block';
                        var pos = getposOffset(this);
                        this.children[j].style.top = (pos.y + this.offsetHeight) + 'px';
                    }
                }
            }
        };

        menu.children[i].onmouseout = function (){
            this.style.backgroundImage = '';
            for (var j = 0; j < this.children.length; j++) {
                    if (this.children[j].tagName.toLowerCase() == 'ul') {
                        this.children[j].style.display = 'none';
                    }
            }
        };

        menu.children[i].onmousedown = function (){
            //if (MENU_FILES[this.className] != null && MENU_FILES[this.className][1] == 1) {
            if (
                MENU_FILES[this.className] != null && (
                    MENU_FILES[this.className][1] == 1 || (
                        MENU_FILES[this.className][4] && MENU_FILES[this.className][4][0].complete
                    )
                )
            ) {
                this.style.backgroundImage = 'url(' + MENU_FILES[this.className][0] + '_pressed.png)';
            }
            //return false;
        };
    }

    for (var h in MENU_FILES) {
        MENU_FILES[h][3] = jQuery("<img>");
        MENU_FILES[h][3].attr('src', MENU_FILES[h][0] + '_over.png').attr('id', 'over-' + h);
        MENU_FILES[h][3].bind('load', _onLoadImgOver);

        MENU_FILES[h][4] = jQuery("<img>");
        MENU_FILES[h][4].attr('src', MENU_FILES[h][0] + '_pressed.png').attr('id', 'down-' + h);
        MENU_FILES[h][4].bind('load', _onLoadImgPress);
    }
}

function _onLoadImgPress()
{
    MENU_FILES[this.id.split('-')[1]][1] = 1;
}

function _onLoadImgOver()
{
    MENU_FILES[this.id.split('-')[1]][2] = 1;
}


