
var ready = false;
var contentIndex = 0;
var imageIndex = 0;
var imageTimeout;
var loadingImage;
var workCount = 0;
var transitioning = false;

function getWorkCount(){
	
	workCount = 0;
	$(".work").each(
		function(){ workCount ++; }
	);
	
}

function hideURLbar(){
	window.scrollTo(0,1);
}

function buttonStates(){
	if(ready == false) return;
	
	var prevImg;
	var nextImg;
	
	if(version == 'iphone'){
		prevImg = (contentIndex == 0)?"iphone_info_prev_grey.png":"iphone_info_prev.png";
		nextImg = (contentIndex == workCount)?"iphone_info_next_grey.png":"iphone_info_next.png";
	}else{
		prevImg = (contentIndex == 0)?"html_prev_grey.gif":"html_prev.gif";
		nextImg = (contentIndex == workCount)?"html_next_grey.gif":"html_next.gif";
	}
	
	$("#prevBtn").attr('src', imageFolder+prevImg);
	$("#nextBtn").attr('src', imageFolder+nextImg);
	
}

function prevClick(){
	if(ready == false) return;
	if(contentIndex == 0) return;
	if(transitioning == true) return;
	
	_gaq.push(['_trackEvent', 'click', 'previous']);
	
	goTo(contentIndex - 1);
	
}

function nextClick(){
	if(ready == false) return;
	if(contentIndex == workCount) return;
	if(transitioning == true) return;
	
	_gaq.push(['_trackEvent', 'click', 'next']);
	
	goTo(contentIndex + 1);
	
}

function goTo(index){
	
	transitioning = true;
	
	if(imageTimeout) clearTimeout(imageTimeout);
	clearImageLoad();
	
	if(ready == true){
		
		$('#contentAnimated').animate(
				{ opacity: 0}, 
	   		200, 
	   		function() {
		    	showContent(index, true);
		  	}
		);
		
	}else{
		
		showContent(index, false);
	}
	
}

function showContent(index, animate){
	
	contentIndex = index;
	
	nextItem = $('#content'+contentIndex);
	
	$('#contentAnimated').html(nextItem.html());
	if(index == 0){
		$('#contentAnimated').addClass('home');
		$('#contentAnimated').removeClass('work');
	}else{
		$('#contentAnimated').removeClass('home');
		$('#contentAnimated').addClass('work');
	}
	
	
	if(animate == true){
		$('#contentAnimated').animate(
				{ opacity: 1}, 
	   		200, 
	   		function() {
		    	transitioning = false;
		  	}
		);
		$.scrollTo( { top:0, left:0}, 500 );
	}else{
		transitioning = false;
	}
	
	buttonStates();
	startImages();
	
}

function startImages(){
	
	clearTimeout(imageTimeout);
	clearImageLoad();
	
	if(images[contentIndex-1]==undefined || images[contentIndex-1].length == 0) return;
	
	imageIndex = 0;
	imageTimeout = setTimeout(loadNextImage, 4000);
	
}

function loadNextImage(){
	imageIndex ++;
	if(imageIndex >= images[contentIndex-1].length) imageIndex=0;
	
	loadingImage = new Image();
	
	$(loadingImage).load(imageLoadedHandler)
    .attr('src', baseUrl+images[contentIndex-1][imageIndex]);
    
    console.log('loading '+baseUrl+images[contentIndex-1][imageIndex]);
	
}

function clearImageLoad(){
	
	$(loadingImage).unbind('load');
	
}

function imageLoadedHandler(){
	
	if(this != loadingImage) return;
	
	$('#contentAnimated .displayImageHolder img').animate(
			{ opacity: 0}, 
   		200, 
   		swapImage
	);
	
}

function swapImage(){
	
	$('#contentAnimated .displayImageHolder img').replaceWith(loadingImage);
	$('#contentAnimated .displayImageHolder').animate(
			{ opacity: 1}, 
   		200, 
   		function(){
   			imageTimeout = setTimeout(loadNextImage, 4000);
   		}
	);
	
}
