var slideshow = {
	
	images: null,
	alpha: 0,
	divTag: null,
	imgTag: null,
	fader: null,
	fading: null,
	image: 0,
	init: function(rotatorElement, imgArray, width, height, shuffle) {
		
		slideshow.divTag = document.getElementById(rotatorElement);
		
		slideshow.imgTag = document.createElement("img");
		slideshow.imgTag.style.width = width + "px";
		slideshow.imgTag.style.height = height + "px";
		slideshow.setAlpha();
		
		slideshow.divTag.appendChild(slideshow.imgTag);	
	
		// shuffle the images
		if(shuffle) {

			for(var i=0; i<imgArray.length; i++) {
				
				var pos = Math.floor( Math.random() * (imgArray.length+1));
				if(pos >= imgArray.length)
					pos = imgArray.length - 1;
					
				if(i != pos) {
					var tmp = imgArray[pos];
					imgArray[pos] = imgArray[i];
					imgArray[i] = tmp;
				}
			}
		}
	
		slideshow.images = new Array(imgArray.length);
		// preload the images
		for(var i=0; i<imgArray.length; i++) {
			slideshow.images[i] = new Image(414, 242);
			slideshow.images[i].src = imgArray[i];
		}
		
		slideshow.fade();	
	},
	
	fade: function() {
		
		clearInterval(slideshow.fader);

		slideshow.image++;
		if(slideshow.image >= slideshow.images.length)
			slideshow.image = 0;
		
		slideshow.imgTag.src = slideshow.images[slideshow.image].src;
		
		slideshow.fading = window.setInterval("slideshow.doFade()", 20);
		 
	},
	flip: function() {
		
		slideshow.alpha = 0;
		slideshow.setAlpha();
		clearInterval(slideshow.fader);
		slideshow.fader = window.setInterval("slideshow.fade()", 2500);
	},
	doFade: function() {
		
		slideshow.alpha += 2;
		slideshow.setAlpha();
		
		if(slideshow.alpha == 100) {
			clearInterval(slideshow.fading);
			slideshow.divTag.style.background = "url(" + slideshow.imgTag.src + ") no-repeat";
			slideshow.fader = window.setInterval("slideshow.flip()", 2500);
		}
	},
	setAlpha: function() {
		slideshow.imgTag.style.filter = "alpha(opacity=" + slideshow.alpha + ")";
		slideshow.imgTag.style.MozOpacity = slideshow.alpha / 100; 
		slideshow.imgTag.style.opacity =  slideshow.alpha / 100; 
		slideshow.imgTag.style.KhtmlOpacity =  slideshow.alpha / 100;
	}
};

var branchImgs = new Array(
	"/images/locations/rotator/01.jpg",
	"/images/locations/rotator/02.jpg",
	"/images/locations/rotator/03.jpg",
	"/images/locations/rotator/04.jpg",
	"/images/locations/rotator/05.jpg",
	"/images/locations/rotator/07.jpg",
	"/images/locations/rotator/09.jpg"
);
slideshow.init('branchRotator', branchImgs, 220, 110, true);