(function($){
    $.slideshow = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;
        base.current = 1;
        
        // Add a reverse reference to the DOM object
        base.$el.data("slideshow", base);
        
        base.init = function(){
        	
            base.options = $.extend({},$.slideshow.defaultOptions, options);
            base.size = $(".photo_slideshow li").size();
             
            if(base.size > 1)
            {
	            	
	            $(".next", base.el).bind("click", function(){
	            	base.current++;
	            	base.start();
	            	base.goto(base.current);
	            	return false;
	            });
	            
	            $(".prev", base.el).bind("click", function(){
	            	base.current--;
	            	base.start();
	            	base.goto(base.current);
	            	return false;
	            });
            	
            } else {
            	$(".next, .prev", base.el).hide();
            }

			base.$el.fadeIn(250);
			base.goto(1);
			base.start();
           
        };
        
        base.reset = function()
        {
            clearInterval(base.Interval);
        }
        
        base.start = function() {
        	base.reset();
        	base.Interval = setInterval(function(){
        		 $(".next", base.el).trigger("click");
        	},base.options.delay)
        }
        
        base.goto = function(id)
        {
        	
        	if(id > base.size){
        		id = 1;
        	}
        	
        	if(id < 1) {
        		id = base.size;
        	}
        	
        	base.current=id;
        	
        	$(".photo_slideshow li").hide();
        	$(".photo_slideshow li:nth-child("+id+")").fadeIn(250);
        }
        
        base.init();
    };
    
    $.slideshow.defaultOptions = {
    	delay : 6500
    };
    
    $.fn.slideshow = function(options){
        return this.each(function(){
            (new $.slideshow(this, options));
        });
    };
    
})(jQuery);
