// JavaScript Document
$(document).ready(function() {
	
	var blinkTimes = 1;
	var index = 0;
	var displayQuoteTime = 5000;
	var navDelay = 800;
	var navBaseTop = 325;
	var navPadding = 0;
	
	$.fn.displayBubble = function (bubbleName)
	{
		setTimeout(function() { $('#bubble').attr('class', 'bubbleSmall').show(); });
		setTimeout(function() { $('#bubble').attr('class', 'bubbleMedium'); },50);
		setTimeout(function() { $('#bubble').attr('class', 'bubbleLarge'); },100);
		setTimeout(function() { $('#bubble').attr('class', bubbleName); },150);
		setTimeout(function() { $('#bubble').attr('class', 'bubbleLarge'); }, 5000);
		setTimeout(function() { $('#bubble').attr('class', 'bubbleMedium'); }, 5050);
		setTimeout(function() { $('#bubble').attr('class', 'bubbleSmall'); }, 5100);
		setTimeout(function() { $('#bubble').hide(); }, 5150);
		
	};
	
	$.fn.bubbleQueue = function ()
	{	
		switch(index)
		{
			case 0:
				$(this).displayBubble('makeMyDay');
				break;
			case 1:
				$(this).displayBubble('takeMeToYourLeader');
				break;
		}
		
		index++;
		
		index = (index > 1) ? 0:index;

		setTimeout(function () { $(this).bubbleQueue(); }, 20000);
	}
	
	$.fn.waveSign = function (delay) {
		setTimeout(function() { $('#caricature').css('background-position', '-150px 0px'); }, delay);
		setTimeout(function() { $('#caricature').css('background-position', '-300px 0px'); }, delay + 100);
		setTimeout(function() { $('#caricature').css('background-position', '-150px 0px'); }, delay + 300);
		setTimeout(function() { $('#caricature').css('background-position', '-300px 0px'); }, delay + 500);
		setTimeout(function() { $('#caricature').css('background-position', '-150px 0px'); }, delay + 700);
		setTimeout(function() { $('#caricature').css('background-position', '-300px 0px'); }, delay + 900);
		setTimeout(function() { $('#caricature').css('background-position', '-450px 0px'); }, delay + 1100);
	};
	
	$.fn.blinkEyes = function () {
		
		if(blinkTimes % 3)
		{
			setTimeout(function() { $('#caricature').css('background-position', '-600px 0px'); });
			setTimeout(function() { $('#caricature').css('background-position', '-450px 0px'); }, 100);
			setTimeout(function () { $(this).blinkEyes();}, 5000);
		}
		else
		{
			setTimeout(function() { $('#caricature').css('background-position', '-750px 0px'); });
			setTimeout(function() { $('#caricature').css('background-position', '-450px 0px'); }, 400);
			setTimeout(function () { $(this).blinkEyes();}, 5000);
		}
		
		blinkTimes++;
	};


	$(window).resize(function() {
		if(($(window).width() > 1050) && !$('#joelpape').is(":visible"))
		{
			$('#joelpape').fadeIn();
		}
		if(($(window).width() < 1050) && $('#joelpape').is(":visible"))
		{
			$('#joelpape').fadeOut();
		}
	});


	if($(window).width() < 1050)
	{
		$('#joelpape').hide();
	}
	else
	{
		$('#joelpape').fadeIn();
	}
	
	$('#download').animate({ top: 0 }, 300);
	
	
	//Intialize Caricature
	$('#caricature').delay(300).animate({ left: 0 }, 300); //Caricature Pop In
	$(this).waveSign(5000); //Welcome Wave sign
	setTimeout(function () { $(this).blinkEyes();}, 10000);  //Initalize Blinking Eyes
	setTimeout(function () { $(this).bubbleQueue(); }, 7000); //Initalize Bubble Messages
	
	
	//Navigation Slide In
	$('#navigation a').each(function () {
		newTop = navBaseTop + navPadding;
		$(this).css('top',  newTop + 'px');
		$(this).delay(navDelay).animate({left: 0});
		navPadding += 50;
		navDelay += 300;
	});
	
	
	//Set Content Display
	$.fn.setContentTab = function (tab) {
		var $lefty = $('#content');
		$lefty.delay(1000).animate({
				left: -600
			},
			500,
			function ()
			{
				$lefty.children('div').hide();
				$('#'+tab).show();
				$lefty.animate({ left: 0 });
			}
    	);
	};
	
	
	//Navigation Click Action
	$('#navigation a').click(function () {
		
		var current = parseInt($(this).data('order'));
		
		//Already Displaying Page
		if(current == '1')
			return true;
		
		$(this).data('order', 0);
		
		$(this).animate({top: "260px"});
		setTimeout(function() { $('#caricature').css('background-position', '-300px 0px'); }, 650);
		setTimeout(function() { $('#caricature').css('background-position', '-450px 0px'); }, 900);
		$(this).delay(200).animate({top: navBaseTop + "px"}, 300, function () { $(this).attr('class', $(this).attr('class') + '_selected')});
		
		
		setTimeout(function() {
			$('#navigation a').each(function () {
				order = parseInt($(this).data('order'));
				newTop = navBaseTop + 50 * (order - 1);
				
				if(order > 1)
					$(this).animate({top: newTop+"px"}, 100);
			});
		},
		600);
		
		
		//SET NEW ORDER AND MOVE UP TO FILL GAP
		$('#navigation a').each(function () {
			
			$(this).attr('class', $(this).attr('class').replace('_selected', ''));
			
			order = parseInt($(this).data('order'));
			
			if(order > current)
			{
				newTop = navBaseTop + 50 * (order - 2);
				$(this).animate({top: newTop+"px"});
			}
			else if(order > 0)
			{
				$(this).data('order', (order + 1));
			}
		});
		
		//Set active nav page to first in line
		$(this).data('order', 1);
		
		$(this).setContentTab($(this).data('tab'));
	});
	
	
	$.fn.quoteDisplay = function (quoteIndex)
	{
		$('#quote').find('div:nth-child(' + quoteIndex + ')').fadeIn(2000).delay(displayQuoteTime).fadeOut(2000);

		if($("#quote > div").size() <= quoteIndex)
			newIndex = 1;
		else
			newIndex = quoteIndex + 1;
		
		setTimeout(function () { $(this).quoteDisplay(newIndex) }, (displayQuoteTime + 3000));	
	};
	
	$(this).quoteDisplay(1);
	$('#download').click(function () { $(this).setContentTab('resumeDownload'); });
	
	
	
	$('#submitRequestForm').click(function () {
		$('#error').hide();
		
		$.ajax({
		  type: "POST",
		  url: "resume.php",
		  data: $('form').serialize(),
		}).done(function( msg ) {
			data = jQuery.parseJSON(msg);
			
			if(data.success)
			{
				$(this).setContentTab('resumeDownloadSuccess');
			}
			else
			{
				$('#error').html(data.error);
				$('#error').show();	
			}
		});
	});
	
	$('#chartPop').click(function () {
		$('.lightBox').show();
	});
	
	$('.lightBox').click(function () {
		$('.lightBox').hide();
	});
});
