$(document).ready(function(){
//	new Preloader();
	new Slider();
});

function Slider() {
	this.xposition = 0;
	this.yposition = 0;
	this.last = Array();
	this.last[0] = $('.portrait').length - 1;
	this.last[1] = $('.nature').length - 1;
	this.last[2] = $('.urban').length - 1;
	this.last[3] = $('.journalism').length - 1;
	this.last[4] = $('.digital').length - 1;
	this.last[5] = $('.darkroom').length - 1;
	this.last[6] = $('.about').length - 1;
	this.last[7] = $('.contact').length - 1;

	this.addHandlers();
}

Slider.prototype.slideTo = function(xpos, ypos, speed) {
	var xpx = xpos * 878 + 'px'; 
	var ypx = ypos * 542 + 'px';
	$('#contentWrap').animate({ right: xpx, bottom: ypx },speed);
	this.xposition = xpos;
	this.yposition = ypos;
}

Slider.prototype.addHandlers = function() {
	var that = this;
	
	$(window).keydown(function(e){
	  	switch (e.keyCode) {
			case 37:
				e.preventDefault();
				if(that.xposition > 0)
					that.slideTo(that.xposition - 1, that.yposition, 'normal');
				else
					that.slideTo(that.last[that.yposition], that.yposition, 'fast');
				break;
				
			case 39:
				e.preventDefault();
				if(that.xposition < that.last[that.yposition])
					that.slideTo(that.xposition + 1, that.yposition, 'normal');
				else
					that.slideTo(0, that.yposition, 'fast');
				break;
  		}
	});
	
	$('.nav').click(function(e) {
		$('.mark').removeClass('mark');
		$(this).addClass('mark');
	});
		
	$('#prev').click(function(e){
		e.preventDefault();
		if(that.xposition > 0)
			that.slideTo(that.xposition - 1, that.yposition, 'normal');
		else
			that.slideTo(that.last[that.yposition], that.yposition, 'fast');
	});
	
	$('#next').click(function(e){
		e.preventDefault();
		if(that.xposition < that.last[that.yposition])
			that.slideTo(that.xposition + 1, that.yposition, 'normal');
		else
			that.slideTo(0, that.yposition, 'fast');
	});
	
	
	$('#people').click(function(e){
		e.preventDefault();
		that.slideTo(0, 0, 'normal');

	});
	
	$('#nature').click(function(e){
		e.preventDefault();
		that.slideTo(0, 1, 'normal');

	});
	
	$('#urban').click(function(e){
		e.preventDefault();
		that.slideTo(0, 2, 'normal');

	});
	
	$('#journalism').click(function(e){
		e.preventDefault();
		that.slideTo(0, 3, 'normal');

	});
	
	$('#digital').click(function(e){
		e.preventDefault();
		that.slideTo(0, 4, 'normal');

	});
	
	$('#darkroom').click(function(e){
		e.preventDefault();
		that.slideTo(0, 5, 'normal');

	});
	
	$('#about').click(function(e){
		e.preventDefault();
		that.slideTo(0, 6, 'normal');

	});
	
	$('#contact').click(function(e){
		e.preventDefault();
		that.slideTo(0, 7, 'normal');

	});
}

jQuery(function( $ ){	
	$('.overlay').css("display","block");
	$('.loading').css("display","block");
	var preloads = $('.load');
	var urls = Array();
	var count = 0;
	var total = preloads.length;
	var percent = 0;
	for(i = 0; i < preloads.length; i++) {
		urls[i] = preloads[i].src;
	}
	
	$.preload( urls, {
		onComplete:function(data){
			count++;
			var img = new Image();
			img.src = data.image;
			if(count != 0) {
				percent = Math.round((count/total)*100);
			}
			$('.loading').html(percent+'%');
		},
		onFinish:function(){
			$('.overlay').css("display","hidden");
			$('.loading').css("display","hidden");
			$('.overlay').remove()
			$('.loading').remove()
		}
	});
});