/*	jQuery Slidester v0.1 - http://www.downtownandybrown.com/slidester		Copyright (c) 2009 Andrew Brown		Inspired by noobSlide for mootools at http://www.efectorelativo.net/laboratory/noobSlide/		Requirements:	- jQuery 1.3.2 ... available via  http://www.jquery.com	- jQuery easing plugin (1.3) ... available via  http://gsgd.co.uk/sandbox/jquery/easing/	- CSS included in index.html*/
// create closure
(function($) {
    var opts = null;
    var _play = false;

    // Slidester plugin definition
    $.fn.Slidester = function(options) {
        // Extend our default options with those provided.
        // Note that the first arg to extend is an empty object -
        // this is to keep from overriding our "defaults" object.
        opts = $.extend({}, $.fn.Slidester.defaults, options);

        if (opts.handles) {
            $.fn.Slidester.addHandleButtons(opts.handles);
        }

        if (opts.addButtons) {
            for (var action in opts.addButtons) {
                $.fn.Slidester.addActionButtons(action, opts.addButtons[action]);
            }
        }

        $.fn.Slidester.walk(opts.startItem, true, true);
    }

    // plugin defaults - added as a property on our plugin function
    $.fn.Slidester.defaults = {
        startItem: 0,
        mode: 'vertical',
        box: [],
        items: [],
        size: 365,
        handles: [],
        handle_event: 'click',
        addButtons: {
            previous: [],
            next: [],
            play: [],
            playback: [],
            stop: []
        },
        button_event: 'click',
        interval: 5000,
        autoPlay: false,
        easeFunc: 'linear',
        easeTime: 'normal',
        onWalk: null
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.Slidester.vals = {
        currentIndex: null,
        previousIndex: null,
        nextIndex: null
    };

    $.fn.Slidester.addHandleButtons = function(handles) {
        handles.bind(opts.handle_event, function() {
            $.fn.Slidester.walk(handles.index(this));
        });
    };

    $.fn.Slidester.addActionButtons = function(action, buttons) {
        switch (action) {
            case 'previous':
                buttons.bind(opts.button_event, function() {
                    $.fn.Slidester.previous(true);
                });
                break;
            case 'next':
                buttons.bind(opts.button_event, function() {
                    $.fn.Slidester.next(true);
                });
                break;
            case 'play':
                buttons.bind(opts.button_event, function() {
                    $.fn.Slidester.play(opts.interval, 'next', false);
                });
                break;
            case 'playback':
                buttons.bind(opts.button_event, function() {
                    $.fn.Slidester.play(opts.interval, 'previous', false);
                });
                break;
            case 'stop':
                buttons.bind(opts.button_event, function() {
                    $.fn.Slidester.stop();
                });
                break;
        }
    };

    $.fn.Slidester.previous = function(manual) {
        $.fn.Slidester.walk(($.fn.Slidester.vals.currentIndex > 0 ? $.fn.Slidester.vals.currentIndex - 1 : opts.items.length - 1), manual);
    };

    $.fn.Slidester.next = function(manual) {
        $.fn.Slidester.walk(($.fn.Slidester.vals.currentIndex < opts.items.length - 1 ? $.fn.Slidester.vals.currentIndex + 1 : 0), manual);
    }

    $.fn.Slidester.play = function(interval, direction, wait) {
        $.fn.Slidester.stop();
        if (!wait) {
            $.fn.Slidester[direction](false);
        }
        _play = setInterval('$.fn.Slidester.' + direction + '(false);', interval);
    };

    $.fn.Slidester.stop = function() {
        clearInterval(_play);
    };

    $.fn.Slidester.walk = function(itemIndex, manual, skipAnimation) {
        if (itemIndex != $.fn.Slidester.vals.currentIndex) {
            $.fn.Slidester.vals.currentIndex = itemIndex;
            $.fn.Slidester.vals.previousIndex = $.fn.Slidester.vals.currentIndex + ($.fn.Slidester.vals.currentIndex > 0 ? -1 : opts.items.length - 1);
            $.fn.Slidester.vals.nextIndex = $.fn.Slidester.vals.currentIndex + ($.fn.Slidester.vals.currentIndex < opts.items.length - 1 ? 1 : 1 - opts.items.length);

            //stop any current activity before moving to the next item
            opts.box.stop();

            //move slider
            if (opts.mode == 'vertical') {
                if (skipAnimation)
                    opts.box.css({ top: -($.fn.Slidester.vals.currentIndex * opts.size) });
                else
                    opts.box.animate({ top: -($.fn.Slidester.vals.currentIndex * opts.size) }, opts.easeTime, opts.easeFunc);
            }
            else if (opts.mode == 'horizontal') {
                if (skipAnimation)
                    opts.box.css({ left: -($.fn.Slidester.vals.currentIndex * opts.size) });
                else
                    opts.box.animate({ left: -($.fn.Slidester.vals.currentIndex * opts.size) }, opts.easeTime, opts.easeFunc);
            }

            if (manual) {
                $.fn.Slidester.stop();
            }

            if (manual && opts.autoPlay) {
                $.fn.Slidester.play(opts.interval, 'next', true);
            }

            if (opts.onWalk) {
                opts.onWalk(opts.items, (opts.items[$.fn.Slidester.vals.currentIndex] || null), opts.handles, (opts.handles && opts.handles[$.fn.Slidester.vals.currentIndex] ? opts.handles[$.fn.Slidester.vals.currentIndex] : null));
            }
        }
    }

    function viewport() {
        return {
            x: $(window).scrollLeft(),
            y: $(window).scrollTop(),
            cx: $(window).width(),
            cy: $(window).height()
        };
    }

    // private function for debugging
    function debug(label, value) {
        if (window.console && window.console.log)
            window.console.log(label + ': ' + value);
    }

    // end of closure
})(jQuery);
