// JavaScript Document

/*  ================================ */
/*  DESIGN & CODE  | COPYRIGHT 2012  */
/*  BRIAN M. TULLY |  NEW YORK, NY   */
/*  ================================ */

// Page Preparations
function prepPage(destination, direction,isQueued) {
	
	if (window.animatedElementsLoaded == null) {
		
	/**********************************/
	/****         INITIALIZE       ****/
	/**********************************/
	
	/* ===================================*/
	/*  Define all animated elements      */
	/*   here for faster loading times    */
	/* ===================================*/
	
	// Global
	window.slider = $('#slider_1');	
	window.allPanels = $('section.track_item');	
	window.maxPages = allPanels.filter('.moveable').size() -1; 
	window.rightArrow = $('#next_page');
	
	// Welcome
	window.welcome_about = $('#about_me');
	window.welcome_about_jobTitle = $('#about_me #job_title');
	window.welcome_about_cityName = $('#about_me #job_city');
	window.welcome_networks = $('#networks');
	window.welcome_network_nbc = $('#nbc_logo'); 
	window.welcome_network_cbs = $('#cbs_logo'); 
	window.welcome_network_fox = $('#fox_logo'); 
	
	// Resume
	window.jobHolder = $('#experience');
	
	// Web
	window.www_holder = $('#technology');
	window.tech_thought_bubble = $('.thought_bubble');
	window.tech_anim_container = $('#tech_anim_container');
	window.tech_anim_recent_work_list = $('#recent_work_list');
	window.tech_anim_html5 = $('#logo_html5'); 
	window.tech_anim_iphone = $('#logo_iphone');
	window.tech_anim_ipad = $('#logo_ipad');
	window.tech_anim_android = $('#logo_android');
	window.tech_anim_cinema_display = $('#logo_cinema_display');
	window.tech_anim_plus_signs = $('.plus_sign');
	window.tech_anim_recent_work = $('#recent_www_work');
	window.tech_anim_back = $('#back_to_recent_www_work');
	
	// Contact
	window.contact_info_header = $('#contact_info>h2:first-child');
	window.contact_info_phone = $('#contact_info_phone');
	window.contact_info_email = $('#contact_info_email');
	window.contact_info_facebook = $('#contact_info_facebook');
	window.contact_icon_phone = $('#contact_icon_phone');
	window.contact_icon_email = $('#contact_icon_email');
	window.contact_icon_facebook = $('#contact_icon_facebook');
	
	
		  		/* = end of items = */
			trace('  Animated Objects Cached.');
			window.animatedElementsLoaded = 1;
		}	/* = end of initializer = */

	

	/**********************************/
	/********  WELCOME PAGE   ********/
	/**********************************/
	if (destination == 0) {
		
	// Let IE Bail.
	if (msie) {$(isQueued).dequeue();  return; }
		
		// Default Speed
		var speed = 700;
		
		if (direction == 'open') {
		/* ====================== */
		/* ==OPENING ANIMATIONS== */
		/* ====================== */
				
			// Setup
			welcome_network_nbc.css({'scale' : '1.2'});
			welcome_network_cbs.css({'scale' : '1.2'});
			welcome_network_fox.css({'scale' : '1.2'});
				
			// Animate
			welcome_about_jobTitle.animate({'opacity' : '1'}, 1800, easing);
			welcome_about_cityName.delay(400).animate({'opacity' : '1'}, speed, easing);
			welcome_network_nbc.delay(700).animate({'opacity' : '1', 'scale' : '0.85'}, speed*1.25, easing);
			welcome_network_cbs.delay(850).animate({'opacity' : '1', 'scale' : '0.85'}, speed*1.25, easing);
			welcome_network_fox.delay(1000).animate({'opacity' : '1', 'scale' : '0.85'}, speed*1.25, easing, function() {	
			
				// Move to Next Animation
				$(isQueued).dequeue();  return; }); 	
			/* = end of opening anims =*/
	
		} else if (direction == 'close') {
		
		/* ====================== */
		/* ==CLOSING ANIMATIONS== */
		/* ====================== */
		
		var speed = speed/4;
		
			// Dissolve Everything Out
			welcome_about.animate({'opacity' : '0', 'scale' : '1'}, speed, easing);
			welcome_networks.delay(100).animate({'opacity' : '0', 'scale' : '0.5'}, speed, easing, function() {   
			
			// Fix Scale + Size + Opacity
			welcome_about_jobTitle.css({'opacity' : '0'});
			welcome_about_cityName.css({'opacity' : '0'});
			welcome_network_nbc.css({'opacity' : '0'});
			welcome_network_cbs.css({'opacity' : '0'});
			welcome_network_fox.css({'opacity' : '0'});
			welcome_about.css({'opacity' : '1'});
			welcome_networks.css({'opacity' : '1', 'scale' : '1'});
					
			// Move to Next Animation
			$(isQueued).dequeue(); return;}); 
			
		/* = end of closing anims =*/
		} else {
			trace('Effect error. Please specify \'open\' or \'close\'.');
			if (isQueued) {$(isQueued).dequeue();}
			return;
		}
	
	
	/**********************************/
	/********  RESUME PAGE   *********/
	/**********************************/
	} else if (destination == 1 && direction == 'close' ) {
		var isOpen = jobHolder.hasClass('open');
		if (isOpen) {
			revealJob('reset', isQueued);
			return;
		} else {
			if (isQueued) {$(isQueued).dequeue(); return;}
		} 
	
	/**********************************/
	/********  WEBPAGE PAGE   *********/
	/**********************************/
	} else if (destination == 2) { 
	
	// Let IE Bail.
	if (msie) {$(isQueued).dequeue();  return; }
	
	// Set Defaults
	var speed = 700;
	var easing = 'easeOutElastic';
	var delay = new Number(500);
	
		if (direction == 'open') {
	
			www_holder.addClass('executing');
			
		/* ====================== */
		/* ==OPENING ANIMATIONS== */
		/* ====================== */
		
			// Setup
			tech_anim_html5.css({'scale' : '1', 'top' : '4em', 'opacity' : '1'});
			tech_anim_ipad.css({'scale' : '0', 'rotate' : '30', 'opacity' : '1'});
			tech_anim_iphone.css({'scale' : '0', 'rotate' : '-20', 'opacity' : '1'});
			tech_anim_android.css({'scale' : '0', 'rotate' : '-20', 'opacity' : '1'});
			tech_anim_cinema_display.css({'scale' : '0', 'rotate' : '10', 'opacity' : '1'});
			tech_thought_bubble.css({'opacity' : '0'}); 
			tech_anim_recent_work.css({'opacity' : '0'});
			
			// Animations: Stage 1
			
			// HTML5 Logo
			tech_anim_html5
			.stop()
			.delay(delay)
			.animate({'scale' : '0', 'top' : '0', 'opacity' : '0'},speed, 'easeOutElastic');
			
			// iPad
			tech_anim_ipad
			.stop()
			.delay(delay)
			.animate({'left' : '1.3em', 'scale' : '1', 'rotate' : '0'}, speed, easing);
			
			// Cinema Display
			tech_anim_cinema_display
			.stop()
			.delay(delay)
			.animate({'left' : '11.25em', 'scale' : '1', 'rotate' : '0'}, speed, easing);
			
			// iPhone
			tech_anim_iphone
			.stop()
			.delay(delay)
			.animate({'left' : '21.5em', 'scale' : '1', 'rotate' : '0'}, speed, easing);
			
			// Android
			tech_anim_android
			.stop()
			.delay(delay + 50)
			.animate({'left' : '20em', 'scale' : '1', 'rotate' : '0'}, speed, easing, function() {  
				
				// Animations: Stage 2
				
				// Plus Signs
				tech_anim_plus_signs
				.stop()
				.animate({'opacity' : '1'}, 500);
				
				// Recent Work Button
				tech_anim_recent_work
				.stop()
				.delay(200)
				.animate({'opacity' : '1'},speed);
				
				// Thought Bubble
				tech_thought_bubble
				.stop()
				.delay(400)
				.animate({'opacity' : '1'}, speed, function() {
			
			/* = end of opening anims =*/
				$(isQueued).dequeue();
				www_holder.removeClass('executing');
				return;
				});
			});
		} else if (direction == 'close') { 
		
		
		/* ====================== */
		/* ==CLOSING FUNCTIONS == */
		/* ====================== */
		
		var recentPages = tech_anim_recent_work_list.hasClass('open');
		
		if (recentPages) {
			recentWebWork('close', null, true);
			var delay = 100;
		} else {
			var delay = 0;	
		}
		
		www_holder.addClass('executing');
	
		/* ====================== */
		/* ==CLOSING ANIMATIONS== */
		/* ====================== */
			
			// Animations: Stage 1
			
			// Thought Bubble
			tech_thought_bubble
			.stop()
			.delay(delay)
			.animate({'opacity' : '0'}, speed/2);
			
			// Recent Work Button
			tech_anim_recent_work
			.stop()
			.delay(delay)
			.animate({'opacity' : '0'},speed/2);
			
			// Plus Signs
			tech_anim_plus_signs
			.stop()
			.delay(delay)
			.animate({'opacity' : '0'}, speed/2, function() {
				
				// Animations: Stage 2 
				
				// iPad
				tech_anim_ipad
				.stop()
				.animate({'left' : '9em', 'scale' : '1', 'rotate' : '0', 'opacity' : '0'}, speed, easing);
				
				// Cinema Display
				tech_anim_cinema_display
				.stop()
				.animate({'left' : '9em', 'scale' : '1', 'rotate' : '0', 'opacity' : '0'}, speed, easing);
				
				// iPhone
				tech_anim_iphone
				.stop()
				.animate({'left' : '9em', 'scale' : '1', 'rotate' : '0', 'opacity' : '0'}, speed, easing);
				
				// Android
				tech_anim_android
				.stop()
				.animate({'left' : '9em', 'scale' : '1', 'rotate' : '0', 'opacity' : '0'}, speed, easing);
				
				// HTML5 Logo
				tech_anim_html5
				.stop()
				.animate({'scale' : '1', 'top' : '4em', 'opacity' : '1' },speed*1.5, 'easeOutElastic', function() {

			/* = end of closing anims =*/
				$(isQueued).dequeue();
				return;
					});	});
		} else {
				trace('Effect error. Please specify \'open\' or \'close\'.');
			if (isQueued) {$(isQueued).dequeue();}
			www_holder.removeClass('executing');
			return;
		}
			
			
	/**********************************/
	/********  CONTACT PAGE   ********/
	/**********************************/
	} else if (destination == 3) {
		
			var speed = 500;
			
		if (direction == 'open') {
		/* ====================== */
		/* ==OPENING ANIMATIONS== */
		/* ====================== */
		
		// IE ONLY!!!
		// IE ONLY!!!
		// IE ONLY!!!
		
		if (msie) {
		
		// IE Header
		contact_info_header
		.stop()
		.delay(1000)
		.animate({'opacity' : '0'},speed,easing);
		
		// IE Phone Icon
		contact_icon_phone
		.stop()
		.delay(1000)
		.animate({'top' : '2em', 'left' : '3.5em'},speed,easing);
		
		// IE Email Icon
		contact_icon_email
		.stop()
		.delay(1100)
		.animate({'top' : '2em', 'left' : '21em'},speed,easing);
		
		// IE Facebook Icon
		contact_icon_facebook
		.stop()
		.delay(1180)
		.animate({'top' : '2em', 'left' : '38.5em'},speed,easing);
				
		// IE Phone Number
		contact_info_phone
		.stop()
		.delay(1000)
		.animate({'top' : '0'}, speed, easing);
		
		// IE Email Address
		contact_info_email
		.stop()
		.delay(1100)
		.animate({'top' : '0'}, speed, easing);
		
		// IE Facebook Link
		contact_info_facebook
		.stop()
		.delay(1190)
		.animate({'top' : '0'}, speed, easing, function() {
		
		/* = end of IE opening anims =*/
		$(isQueued).dequeue();
		return; });
				
		} else {
		// OTHER BROWSERS!!!
		// OTHER BROWSERS!!!
		// OTHER BROWSERS!!!
		
		// Header
		contact_info_header
		.stop()
		.delay(1000)
		.animate({'scale' : '0', 'opacity' : '0'},speed,easing);
		
		// Phone Icon
		contact_icon_phone
		.stop()
		.delay(1000)
		.animate({'top' : '2em', 'left' : '3.5em', 'rotate' : '0'},speed,easing);
		
		// Email Icon
		contact_icon_email
		.stop()
		.delay(1100)
		.animate({'top' : '2em', 'left' : '21em', 'rotate' : '0'},speed,easing);
		
		// Facebook Icon
		contact_icon_facebook
		.stop()
		.delay(1180)
		.animate({'top' : '2em', 'left' : '38.5em', 'rotate' : '0'},speed,easing);
				
		// Phone Number
		contact_info_phone
		.stop()
		.delay(1000)
		.animate({'top' : '0'}, speed, easing);
		
		// Email Address
		contact_info_email
		.stop()
		.delay(1100)
		.animate({'top' : '0'}, speed, easing);
		
		// Facebook Link
		contact_info_facebook
		.stop()
		.delay(1190)
		.animate({'top' : '0'}, speed, easing, function() {
		
		
			/* = end of opening anims =*/
				$(isQueued).dequeue();
				return;  });}

		} else if (direction == 'close') {
			
		/* ====================== */
		/* ==CLOSING ANIMATIONS== */
		/* ====================== */
		
		if (msie) {
			
			// IE Header
			contact_info_header
			.animate({'opacity' : '1'},speed,easing);
		
			// IE Phone Icon
			contact_icon_phone
			.animate({'top' : '0', 'left' : '30em'},speed, easing);
			
			// IE Email Icon
			contact_icon_email
			.animate({'top' : '10em', 'left' : '31em'},speed,easing);
			
			// IE Facebook Icon
			contact_icon_facebook
			.animate({'top' : '5em', 'left' : '40em'},speed,easing);
			
			// IE Facebook Link
			contact_info_facebook
			.animate({'top' : '10em'}, speed, easing);
			
			// IE Email Address
			contact_info_email
			.delay(250)
			.animate({'top' : '10em'}, speed, easing);
			
			// IE Phone Number
			contact_info_phone
			.delay(500)
			.animate({'top' : '10em'}, speed, easing, function() {
				
				/* = end of IE closing anims =*/
				$(isQueued).dequeue();   return;
				});	
			
		} else {
		
		// Header
		contact_info_header
		.animate({'scale' : '1', 'opacity' : '1'},speed,easing);
	
		// Phone Icon
		contact_icon_phone
		.animate({'top' : '0', 'left' : '30em'},speed, easing);
		contact_icon_phone.animate({'rotate' : '-3'});
		
		// Email Icon
		contact_icon_email
		.animate({'top' : '10em', 'left' : '31em'},speed,easing);
		contact_icon_email.animate({'rotate' : '-1'});
		
		// Facebook Icon
		contact_icon_facebook
		.animate({'top' : '5em', 'left' : '40em'},speed,easing);
		contact_icon_facebook.animate({'rotate' : '4'});
		
		// Facebook Link
		contact_info_facebook
		.animate({'top' : '10em'}, speed, easing);
		
		// Email Address
		contact_info_email
		.delay(250)
		.animate({'top' : '10em'}, speed, easing);
		
		// Phone Number
		contact_info_phone
		.delay(500)
		.animate({'top' : '10em'}, speed, easing, function() {
			
			/* = end of closing anims =*/
			$(isQueued).dequeue(); 
			});}
		}
	} else {
		
		// Default Action:
			// Release Queue(s)
			if (isQueued) {$(isQueued).dequeue();}
			return;

	}
}



function recentWebWork(direction, isQueued, force) {
	
	var speed = 700;
	
	
	if (direction == 'open') {
		
		// Out with the old
		tech_anim_container.animate({'left' : '-32em', 'opacity' : '0'}, speed,easing);
		tech_anim_recent_work.animate({'left' : '-25em', 'opacity' : '0'}, speed*2,easing);
		
		// In with the new
		tech_anim_recent_work_list.animate({'left' : '1em', 'opacity' : '1'}, speed, easing);
		tech_anim_back.animate({'left' : '0', 'opacity' : '1'}, speed,easing, function() {
			
			tech_anim_recent_work_list.addClass('open');
			
			// Bust Queue
			if (isQueued) {$(isQueued).dequeue(); return;
			} else { return; }
			});
	
	} else if (direction == 'close') {
		
		// Out with the new
		tech_anim_recent_work_list.animate({'left' : '30em', 'opacity' : '0'}, speed, easing);
		tech_anim_back.animate({'left' : '15em', 'opacity' : '0'}, speed,easing);
		
		// Back with the old
		if (!force) {
		tech_anim_container.animate({'left' : '1em', 'opacity' : '1'}, speed,easing);
		tech_anim_recent_work.animate({'left' : '4.55em', 'opacity' : '1'}, speed,easing, function() {
			
			tech_anim_recent_work_list.removeClass('open');

				// Bust Queue
				if (isQueued) {$(isQueued).dequeue(); return;
				} else { return; }
			});	
			
		// FORCE QUICK TRANSITION
		} else {
			tech_anim_container.animate({'left' : '1em', 'opacity' : '1'}, speed,easing);
			tech_anim_recent_work.css({'left' : '4.55em'});
			tech_anim_recent_work_list.removeClass('open');
			return;
		}
	} else {
		trace('Can\'t move to recent sites.');	
	}
}


