$(document).ready(function(){
	var doubleNav = $('#footer .navigation ul').clone();
	
	doubleNav.addClass('clone');
	doubleNav.css('z-index',4);
	doubleNav.css('opacity',0);
	$('#footer .navigation').prepend(doubleNav);
	
	$('li',doubleNav).mouseenter(function() {
		$(this).parent().stop(true,true);
		$(this).parent().animate({opacity:1},600);
	});
	
	$('li',doubleNav).mouseleave(function() {
		$(this).parent().stop(true,true);
		$(this).parent().animate({opacity:0},600);
	});
	
});

$(document).ready(function(){
    $.jTwitter('jotapeh', 3, function(data){
        $('.tweets ul').empty();
        $.each(data, function(i, post){
            $('.tweets ul').append(
                '<li><p># ' + post.text +
                '<br /><span class="source">via '+ post.source +'</span></p></li>'
            );
        });
    });
	    
    var featureHerosList = $('.content-panel .feature-heros .feature-hero');
    var heroNavDots = $('<ul class="hero-navigation"></ul>');
    
    for (var i=0; i<featureHerosList.length; i++) {
    	if (i==0)
	    	heroNavDots.append('<li class="active"><a class="nav-dot">Hero '+i+'</a></li>');
	    else
	    	heroNavDots.append('<li><a class="nav-dot">Hero '+i+'</a></li>');
    }
    
    $('.content-panel .feature-heros').prepend(heroNavDots);
    
	var carouselTimeoutID = window.setTimeout(cycleHero, 6000);
	var titleAnimating = false;
	var heroAnimating = false;
	var currentHeroIndex = 0;
	
	$('.hero-navigation li').click(function() {
		window.clearTimeout(carouselTimeoutID);
		currentHeroIndex = $(this).index()-1;
		cycleHero();
	});
	
	function cycleHero() {
		if (heroAnimating || titleAnimating) {
			window.clearTimeout(carouselTimeoutID);
			window.setTimeout(cycleHero,6000);
			return;
		}
		
		// Prevent animation until complete
		heroAnimating = true;
		titleAnimating = true;
		
		// Increment hero index
		var heroNavList = $('.hero-navigation li');
		
		currentHeroIndex++;
		if (currentHeroIndex >= heroNavList.length)
			currentHeroIndex = 0;
			
		$('.hero-navigation li').removeClass('active');
		$(heroNavList[currentHeroIndex]).addClass('active');
		
		$('.feature-hero.active').fadeOut(800,function() {
			$(this).removeClass('active');
			
			var featureHeroList = $('.feature-hero');
			$(featureHeroList[currentHeroIndex]).fadeIn(800,function() {
				$(this).addClass('active');
				heroAnimating = false;
			});
		});
		
		var contentTitle = $('.content-panel > h2').first();
		contentTitle.animate({opacity:0,textIndent:'-300px'},800,function() {
			$(this).css('textIndent','300px');
			
			var featureHeroList = $('.feature-hero');
			var nextTitle = $(featureHeroList[currentHeroIndex]).children('h2').first().text();
	
			$(this).text(nextTitle);
			$(this).animate({opacity:1.0,textIndent:0},800,function() {
				titleAnimating = false;
			});
		});
	
		window.clearTimeout(carouselTimeoutID);
		carouselTimeoutID = window.setTimeout(cycleHero, 6000);
	}
});
