/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/


   var images = new Array()
   			function preload() {
   				for (i = 0; i < preload.arguments.length; i++) {
   					images[i] = new Image()
   					images[i].src = preload.arguments[i]
   				}
   			}
   			preload(
   				"images/ac_painting_top.jpg",
   				"images/ac_painting_footer.jpg",
   				"images/40acres_grass_top.jpg",
   				"images/40acres_grass_footer.jpg",
   				"images/skeane_bg_top.jpg",
   				"images/skeane_bg_foot.jpg",
   				"images/dartry_bg_top.jpg",
   				"images/dartry_bg_foot.jpg",
   				"images/accordion_bg_top.jpg",
   				"images/accordion_bg_foot.jpg",
   				"images/rira_bg_top.jpg",
   				"images/rira_bg_foot.jpg",
   				"images/harp_bg_top.jpg",
   				"images/harp_bg_foot.jpg",
   				"images/jfaulkner_bg_top.jpg",
   				"images/jfaulkner_bg_foot.jpg",
   				"images/aindrias_bg_top.jpg",
   				"images/aindrias_bg_foot.jpg",
   				"images/session_bg_top.jpg",
   				"images/session_bg_foot.jpg",
   				"images/kidsparty_bg_top.jpg",
   				"images/kidsparty_bg_foot.jpg",
   				"images/tunepal_bg_top.jpg",
   				"images/tunepal_bg_foot.jpg",
   				"images/violin_bg_top.jpg",
   				"images/violin_bg_foot.jpg"
			)

  
// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "Welcome to the 2011 Tuam Trad Festival",
		"headimage" : "ac_painting_top.jpg",
		"footimage" : "ac_painting_footer.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "",
		"secondline" : "2nd line 1?"
	}, {
		"title" : "The best of traditional entertainment",
		"headimage" : "40acres_grass_top.jpg",
		"footimage" : "40acres_grass_footer.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "",
		"secondline" : "2nd line 2"
	}, {
		"title" : "Se&aacute;n Keane",
		"headimage" : "skeane_bg_top.jpg",
		"footimage" : "skeane_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "OpenDialogNyName('seankeane');return:false;",
		"firstline" : "Official Opening, Fri 16th, 7pm",
		"secondline" : "2nd line 9"
	}, 
	{
		"title" : "Dartry Céilí Band",
		"headimage" : "dartry_bg_top.jpg",
		"footimage" : "dartry_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "The Ard R&iacute; Hotel, Sat 17th",
		"secondline" : "2nd line 7"
	}, {
		"title" : "Gary Quinn",
		"headimage" : "accordion_bg_top.jpg",
		"footimage" : "accordion_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "",
		"secondline" : "2nd line 8"
	}, {
		"title" : "John Faulkner",
		"headimage" : "jfaulkner_bg_top.jpg",
		"footimage" : "jfaulkner_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "Geoghegan's, Sun 18th, 3pm",
		"secondline" : "2nd line 5"
	}, {
		"title" : "Aindrias de Staic",
		"headimage" : "aindrias_bg_top.jpg",
		"footimage" : "aindrias_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "Canavan's, Sat 17th, 9.30pm",
		"secondline" : "2nd line 5"
	}, {
		"title" : "R&iacute; R&aacute;",
		"headimage" : "rira_bg_top.jpg",
		"footimage" : "rira_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "",
		"secondline" : "2nd line 5"
	}, {
		"title" : "Groupa&iacute; Cheoil",
		"headimage" : "harp_bg_top.jpg",
		"footimage" : "harp_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "Corofin CCE & Miltown CCE",
		"secondline" : "2nd line 6"
	}, {
		"title" : "Open Sessions ",
		"headimage" : "session_bg_top.jpg",
		"footimage" : "session_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "Free, open sessions all weekend",
		"secondline" : "2nd line 2"
	}, {
		"title" : "Kids Party Club &amp; Trad Disco",
		"headimage" : "kidsparty_bg_top.jpg",
		"footimage" : "kidsparty_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "With DJ Slopey, Corralea Court, Sunday 18th, 2pm",
		"secondline" : "2nd line 2"
	}, {
		"title" : "Tunepal Showcase",
		"headimage" : "tunepal_bg_top.jpg",
		"footimage" : "tunepal_bg_foot.jpg",
		"url" : "http://tunepal.org/tunepal/index.php",
		"onclickev" : "",
		"firstline" : "Smartphone App, with Bryan Duggan",
		"secondline" : "2nd line 2"
	}, {
		"title" : "Workshops",
		"headimage" : "violin_bg_top.jpg",
		"footimage" : "violin_bg_foot.jpg",
		"url" : "#",
		"onclickev" : "",
		"firstline" : "Music & Dance Workshops Sat 17th - Sun 18th",
		"secondline" : "2nd line 2"
	}
	
	
	
	
	
	
	
];



$(document).ready(function() {
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/btn_pause.png)" });
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		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;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.headimage + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		$("#footerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.footimage + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		// Hide the header text
		//$("#headertxt").css({"display" : "none"});
		$("#headertxt").css({"margin-left" : "400px"});
		//$("#pictured").css({"display" : "none"});
		$("#firstline").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);
				
		// Fade out the current container
		// and display the header text when animation is complete
		$("#footerimg" + currentContainer).fadeOut();
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				//$("#headertxt").css({"display" : "block"});
				//$("#headertxt").show('slide', {direction: 'right'}, 1000);
				//$("#headertxt").fadeIn(function() {
				$("#headertxt").animate({marginLeft:'0px'}, 1500, function() {
					if (photoObject.firstline)
					{
						$("#firstline").slideDown('slow');
					}
				});
				
					
				
				//});
				
							
				//$("#headertxt").show("slow");
				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);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
