$(document).ready(function(e) {
   
    var image_array = ['/elements/img/photos/homepage-billboards/billboard-planet.jpg', '/elements/img/photos/homepage-billboards/billboard-science.jpg', '/elements/img/photos/homepage-billboards/billboard-talent.jpg'];
    // featured rotator 
    $('div#home-rotator-widget').rotator({
        width: 980,  
        images: image_array,
        initial_image_idx: Math.floor(Math.random() * image_array.length) 
    });
    
    // tabs beneath the rotator
    setupTabs();
    
    // setup primary nav scrolling effect
    setupNavBar(['logo','region','clusters','assets']);
    
    // for IE7 and below, activate the floating nav bar
    if(lessThanIE7()) {
        activateFloatingBar();
    }
    
});

// global for determining whether to select on address change
var tabToSelect;

function setupTabs() {
    
    // tab is clicked
    $('a.interactive-tab').click(function(e) {
        
        // switch to the current tab
        selectTab($(this));
        
        return false;
        
    });
    
    // browser forward/back
    $.address.change(function(e) {
        
        // grab the hash without the # or the -tab
        var hash = window.location.hash.substr(1, window.location.hash.length - 5);
        
        if(hash) {
            
            // get the tab that needs to be selected
            tab = $('a[rel="'+hash+'"]');
            
            // if we're clicking a tab, don't select it twice
            if(tabToSelect === tab)
                return false
            else
                tabToSelect = tab;
            
            // if there isn't a tab for that hash, get out
            if(!tabToSelect.attr('rel'))
                return false;
            
            // select the tab according to the hash
            selectTab(tabToSelect);
            
        }
        
        return false;
        
    });
    
    function selectTab(tab) {
        
         // clear previously selected tab
         $('a.interactive-tab').removeClass('home-tab-selected');

         // mark current tab as selected
         tab.addClass('home-tab-selected');

        // grab the rel attribute to find
        // the corresponding div for the tab
        var divLink = tab.attr('rel'); 

        // append the correct hash to the url, add -tab for IE not to jump
        window.location.hash = divLink + "-tab";

        // hide the current div
        $('div.home-intro-tab-content').hide();

        // fade in the div for the tab
        $('div#'+divLink).fadeIn('fast');
        
    }
    
}

function setupNavBar(buttons) {

    // setup the scrolling effect for buttons on nav bar
    for(var i = 0; i < buttons.length; i++) {
        
        // register click handler
        $('a.' + buttons[i]).bind('click', i, function(evt) {
            
            // clear selections while scrolling
            clearNavSelections();
            
            // keep track of current button
            var index = evt.data;
            
            // check to see if home logo was clicked
            if(buttons[index] != 'logo') {
                
                var h2element = $('#' + buttons[index]);
                
                // get scroll position of h2
                var pos = h2element.position().top;
                
                // gently scroll to the corresponding h2 on the page
                $.scrollTo(pos, { 
                    axis: 'y',
                    duration: 500, 
                    onAfter: function() {
                        
                        // select current section
                        $('a.' + buttons[index]).addClass('selected');
                    }
                });
                
            } else {
                // go to the top
                $.scrollTo(0, 500);
                
                // clear last that was made
                clearNavSelections();
            }
            
            return false;
            
        });
        
    }
    
    function clearNavSelections() {
        
        // remove all selected classes on the nav buttons
        for(var i = 0; i < buttons.length; i++) {
            
            $('a.' + buttons[i]).removeClass('selected');
            
        }
        
    }
    
}

function activateFloatingBar() {

    // move the nav bar on scroll
    $(window).scroll(function() {

        // give it a slight animation to make it look cool
        $('div#header-container').animate({
           top: $(window).scrollTop() 
        }, 10);

    });

}

function lessThanIE7() {
    return $.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0');
}

