/**
 * Simple jQuery Gallery 0.1 - 2009.04.16
 * Author: Fabrizio Calderan, http://www.fabriziocalderan.it/
 * Do not remove this comment
 */

jQueryGallery = function(idGallery) {

    this.currentphoto  = 1;

    /* elemento gallery */
    this.g = {
        'cnt'        : undefined,
        'list'       : undefined,
        'offset'     : 0,
        'width'      : 0
    };

    this.listWidth   = 0;
    this.imagesWidth = [];
    this.actionsCnt  = undefined;

    /* azioni della gallery */
    var _actions    = {
        'prev'  : '< precedente',
        'next'  : 'successiva >'
    };

    var _scrollTime = 500;

    var _addGalleryActions = function(self) {
        $('<p class="gallerycontrols"></p>').appendTo(self.g['cnt']);
        self.actionsCnt = $('p.gallerycontrols', self.g['cnt']);

        var addAction = function(label, classname, f) {
                $('<a></a>')
                .html(label)
                .attr('href', 'javascript:void(0)')
                .attr('class', classname)
                .bind('click', f)
                .appendTo(self.actionsCnt);
        };

        /* azioni 'prev' e 'next' */
        addAction(_actions['prev'], 'prev', function() { return _imageClick('prev', self); });
        addAction(_actions['next'], 'next', function() { return _imageClick('next', self); });

        /* il link 'previous' e' disabilitato allo stato iniziale */
        $('a.prev', self.actionsCnt).addClass('disabled');

    };


    var _imageClick = function(direction, self) {

        switch (direction) {
            case 'prev' :
                if (self.g['offset'] < 0) {
                      self.g['offset'] += self.imagesWidth[self.currentphoto-2];
                      self.g['list'].animate({ 'left' : self.g['offset'] }, _scrollTime);
                      self.currentphoto--;

                      /* abilito next e disabilito prev se sono all'inizio della gallery */
                      $('a.next', self.actionsCnt).removeClass('disabled');
                      $('a.prev', self.actionsCnt).toggleClass('disabled', self.g['offset'] >= 0);

                };
                break;

            case 'next' :
                if (self.g['offset'] > -(self.listWidth - self.g['width'])) {
                        self.g['offset'] -= self.imagesWidth[self.currentphoto-1];

                        /* lo spostamento della gallery deve considerare la parte restante che puo' essere
                           minore rispetto alla larghezza del <li> successivo. Spostiamo la gallery del
                           valore minimo tra i due. */
                        self.g['list'].animate({ 'left' : -(Math.min(-self.g['offset'], (self.listWidth - self.g['width']))) }, _scrollTime);
                        self.currentphoto++;

                        /* abilito prev e disabilito next se sono alla fine della gallery */
                        $('a.prev', self.actionsCnt).removeClass('disabled');
                        $('a.next', self.actionsCnt).toggleClass('disabled', self.g['offset'] <= -(self.listWidth - self.g['width']));
                };
                break;
        };
        return false;
    };

    /* funzione init */
    (function(self) {
        if (self.g['cnt'] = $(idGallery)) {
            self.g['list'] = $('ul', self.g['cnt']);
            self.g['list'].addClass('jquery');
            self.g['width'] = self.g['cnt'].width();

            $('ul li', self.g['cnt']).each(function(i) {
                self.imagesWidth[i] = $(this).outerWidth();
                self.listWidth += self.imagesWidth[i];
            });

            if (self.listWidth < self.g['width']) return false;
            _addGalleryActions(self);
        };
    })(this);
};
