(function($) {

$.fn.ui_slideshow = function(options) {
	
		
	this.each(function() {

	
		return ui_slideshow(this,options)/* , false */;


  	})
  
}

ui_slideshow = function (content,options) {


		return new ui_fn_slideshow(content,options)/* , false */;

}

ui_fn_slideshow = function (content,options) {

		var self = this;
		
		this.content= content;
		
		this.settings= $.extend({}, $.fn.ui_slideshow.defaults, options);

		this.setup_slideshow()	

		return false;

}

ui_fn_slideshow.prototype = {

	
	autorun: function () {
	
		var self = this
		
		setInterval(function () { self.show_next() },  self.settings.autorun)

	
	},
	
		
	count: function () {
	
		return this.count = $('li',this.slides).size();
	
	},


	
	next : function () {
	
		var self = this;
		
		this.next =  jQuery('<a/>', {
		
						'class': 'ui-slideshow-next',
						href:'#',
						title:'next',
						click: function () {
							
							self.show_next();
							
							return false;	
						
						}
						
						
						
					})
				
		
		return this.next;	
	
	},


	
	
	prev : function () {
	
		var self = this;	
	
		this.prev =  jQuery('<a/>', {
		
						'class': 'ui-slideshow-prev',
						href:'#',
						title:'prev',
						click: function () {

							self.show_prev()
							
							return false;
						
						}
						
					})
				
		return this.prev;
	
	},
	

	
	
	setup_slideshow: function () {
	
		var self = this;
	
		this.slides()
			
		this.scroller()	
		
		if (this.settings.prev_next) {
		this.prev()
		this.next()	
		}

		this.slideshow =  jQuery('<div/>', {'class': 'ui-slideshow'}).css({width:this.settings.width, height:this.settings.height+40}).append(this.slides,this.prev,this.next,this.scroller);
		
		if(this.settings.autorun)	{this.autorun()}
		
		$(this.content).replaceWith(this.slideshow);
		
		if (this.settings.sroller) {$(this.scroller).width($('li',self.scroller).width()*self.count);}
		
		this.slides_resize();	
		
		return this.slideshow
		
		
	},
	
	scroller :function () {
		
		var self=this;
		
		this.count()
		
		this.scroller = jQuery('<ol/>', {'class': 'ui-slideshow-scroller'});
	
		for (i=0;i<this.count;i++) {
		
			var active_slide = '';  
			if (i==0){active_slide = 'active_slide'};
			var index = i
			var li = jQuery('<li/>')
			var a = jQuery('<a/>', {
							href: i,
   							title: 'screenshot',
   							'class':self.settings.scroller+' ui_corner_all '+active_slide,
   							click: function () {
   							
   							self.scroller_action(this)
							return false;

   							 	}
    						}
    					)
    					
    			if (self.settings.scroller=='numeric') {jQuery(a).text(i+1)}		
    			$(li).append(a).appendTo(this.scroller);
    			
			
		 }
		
		
		return  this.scroller;
	
	},
	
	scroller_action : function (scroller_button) {
	
		var index = $(scroller_button).attr('href');

   		return this.show_slide(index);
   		    	 			
	
	},	
	
	show_active_scroller : function (index) {
	
		$('.active_slide',this.scroller).removeClass('active_slide')
   		$('a:eq('+index+')',this.scroller).addClass('active_slide')
   		
   		return ;
	
	},		
	
	show_next : function () {
	
		var self = this;
		
		self.slide_index()
		
		if (self.active_slide == self.count-1 ) { 
			
			self.active_slide = -1
		
		} 
		
			self.active_slide = self.active_slide + 1;

						
		
		self.show_slide(self.active_slide)	
				
		return self.active_slide;

														
			
	},	
		
	show_prev : function () {
	
		var self = this;

		self.slide_index()

		if (self.active_slide == 0) { 
		
			self.active_slide = self.count		
		
		} 
		
			self.active_slide = self.active_slide - 1;

		
		self.show_slide(self.active_slide)	
				
		return self.active_slide;

	
	},	

	
	show_slide : function (index,fn) {
	
		var self = this;
		
		if ($('li:visible',this.slides)) {

   		$('.active_slide',this.slides).removeClass('active_slide').ui_hide(self.settings.effect, self.settings.speed, function () { 
   		
   		
   			$('li:eq('+index+')',self.slides).addClass('active_slide').ui_show(self.settings.effect,self.settings.speed, function () { 							
   		   			
   			}) })
   			
   		} else {
   		
   		$('li:eq('+index+')',self.slides).addClass('active_slide').ui_show(self.settings.effect,self.settings.speed, function () { 					
   		   			
   			})
   		
   		
   		}				
   			
   		
   			self.show_active_scroller(index);	

	},
	
	
	slides :function () {
		
		var self= this;	
		
		this.slides = $('<ol/>', {'class': 'ui-slideshow-slides'}).html($(this.content).html());
		
		$('li:not(:first)', this.slides).ui_hide(self.settings.effect, 0, function () { $(this).removeClass('active_slide')})
		
		$('> li:first',this.slides).ui_show(self.settings.effect, 0, function () {$(this).addClass('active_slide')})
		
		return this.slides;

	},
	
	slide_index : function () {
			
		var self= this;	
	
		self.active_slide =$('li:has(a.active_slide)', self.scroller).index();
		
		return  this.active_slide;
	
	},


	
	slides_resize : function () {
	var self= this;	
		
		$('li > *',this.slides).css({margin:'0 auto',display:'block'}).each(function () {	
		


			if( $(this).height() >  self.settings.height || $(this).width() > self.settings.width || $(this).width() == 0 || $(this).height() == 0 ) {
			
				$(this).ui_resize(self.settings.width, self.settings.height)		
			
				}
			
			
			
			
		})


		$(this.slides).ui_resize(self.settings.width, self.settings.height )	
		

	}	
	
	

	
	
};


$.fn.ui_slideshow.defaults = {
		autorun:false,
		prev_next:true,
		scroller:'dots',
		effect:ui.effect,
		speed:750,
		width:640,
		height:480,
		callback:jQuery.noop
};
 })(jQuery);
