// JavaScript Document

/* GLOBAL VARIABLES */
	var currentScene = 3;
	var sceneMove = new Array([],[],[],[],[]);
	
$(document).ready(function(){
						   
	$('#sendFriendSuccess').hide(0);
	$('#sendFriendError').hide(0);
	$('#sendFriend').validate();
	
	if($(window).width()>700){
		
		if( String(window.location).indexOf('#') === -1 ){
			window.location += '#content3';
			changeScene('#scene'+3);
		} else {
			currentScene = String(window.location).substr( String(window.location).length-1, String(window.location).length-2 );
			changeScene('#scene'+currentScene);
		}
		
	} else {
		if( String(window.location).indexOf('#') === -1 ){
			window.location += '#content0';
		} else {
			currentScene = String(window.location).substr( String(window.location).length-1, String(window.location).length-2 );
			changeScene('#scene'+currentScene);
		}
	}
		
	$(window).bind('resize', function(){
		if($(window).width()>700){
			changeScene('#scene'+currentScene);
		}
	});
	
	$('#navigation a').bind('click', function(event){
		if($(window).width()>700){
			event.preventDefault();
			changeScene($(this).attr('href'));
		} /*
		else {
			$('#navigation a').removeClass('active');
			$('#nav-content'+$(this).attr('href')).addClass('active');
		}
		*/
	});	
	
	$('#sendFriendSubmit').bind('click', function(event){
		event.preventDefault();
  		if($('#sendFriend').valid()){
			sendFriend();
		}
    });
	
	$('#sendFriendAgain').bind('click', function(event){
		event.preventDefault();
  		$('#sendFriendSuccess').hide();
		$('#friendName').val('');
		$('#friendEmail').val('');
		$('#sendFriend').show();
    });
	
});

function changeScene(sceneID){
	
	sceneID = sceneID.substr(sceneID.length-1, sceneID.length);
	prevScene = currentScene;
	currentScene = sceneID;
	dur = Math.abs(prevScene - currentScene) * 2500;
	
	$('.content').css('height','auto').css('overflow', 'hidden');
	
	
	$('#scene-wrapper').stop()
					   .scrollTo('#content'+sceneID, dur, { onAfter:
								 function(){
										setLocation(); 
										checkContentHeight();
										$('#navigation a').removeClass('active');
										$('#nav-content'+sceneID).addClass('active');
									} 
								});
					   
	
	$('#background-wrapper').stop().animate({left:(20*sceneID-60)+'%'}, dur);
	$('#people-wrapper').stop().animate({left:(25*(1-sceneID))+'%'}, dur)
	
	for(var i=0; i<$('.moved').length; i++){		
		
		$('.moved:eq('+i+')')
				.stop()
				.addClass('walking')
				.animate({left:$('.moved:eq('+i+')').css('textIndent')}, dur/1.5, 
														function(){
															 $(this).css('textIndent', 0);
															 $(this).removeClass('moved');
															 $(this).removeClass('walking');
														 });
	};
	
	for(var i=0; i<sceneMove[currentScene-1].length; i++){
		$(sceneMove[currentScene-1][i][0])
									.stop()
									.css('textIndent', sceneMove[currentScene-1][i][2])
									.addClass('walking')
									.addClass('moved')
									.delay(dur/3)
									.animate({left:sceneMove[currentScene-1][i][1]}, dur, 
														 function(){
															 $(this).removeClass('walking');   
														 });
	};
	
}

function setLocation(){
	window.location = String(window.location).substr(0, String(window.location).length-1) + currentScene;
}

function checkContentHeight(){
	windowHeight = $(window).height();
	contentHeight = $('#content'+currentScene).height();
	
	if( contentHeight > windowHeight){
		$('#content'+currentScene).css('height','100%').css('overflow', 'scroll');
	} else {
		$('#content'+currentScene).css('height','auto').css('overflow', 'hidden');
	}
}

function sendFriend(){
	sendData = $('#sendFriend').serialize();
	
	$.ajax({
	  url: "sendMail.php",
	  type: "POST",
	  data: sendData,
	  success: function(successful){
		if(successful){
			$('#sendFriend').hide();
			$('#sendFriendSuccess').show();
		}
		else{
			$('#sendFriend').hide();
			$('#sendFriendError').show();
		}
	  }
	});
	
}

