var Slideshow = new function(){
	this.slideshowDiv = null;
	this.num_images_in_grid = null;
	this.photo_groups = [];
	this.number_sets = [];
	this.fade_time = null;
	this.time_between_shuffle = null;
	this.time_start_shuffle = null;
	this.image_path = null;
	this.set_counter = null;
	this.img_num_counter = null;
	this.group_counter = null;
	
	this.init = function(){
		var self = Slideshow;
		
		if (!document.getElementById){
			return;
		}
		
		self.slideshowDiv = "slideshow";
		
		if (!$(self.slideshowDiv)){
			return;
		}
		
		self.num_images_in_grid = 8;
		
		self.number_sets = [
												[2,5,7,8,1,4,6,3],
												[6,4,3,1,2,8,7,5],
												[2,5,7,1,4,3,6,8],
												[5,3,7,4,6,1,8,2],
												[3,5,7,6,4,2,1,8],
												[6,2,7,4,5,1,8,3]
												];
		self.set_counter = 0;
		self.image_num_counter = 0;
												
		self.fade_time = 0.4;
		self.time_between_shuffle = 1.2;
		self.time_start_shuffle = 2;
		
		self.photo_groups = ["event_prom","newpic","buzz","airport","mobile","brand_install","wild"];
		self.group_counter = 0;
		
		self.image_path = "images/slideshow/";
		
		self.buildGrid();
		setTimeout(function (){ self.doSlideShow()}, self.time_start_shuffle * 1000);

		// clears the loop, for testing
		// setTimeout(function() { clearInterval(self.timeout) }, 40000);
		
	};
	
	this.doSlideShow = function(){
		var self = Slideshow;
		
		var group = self.photo_groups[self.group_counter];
		var img_num = self.number_sets[self.set_counter][self.image_num_counter];
		var img_id = "photo_" + img_num;
		
		self.fade(img_id,self.fade_time,15,"out");
		
		setTimeout(function() { self.loadPhoto(group,img_num) },(self.fade_time*1000)+500);
		
		if (++self.image_num_counter == (self.num_images_in_grid)){
			self.set_counter++;
			self.image_num_counter = 0;
			self.group_counter++;
			
			if (self.group_counter == self.photo_groups.length){
				self.group_counter = 0;
			}
			if (self.set_counter == self.number_sets.length){
				self.set_counter = 0;
			}
		}
		
		var shuffle = (self.time_between_shuffle + (self.fade_time * 2)) * 1000;
		
		self.timeout = setTimeout(function(){ self.doSlideShow() },shuffle);
	};
	
	this.loadPhoto = function(photo_group,photo_num,starting_animation){
		var self = Slideshow;
		
		var img_id = "photo_" + photo_num;
		
		var next = new Image();
		
		next.onerror = function(){
			
		};
		
		next.onload = function(){
			if ($("photo_div_" + photo_num).hasChildNodes()){
				$("photo_div_" + photo_num).removeChild($("photo_div_" + photo_num).childNodes[0]);
				
				/*if (!starting_animation){ // set image to transparant to fade in
					next.style.opacity = 0;
					next.style.MozOpacity = 0;
					next.style.KhtmlOpacity = 0;
					next.style.filter = "alpha(opacity=0)";
				}*/

				$("photo_div_" + photo_num).appendChild(next);
				
			}
			else {
				
				/*if (!starting_animation){ // set image to transparant to fade in
					next.style.opacity = 0;
					next.style.MozOpacity = 0;
					next.style.KhtmlOpacity = 0;
					next.style.filter = "alpha(opacity=0)";
				}*/
				
				$("photo_div_" + photo_num).appendChild(next);
			}
			
			
			// fade image back in
			if (!starting_animation){
				setTimeout(function() {self.fade(img_id,self.fade_time,15,"in");},self.time_between_shuffle);
			}
			
		};
		
		next.src = self.image_path + photo_group + "_" + photo_num + ".jpg";
		next.id = img_id;
		
	};
	
	this.buildGrid = function(){
		var self = Slideshow;
		
		var container = $(self.slideshowDiv);
		container.style.width = "570px";
		for (var i = 0; i < self.num_images_in_grid; i++){
			var div = document.createElement("div");
			div.className = "slideshow_image";
			div.id = "photo_div_" + (i + 1);
			container.appendChild(div);
			var group = self.photo_groups[self.group_counter];
			var img_num = i + 1;
			self.loadPhoto(group,img_num,"start");
		}
		self.image_num_counter = 0;
		self.group_counter++;
		self.set_counter++;
	};
	
	
	// fade script designed to fade fully in or full out... could be re-written for partial fades
	this.fade = function(img_id,time,fps,dir){
		var self = Slideshow;
		var img = $(img_id);
		
		var steps = time * fps;
		
		if (typeof img.style.opacity != "undefined"){
			var otype = "w3c";
		}
		else if (typeof img.style.MozOpacity != "undefined"){
			otype = "moz";
		}
		else if (typeof img.style.KhtmlOpacity != "undefined"){
			otype = "khtml";
		}
		else if (typeof img.filters == "object"){
			otype = (img.filters.length > 0 && typeof img.filters.alpha == "object" && typeof img.filters.alpha.opacity == "number") ? "ie" : "none";
		}
		else {
			otype = "none";
		}
		
		if (otype != "none"){
			if (dir == "out"){
				self.dofade(fps,steps, img, 1, false, otype);
			}
			else {
				self.dofade(fps,steps, img, 0, true, otype);
			}
		}
	};
	
	this.dofade = function(fps,steps,img,value,targetvisibility,otype){
		var self = Slideshow;
		value += (targetvisibility ? 1 : -1) / steps;
		if (targetvisibility ? value > 1 : value < 0){
			value = targetvisibility ? 1 : 0;
		}
		
		self.setfade(img,value,otype);
		
		if (targetvisibility ? value < 1 : value > 0){
			setTimeout(
			function(){self.dofade(fps,steps,img,value,targetvisibility,otype); },
			1000 / fps);
		}
		
	};
	
	this.setfade = function(img, value, otype){
		switch(otype){
			case "ie":
				img.filters.alpha.opacity = value*100;
				break;
			case "khtml":
				img.style.KhtmlOpacity = value;
				break;
			case "moz":
				img.style.MozOpacity = (value == 1 ? 0.999999999 : value);
				break;
			default:
				img.style.opacity = (value == 1 ? 0.999999999 : value);
		}
	};
	
	this.cleanup = function(){
		var self = Slideshow;
		self.removeChildNodes($(self.slideshowDiv));
	}
	
	this.removeChildNodes = function(node){
	  while (node.childNodes[0]){
	    node.removeChild(node.childNodes[0]);
	  }
	}
	
};


attachEventListener(window,"load",Slideshow.init,false);
attachEventListener(window,"unload",Slideshow.cleanup,false);

