/*
Requires: Mootools' Function.js ans Array.js <http://mootools.net>
*/

/*

Example :
    diaporama = new Diaporama({
        // time before displaying the next image (in milliseconds)
        // Default : 4000
        autonext_time: 3000,

        // id of the <img /> tag
        img_tag: 'my_diaporama'

        // list of URL of all images
        image_list: ['/images/1.jpg', '/images/2.jpg', '/images/3.jpg',
                    '/images/4.jpg', '/images/5.jpg']

        // index of the image to start with (defaults to 0)
        current_image: 2, // this is '/images/3.jpg'

        // id of an optional "Previous image" button
        previous_button: 'my_button1',

        // id of an optional "Next image" button
        next_button: 'my_button2',
    });

*/
var Diaporama = new Class({
    initialize: function(params) {
        var diaporama = this

        this.autonext_time = params.autonext_time || 4000;

        this.image_list = params.image_list;
        this.current_image = params.current_image || 0;

        this.img_tag = $(params.img_tag);

        this.previous_button = $(params.previous_button);
        this.next_button = $(params.next_button);

        this.autonext_state = params.autonext_state_tag;
        this.autonext_state_tag = $(this.autonext_state.id);
        if (this.autonext_state_tag) {
            this.autonext_state_tag.addEvent('click', function () {
                diaporama.autonext_toggle();
            });
        }

        if (this.previous_button) {
            this.previous_button.removeAttribute('href'); // disable link
            this.previous_button.addEvent('click', function () {
                diaporama.previous();
            });
        }
        if (this.next_button) {
            this.next_button.removeAttribute('href'); // disable link
            this.next_button.addEvent('click', function () {
                diaporama.next();
            });
        }

        // disable everything if there is no <img /> tag or no image
        this.disabled = false;
        if (!this.img_tag)      { this.disabled = true; }
        if (!this.image_list[0]) { this.disabled = true; }

        this.autonext_on();
    },

    // Display another image
    display: function(number) {
        if (this.disabled) { return; }
        if (number < 0) {
            number = this.image_list.length - 1;
        }
        if (number > this.image_list.length - 1) {
            number = 0;
        }
        this.current_image = number;
        this.img_tag.setProperty('src', this.image_list[number]);
    },

    next: function() {
        this.autonext_off();
        this._next();
        this.autonext_on(); // reset the timeout
    },

    _next: function() {
        this.display(this.current_image + 1);
    },

    previous: function() {
        this.autonext_off();
        this.display(this.current_image - 1);
    },

    // Make this._next periodically called
    autonext_on: function() {
        this.autonext = true;
        $clear(this.autonext_timer);
        this.autonext_timer = this._next.periodical(this.autonext_time);

        if (this.autonext_state_tag) {
            this.autonext_state_tag.setHTML(this.autonext_state.content_on)
        }
    },

    // Disable the periodical call of this._next
    autonext_off: function() {
        this.autonext = false;
        $clear(this.autonext_timer);

        if (this.autonext_state_tag) {
            this.autonext_state_tag.setHTML(this.autonext_state.content_off)
        }
    },

    autonext_toggle: function() {
        if (this.autonext) {
            this.autonext_off();
        } else {
            this.autonext_on();
        }
    },
})