/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 12000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
// &Auml;	Ä &Aring;	Å    &AElig;	Æ  &Oslash;	Ø    
var photos = [
{
    "title": "Tegnestuen Vandkunsten",
    "image": "03-03.jpg",
    "url": "building.aspx?buildingid=13143",
    "firstline": "",
    "secondline": "Hamar Kulturhus, Norge"
},
{
    "title": "Mette Blankenberg og Eyrun Margret Stefansdottir",
    "image": "03-02.jpg",
    "url": "building.aspx?buildingid=13775",
    "firstline": "",
    "secondline": "Europan 11, Aller&oslash;d"
},
{
    "title": "BIG",
    "image": "03-01.jpg",
    "url": "building.aspx?buildingid=13213",
    "firstline": "",
    "secondline": "Superkilen"
},
{
    "title": "Schmidt, Hammer & Lassen",
    "image": "02-14.jpg",
    "url": "building.aspx?buildingid=12915",
    "firstline": "",
    "secondline": "Performers House"
},
{
    "title": "Nord arkitekter",
    "image": "02-12.jpg",
    "url": "building.aspx?buildingid=13337",
    "firstline": "",
    "secondline": "Center for Kr&aelig;ft & Sundhed, K&oslash;benhavn"
},
{
    "title": "Cobe og Transform",
    "image": "02-11.jpg",
    "url": "building.aspx?buildingid=2437",
    "firstline": "",
    "secondline": "Porsgrunn S&oslash;fartsmuseum, Norge"
},
{
    "title": "KKET, Eva og Nils Koppel, Gert Edstrand og Poul Erik Thyrring",
    "image": "02-10.jpg",
    "url": "building.aspx?buildingid=2437",
    "firstline": "",
    "secondline": "Panum Instituttet, K&oslash;benhavns Universitet"
},
{
    "title": "Nord",
    "image": "02-09.jpg",
    "url": "building.aspx?buildingid=13742",
    "firstline": "",
    "secondline": "BaNanna Park, K&oslash;benhavn"
},
{
    "title": "3xn",
    "image": "02-08.jpg",
    "url": "building.aspx?buildingid=13095",
    "firstline": "",
    "secondline": "Den Bl&aring; Planet, Kastrup"
},
{
    "title": "Henning Larsens Tegnestue,  Batter&#237;i&eth; Architects og Olafur Eliasson",
    "image": "02-07a.jpg",
    "url": "building.aspx?buildingid=574",
    "firstline": "",
    "secondline": "Harpa, Reykjavik koncert- og kongrescenter"
},
	{
	    "title": "Schmidt, Hammer & Lassen",
	    "image": "02-06.jpg",
	    "url": "building.aspx?buildingid=13753",
	    "firstline": "",
	    "secondline": "Warszawa Kontorh&oslash;jhus"
	},
    {
        "title": "Dissing + Weitling",
        "image": "02-05.jpg",
        "url": "building.aspx?buildingid=12809",
        "firstline": "",
        "secondline": "Humanistisk Forskningsbibliotek"
    },
    /*    {
    "title": "Svend Wiig Hansen, kunstner",
    "image": "02-04.jpg",
    "url": "building.aspx?buildingid=13721",
    "firstline": "",
    "secondline": "Mennesket ved Havet"
    }, 
    {
    "title" : "Witraz Arkitekter",
    "image" : "02-01.jpg",
    "url" : "building.aspx?buildingid=13675",
    "firstline" : "",
    "secondline" : "Gr&oslash;nne Trekants Aktivitetshus"
    }, 
    {
    "title" : "Jensen + J&oslash;rgensen + Wohlfeldt",
    "image" : "02-02.jpg",
    "url" : "building.aspx?buildingid=13370",
    "firstline" : "",
    "secondline" : "Universet, K&oslash;benhavn"
    },
	
   
    {
    "title" : "Schmidt, Hammer & Lassen",
    "image" : "02-06.jpg",
    "url" : "building.aspx?buildingid=13753",
    "firstline" : "",
    "secondline" : "Warszawa Kontorhøjhus"
    }, 
    {
    "title" : "Theophilus Hansen",
    "image" : "food.jpg",
    "url" : "http://www.sxc.hu/photo/1042413",
    "firstline" : "Ens",
    "secondline" : ""
    }
    {
    "title" : "Cobe og Transform",
    "image" : "02-03.jpg",
    "url" : "building.aspx?buildingid=13276",
    "firstline" : "",
    "secondline" : "Nordvests Kulturhus og Bibliotek"
    },
    */
];



$(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(s-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(s-images/" + 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);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#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(s-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);
	
});
