//////////////////////////////////////////////// INITIALIZE APPLICAITON ////////////////////////////////////////////////

$(document).ready(function() {
						   
	initWindowResizing();
	initCarousel();
	initKeyboard();
	initTray();
	initFooter();
	initLoading();

});

function trace(arg) {
	document.getElementById('terms').innerHTML = arg;
};



//////////////////////////////////////////////// INITIALIZE LOADING ////////////////////////////////////////////////

function initLoading() {
	$('.loader').html("Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience! Now loading... thanks for your patience!");
}


//////////////////////////////////////////////// INITIALIZE WINDOW RESIZE HANDLING ////////////////////////////////////////////////

function initWindowResizing() {
	windowResizeHandler();
	setTimeout("windowResizeHandler()", 2000);  // Fail-safe for Safari on iPhone
	setTimeout("windowResizeHandler()", 4000); 
	setTimeout("windowResizeHandler()", 6000); 
	setTimeout("windowResizeHandler()", 8000); 
	setTimeout("windowResizeHandler()", 12000); // Fail-safe for Safari on iPhone (on slow connection)
	$(window).resize(windowResizeHandler);
}
	
function windowResizeHandler() {
	
	var viewerHeight = $('#viewer').height();
	var imgHeight = $('#work :first-child img').height();
	var topOffset = (viewerHeight - imgHeight) / 2;
	
	if(imgHeight < viewerHeight) {
		 $('#work').css('top',topOffset);
		 $('#work li img').css('margin-top',0);
	} else {
		 $('#work').css('top',0);
		 $('#work li img').css('margin-top',topOffset);
	}
	
	var loaderHeight = $('#work li:first-child img').outerHeight();	
	$('.loader').css('height',loaderHeight);
	
}


//////////////////////////////////////////////// INITIALIZE CAROUSEL ////////////////////////////////////////////////

var activeWork = 1;
var totalWorks = $('#work').children().length;

function initCarousel() {
	$('#pattern').css({'opacity':0.06, 'display':'block'});
	
	if(window.location.hash) {		
		for(i = 0; i <= totalWorks; i++) {
			if($('#thumbs li:nth-child('+i+') a').attr('href') == window.location.hash) {
				activeWork = i;
			}
		}
	}

	
	$('#left').click(function(){
		if(activeWork != 1) {
			onState(-1);
		}
		
		return false;
	});
	
	$('#left').hover(
		function(){
				$('#left span').css({left:'12px',opacity:'0.5'}).stop().animate({left:'7px',opacity:'1'}, 300, 'easeInOutCubic');
		},
		function(){
				$('#left span').css('left','7px')
		}
	);
	
	$('#right').click(function(){
		if(activeWork != totalWorks) {					   
			onState(1);
		}
		
		return false;
	});
	
	$('#right').hover(
		function(){
				$('#right span').css({right:'12px',opacity:'0.5'}).stop().animate({right:'7px',opacity:'1'}, 300, 'easeInOutCubic');
		},
		function(){
				$('#right span').css('right','7px')
		}
	);
	
	for(i = 0; i <= totalWorks; i++) {
		$('#work li:nth-child('+i+') .loader').css('color', $('#tray #description li:nth-child('+i+')').css('color'));	
	}
	
	$('#tray #thumbs li:nth-child('+activeWork+')').addClass('active');
	
}

function initKeyboard() {
	$(document.documentElement).keyup(function (event) {
		// handle cursor keys
		if (event.keyCode == 37) {
			if(activeWork != 1) {
				onState(-1);
			}
		} else if (event.keyCode == 39) {
			if(activeWork != totalWorks) {					   
				onState(1);
			}
		}
	});
}

function onState(change) {
	activeWork += change;
	
	window.location.hash = $('#thumbs li:nth-child('+activeWork+') a').attr('href');
	
	pageTracker._trackPageview(window.location.hash);
	
	var targetPercent = (-100 * (activeWork - 1)) +"%";
	$('#work').stop().animate({left:targetPercent}, 900, 'easeInOutCubic');
	
	$('#tray #thumbs li').removeClass('active');
	$('#tray #thumbs li:nth-child('+activeWork+')').addClass('active');
	$('#tray #description li').hide();
	$('#tray #description li:nth-child('+activeWork+')').fadeIn(600);
	
	var activeColor = $('#tray #description li:nth-child('+activeWork+')').css('color');
	$('#tray #toggle-tray').stop().animate({ color: activeColor},600);
	$('#contact').stop().animate({ color: activeColor},600);
	$('#twitter_update_list a').stop().animate({ color: activeColor},600);
	
	if(activeWork == 1) {
		$('#left').stop().animate({'margin-left':'-50px'}, 600, 'easeInOutCubic');
	} else {
		$('#left').stop().animate({'margin-left':'0'}, 600, 'easeInOutCubic');
	}
	
	if(activeWork == totalWorks) {
		$('#right').stop().animate({'margin-right':'-50px'}, 600, 'easeInOutCubic');
	} else {
		$('#right').stop().animate({'margin-right':'0'}, 600, 'easeInOutCubic');
	}
}


//////////////////////////////////////////////// INITIALIZE TRAY ////////////////////////////////////////////////

function initTray() {
	$('#tray #thumbs li').css({'opacity':0.25});
	$('#tray #description li').hide();
	
	// Toggle tray function
	$('#tray').hover(
		function(){
				$('#tray').addClass('open');
				$('#tray').stop().animate({top:'0'}, 400);
		},
		function(){
				$('#tray').removeClass('open');
				$('#tray').stop().animate({top:'-70px'}, 400);
		}
	);
	
	for(var i = 0; i <= $('#tray #thumbs').children().length; i++) {
		$('#tray #thumbs li:nth-child('+i+')').attr('name',i);
	}
	
	$('#tray #thumbs li').hover(
		function () {
			$(this).animate({opacity:'1'}, 300);
		}, 
		function () {
			$(this).animate({opacity:'0.5'}, 300);
		}
	);
	
	$('#tray #thumbs li').click(
		function() {
			activeWork = parseInt($(this).attr('name'));
			onState(0);
		}
	);
	
	
	//Get our elements for faster access and set overlay width
    var holder = $('#tray');
	var ul = $('#thumbs');
		
	//Find last image container
	var lastLi = ul.find('li:last-child');
	
	//When user move mouse over menu
	holder.mousemove(function(e){
		var holderWidth = holder.width();
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
		
		if(ulWidth < holderWidth) {
			ul.animate({left: 0},20);
		} else {
			var left = -(e.pageX - holder.offset().left) * (ulWidth-holderWidth) / holderWidth;
			ul.animate({left: left},20);
		}
	});
	
	onState(0);
}



//////////////////////////////////////////////// INITIALIZE FOOTER ////////////////////////////////////////////////

var activeTweet = 0;
var totalTweets = 8;

function initFooter() {
	$('#follow').animate({opacity:'0.3'});
	
	$('#follow').hover(
		function () {
			$(this).animate({opacity:'1'}, 300);
		}, 
		function () {
			$(this).animate({opacity:'0.3'}, 300);
		}
	);
	
	$('#twitter_update_list').hover(
		function () {
			$(this).addClass('over');
		}, 
		function () {
			$(this).removeClass('over');
		}
	);
	
	$.getScript("http://twitter.com/javascripts/blogger.js");
	$.getScript("http://twitter.com/statuses/user_timeline/zoeweir.json?callback=twitterCallback2&count="+totalTweets, function() {
		$('#twitter_update_list li').hide();
		$('#twitter_update_list li a').attr('target','_blank');
		
		var activeColor = $('#tray #description li:nth-child('+activeWork+')').css('color');
		$('#twitter_update_list a').animate({ color: activeColor},600);
		
		tweetTimer();
	});
}

function tweetTimer() {
	activeTweet++;
	
	if(activeTweet > totalTweets) {
		activeTweet = 1;
	}
	
	if(!$('#twitter_update_list').hasClass('over')) {
		$('#twitter_update_list li').hide();
		$('#twitter_update_list li:nth-child('+activeTweet+')').fadeIn(600);
	}
	tt = setTimeout("tweetTimer()", 6000);
}
