var slideshow = {
	
	loop : 3,
	
	imgs :  [
		{
			src : 'assets/templates/modx.kp/resources/slideshow/slide.01.jpg', 
			alt : ''
		},
		{
			src : 'assets/templates/modx.kp/resources/slideshow/slide.02.jpg', 
			alt : ''
		},
		{
			src : 'assets/templates/modx.kp/resources/slideshow/slide.03.jpg', 
			alt : ''
		},
		{
			src : 'assets/templates/modx.kp/resources/slideshow/slide.04.jpg', 
			alt : ''
		},
		{
			src : 'assets/templates/modx.kp/resources/slideshow/slide.05.jpg', 
			alt : ''
		}
	
	],
	
	init : function(keys)
	{
		// If specific imgs chosen
		if(keys!=undefined)
		{
			// Split into array
			keys = keys.split(',');
			imgs = new Array();
			// Iterate through keys
			JQ.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 = JQ("#slideshow");
		
		JQ("#slideshow").append('<img src="assets/templates/modx.kp/resources/slideshow/slide.01.jpg" alt="" />');
		
		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();
		
		JQ.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()
	{
		JQ("img",this.container).hide();
		//this.container.css("background","#000 url(assets/templates/modx.kp/resources/loader.gif) no-repeat 50% 50%");
		this.container.css("background","#000");
	},
	
	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++;
			}
		}
		JQ("img",this.container).attr({	src:this.imgs[this.current_image].src,
																		alt:this.imgs[this.current_image].alt });
		JQ("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)
		{
			JQ("img",this.container).after(this.imgs[this.previous_image].link);
			JQ("a",this.container).fadeIn("slow");
		}
	},
	
	pause: function()
	{
		clearTimeout(this.imgtimeout);
		clearTimeout(this.timein);
	},
	
	fadeImage: function()
	{
		if(this.imgs[this.previous_image].link!=undefined)
		{
			JQ("a",this.container).fadeOut(500,function(){JQ("a",slideshow.container).remove();});
		}
		JQ("img",this.container).fadeOut(1000);
		
		this.timein = setTimeout('slideshow.showImage()',1000);
	}
	
}

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