var numberImages = 0;
var click = 0;
var lastDirection;

jQuery(function() {
	// load images
	if(jQuery("#folio_slide").length > 0) {
		jQuery("#folio_slide li:last").css("background", "white");
		numberImages = jQuery("#folio_slide ul img").length;
	}

	jQuery(".folio_slide_nav a").click(function() {
		// increment clicks
		
		if(click >= 1) {
			click++;
			return false;
		}
		else {
			click++;
			scrollPage(window.scrollX, jQuery(this).parent().hasClass("fsPrevBtn"));
		}
		return false;
	});
	
	$(".fullwrap a").click(function() {
		var href = $(this).attr("href");
		
		$(".body").fadeOut('slow', function() {
			window.location = href;
		});
		
		return false;
	});
});

function imageLoaded(t) {
	jQuery("#folio_slide img").eq(t).animate({opacity: '1'}, 'fast');
	if(numberImages - 1 == 0) {
		// set last areas width to window.innerwidth
		var mR = parseInt(jQuery("#folio_slide li:last").css("marginRight"));
		if(window.innerWidth - mR > 700) {
			jQuery("#folio_slide li:last").width(window.innerWidth - mR).css("marginRight", "0");
		}
		
		var w = 0;
		jQuery("#folio_slide li").each(function() {
			w += jQuery(this).outerWidth() + parseInt(jQuery(this).css("marginRight"));
		});
		
		if(jQuery("#folio_slide li:last img").length == 0) {
			jQuery("#folio_slide li:last > div").css("width", "480px");
		}
		
		jQuery("#folio_slide").css("width", w + "px");
		
		// console.log("width set");
	}
	else {
		numberImages--;
	}
} 

function scrollPage(current, leftRight) {
	
	
	var left = 0;

	if(leftRight) { // move left
		
		var temp = 0;
		var r = jQuery("#folio_slide li").get().reverse();
		jQuery(r).each(function() {
			left = jQuery(this).position().left;
			if(left < current) {
				return false;
			}
		});
	}
	else { // move right
		
		jQuery("#folio_slide li").each(function() {
			left = jQuery(this).position().left;
			if(left > current + 1) {
				return false;
			}
		});
	}
	
	
	
	lastDirection = leftRight;
	
	
	$('html,body').animate({
		scrollLeft: left
    }, 1000, function() { 
    	if(jQuery(this).is("html")) {
    		
    		click--; 
	    	if(click > 0) { 
	    		
	    		scrollPage(window.scrollX, lastDirection); 
	    	}
    	}    	 
    });
}
