var glHome = {
    init: function() {

        this.initPaneAnimation();
        this.positionGlobe();
        this.initSlider();
        this.initNavEvents();
        this.positionPromoNav();
    },
    initNavEvents: function() {
        $('#mainNav a').each(function(navIndex) {
            $(this).bind('mouseover', function() {
                $('#promoPanes').cycle('pause');
            });
        });
        $('.fatNavMenu').bind('mouseleave', function() {
            $('#promoPanes').cycle('resume');
        });
    },
    initPaneAnimation: function() {
        $('#promoPanes').cycle({
            fx: 'fade',
            speed: 2500,
            timeout: 8000,
            delay: -3000,
            pauseOnPagerHover: 1,
            pager: '#promoNav'
        });
    },
    initSlider: function() {
        $('.tierA .columnD').serialScroll({
        	target: '.companySlider',
        	items: 'li',
        	prev: '.btnPrev',
        	next: '.btnNext',
        	axis: 'x',
        	duration: 700,
        	force: true,
        	cycle: false,
        	step: 3,
        	onBefore: function(e, elem, $pane, $items, pos) {
        		e.preventDefault();
        		if (this.blur) this.blur();
        	},
        	onAfter: function(elem) {
        		//'this' is the element being scrolled ($pane) not jqueryfied
        	}
        });
    },
    positionGlobe: function() {
        var promoOffset = $('#promoPanes .promoContent').offset();
        var leftSpill = 250;
        var myLeft = promoOffset.left - leftSpill;
        var globePromos = $('#promoPanes .globePane');

        globePromos.css({'background-position': myLeft + 'px 0'});
    },
    positionPromoNav: function() {
        var winWidth = $(window).width();
        var myLeft = Math.floor(winWidth/2) - (Math.round($('#promoNav').width() / 2));

        $('#promoNav').css({'left': myLeft + 'px'});
    }
}

$(function() {
    $('#promo').append('<div id="promoNav"></div>');

    glHome.init();

    $(window).bind('resize', function() {
        $('#promoPanes').unbind();
        $('#promoNav').empty();
        glHome.init();
    });

    // Load latest tweet
    $.getJSON('/wp-content/themes/globalive/lacavera_tweet.php', function(data){
        $("#tweetText").html(data.tweet);
        $("#tweetTime").prepend(data.tweetTime);
    });
});

