//Author: Suki Design Studio



//Global variables (initialization)
var total_projects = 0;
var current_position = 0;


function loadNextImg(){
	if (current_position+2 <= total_projects-1 ){
		//alert($("#gallery-wrapper img").eq(current_position+2).attr("real_src"));
		var elem = $("#gallery-wrapper img").eq(current_position+2);
		elem.attr("src", elem.attr("alt"));
	}
}




$(function(){

	//load call and fadein for the first image
	$("#gallery-wrapper img").each(	function(){
		$(this).load(
			$(this).attr("src"), function(){
				$(this).fadeIn(2000);
			});
	});


	/*code to make the slider gallery*/
	
	total_projects = $("#gallery-wrapper").children().length;
	var project_width = 560;
	var pixels = "0";
	var prev_active = 1;
	
	//to set the total width of the ul-list so we don't have to do it in the CSS file
	$("#gallery-wrapper").css("width",total_projects*project_width+"px");
	
	
	//if there's only one img
	if($("#gallery-wrapper").innerWidth() == project_width+70 ){
		$("#next, #prev").css("visibility", "hidden");
	}
	
	$("#next").click(function(){
		
		if($('#gallery-wrapper').css("left") == "-"+(total_projects-1)*project_width+"px"){
			pixels = "0";
			current_position=0;
			prev_active = 0;
			
		} else {
			pixels = "-"+(current_position+1)*project_width;
			current_position++;
			prev_active = 1;
		};
		
		$('#gallery-wrapper').animate(	
			{	"left"		:	pixels+"px"}, 
			800,
			"easeOutQuint"
		);
		
		if(!prev_active){
			$('#prev').css("display", "none");
		} else {
			$('#prev').css("display", "block");
		}
		
		loadNextImg();
		
		return false;
	});
	
	$("#prev").click(function(){
		
		var cssLeft = $('#gallery-wrapper').css("left");
		
		pLeft = parseInt(cssLeft.substring(0,cssLeft.length-2));
		
		if(pLeft >= -1*project_width){
			pixels = 0; //"-"+(total_projects-1)*project_width;
			current_position=0;
			prev_active = 0;	
		} else {
			pixels = "-"+(current_position-1)*project_width;
			current_position--;
			prev_active = 1;
		};
						
		$('#gallery-wrapper').animate(	
			{	"left"		:	pixels+"px"}, 
			800,
			"easeOutQuint"
			
		);
		
		if(!prev_active){
			$('#prev').css("display", "none");
		} else {
			$('#prev').css("display", "block");
		}
		
		return false;
	});

	/*------------------------------------------------------------------------*/





});

