﻿/*

	Started on June 6, 2008 with code from

		Jeffrey Jordan Way, a Web Developer from Nashville, TN
			detacheddesigns.com in a post called " Why Aren't You Using jQuery: PART 3"

		http://www.detacheddesigns.com/blog/blogSpecific.aspx?BlogId=62
		http://www.detacheddesigns.com/blog/files/jQueryImageGallery.zip
		http://www.detacheddesigns.com/blog/samples/jQueryImageGallery/gallery.htm

	This revision by David Van Vickle - http://www.davidvanvickle.com
		
		Added
			Randomness
			Shuffling
			Automatic transitions
		Removed
			Thumbnails
	
*/

// change these paths for your images
//var myImages = ['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg'];

// how many times should the photo change per page load
var maxChanges = 0; //startbilder.length * 2;

// shuffle images so each time page loads, the photos show in different order
var do_shuffle = true;

// use simple randomness instead of shuffling (tends to repeat images too often)
var do_randomly = false;

// number of seconds between photo changes
var seconds_between_photos = 3;

// name of DIV to load photos into
var div_name = "bild";








var changes = 0;
var timer;
var thisImg = startbilder.length - 1;
var nr = 1; // Bild Nr. (1 bis 3)
var zindx = 1;
var bilddiv = [true, true, true, true];

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};




// shuffling is better than random because of less potential repetition
if (do_shuffle) {
	myImages = shuffle(startbilder);	
}



function nextImage () {
	
	var low = 0;
	var high = startbilder.length - 1;
	var rand_no = Math.floor((high-(low - 1))*Math.random()) + low;
	
	thisImg++;
	changes++;
	if (thisImg==startbilder.length) {
		thisImg = 0;
	}
	if (changes==maxChanges) {
		clearInterval(timer);
	}
	if (do_randomly) {
		thisImg = rand_no;
		return startbilder[rand_no];
	} else {
		return startbilder[thisImg];
	}
}

function changeImage () {
	
	var t = startbilder[thisImg];
	var n = nextImage();
	
	nr++;
	if (nr > 3) nr = 1;
	
	if (t != n) {
		$("#"+div_name+nr).addClass("loading");
		showImage(n);
	} else { 
		changeImage();
	}
}

function showImage(src)
{
	//$("#"+div_name+nr+" img:first");//.css({'z-index':10});
	var largeImage = new Image();
	$(largeImage).load(function()
                        {
							$(this).hide();
                        	$("#"+div_name+nr).prepend(this).removeClass("loading");
                                             
                       		$(this).show();              
                        });    
	$(largeImage).attr("src", '../fotos/196/'+src);                                                                               
	if (bilddiv[nr]) {
		$("#"+div_name+nr).fadeTo(500,0.8);
		bilddiv[nr]=false;
	}
	$("#"+div_name+nr+" img:first").fadeOut(500, function(){$(this).remove()});
}

function checkForLoaded () {
	if (document.getElementById("startbilder") != null) {
		clearInterval(timer);
		
		$("#startbilder").html('<div id="bild1" style="opacity:0.001;"></div><div id="bild2" style="opacity:0.001;"></div><div id="bild3" style="opacity:0.001;"></div>');
				
		changeImage();
		timer = setInterval(changeImage, (seconds_between_photos * 1000));
	}
}

// check every second to see if DIV exists, when it does, start photo changing timer
timer = setInterval(checkForLoaded, 500); 



