/**
*
*
* bxSlider: Content slider / fade / ticker using the jQuery javascript library.
*
* Author: Steven Wanderski
* Email: wandoledzep@gmail.com
* URL: http://bxslider.com
* 
*
**/

jQuery.fn.bxSlider = function(options) {

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Declare variables and functions
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    var defaults = {
        mode: 'slide',
        speed: 500,
        auto: true,
        pause: 8000,
        prev_text: '<< prev',
        next_text: 'next >>',
        width: $(this).children().width(),
        prev_img: 'images/prev.gif',
        next_img: 'images/next.gif',
        ticker_direction: 'left',
        wrapper_class: 'container'
    };

    options = $.extend(defaults, options);

    /*if (options.mode == 'ticker') {
    options.auto = true;
    }*/

    var $this = $(this);
    var $parent_width = options.width + 30;
    var current = 0;
    var is_working = false;
    var child_count = $this.children().size();
    var i = 0;
    var j = 0;
    var k = 0;
    var t;

    function animate_next() {

        is_working = true;

        $this.animate({ 'left': '-' + $parent_width * 2 + 'px' }, options.speed, function() {

            $this.css({ 'left': '-' + $parent_width + 'px' }).children(':first').appendTo($this);

            is_working = false;

        });

    }

    function animate_prev() {

        is_working = true;

        $this.animate({ 'left': 0 }, options.speed, function() {

            $this.css({ 'left': '-' + $parent_width + 'px' }).children(':last').insertBefore($this.children(':first'));

            is_working = false;

        });

    }

    function fade(direction) {

        if (direction == 'next') {

            var last_before_switch = child_count - 1;
            var start_over = 0;
            var incr = k + 1;

        } else if (direction == 'prev') {

            var last_before_switch = 0;
            var start_over = child_count - 1;
            var incr = k - 1;

        }

        is_working = true;

        if (k == last_before_switch) {

            $this.children().eq(k).fadeTo(options.speed, 0);
            $this.children().eq(start_over).fadeTo(options.speed, 1, function() {

                is_working = false;

                k = start_over;

            });

        } else {

            $this.children().eq(k).fadeTo(options.speed, 0);
            $this.children().eq(incr).fadeTo(options.speed, 1, function() {

                is_working = false;

                k = incr;

            });

        }

    }

    function ticker() {

        if (options.ticker_direction == 'left') {

            $this.animate({ 'left': '-' + $parent_width * 2 + 'px' }, options.speed, 'linear', function() {

                $this.css({ 'left': '-' + $parent_width + 'px' }).children(':first').appendTo($this);

                ticker();

            });

        } else if (options.ticker_direction == 'right') {

            $this.animate({ 'left': 0 }, options.speed, 'linear', function() {

                $this.css({ 'left': '-' + $parent_width + 'px' }).children(':last').insertBefore($this.children(':first'));

                ticker();

            });

        }

    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Set a timed interval 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    function startInterval() {
        t = setInterval(function() { animate_next(); }, options.pause);
    }

    function resetInterval() {
        clearInterval(t);
        startInterval();
    }
    

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Some CSS
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    $this.parent().css({
        'overflow': 'hidden',
        'position': 'relative',
        'width': options.width + 'px'
    });

    $this.css({
        'width': '999999px',
        'position': 'relative',
        'left': '-' + $parent_width + 'px'
    });

    $this.children().css({
        'float': 'left',
        'width': $parent_width
    });

    $this.children(':last').insertBefore($this.children(':first'));

    $('#slider_prev').css({ 'cursor': 'pointer' });
    $('#slider_next').css({ 'cursor': 'pointer' });



    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Actions when user clicks next / prev buttons        
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    $('#slider_next').click(function() {

        if (!is_working) {
            animate_next();
            resetInterval();
        }

        return false;

    });

    $('#slider_prev').click(function() {

        if (!is_working) {
            animate_prev();
            resetInterval();
        }

        return false;

    });

    //last but not least: start the interval
    startInterval();

}