/* Copyright 2011 © DutchBridge
 * http://www.dutchbridge.nl
 * Author: Kenny Korte <kenny@dutchbridge.nl>
 */

(function($)
{
    var settings = {
        'auto'              : true,
        'interval'          : 10000,
        'cssClass'          : 'dbSlideshow',
        'quickstart'        : false,
        'target'            : 'img',
        'pauseButton'       : true,
        'panel'             : true,
        'autoResume'        : false,
        'debug'             : false
    };

    var methods = {
        init : function( options )
        {
            return this.each(function() {
                var $this = $(this), data = $this.data('slideshow'), slideshow = $('<div />');
                
                if ( options ) {
                    $.extend( settings, options );
                }

                if ( ! data ) {
                    $(this).data('slideshow', {
                        target : $this,
                        slideshow : slideshow,
                        timeout : null,
                        panel: null
                    });
                    data = $(this).data('slideshow');
                }

                $this.addClass(settings.cssClass);
                $this.dbSlideshow('panel');

                if ($(settings.target + '.active', this).length === 0) {
                    $(settings.target + ':first', this).addClass('active');
                    $('li:first', data.panel).addClass('active');
                }

                if (settings.auto) {
                    $this.dbSlideshow('start');
                }
            });
            
        },
        start : function( ) {
            return this.each(function(){
                if (settings.debug === true) {
                    console.log('start');
                }

                var $this = $(this), data = $this.data('slideshow');
                data.timeout = setTimeout(function() {
                    if (settings.auto === true) {
                        $this.dbSlideshow('next');
                        data.timeout = setTimeout(arguments.callee, settings.interval);
                    }
                }, settings.interval);
            });
        },
        stop : function() {
            return this.each(function(){
                if (settings.debug === true) {
                    console.log('stop');
                }

                var $this = $(this), data = $this.data('slideshow');
                if (!data.timeout) {
                    return;
                }
                data.timeout = clearTimeout(data.timeout);
            });
        },
        toggle : function() {
            return this.each(function(){
                if (settings.debug === true) {
                    console.log('toggle');
                }

                var $this = $(this), data = $this.data('slideshow');
                if (!data.timeout) {
                    if (settings.quickstart) {
                        $this.dbSlideshow('next');
                    }
                    $this.dbSlideshow('start');
                } else {
                    $this.dbSlideshow('stop');
                }
            });
        },
        next : function(options) {
            return this.each(function(){
                var $this = $(this), data = $this.data('slideshow');
                
                var $active = $(settings.target + '.active', data.target);

                if ( $active.length == 0 ) {
                    $active = $(settings.target + ':first', data.target);
                }

                var $next;
                if (options && options.id) {
                    if (data.timeout) {
                        $this.dbSlideshow('stop');
                    }
                    $next = $(settings.target + '#' + options.id);
                }

                if (!$next) {
                    if ($active.next(settings.target).length) {
                        $next = $active.next(settings.target);
                    } else {
                        $next = $(settings.target + ':first', data.target);
                    }
                }

                if ($active.attr('id') === $next.attr('id')) {
                    return;
                }
                
                if (settings.debug === true) {
                    console.log('next: ' + $next.attr('id'));
                }

                if (data.timeout === undefined && settings.autoResume === true) {
                    $this.dbSlideshow('start');
                }

                $active.removeClass('active');
                $next.addClass('active');


                $("a[href='#" + $next.attr('id') + "']", data.panel).parent('li').addClass('active').siblings().removeClass('active');
            });
        },
        panel : function() {
            return this.each(function(){
                if (settings.debug === true) {
                    console.log('panel');
                }

                var $this = $(this), data = $this.data('slideshow');

                var list = $('<ul />');
                $(settings.target, data.target).each(function(i){
                    var id = $(this).attr('id');
                    if (!id) {
                        id = settings.cssClass + "-image-" + i;
                        $(this).attr('id', id);
                    }
                    var a = $('<a />').attr('href', '#' + id).html($('<span />').text(i)).click(function(e){
                        e.preventDefault();
                        $this.dbSlideshow('next', {'id': id});
                    });
                    var $extraClass = $(this).attr('class');
                    var li = $('<li />').addClass($extraClass).html(a);
                    list.append(li);
                });

                data.panel = $('<div />').addClass(settings.cssClass + '-panel').append(list);

                if (settings.pauseButton === true) {
                    var button = $('<a />').attr('href', '#pause').html($('<span />').text('pause')).click(function(e){
                        e.preventDefault();
                        $this.dbSlideshow('toggle');
                    });
                    var toggle = $('<span />').addClass('toggle').append(button);
                    data.panel.append(toggle);
                }

                $this.append(data.panel);
            });
        }
    };

    // $.fn is the object we add our custom functions to
    $.fn.dbSlideshow = function(method)
    {
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.dbSlideshow' );
        }
        return false;
    };
    
})(jQuery);
