var _config = {
    menuAnimationOffset:30
}

function documentClickWhenMenuVisible(e) {
    if ($(e.target).parents('#mm').length == 0) {
        hideMenu();
    }
}

function showMenu() {
    var $menu = $('#mm');
    $menu.show()

    var menuTopOffset = $menu.offset().top;

    if (!$.browser.msie) {
        $menu.css({opacity:0}).css('top', menuTopOffset + _config.menuAnimationOffset);
        $menu.animate({opacity:1,top:menuTopOffset}, 300, function() {
            $(document).bind('click', documentClickWhenMenuVisible);
        });
    }
    else {
        setTimeout(function() {
            $(document).bind('click', documentClickWhenMenuVisible)
        }, 100)
    }

}

function hideMenu() {
    var $menu = $('#mm'),
        menuTopOffset = $menu.offset().top;

    $(document).unbind('click', documentClickWhenMenuVisible);

    if (!$.browser.msie) {
        $menu.animate({opacity:0,top:menuTopOffset + _config.menuAnimationOffset}, 300, function () {
            $menu.css('top', menuTopOffset).hide();
        })
    }
    else {
        $menu.css('top', menuTopOffset).hide();
    }
}


$(document).ready(function() {
    makeSearchInput('.header .search-box');
    makeSearchInput('input.dept-filter');
    resizeOnFocus('.header .search-box', 80, 130);

    $('#open-full-menu').bind('click', showMenu);
    $('#close-full-menu').bind('click', hideMenu);

    var preloadImages = [
        'menu-tab-background.png',
        'mm11.png',
        'mm31.png',
        'dropdown-up.gif'
    ]

    var preloadImageObject = new Image();
    for (var i in preloadImages) {
        preloadImageObject.src = '/i/'+preloadImages[i];
    }


    var _deptsKeywords = new Array();
    $('#dept-list li').each(function() {
        _deptsKeywords.push($(this).attr('keywords').split(' '));
    });

    $('input.dept-filter').keyup(function() {
        var filterString = $(this).val().toLowerCase(),
                filterWords = filterString.split(' '),
                foundWords = 0,
                totalWords = filterWords.length;

        $('#dept-list li').show();

        if (filterString !== '') {

            for (var i = 0; i < filterWords.length; i++) {
                for (var j = 0; j < _deptsKeywords.length; j++) {
                    foundWords = 0;
                    for (var k = 0; k < _deptsKeywords[j].length; k++) {
                        if (_deptsKeywords[j][k].indexOf(filterWords[i]) === 0)
                            foundWords++;
                    }

                    if (foundWords < totalWords)
                        $('#dept-list li').eq(j).hide();
                }
            }
        }
    });
});


function makeSearchInput(selector) {
    $(selector).each(function() {
        if (typeof($(this).attr('defaultValue')) === undefined)
            $(this).attr('defaultValue', $(this).val())
    });

    $(selector).live('click', function() {
        var defaultValue = $(this).attr('defaultValue');
        if ($(this).val() == defaultValue) {
            $(this).val('').css({fontStyle:'normal',color:'black'});
        }
    });

    $(selector).live('focusout', function() {
        var defaultValue = $(this).attr('defaultValue');
        if ($(this).val() == '') {
            $(this).val(defaultValue).css({fontStyle:'italic',color:'#999'});
        }
    });
}

function resizeOnFocus(selector, increaseBy, speed) {
    if (typeof(increaseBy) === 'undefined') increaseBy = 50;
    if (typeof(speed) === 'undefined') speed = 200;
    $(selector).each(function() {
        $(this).attr('defaultWidth', $(this).width())
    });

    $(selector).live('click', function() {
        var newWidth = parseInt($(this).attr('defaultWidth')) + increaseBy;
        $(this).animate({width:newWidth}, speed)
    });

    $(selector).live('focusout', function() {
        $(this).animate({width:$(this).attr('defaultWidth')}, speed)
    });
}

