﻿/**
 * @author pop webdev [cn]
 * @version: 0.2.
 * @classDescription: Image Rotator
 * @dependencies: Prototype v1.6.1.
 */
// start Rotator
var Rotator = Class.create({
    initialize: function (items, lnkPrev, lnkNext, options) {
        this.items = items;
        this.length = this.items.size();
        this.lnkPrev = lnkPrev;
        this.lnkNext = lnkNext;
        this.options = Object.extend({
            initialIndex: 0,
            currentItemClassName: 'current',
            speed: 0.5,
            autoRotate: false
        }, options || {});
        if (!isNaN(this.options.initialIndex) && this.options.initialIndex >= this.length) {
            this.options.initialIndex = 0;
        }
        if (isNaN(this.options.initialIndex) && this.options.initialIndex == 'random') {
            var len = items.length;
            var rand = Math.floor(Math.random() * len);
            this.options.initialIndex = rand;
        }
        if (this.length < 2) {
            this.lnkPrev.hide();
            this.lnkNext.hide();
        }
        this.isAnimating = false;
        this.currentItem = this.options.initialIndex;
        this.lnkPrev.observe('click', this.__ClickPrev.bindAsEventListener(this));
        this.lnkNext.observe('click', this.__ClickNext.bindAsEventListener(this));
        // set initial position
        this.SwapItem(this.currentItem);
        // rotate every 3 seconds
        if (this.options.autoRotate) {
            this.interval = setInterval(function () {
                this.RotateItems();
            } .bind(this), 3000);
        }
    },
    __ClickPrev: function (e) {
        e.stop();
        var element = e.findElement('a');
        clearInterval(this.interval);
        if (!this.isAnimating) {
            if (this.currentItem == 0) {
                this.currentItem = this.length;
            }
            this.currentItem--;
            this.SwapItem(this.currentItem);
        }
    },
    __ClickNext: function (e) {
        e.stop();
        var element = e.findElement('a');
        clearInterval(this.interval);
        if (!this.isAnimating) {
            this.currentItem++;
            if (this.currentItem == this.length) {
                this.currentItem = 0;
            }
            this.SwapItem(this.currentItem);
        }
    },
    // rotate thru items
    RotateItems: function () {
        this.currentItem++;
        if (this.currentItem == this.length) {
            this.currentItem = 0;
        }
        this.SwapItem(this.currentItem);
    },
    SwapItem: function (index) {
        this.items.invoke('hide').invoke('removeClassName', this.options.currentItemClassName);
        this.items[index].appear({
            duration: this.options.speed,
            beforeStart: function () {
                this.isAnimating = true;
            } .bind(this),
            afterFinish: function () {
                this.items[index].addClassName(this.options.currentItemClassName);
                this.isAnimating = false;
            } .bind(this)
        });
    }
});
// end Rotator

