// globala variabler
var g_fade_timeout = 7;

//var g_fade_list_images = new Array("gardabrygga1.png", "gardabrygga10.png", "gardabrygga11.png", "gardabrygga12.png", "gardabrygga13.png", "gardabrygga3.png", "lina.png");
// felsök med: console.log()

// När dokumentet är redo (när hela DOM har laddat färdigt)
$(document).ready(function () {
	
//	Hämta data från PHP-skript m.h.a. JSON standarden.
//	Vi skapar nu en img-tagg för varje bildsökväg som returneras
	$(function() {
//		http://docs.jquery.com/Ajax/jQuery.getJSON
		$.getJSON("list_headers.php", {'key' : 'gallery'}, function(data, textStatus) {
//			Nu är 'data' av typen Array
			var imageArray = data;
			
			if (imageArray == null || data == false) {
				console.log("Error: empty image array");
				return false;
			}
			
//			Förladda bilderna
			preloadImages('', imageArray);
			
			// Hämta data som redan finns i box
			var gallery_box = $("#gallery").html();
			
//			Loopa igenom varje bild och skapa tagg
			for (var i = 0; i < imageArray.length; i++) {
			//width=\"480\" height=\"125\"
				gallery_box = gallery_box + "<img src=\""+ imageArray[i] +"\" alt=\"foto\" />";
			}
			
//			Skicka img-taggarna dynamiskt till DOM och uppdatera HTML (glöm inte testa i alla webbläsare: http://www.stainlessvision.com/jquery-html-vs-innerxhtml)
			$("#gallery").html(gallery_box);
			
//			Starta bildspelet
			slideshow();
		});
	});
	
	
	

	
	
});// end of $(document).ready()




function slideshow() {

	//Set the opacity of all images to 0
	$('#gallery img').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery img:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
//	$('#gallery .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
//	$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
//	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
//		.animate({opacity: 0.7}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	var interval = g_fade_timeout;
	setInterval('gallery()', interval * 1000);
	
}

function gallery() {	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery img.showImage')?  $('#gallery img.showImage') : $('#gallery img:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery img:first') :current.next()) : $('#gallery img:first'));	
	
	//Get next image caption
//	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
		.addClass('showImage')
		.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
		.removeClass('showImage');
	
	//Set the opacity to 0 and height to 1px
//	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
//	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	
	//Display the content
//	$('#gallery .content').html(caption);
	
	
}


/**
 * Förladda bilder
 */
function preloadImages(imagePath,imageArray)
{
	var imageholder = new Array();
	for (i = 0; i < imageArray.length; i++) {
		imageholder[i] = new Image();
		imageholder[i].src = imagePath + imageArray[i];
	}
}
