var slideshow = {
	
	loop : 3,
	
	imgs :  [
		{
			src : 'images/slideshow/slide01.jpg', 
			alt : 'Door 1'
		},
		{
			src : 'images/slideshow/slide02.jpg', 
			alt : 'door 2'
		},
		{
			src : 'images/slideshow/slide03.jpg', 
			alt : 'door 3'
		},
		{
			src : 'images/slideshow/slide04.jpg', 
			alt : 'door 4'
		},
		{
			src : 'images/slideshow/slide05.jpg', 
			alt : 'door 5'
		},
		{
			src : 'images/slideshow/slide06.jpg', 
			alt : 'door 6'
		},
		{
			src : 'images/slideshow/slide07.jpg', 
			alt : 'door 7'
		},
		{
			src : 'images/slideshow/slide08.jpg', 
			alt : 'door 8'
		},
		{
			src : 'images/slideshow/slide09.jpg', 
			alt : 'door 9'
		},
		{
			src : 'images/slideshow/slide10.jpg', 
			alt : 'door 10'
		}
		
	],
	
	init : function(keys)
	{
		// If specific imgs chosen
		if(keys!=undefined)
		{
			// Split into array
			keys = keys.split(',');
			imgs = new Array();
			// Iterate through keys
			$.each(keys,function(i) {
				// Remove the links if keys chosen
				slideshow.imgs[this].link = undefined;
				// Add chosen imgs to new array
				imgs.push(slideshow.imgs[this]);
			});
			slideshow.imgs = imgs;
		}
		
		this.container = $("#slideshow");
		
		$("#slideshow").append('<img src="/images/slideshow/slide01.jpg" alt="Door 1" />');
		
		this.number_of_images = this.imgs.length;
		if(this.number_of_images==0) return false;
		
		this.current_image = 0;
		this.previous_image = null;
		this.current_loop = 0;
		
		this.hideImages();
		
		$.each(this.imgs,function(i) {
			this.preload = new Image();
			this.preload.onload = function() { slideshow.isLoaded(i); }
			this.preload.src = this.src;
		});
		
	},
	
	isLoaded : function(i)
	{
		this.imgs[i].loaded = (this.imgs[i].preload.width!=0) ? true : null;
		if(i==0) { this.showImage(); }
	},
	
	hideImages : function()
	{
		$("img",this.container).hide();
		this.container.css("background","#1E1E1E url(http://67.19.197.226/~wwwross/images/loader.gif) no-repeat 50% 50%");
	},
	
	showImage : function()
	{
		if(!this.imgs[this.current_image].loaded) {
			if(this.current_image!=this.number_of_images-1)
			{
				this.current_image++;
			}
			else
			{
				this.current_image=0;
				this.current_loop++;
			}
		}
		$("img",this.container).attr({	src:this.imgs[this.current_image].src,
																		alt:this.imgs[this.current_image].alt });
		$("img",this.container).fadeIn("slow");
		
		//this.linktimein = setTimeout('slideshow.showLinks()',1000);
		this.imgtimeout = setTimeout('slideshow.fadeImage()',8000);
		
		this.previous_image=this.current_image;
		
		if(this.current_loop==this.loop||this.number_of_images==1) this.pause();
		
		if(this.current_image!=this.number_of_images-1)
		{
			this.current_image++;
		}
		else
		{ 
			this.current_image=0;
			this.current_loop++;
		}
	},
	
	showLinks : function()
	{
		if(this.imgs[this.previous_image].link!=undefined)
		{
			$("img",this.container).after(this.imgs[this.previous_image].link);
			$("a",this.container).fadeIn("slow");
		}
	},
	
	pause : function()
	{
		clearTimeout(this.imgtimeout);
		clearTimeout(this.timein);
	},
	
	fadeImage : function()
	{
		if(this.imgs[this.previous_image].link!=undefined)
		{
			$("a",this.container).fadeOut(500,function(){$("a",slideshow.container).remove();});
		}
		$("img",this.container).fadeOut(1000);
		
		this.timein = setTimeout('slideshow.showImage()',1000);
	}
	
}

window.onunload = function() { 
	clearTimeout(this.linktimein);
	slideshow.pause();
};