var slideshowSpeed =15000;
var rand_slideshow =1;

var photos = [ {
		"title" : "",
		"image" : p+"sys/img/imprezy_firmowe/mirage_f1.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}, {
		"title" : "",
		"image" : p+"sys/img/imprezy_firmowe/mirage_f2.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}
];


var navigate;
var interval;
$(document).ready(function() {

	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	 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;
		}
		
		
		if(rand_slideshow)currentImg=rand(1,photos.length);
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	navigate("next");
	
});

function firmowe_back_run(){
	interval = setInterval(function() {		navigate("next");	}, slideshowSpeed);	
}

function firmowe_back_stop(){
	clearInterval(interval);
}
