(function($){
    $(document).ready(function() {
        $('#searchTerm').example('site search', { className: 'gray'});

        resize();

        $('#nav li ul li').hover(
        function() { $(this).addClass('over'); },
        function() { $(this).removeClass('over'); }
        );

        $('#nav a').filter(function() {
            return $('ul', $(this).parent('li:first')).length > 0;
        }).click(function() {
            var ul = $('ul', $(this).parent('li:first'));
            if (ul.length > 0) {
                if  (ul.css('display') == 'none') {
                    // Show
                    $('#sideContent').animate({
                        height: '+=' + ul.height()
                    }, 'fast');
                    $('#wrapper').animate({
                        height: '+=' + ul.height()
                    }, 'fast');
                    ul.show('fast');
                } else if (!$(this).hasClass('current') && !$(this).hasClass('currentParent')) {
                    // Hide
                    $('#sideContent').animate({
                            height: '-=' + ul.height()
                        }, 'fast');
                        $('#wrapper').animate({
                            height: '-=' + ul.height()
                        }, 'fast');
                        ul.hide('fast');
                }
                return false;
            }
        });


//        $('#nav li').hoverIntent({
//            over: function() {
//                var ul = $('ul', this);
//                if (ul.length > 0 && ul.css('display') == 'none') {
//                    $('#sideContent').animate({
//                        height: '+=' + ul.height()
//                    }, 'fast');
//                    $('#wrapper').animate({
//                        height: '+=' + ul.height()
//                    }, 'fast');
//                    ul.show('fast');
//                }
//            },
//            out: function() {
//                if($(this).hasClass('current') || $(this).hasClass('currentParent')){
//                }
//                else{
//                    var ul = $('ul', this);
//                    if (ul.length > 0) {
//                        $('#sideContent').animate({
//                            height: '-=' + ul.height()
//                        }, 'fast');
//                        $('#wrapper').animate({
//                            height: '-=' + ul.height()
//                        }, 'fast');
//                        ul.hide('fast');
//                    }
//                }
//            },
//            timeout: 0
//        });

        function resize() {
            if($('#wrapper').height() > $('#sideContent').height() + 164) {
                if($('#wrapper').height() > 900){
                    $('#sideContent').height($('#wrapper').height() - 181);
                }
                else{
                    $('#wrapper').height(900);
                    $('#sideContent').height($('#wrapper').height() - 181);
                }
            }
            else {
                if($('#sideContent').height() + 164 > 900){
                    $('#wrapper').height($('#sideContent').height() + 181);
                }
                else{
                    $('#wrapper').height(900);
                    $('#sideContent').height($('#wrapper').height() - 181);
                }
            }
        };
    });

})(jQuery);