/* A Million Typing Monkeys were here */
var currentPage = false;
var windowWidth = false;
var windowHeight = false;
var timer = false;

/* Ship */
var shipInt = false;
var shipDirection = 'r';
var shipPosX = -80;
var shipPosY = 100;
var shipWidth = 80;
var shipSpeed = 1;

/* Cloud */
var cloudInt = false;
var cloudSpeed = 1;
var cloudDirection = 'l';
var cloudPosX = false; // starts from the right
var cloudPosY = 200;
var cloudWidth = 400;
var cloudHeight = 189;

/* Other */
var groundHeight = 150;

/**
 * Nothing Could Go Wrong
 */
$(document).ready(function() {
	init(true);	

	shipInt = setInterval('ship()',150);
	cloudInt = setInterval('cloud()',1000);
	
	$('#nav a').click(function() {
		currentPage = $(this).attr('href').replace('#','');
		toPage();
		return false;
	});
});

$(window).bind('resize',function() {
	init(false);
});

/**
 * Init
 */
function init(setInitialPlaces) {
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	paddingLeft = $('.page').css('paddingLeft');
	cloudPosY = (windowHeight / 3) - (cloudHeight / 2);
	cloudPosX = windowWidth - (windowWidth/10) - cloudWidth;

	$('#container').css({'marginTop':(windowHeight+100)+'px'});
	$('.page').css({'width':(windowWidth-paddingLeft)+'px'});
	$('#logo').css({'top':cloudPosY+'px','left':cloudPosX+'px'});
	
	if(setInitialPlaces) {
		$('#ship').css({'top':shipPosY+'px'},100);
	} else {
		$('#ship').css({'top':shipPosY+'px'},100);
	}
}

/**
 * Page moving
 */
function toPage() {
	if(currentPage) {
		$.scrollTo('#'+currentPage,500);
		$('#nav li').removeClass('current');
		$('#nav-'+currentPage).addClass('current');
	}
}

function toNextPage() {
	if(currentPage) {
		currentPageIndex = $('#'+currentPage).index();
		if(pageId = $('.page').eq(currentPageIndex).next('.page').attr('id')) {
			currentPage = pageId;
			toPage();
		}
	} else {
		currentPage = $('.page:first').attr('id');
		toPage();
	}
}

function toPreviousPage() {
	if(currentPage) {
		currentPageIndex = $('#'+currentPage).index();
		if(pageId = $('.page').eq(currentPageIndex).prev('.page').attr('id')) {
			currentPage = pageId;
			toPage();
		}
	} else {
		return false;
	}
}

/**
 * Ship!
 */
function ship() {
	if(shipDirection == 'r') {
		shipPosX += shipSpeed;
	} else {
		shipPosX -= shipSpeed;
	}
	
	if(shipPosX > (windowWidth - shipWidth - 5)) {
		shipDirection = 'l';
		$('#ship').removeClass('ship-right').addClass('ship-left');
		shipPosY = (Math.random() * (windowHeight - (windowHeight / 2))) + 100;
	} else if(shipPosX < (0 - (shipWidth*2))) {
		shipDirection = 'r';
		$('#ship').removeClass('ship-left').addClass('ship-right');
		shopPosY = (Math.random() * (windowHeight/4)) + 100;
		$('#ship').css({'top':shipPosY+'px'});
	}
	
	$('#ship').css({'left':shipPosX+'px'});	
}

/**
 * Cloud!
 */
function cloud() {
	if(cloudDirection == 'r') {
		cloudPosX += cloudSpeed;
	} else {
		cloudPosX -= cloudSpeed;
	}
	
	if(cloudPosX > (windowWidth + (cloudWidth * 2))) {
		cloudDirection = 'l';
	} else if(cloudPosX < (0 - (cloudWidth*2))) {
		cloudDirection = 'r';
	}
	
	$('#logo').css({'left':cloudPosX+'px'});	
}
