/**
 * Javascript
 * 
 */
var Site = function(){
    return {
        init: function() {
           
            $.each($('.menu'), function(){
                $(this).find('li:first').addClass('first-item');
                $(this).find('li:last').addClass('last-item');
            });
            
            $('.main-navigation li, .main-navigation td.secondLevel').hover(function(){
                $(this).addClass('hover');
                if ($(this).find('.level2').length) {
                    $(this).addClass('alt');
                }
            }, function(){
                $(this).removeClass('hover');
                if ($(this).find('.level2').length) {
                    $(this).removeClass('alt');
                }
            });
            
            $('.header-image .quote').wrapInner('<span />');
            
            $('.homepage-tabs .sectionTitle').click(function() {
                $('.homepage-tabs .section').removeClass('selected');
                $('.homepage-tabs .section * :not(class="sectionTitle")').hide();
                $(this).parent('.section').addClass('selected');
                $(this).parent('.section').find('* :not(class="sectionTitle")').show();
            });
            
            $('.homepage-tabs .section:first .sectionTitle').trigger('click');
			
            $('.landingpage-tabs .sectionTitle').click(function() {
                $('.landingpage-tabs .section').removeClass('selected');
                $('.landingpage-tabs .section * :not(class="sectionTitle")').hide();
                $(this).parent('.section').addClass('selected');
                $(this).parent('.section').find('* :not(class="sectionTitle")').show();
            });
            
            $('.landingpage-tabs .section:first .sectionTitle').trigger('click');
            
			$('.feature-page img.image, .sponsor img.image').each(function(){
                var imageSrc = $(this).attr('src').replace(/_web\./, '_800x600.');
                $(this).wrap('<a class="fancybox" rel="gallery" href="' + imageSrc + '" title="' + $(this).attr('alt') + '" />');
                $(this).parent('a').fancybox({
                    autoScale: true,
                    autoDimensions: true,
                    cyclic: true,
                    overlayShow: true,
                    centerOnScroll: true,
                    transitionIn: 'elastic',
                    transitionOut: 'elastic',
                    easingIn: 'swing',
                    easingOut: 'swing',
                    showCloseButton: true
                });
            });
            
            if ($('.feature-box').length) {
                var maxIntroHeight = 0;
                var maxLinksHeight = 0;
                $.each($('.feature-box'), function(){
                    if ($(this).find('.snippet-box-intro').height() > maxIntroHeight) {
                        maxIntroHeight = $(this).find('.snippet-box-intro').height();
                    }
                    
                    if ($(this).find('.snippet-link').height() > maxLinksHeight) {
                        maxLinksHeight = $(this).find('.snippet-link').height();
                    }
                });
                
                $.each($('.feature-box'), function(){
                    $('.snippet-box-intro').height(maxIntroHeight);
                    $('.snippet-link').height(maxLinksHeight);
                });
            }
            
            if ($('.sub-navigation .result').length) {
                $.each($('.sub-navigation .result'), function(){
                    if ($(this).find('a').attr('href') === window.location.pathname) {
                        $(this).addClass('selected');
                    }
                });
            }
            
            /*if ($('.sub-navigation .result').length) {
                $.each($('.sub-navigation .result'), function(){
                    if ($(this).find('a').attr('href') === window.location.pathname) {
                        $(this).addClass('selected');
                    }
                });
            }*/
            
            $('.faq .answer').hide();
            $('.faq .question').click(function() {
                $(this).next('.answer').toggle(); 
            });
            var pathname = window.location.pathname,
            	section = pathname.split('/')[1];
            if($('.sub-navigation').length) {
            	$('.sub-navigation h3.title').wrapInner('<a href="/' +  section + '/"></a>');
            }
        }
    };
}();

try{Typekit.load();}catch(e){}

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

$(window).load(function () {
	$.each($('.cover'), function() {
	    var width = $(this).parents('li').width();
	    $(this).width(width - 1);
	});
});


