var slideshowPlaying = true;

var slideshowSpeed = 4000;
var fadeOutSpeed = 2000;

var photos = [ {
		"image" : "/images/site-design/home_splash_banner1.jpg"
	}, {
		"image" : "/images/site-design/home_splash_banner2.jpg"
	}, {
		"image" : "/images/site-design/home_splash_banner3.jpg"
	}
];

var ticketBoxClass = [ {
		"theclass" : "splashTicketBox2"
	}, {
		"theclass" : "splashTicketBox1"
	}, {
		"theclass" : "splashTicketBox1"
	}
];

jQuery(document).ready(function() {
	
	var interval = setInterval(function() {
		if (slideshowPlaying) {
			navigate("next");
		}
	}, slideshowSpeed);
		
	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], ticketBoxClass[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var bottomZindex = 10;
	var topZindex = 11;
	var showImage = function(photoObject, classObject, currentContainer, activeContainer) {
		animating = true;
		jQuery("#headerimg" + currentContainer).css('z-index', topZindex);
		jQuery("img","#headerimg" + activeContainer).attr("src",photoObject.image);
		jQuery("#headerimg" + activeContainer).css({
			'z-index': bottomZindex,
			'display': 'block'
		});
		jQuery(".ticket_ad, .ticket_form","#headerimg" + activeContainer).removeClass("splashTicketBox1");
		jQuery(".ticket_ad, .ticket_form","#headerimg" + activeContainer).removeClass("splashTicketBox2");
		jQuery(".ticket_ad, .ticket_form","#headerimg" + activeContainer).addClass(classObject.theclass);
		jQuery("#headerimg" + currentContainer).fadeOut(fadeOutSpeed, function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		slideshowPlaying = false;
	};
	
	navigate("next");
	
	interval = setInterval(function() {
		if (slideshowPlaying) {
			navigate("next");
		}
	}, slideshowSpeed);
	
	jQuery(".ticket_ad").click(function() {
		jQuery(".ticket_ad").hide();
		jQuery(".ticket_form").show();
		stopAnimation();
	});
	
});
