﻿/*

	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 myImages2 = ['campanhas/images/ameverdeamevida.png','campanhas/images/entrecinzas.png'];

// how many times should the photo change per page load
var maxchanges2 = myImages2.length * 10000;

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

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

// number of seconds between photo changes
var seconds_between_photos2 = 4;

// name of DIV to load photos into
var div_name2 = "campanhas";








var changes2 = 0;
var timer2;
var thisImg2 = myImages2.length - 1;

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

shuffle2 = 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_shuffle2) {
	myImages2 = shuffle2(myImages2);	
}



function nextImage2 () {
	
	var low = 0;
	var high = myImages2.length - 1;
	var rand_no = Math.floor((high-(low - 1))*Math.random()) + low;
	
	thisImg2++;
	changes2++;
	if (thisImg2==myImages2.length) {
		thisImg2 = 0;
	}
	if (changes2==maxchanges22) {
		clearInterval(timer2);
	}
	if (do_randomly2) {
		thisImg2 = rand_no;
		return myImages2[rand_no];
	} else {
		return myImages2[thisImg2];
	}
}

function changeImage2 () {
	
	var t = myImages2[thisImg2];
	var n = nextImage2();
	
	if (t != n) {
		$("#"+div_name2).addClass("loading");
		showImage2(n);
	} else { 
		changeImage2();
	}
}

function showImage2(src)
{
	$("#"+div_name2+" img").fadeOut("normal").remove();
	var largeImage = new Image();
	$(largeImage).load(function()
                        {
							$(this).hide();
                        	$("#"+div_name2).append(this).removeClass("loading");
                                             
                       		$(this).fadeIn("slow");              
                        });    
	$(largeImage).attr("src", src);                                                                               
}

function checkForLoaded2 () {
	if (document.getElementById(div_name2) != null) {
		//alert("loaded");
		clearInterval(timer2);
		changeImage2();
		timer2 = setInterval(changeImage2, (seconds_between_photos2 * 1000));
	}
}

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



