var slideshow = {

	init : function()
	{
		this.container = $("#slideshow");
		
		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();
		this.container.addClass('loading');
		
		this.imgloadtimeout = setTimeout('slideshow.loadImages()',this.speed*2);
		
		this.addSlideInfo();
	},
	
	addSlideInfo : function()
	{
		var controlinks = '';
		for(i=1;i<=this.number_of_images;i++) {
			controlinks += '<a class="l-'+i+'">'+i+'</a> ';
		}
		this.container.append('<div id="slideinfo"><div id="slidecaption"></div><p id="slidecontrol">'+controlinks+'</p></div>');
		$("a","#slidecontrol").click(function(){
			number = $(this).text();
			slideshow.pause();
			slideshow.fadeImage(number);
			return false; 
		});
	},
	
	loadImages : function()
	{
		$.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();
	},
	
	showImage : function(number)
	{
		if(number){ this.current_image=number-1; number=undefined; };
		
		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(this.speed);
		//$("dt",this.container).text(this.imgs[this.current_image].title);
		$("#slidecaption",this.container).html(this.imgs[this.current_image].caption);
		
		this.imagelink = $("a","#slidecaption").attr('href');
		
		if(this.imagelink) {
			this.container.attr('title','Click to read more about Philtone and '+this.imgs[this.current_image].alt);
			this.container.css('cursor','pointer');
			this.container.click(function(){ document.location = slideshow.imagelink; });
		}

		this.container.addClass('slide-'+this.current_image);

		this.imgtimeout = setTimeout('slideshow.fadeImage()',this.speed*5);
		
		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++;
		}
	},
	
	pause : function()
	{
		clearTimeout(this.imgtimeout);
		clearTimeout(this.timein);
	},
	
	fadeImage : function(number)
	{
		if(this.imgs[this.previous_image].link!=undefined)
		{
$("a",this.container).fadeOut(this.speed/2,function(){$("a",slideshow.container).remove();});
		}
		this.container.removeClass('loading');
		this.container.removeClass('slide-'+this.previous_image);
		$("img",this.container).fadeOut(this.speed);
		
		//$("dt",this.container).text('');
		$("#slidecaption",this.container).html('');
		this.container.removeAttr('title');
		this.container.css('cursor','default');
		this.container.click(function(){ return false; });
		
		
		this.timein = setTimeout('slideshow.showImage('+number+')',this.speed);
	}
	
};

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




$(document).ready(function(){
	
	$.getJSON("/slideshow", function(json){
		var slideshowdata = json;
		slideshow.imgs = slideshowdata.imgs;
		slideshow.loop = slideshowdata.loop;
		slideshow.speed = slideshowdata.speed;
		slideshow.init();
	});
	
});