jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
	random: function(a, i, m, r) {
		if (i == 0) {
			jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
		};
		return i == jQuery.jQueryRandom;
	}
});

$(document).ready(function() {

	var allDone = false;
	
	// Load images
	var images = new Array();
	var total = 3;
	images[0] = '../images/front/mfront.jpg';	// Face Background
	images[1] = '../images/front/mfront.png';	// Overlay Layer
	images[2] = '../images/front/front_im.png';	// Text Sprites
	var counter = 0;
	$(images).each(function(index,value){
		var img = new Image();
		$(img).load(images[counter], function(){
			counter++;
			total--;
			if(total == 0) doAnimation();		// Loading done, do animation
		});
	});
	
	// Animation
	function doAnimation() {
		$('#fbg').delay(300).fadeIn(2300, function(){
		
			// Sliding words intro
			$('div#fb_orange').delay(300).animate({opacity: 'show',left: '+=20'},3023, 'easeOutQuad');
			$('div#fb_prison').delay(1523).animate({opacity: 'show',left: '+=20'},3023, 'easeOutQuad');
			$('div#fb_wild').delay(1623).animate({opacity: 'show',left: '-=30'},2623, 'easeOutQuad');
			$('div#fb_esperanza').delay(1800).animate({opacity: 'show',left: '-=20'},4023, 'easeOutQuad');
			
			$('div#fb_he').delay(2800).animate({opacity: 'show',left: '+=20'},1900, 'easeOutQuad');
			$('div#fb_pressed').delay(3350).animate({opacity: 'show',left: '-=30'},3023, 'easeOutQuad');
			
			$('div#fb_barber').delay(3600).animate({opacity: 'show',left: '-=40'},2023, 'easeOutQuad');
			$('div#fb_tblock').delay(3650).animate({opacity: 'show',left: '-=20'},4023, 'easeOutQuad');
			$('div#fb_gossip').delay(3800).animate({opacity: 'show',left: '-=20'},3023, 'easeOutQuad');
			$('div#fb_against').delay(4300).animate({opacity: 'show',left: '+=30'},2623, 'easeOutQuad');
			
			// Pink words
			$('div#f_p1').delay(3300).animate({opacity: 'show',left: '+=30'},1250, 'easeOutQuad', function(){
				$('div#f_p2').animate({opacity: 'show',left: '-=30'},1250, 'easeOutQuad', function(){
					$('div#f_p3').animate({opacity: 'show',left: '+=30'},1250, 'easeOutQuad', function(){
					
						// Name
						$('div#f_maria').animate({opacity: 'show',left: '+=13',top: '+=13'},623, 'easeOutQuad', function(){
							$('div#f_amparo').animate({opacity: 'show',left: '+=13',top: '+=13'},623, 'easeOutQuad', function(){
								$('div#f_escandon').animate({opacity: 'show',left: '+=13',top: '+=13'},623, 'easeOutQuad', function(){
									// All done!
									allDone = true;
								});
							});
						});
					});
				});
			});
		});
	}
	
	window.setInterval(function() {doTwitch();}, 4500);

	
	// Twitch
	function doTwitch() {
		var randTime = Math.floor(Math.random()*301);
		if(allDone) {
			$('.twitch:random').animate({left: '+=10'},3000+randTime, 'easeInOutQuad', function(){
				$(this).delay(6500).animate({left: '-=10'},3000+randTime, 'easeInOutQuad');
			});
		}
	}
	
});

