
function menu() {
    var items = [];
    var tids = []; // timeout ids
    var curr = -1; // current menu
    
    var _hide = function(e, fade) {
        if (fade) {
            $(e).find('div').fadeOut(100);
        } else {
            $(e).find('div').stop(true, true).hide();
        }
    }
    var _show = function(e, slide) {
        var dropdown = $(e).find('div');
        if (dropdown.queue() == 'inprogress' || !slide) {
            dropdown.stop(true, true).show();
        } else {
            dropdown.find('ul').hide();
            dropdown.show().find('ul').animate({ opacity: 'show', height: 'toggle' }, 70);
        }
    }
    
    $('#menubar > ul > li').each(function(idx, val) {
        items.push(val);

        var a = $(val).children('a').css('border', 'none').clone().css({
            'border-bottom': '3px solid white',
            'background-image': $(val).css('background-image'),
            'background-position': idx * -164 + 'px -34px'
        });
        $(val).find('ul').wrap('<div style="position: relative; top: -34px; display: none" />').before(a);
        $(val).css('overflow', 'visible'); // switch off css behaviour

        $(val).mouseover(function(e) {
            clearTimeout(tids[curr]);
            if (curr == idx) return;
            if (curr == -1) {
                _show(val, true);
            } else {
                _hide(items[curr]);
                _show(val);
            }
            clearTimeout(tids[curr = idx]);
        }).mouseout(function(e) {
            tids[idx] = setTimeout(function() { _hide(val, true); curr = -1; }, 150)
        });
    });
}


$(document).ready(function() {

    menu();

    // slimbox loading
    if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
        jQuery(function($) {
            $("a[rel^='lightbox']").slimbox({
                overlayOpacity: .9,
                overlayFadeDuration: 300,
                counterText: false,
                resizeDuration: 200,
                imageFadeDuration: 200,
                captionAnimationDuration: 200
            }, null, function(el) {
                return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
            });
        });
    }

    isNotDummy = function() {
        return $(this).css('left') != '-10000px' && $(this).css('display') != 'none';
    }
    
    keyPressOrKeyDown = $.browser.opera ? 'keypress' : 'keydown';

    // form value-captions
    $('#content input[type=text], #content input[type=password], #content textarea').not('.pw-text')
    .focus(function(e) {
        if (!e.target._defVal) e.target._defVal = e.target.value;
        if (!e.target._typed) e.target.value = '';
    }).blur(function(e) {
        if (e.target.value == '') e.target._typed = false;
        if (!e.target._typed && (e.target.type != 'password')) e.target.value = e.target._defVal;
    }).keyup(function(e) {
        e.target._typed = true;
    })[keyPressOrKeyDown](function(e) {
        TAB = 9;
        if (e.keyCode == TAB) {
            if (!e.shiftKey) {
                $(e.target).nextAll('input, textarea').filter(isNotDummy)[0].focus()
            } else {
                prevs = $(e.target).prevAll('input, textarea').filter(isNotDummy);
                if (prevs.length > 0) prevs[0].focus()
                    else return true;
            }
            return false;
        }
    });

    $('#content input[type=password]').focus(function(e) {
        if ($(e.target).next().find('input').css('display') != 'none') {
            setTimeout(function() {
                $(e.target).next().find('input')[0].focus();
            }, 10);
        }
    }).blur(function(e) {
        if (!e.target._typed) $(e.target).next().find('input').show();
    });

    $('#content input.pw-text').focus(function(e) {
        $(e.target).hide();
        $(e.target).parent().prev().focus();
    });

    $('form').submit(function(e) {
        inputs = $(e.target).find('input[type=text], input[type=password], textarea').not('.pw-text');
        inputs.filter(function() { return !this._typed; }).each(function(idx, val) {
            val.value = '';
        });
        filled = (inputs.filter(function() { return !!this._typed; }).length == inputs.length);

        //return false;
    });
    
});







