/*
    jQuery Content Slider 1.0 (24 April 2011)
    Extreamely simple carousel plugin (slider)

    ~ Copyright (c) Piotr Kulpinski - http://piotrkulpinski.com
    ~ Dual licensed under the MIT and GPL version 2 licenses.

    ~~ Project site: http://plugins.piotrkulpinski.com/content-slider/
    ~~ Github site: http://github.com/piotrkulpinski/content-slider
*/

(function($) {

    $.ContentSlider = function (element, options) {
	
        //  Extending default options
        opts = $.extend($.fn.ContentSlider.defaults, options);

        // Private instance variables
        var paused = false, inMotion = false;

        // Markup holders
        $wrapper = $(element)
            .prepend('<a href="#" class="' + classPrefix('lb') + '" />')
            .append('<a href="#" class="' + classPrefix('rb') + '" />')
            .hover(function() { paused = true; }, function() { paused = false; });

        // Run animation if specified
        if (opts.autoload) setTimeout(animate, opts.pauseTime);

        $('.' + classPrefix('lb')).click(function() {
            if (!inMotion) moveSlider('left');
            return false;
        });

        $('.' + classPrefix('rb')).click(function() {
            if (!inMotion) moveSlider('right');
            return false;
        });
        
        function classPrefix(cls) {
            return opts.classPrefix + '-' + cls;
        };

        // Recursive initial function
        function animate() {
            if (!paused) moveSlider();
            setTimeout(animate, opts.pauseTime);
        };

        function moveSlider(dir) {

            inMotion = true;

            $slider = $('ul', $wrapper);
            
            $current = $slider.find('.current');

            dir     = (typeof(dir) == 'undefined') ? opts.slideDir : dir;
            $next   = (dir == 'right') ? $current.next() : $current.prev();
            
            if (!$next.length)
              $next = (dir == 'right') ? $slider.find('li').first() : $slider.find('li').last();
            
            $current.fadeOut(opts.animSpeed, function() { 
              $(this).removeClass('current');
              
              $next.fadeIn(opts.animSpeed - 200, function() { 
                $(this).addClass('current'); inMotion = false; });
            });
        };
    };

    /* ContentSlider starts here */
    $.fn.ContentSlider = function(options) {

        // If options is not object, make it clear
        if (typeof(options) !== 'object') options = { };

        // For each matching element start new function
        this.each(function() {

            // Call main method
            $.ContentSlider(this, options);
            return this;

        });

        return this;
    };

    /* Publicly accessible defaults. */
    $.fn.ContentSlider.defaults = {

        classPrefix     : 'slider',
        autoload        : true,
        animSpeed       : 500,
        pauseTime       : 5000,
        pauseOnHover    : true,
        slideDir        : 'right'

    };

})(jQuery);
