var slideshow = {

// fade up default html image
// build and show slide list
// preload other slide images
// start slideshow when images are loaded

	init : function()
	{
		this.container = $("#slideshow");
		this.container.addClass('loading');
		
		// show the default image already in the html
		setTimeout('$("img",this.container).fadeIn(this.speed)',this.speed/4);
		
		// check that we have slides to use
		this.number_of_slides = this.slides.length;
		if(this.number_of_slides==0) return false;

		// setup some vars
		this.current_slide = 0;
		this.previous_slide = null;
		this.current_loop = 0;
		this.playing=true;

		// add the slides list
		setTimeout('slideshow.addSlideInfo()',this.speed/4);
		
		// start loading the slide images
		this.imgloadtimeout = setTimeout('slideshow.loadImages()',this.speed/4);

		// set a timeout to start the slideshow
		this.slidestart = setTimeout('slideshow.start()',this.speed);
	},
	
	addSlideInfo : function()
	{
		var slide_buttons = '';
		var slide_ind  = '';
		
		$.each(this.slides,function(i){
			slide_buttons += '<li class="s-'+(i+1)+'"><a href="'+slideshow.slides[i].permlink+'"><strong>'+slideshow.slides[i].title+'</strong>'+slideshow.slides[i].desc+'</a></li> ';
			slide_ind += '<li class="s-'+(i+1)+'"><a href="#">'+i+'</a></li> ';
		});

		$('<ul id="slides">'+slide_buttons+'</ul>').appendTo(this.container).hide().fadeIn(this.speed);
		$('<ul id="slide-ind">'+slide_ind+'</ul>').appendTo(this.container).hide().fadeIn(this.speed);

		$("a","#slide-ind").click(function(){
			var number = $(this).parent().attr('class');
			number = number.substring(2);
			slideshow.pause();
			slideshow.fadeImage(number);
			slideshow.playing = false;
			return false;
		});

	},
	
	loadImages : function()
	{
		$.each(this.slides,function(i) {
			this.preload = new Image();
			this.preload.onload = function() { slideshow.isLoaded(i); };
			this.preload.src = this.imgsrc;
		});
	},
	
	isLoaded : function(i)
	{
		this.slides[i].loaded = (this.slides[i].preload.width!=0) ? true : null;
	},
	
	start : function()
	{
		if(this.slides[0].loaded) this.fadeImage();
		else setTimeout('slideshow.start()',this.speed);
	},
	
	hideImages : function()
	{
		$("img",this.container).hide();
	},
	
	showImage : function(number)
	{
		if(number){ this.current_slide=number-1; number=undefined; };
		
		if(!this.slides[this.current_slide].loaded) {
			if(this.current_slide!=this.number_of_slides-1) {
				this.current_slide++;
			} else {
				this.current_slide=0;
				this.current_loop++;
			}
		}
		
		// select the button
		slideInfos = $("#slides>li",this.container);
		$("a",slideInfos).removeClass("selected");
		$("a",slideInfos[this.current_slide]).addClass("selected");
		
		// and the dot
		slideDots = $("#slide-ind>li",this.container);
		$("a",slideDots).removeClass("selected");
		$("a",slideDots[this.current_slide]).addClass("selected");

		// add a class to show which slide
		this.container.addClass('slide-'+this.current_slide);
		
		// change the img attributes and fade in
		$("img",this.container).attr({	src:this.slides[this.current_slide].imgsrc,
																		alt:this.slides[this.current_slide].imgalt,
																		title:this.slides[this.current_slide].desc });
		$("img",this.container).fadeIn(this.speed);
		
		// make the image a link
		var imglink = slideshow.slides[slideshow.current_slide].permlink;
		$("img",this.container).click(function(){ document.location = imglink; _gaq.push(['_trackEvent','Slideshow','Link',imglink]); });
		// make the cursor look link like
		$("img",this.container).css('cursor','pointer');

		// trigger fadeOut if required
		if(this.current_loop<this.loop && this.number_of_slides>1 && this.playing==true) {
			this.imgtimeout = setTimeout('slideshow.fadeImage()',this.speed*5);
		} else {
			this.pause();
		}

		// increment the counts
		this.previous_slide=this.current_slide;
		if(this.current_slide!=this.number_of_slides-1) {
			this.current_slide++;
		} else { 
			this.current_slide=0;
			this.current_loop++;
		}
	},
	
	pause : function()
	{
		clearTimeout(this.imgtimeout);
	},
	
	fadeImage : function(number)
	{
		this.container.removeClass('loading');
		this.container.removeClass('slide-'+this.previous_slide);
		slideInfos = $("#slides>li",this.container); $("a",slideInfos).removeClass("selected");
		slideDots = $("#slide-ind>li",this.container); $("a",slideDots).removeClass("selected");
		$("img",this.container).css('cursor','default');
		$("img",this.container).unbind("click");
		$("img",this.container).fadeOut(this.speed/4, function(){
			slideshow.showImage(number);
		});

	}
	
};

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


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