// elieni.js
// javascript library for elieni.com
// by eli enigenburg

function updateBreakdown(pos) {
	chapters = [0.0, 7.7, 65.2, 97.6, 107.2, 117.3, 127.6, 163.1, 181.9, 185.7, 195.9, 200.5, 209.2, 212.4, 216.6, 220.8, 225.6, 227.7, 233.3, 240];
	breakdown = [];
	breakdown[0] = '&nbsp;';
	breakdown[1] = 'V <span style="color: #adada8">&nbsp;|&nbsp;</span> animation director <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, character designs, models';
	breakdown[2] = 'Public Trust <span style="color: #adada8">&nbsp;|&nbsp;</span> director <span style="color: #adada8">&nbsp;|&nbsp;</span> camera, animation, rigging, models, lighting/rendering, compositing';
	breakdown[3] = 'Comfort <span style="color: #adada8">&nbsp;|&nbsp;</span> director <span style="color: #adada8">&nbsp;|&nbsp;</span> animation supervision, effects, lighting/rendering';
	breakdown[4] = '&nbsp;';
	breakdown[5] = 'G.I. Joe <span style="color: #adada8">&nbsp;|&nbsp;</span> previs artist <span style="color: #adada8">&nbsp;|&nbsp;</span> camera, previs character animation, missiles, smoke, missile launcher';
	breakdown[6] = 'The Flash <span style="color: #adada8">&nbsp;|&nbsp;</span> previs artist <span style="color: #adada8">&nbsp;|&nbsp;</span> camera, animation, effects, lighting/rendering, compositing, audio';
	breakdown[7] = 'The Bourne Conspiracy <span style="color: #adada8">&nbsp;|&nbsp;</span> previs artist <span style="color: #adada8">&nbsp;|&nbsp;</span> camera, animation, effects, lighting/rendering, compositing';
	breakdown[8] = '&nbsp;';
	breakdown[9] = 'The Flash <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> camera, animation, shaders, lighting/rendering, compositing, audio';
	breakdown[10] = 'Butterflies <span style="color: #adada8">&nbsp;|&nbsp;</span> animation supervisor <span style="color: #adada8">&nbsp;|&nbsp;</span> animation (2nd shot), effects, lighting/rendering, compositing';
	breakdown[11] = 'Robot <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, effects, models, lighting/rendering';
	breakdown[12] = 'Legs <span style="color: #adada8">&nbsp;|&nbsp;</span> animation director <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, models, lighting/rendering';
	breakdown[13] = 'Viking <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, lighting/rendering';
	breakdown[14] = 'Sly Cooper <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, models, lighting/rendering, compositing';
	breakdown[15] = 'Medieval <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, effects, lighting/rendering, compositing';
	breakdown[16] = 'Venus <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, rigging, models, lighting/rendering';
	breakdown[17] = 'Neopets <span style="color: #adada8">&nbsp;|&nbsp;</span> animator <span style="color: #adada8">&nbsp;|&nbsp;</span> animation, lighting/rendering, compositing';
	breakdown[18] = '&nbsp;';
	for (i = 0; i < (chapters.length - 1); i++) {
		if (pos >= chapters[i] && pos < chapters[i + 1]) {
			$('#breakdown').html(breakdown[i]);
			break;
		}
	}
}


function switchLanguage(val) {
	document.lang_form.lang.value = val;
	document.lang_form.submit();
}

function getMovieIndexByPlayerId(moviePlayerId) {
	for (i = 0; i < moviePlayerIds.length; i++) {
		if (moviePlayerIds[i] == moviePlayerId) return (i + 1);
	}
	return 0;
}

function changeStatus(msg) {
	window.status = msg;
	return true;
}

function imageFlip(i, img_name, msg) {
	document.getElementById(img_name).src = i;
	window.status = msg;
	return true;
}

// set/update the position of the ".centered" content div
function centeredContent() {
	h = getViewportDimension('h');
	$('.centered').css({
		'marginTop': (h - ($('.centered').height())) / 2 + 'px',
	});
}

function setFrameDims() {
	var ht = (getViewportDimension('h') - 102);
	var wt = getViewportDimension('w');
	document.getElementById('resume_frame').height = ht;
	$('#projects_frame').css({
		height: ((ht - 5) + 'px'), width: ((wt - 100) + 'px')
	});
	$('.resume_docs').css({
		left: ((((wt - 865) / 2) + 18) + 'px')
	});
	$('#projects_iframe').attr('height', (ht - 5));
	$('#resume_frame').css({
		height: ((ht - 5) + 'px'), width: ((wt - 100) + 'px')
	});
}

function theaterGo(v) {
	if (v) {
		$('#theater').fadeTo(1500, 0.5);
		$('.movieLink').fadeTo(1500, 0.25);
		$('.nav_bot span, .nav_side span, .nav_top span').fadeTo(1500, 0.25);
	} else {
		$('#theater').fadeTo(1500, 0);
		$('.movieLink').fadeTo(1500, 1.0);
		$('.nav_bot span, .nav_side span, .nav_top span').fadeTo(1500, 1.0);;
	}
}

function soundGo(s, delay) {
	for (a = 0; a < audioChannels.length; a++) {
		thisTime = new Date();
		if (audioChannels[a]['finished'] < thisTime.getTime()) {
			audioChannels[a]['finished'] = thisTime.getTime() + document.getElementById(s).duration * 1000;
			audioChannels[a]['channel'].src = document.getElementById(s).src;
			audioChannels[a]['channel'].load();
			if (!delay) {
				audioChannels[a]['channel'].play();
			} else {
				setTimeout("audioChannels[a]['channel'].play();", delay);
			}
			break;
		}
	}
}

function tweetGo(twCurr, twMax) {
	transTime = 1000;
	cycleTime = 10000;
	if (twCurr != 0) {										// if not the first time
		tweetDisp(0, transTime);						// hide tweet
	} else {
		$('.tweet').tweet({page: 1});				// otherwise get the first tweet
	}
	if (twCurr < twMax) {									// if the current tweet is less than the max number of tweets
		n = twCurr + 1;											// increment currect tweet
	} else {
		n = 1;															// otherwise start over
	}
	twCurr = n;														// and set the global current tweet
	if (twMax > 1) {
		setTimeout('$(\'.tweet\').tweet({page: ' + n + '});', transTime + 100);		//set the timer to get the next tweet after the fade out
		setTimeout('tweetGo(' + twCurr + ', ' + twMax + ');', cycleTime);					//set the timer to begin the next cycle
	}
	setTimeout('tweetDisp(1, transTime)', transTime + 100);										//set the timer to show tweet (100ms after getting it)
}

function tweetDisp(disp, transTime) {
	if (!disp) {
		if (twMax > 1) {
			setTimeout('$(\'.tweet\').empty();', transTime + 100);
		}
		if ($('.tweet').css('display') != 'none') {
			$('.tweet').css({zIndex: -99});
			$('.tweet').fadeOut(transTime);
		}
	} else {
		if ($('.tweet').css('display') == 'none') {
			if (twVis && !movieReady) {
				$('.tweet').css({zIndex: 0});
				$('.tweet').fadeIn(transTime);
			}
		}
	}
}

function contentGo(id) {
	fadeTime = 400;
	if (id != 'movie_menu') {
		twVis = 0;
		tweetDisp(0, 500);
		resetMovies();
		movieBugGo(1);
		$('.slide').css({zIndex: -99});
		if (twMax > 1) {
			setTimeout('$(\'.tweet\').empty();', 500);
		}
	} else {
		twVis = 1;
		movieBugGo(0);
		$('.slide').css({zIndex: 0});
	}
	id = ('#' + id);
	for (i = 0; i < contentBlockIds.length; i++) {
		if (contentBlockIds[i] != id) {
			if ($(contentBlockIds[i]).css('display') != 'none') {
				$(contentBlockIds[i]).fadeOut(fadeTime);
			}
			$(contentBlockIds[i]).css({zIndex: -99});
		}
	}
	setTimeout('$(\'' + id + '\').fadeIn(fadeTime)', fadeTime);
	$(id).css({zIndex: 10});
}

function movieBugGo(v) {
	if (v) {
		$('#splash').fadeOut(1500);
		$('#myname').animate({
			marginLeft: -200}, {
			duration: 500,
			easing: 'easeInBack'
		});
		$('.contentcopy#jobtitle').animate({
			left: -220}, {
			duration: 600,
			easing: 'easeInBack'
		});
		tweetDisp(0, 1000);
	} else {
		$('#splash').fadeIn(1500);
		$('#myname').animate({
			marginLeft: -5}, {
			duration: 500,
			easing: 'easeOutBounce'
		});
		$('.contentcopy#jobtitle').animate({
			left: 90}, {
			duration: 600,
			easing: 'easeOutBounce'
		});
		tweetDisp(1, 1000);
	}
	theaterGo(movieReady * (($('.slide').css('zIndex')) == 0));
}

// get the specified dimension of the browser viewport to know how far "offscreen" is
function getViewportDimension(d) {
	var viewportwidth;
	var viewportheight;
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	if (d == "w") {
		return viewportwidth;
	} else if (d == "h") {
		return viewportheight;
	}
}

function moveMovieMenu(a, v) {
	v = Math.round(v);
	if (v == 0) {
		movieBugGo(1);
		v = 7;
	} else {
		movieBugGo((Math.abs(movieReady - 1) * (($('.slide').css('zIndex')) != 0)));
	}
	var i = 0;
	for (i = 0; i < numMovies; i++) {
		$icon = $('#movie_icon' + (i + 1));
		if (a) {
			$icon.stop(1,0).animate({top: v + 'px'}, {duration: (700 + (100 * (i + 1))), easing: 'easeOutQuint'});
		} else {
			$icon.css({top: v + 'px'});
		}
	}
}

function moveMovies() {
	var offset = (getViewportDimension('h') * 0.5);
	var i = 0;
	for (i = 0; i < numMovies; i++) {
		var rounded = Math.round((offset - (movieHeights[i] + 220) * 0.5));
		$('.movie' + (i + 1)).css({top: rounded + 'px'});
	}
}

// slide an element to the center (toggle) or slide an element just offscreen based on the viewport size
function movieGo(moviePlayerId) {
	var movieIndex = getMovieIndexByPlayerId(moviePlayerId);
	var $marginSlider = $('#movies div.movie' + movieIndex);
	$marginSlider.css({visibility: 'visible'});
	var offscreen = (getViewportDimension('w') + 50);
	var onscreen = 0;
	$marginSlider.animate({
		marginLeft: parseInt($marginSlider.css('marginLeft'),10) == onscreen ? offscreen : onscreen}, {
		duration: 1100,
		easing: 'easeInOutElastic',
		completed: movieReady = (parseInt($marginSlider.css('marginLeft'),10) == onscreen ? 0 : 1)
	});
	// this is the movie menu bar part
	v = Math.floor((Math.abs(movieReady - 1) * ((getViewportDimension('h') * 0.5) - 45))); // this is the amount to slide up
	if (!v) {
		moveMovieMenu(1, v);
	} else {
		setTimeout("moveMovieMenu(1, v);", 650);
	}
}

// when the elements are all marked up, hide them offscreen
function initMovies() {
	var i;
	for (i = 0; i < numMovies; i++) {
		var $marginSlider = $('#movies div.movie' + (i + 1));
		$marginSlider.css({
			marginLeft: ((getViewportDimension('w') + 50) + 'px')
		});
	}
}

// if there are visible movies, send them offscreen
function resetMovies() {
	var i;
	for (i = 0; i < numMovies; i++) {
		jwplayer(moviePlayerIds[i]).stop();
		var $marginSlider = $('#movies div.movie' + (i + 1));
		if (parseInt($marginSlider.css('marginLeft'),10) == 0) {
			movieGo(moviePlayerIds[i]);
		}
	}
}

// for each movie that is offscreen, update its margin and visibility
function updateMovies() {
	var i;
	for (i = 0; i < numMovies; i++) {
		var $marginSlider = $('#movies div.movie' + (i + 1));
		if (parseInt($marginSlider.css('marginLeft'),10) != 0) {
			$marginSlider.css({marginLeft: ((getViewportDimension('w') + 50) + 'px')});
			$marginSlider.css({visibility: 'hidden'});
		}
	}
	moveMovieMenu(0, (Math.abs(movieReady - 1) * (($('.slide').css('zIndex')) == 0) * ((getViewportDimension('h') * 0.5) - 45)));
	moveMovies();
}
