/**
 * Klasse für die Bildergalerie.
 * 
 * @author  Helmut Lüdders <luedders@topconcepts.de>
 */
var CM_ImageGallerySingle = Class.create();

CM_ImageGallerySingle.prototype = {
    /**
     * Die Id der Galerie.
     * 
     * @type    {Number}
     */
    _id: undefined,

    /**
     * Der Index des aktuellen Bildes.
     * 
     * @type    {Number}
     */
    _itemIndex: 0,

    /**
     * Die Optionen der Diashow.
     * 
     * @type    {Hash}
     */
    _options: {},

    /**
     * Ob die Diashow läuft.
     * 
     * @type    {Boolean}
     */
    _running: false,

    /**
     * Timeout für die Diashow.
     * 
     * @type    {Number}
     */
    _timeout: undefined,



    /**
     * Aktualisiert die Daten des Bildes.
     */
    _setData: function() {
        if (this._options.items[this._itemIndex].title == null) {
            this._options.items[this._itemIndex].title = '';
        }
        if (this._options.items[this._itemIndex].description == null) {
            this._options.items[this._itemIndex].description = '';
        }

        var item = $('image_gallery_' + this._id + '_item');
        item.getElementsByTagName('img')[0].src = this._options.items[this._itemIndex].href;
        item.getElementsByTagName('img')[0].alt = this._options.items[this._itemIndex].title;
        item.getElementsByTagName('span')[0].innerHTML = this._options.items[this._itemIndex].title;
        item.getElementsByTagName('p')[0].innerHTML = this._options.items[this._itemIndex].description;

        this._setLinkStatus();

        $A(item.getElementsByTagName('a')).each(
            function(link) {
                link.href = this._options.items[this._itemIndex].link;
            }.bind(this)
        );
    },



    /**
     * Aktiviert / Deaktiviert den Link des Bildes.
     */
    _setLinkStatus: function() {
        if (this._options.items[this._itemIndex].link == null) {
            this._options.items[this._itemIndex].link = '';
        }

        var item = $('image_gallery_' + this._id + '_item');
        if (this._options.items[this._itemIndex].link == '') {
            item.getElementsByTagName('a')[0].onclick = function() { return false; };
            item.getElementsByTagName('a')[0].style.cursor = 'default';
        } else {
            item.getElementsByTagName('a')[0].onclick = '';
            item.getElementsByTagName('a')[0].style.cursor = 'pointer';
        }
    },



    /**
     * Zeigt das nächste Bild der Diashow an.
     */
    _slideShowNext: function() {
        if (this._itemIndex + 1 == this._options.items.length) {
            this._itemIndex = -1;
        }

        if (this._itemIndex + 2 == this._options.items.length && !this._options.slideShow.repeat) {
            this.slideShowStop();
        }

        this.next();
    },



    /**
     * Startet den Timeout für das nächste Bild.
     */
    _slideShowStartTimeout: function() {
        if (this._running) {
            this._timeout = window.setTimeout(this._slideShowNext.bind(this), this._options.slideShow.interval * 1000);
        }
    },



    /**
     * Aktualisiert die Navigations Elemente.
     */
    _updateNav: function() {
        if (!this._running) {
            if (this._itemIndex == 0) {
                $('image_gallery_' + this._id + '_prev_link').hide();
            } else {
                $('image_gallery_' + this._id + '_prev_link').show();
            }

            if (this._itemIndex + 1 == this._options.items.length) {
                $('image_gallery_' + this._id + '_next_link').hide();
            } else {
                $('image_gallery_' + this._id + '_next_link').show();
            }

            if (this._options.slideShow.repeat || this._itemIndex + 1 != this._options.items.length) {
                $('image_gallery_' + this._id + '_slide_show_start_link').show();
            } else {
                $('image_gallery_' + this._id + '_slide_show_start_link').hide();
            }
            $('image_gallery_' + this._id + '_slide_show_stop_link').hide();
        } else {
            $('image_gallery_' + this._id + '_prev_link').hide();
            $('image_gallery_' + this._id + '_next_link').hide();

            $('image_gallery_' + this._id + '_slide_show_start_link').hide();
            $('image_gallery_' + this._id + '_slide_show_stop_link').show();
        }
    },



    /**
     * Setzt den Timeout.
     * @constructor
     * 
     * @param   {Number}    id      Die Id der Bildergalerie.
     * @param   {Hash}      options Die Diashow Optionen.
     */
    initialize: function(id, options) {
        this._id = id;
        this._options = options;

        this._updateNav();
        this._setLinkStatus();

        if (this._options.slideShow.type == 1) {
            this.slideShowStart();
        }
    },



    /**
     * Zeigt das nächste Bild an.
     */
    next: function() {
        this._itemIndex++;

        this._updateNav();
        this._setData();

        if (this._running) {
            this._slideShowStartTimeout();
        }
    },



    /**
     * Zeigt das vorherige Bild an.
     */
    prev: function() {
        this._itemIndex--;

        this._updateNav();
        this._setData();
    },



    /**
     * Startet die Diashow.
     */
    slideShowStart: function() {
        this._running = true;
        this._slideShowStartTimeout();
        this._updateNav();
    },



    /**
     * Beendet die Diashow.
     */
    slideShowStop: function() {
        this._running = false;
        window.clearTimeout(this._timeout);
        this._updateNav();
    }
}