$.extend({
    _submenu:null,
    _submenuSender:null,
    _submenuContainers:null,
    _footer:null,
    initSubmenu:function() {
        $._submenu = $('div#main-submenu');
        $._submenu.bind('mouseover', function(e) {
            $.showSubmenu($._submenuSender);
            return;
        });
        $._submenu.bind('mouseout', function(e) {
            $.hideSubmenu();
            return;
        });
        $._submenuContainers = $._submenu.find('div.body > div.submenu-container');
        $.setSubmenuSize();
        $(window).bind('resize', $.setSubmenuSize);
        $._submenu.hide();
        return;
    },
    setSubmenuSize:function(e) {
        var _left = $('#menu-philosophy').offset().left-68;
        $._submenu.css({'left':_left});
        $._submenu.find('div.body').css({'width':$(window).width()-_left-70});
        return;
    },
    showSubmenu:function(e) {
        $.hideSubmenu();
        $.setSubmenuSize();
        $._submenuSender = e;
        $._submenuSender.addClass('selected');
        $._submenu.show();
        $('div#'+e.attr('rel')).css({
            'display':'block',
            'visibility':'visible'
        });
        return;
    },
    hideSubmenu:function() {
        $('ul#menu > li > a').removeClass('selected');
        $._submenu.hide();
        $._submenuContainers.css({
            'display':'none',
            'visibility':'hidden'
        });
        return;
    },

    initSlideMenu:function(_subpage, _subsection) {
        var _tabs = $('div.submenu > ul > li');
        var _links = _tabs.children('a');
        var _content = $('div.content-text');
        var _subsectionTabs = $('div.right > ul.submenu > li > a');
        var _speed = 0;
        var _splash = $('div#splash-angled');
        var _splashImgHolder = $('div#splash-angled-images-holder');
        var _splashTextHolder = $('div#splash-angled-text-holder');
        var _selected;

        _links.bind('click', function(e) {
            e.preventDefault();

            var _rel = $(this).attr('rel');

            if (_rel == _selected)
                return false;

            _selected = _rel;

            _tabs.removeClass('selected');
            $(this).parent().addClass('selected');

            _content.hide();

            var _thisContent = $('div#content-'+_rel);
            var _color = $('span#splash-angled-color-'+_rel).html();
            var _y = -((parseInt(_rel)-1)*310);

            _thisContent.fadeIn('medium');
            _splash.stop().animate({backgroundColor:_color}, _speed, 'easeInOutExpo');
            _splashImgHolder.stop().animate({'top':_y}, _speed, 'easeInOutExpo');

            _splashTextHolder.css('top',_y);

            var _textHolder = _splashTextHolder.children('div#text-'+_rel);
            _textHolder.children('h3').stop().css({'top':'80px', 'opacity':0}).animate({'top':'110px', 'opacity':1}, _speed*2, 'easeInOutExpo');
            _textHolder.children('h4').stop().css({'top':'220px', 'opacity':0}).delay(300).animate({'top':'190px', 'opacity':1}, _speed*2, 'easeInOutExpo');

            _thisContent.children('div.right').children('ul.submenu').children('li:first').children('a').trigger('click');

            return false;
        });

        _subsectionTabs.bind('click', function(e) {

            if ($(this).attr('rel') == 'link')
                return true;

            e.preventDefault();

            var _index = $(this).parent().index();
            var _subsections = $(this).parent().parent().parent().parent().children('div.middle').children('div.subsection');

            _subsections.hide();
            $(_subsections[_index]).fadeIn('medium');

            _subsectionTabs.parent().removeClass('selected');
            $(this).parent().addClass('selected');

            return false;
        });

        if (_subpage) {

            _links.each(function(i) {
                if (String($(this).attr('href')).indexOf(_subpage) > -1)
                    $(this).trigger('click');
            });

            if (_subsection)
                _subsectionTabs.each(function(i) {
                    if (String($(this).attr('href')).indexOf(_subsection) > -1)
                        $(this).trigger('click');
                });

        } else
            $(_links[0]).trigger('click');

        _speed = 1000;

        return;
    },
    initFooter:function() {

        if ($._footer == null)
            $._footer = $('#footer');

        var _links = $._footer.children('div.wrapper').children('ul').children('li').children('a');

        _links.hover(function() {

            var _span = $(this).find('span');

            if (_span.length == 0) {
                $(this).append('<span class="hover"></span>');
                _span = $(this).find('span');
                _span.hide();
            }

            _span.fadeIn('fast');

            return;

        }, function() {

            var _span = $(this).find('span');

            if (_span.length > 0)
                _span.fadeOut('fast');

            return;

        });

        return;

    },
    initFadingLink:function(_obj) {

        if ($.browser.msie)
            return;

        _obj.parent().append('<span class="link-fade-helper"></span>');
        _obj.addClass('selected');

        var _span = _obj.parent().find('span.link-fade-helper:last');
        _obj.clone().appendTo(_span);
        _obj.removeClass('selected');

        _span.show().css('opacity',0);

        $(_span, _span.children()).unbind('click');
        _span.bind('mouseover', function() {
            $(this).stop().animate({'opacity':1}, 'fast');
            _obj.trigger('mouseover');
        });
        _span.bind('mouseout', function() {
            $(this).stop().animate({'opacity':0}, 'fast');
            _obj.trigger('mouseout');
        });

    }

});
$(document).ready(function() {

    $.initSubmenu();
    $.initFooter();

    var _menu = $('ul#menu');
    var _links = _menu.find('li > a');

    _links.each(function(i) {

        if (i == 2 || i == 5) {

            $(this).bind('mouseover', function(e) {
                e.preventDefault();
                $.showSubmenu($(this));
                return;
            });
            $(this).bind('mouseout', function(e) {
                e.preventDefault();
                $.hideSubmenu();
                return;
            });

            if ($(this).attr('href') == '#')
                $(this).bind('click', function(e) {
                    e.preventDefault();
                    return false;
                });

        }

        $.initFadingLink($(this));

    });

    var _twitter_box = $('div.twitter');
    var _twitter_feeds = _twitter_box.find('div.feed');
    var _twitter_nav = _twitter_box.find('div.nav').children('a');
    var _twitter_balloon = _twitter_box.find('div.balloon');

    if (_twitter_feeds.length > 0) {

        var _twitter_current = 0;

        _twitter_nav.bind('click', function(e) {

            e.preventDefault();

            if ($(this).hasClass('nav-prev')) {
                _twitter_current++;
                if (_twitter_current == _twitter_feeds.length)
                    _twitter_current = 0;
            } else {
                _twitter_current--;
                if (_twitter_current < 0)
                    _twitter_current = _twitter_feeds.length-1;
            }

            _twitter_feeds.hide();
            $(_twitter_feeds[_twitter_current]).fadeIn('fast');

        });
        $(_twitter_feeds[0]).show();

    } else {
        _twitter_nav.remove();
        _twitter_balloon.remove();
    }

    $('a[rel*="lightbox"], a[href*="/lightbox/"]').colorbox();
    $('a[rel*="LIGHTBOX"]').colorbox();

    $('a#bt-return-top').bind('click', function(e) {
        e.preventDefault();
        $('html, body').animate({scrollTop:0}, 1000, 'easeOutExpo');
        return false;
    });

    $('div#top-login').children('a').each(function() {
        $.initFadingLink($(this));
    });
    $('div#top-login').find('span > a.ask-question').each(function(e) {
        $(this).removeClass('cboxElement');
        $(this).removeClass('selected');
        $(this).bind('click', function(e) {
            e.preventDefault();
            $(this).parent().parent().find('a.ask-question:first').trigger('click');
        });
    });

    $('*').bind('contextmenu', function(e) { e.preventDefault() });

});
