(function($) {
    $.fn.esc_stdslideshow = function(o) {
        return this.each(function() {
            new $esc_stdslideshow(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
        speed: 1000,
        autoScrollSpeed: 6000,
        autoScroll: false
    };

    /**
     * The esc_stdslideshow object.
     *
     * @constructor
     * @name $.esc_stdslideshow
     * @param Object e The element to create the slideshow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/esc_stdslideshow
     */
    $.esc_stdslideshow = function(e, o) {
        this.options    = $.extend({}, defaults, o || {});
        
        this.container   = $(e);
        this.radios      = this.container.find('.radiobtns a');
        this.scrollitems = this.container.find('.items-holder li');
        this.running     = false;
        this.autoscrolltimer = true;
        
        var self = this;

        self.setup();
    };

    // Create shortcut for internal use
    var $esc_stdslideshow = $.esc_stdslideshow;

    $esc_stdslideshow.fn = $esc_stdslideshow.prototype = {
        esc_stdslideshow: '0.1'
    };

    $esc_stdslideshow.fn.extend = $esc_stdslideshow.extend = $.extend;

    $esc_stdslideshow.fn.extend({
        /**
         * Setups the slideshow.
         *
         * @name setup
         * @type undefined
         * @cat Plugins/esc_stdslideshow
         */
        setup: function() {
            this.options.speed = this.container.width() * 1.2;
            this.setup_radios();
            if( this.options.autoScroll ) {
                this.setup_autoscroll();
            }
        },
        
        /**
         * Setups the slideshow radio buttons.
         *
         * @name setup_radios
         * @type undefined
         * @cat Plugins/esc_stdslideshow
         */
        setup_radios: function() {            
            var self=this;
            
            this.radios.click(function() {
                
                self.slide_to(
                    $(this).parent().parent().children('li').index( $(this).parent() )
                );
                if( self.options.autoScroll ) {
                    clearInterval(self.autoscrolltimer);
                    self.setup_autoscroll();
                }
                
                return false;
            });
        },
        
        /**
         * Setups the slideshow to autoscroll
         *
         * @name setup_autoscroll
         * @type undefined
         * @cat Plugins/esc_stdslideshow
         */
        setup_autoscroll: function() {            
            var self=this;
            
            self.autoscrolltimer = setInterval(function() {self.next()}, self.options.autoScrollSpeed);
            
            self.scrollitems.unbind("hover");
            self.scrollitems.hover(function() {
                clearInterval(self.autoscrolltimer);
            }, function() {
                if( self.options.autoScroll == true ) {
                    clearInterval(self.autoscrolltimer);
                    self.autoscrolltimer = setInterval(function() {self.next()}, self.options.autoScrollSpeed);
                }
            });
            self.scrollitems.find('.videoplayer').focusin(function() { 
                clearInterval(self.autoscrolltimer); 
                self.options.autoScroll = false; 
                self.scrollitems.unbind("hover"); 
            });
        },
        
        /**
         * Setups the slideshow slide to the next item
         *
         * @name setup_next
         * @type undefined
         * @cat Plugins/esc_stdslideshow
         */
        next: function() {            
            var self=this;
            
            
            
            var next_index = this.radios.parent().index( this.radios.parent().filter(".on") );
            next_index++;
            if( next_index == this.radios.length ) next_index = 0;
            this.slide_to( next_index );
        },
        
        /**
         * Slide to a slideshow
         *
         * @name slide_to
         * @type undefined
         * @param Int index the index of the slideshow item to slide to.
         * @cat Plugins/esc_stdslideshow
         */
        slide_to: function( index ) {
            var self = this;
            self.radios.parent().removeClass('on').filter(':eq('+index+')').addClass('on');
            
            self.scrollitems.parent().animate({
                    'marginLeft' : 0-(self.container.width()*index)
                },
                self.options.speed
            );
        }
    });

    $esc_stdslideshow.extend({
        /**
         * Gets/Sets the global default configuration properties.
         *
         * @name defaults
         * @descr Gets/Sets the global default configuration properties.
         * @type Hash
         * @param Hash d A set of key/value pairs to set as configuration properties.
         * @cat Plugins/jCarousel
         */
        defaults: function(d) {
            return $.extend(defaults, d || {});
        }
    });

})(jQuery);