var slideshowSpeed = 2000;
var k=0;
$(document).ready(function() {
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {		
		if(animating) {
			return;
		}
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}			
		showImage(photos[currentImg - 1], currentContainer, activeContainer);		
	};	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {			
		animating = true;		
		currentZindex--;		
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block"			
		});		
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {				
				animating = false;
			}, 500);
		});		
	};	
	navigate("next");	
	interval = setInterval(function() {
		if(k==1) navigate("next");
	}, slideshowSpeed);
	
});

(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", "images/"+imgArr[i].image)
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);

