(function($, undefined){
	$.widget("ui.slideshow", {
	   // default options
		options: {
			speed: 800,
			delay: 5000
		},
		_create: function() {
			var self = this;
			this.nrPhotos = $(".photo", this.element).length;
			this.curPhoto = 1;
			if( this.nrPhotos > 1){
				this.loadNextPic($(".photo.selected",this.element));
			}
		},
		
		loadNextPic: function(pic){
			var self = this;
			pic.delay(self.options.delay).fadeOut(self.options.speed);
			if(self.curPhoto < self.nrPhotos){
		  		pic.next().delay(self.options.delay).fadeIn(self.options.speed, function(){
		  			$(this).addClass("selected");
			  		pic.removeClass("selected");
			  		self.loadNextPic($(this));
		  		});
	  			self.curPhoto ++;
	  		}else{
	  			pic.siblings(".photo:first-child").delay(self.options.delay).fadeIn(self.options.speed, function(){
	  				$(this).addClass("selected");
			  		pic.removeClass("selected");
			  		self.loadNextPic(pic.siblings(".photo:first-child"));
	  			});	
	  			self.curPhoto = 1;
	  		}
		},
		destroy: function() {
			$.Widget.prototype.destroy.apply(this, arguments); // default destroy
			// now do other stuff particular to this widget
		}
	});
	$.extend($.ui.slideshow, {
        version: "0.1.0"
    });
})(jQuery);
