(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 = $(".slides li").size();
            
            var html = "";
            for(var i=1;i<=base.size;i++){
            	html += "<li><a class=\"circle-btn\" href=\"#\"><a></li>";
            }
            $("#slideshow-nav").html(html);
            
            $("#slideshow-nav li").bind("click", function(e){
            	e.preventDefault();
            	var index = $(this).index() + 1;
            	base.goto(index);
            })

			base.$el.fadeIn(250);
			base.goto(1);
			base.start();
           
        };
        
        base.reset = function()
        {
            clearInterval(base.Interval);
        }
        
        base.start = function() {
        	base.reset();
        	base.Interval = setInterval(function(){
				base.goto(++base.current);
        	},base.options.delay)
        }
        
        base.goto = function(id)
        {
        	
        	if(id > base.size){
        		id = 1;
        	}
        	
        	if(id < 1) {
        		id = base.size;
        	}
        	
        	base.current=id;
        	
        	$("#slideshow-nav li").removeClass("active");
        	$("#slideshow-nav li:nth-child("+id+")").addClass("active");
        	$(".slides li").hide();
        	$(".slides li:nth-child("+id+")").fadeIn(250);
        }
        
        base.init();
    };
    
    $.slideshow.defaultOptions = {
    	delay : 4500
    };
    
    $.fn.slideshow = function(options){
        return this.each(function(){
            (new $.slideshow(this, options));
        });
    };
    
})(jQuery);
