jQueryTopGallery = function(idGallery) {


    this.currentphoto  = 0;
    this.autorotation = null;
    /* elemento gallery */
    this.g = {
        'cnt'        : undefined,
        'frames'  : undefined,
        'links'     : undefined
    };

    var _autoRotation = function(self) {

       /*  console.log('autorotation');
        console.log(self.g['frames'].length); */
        if (self.currentphoto < self.g['frames'].length - 1) {
            _gotophoto(self, self.currentphoto + 1);
        }
        else {
            _gotophoto(self, 0);
        }

        self.autorotation = setTimeout(function() {
            _autoRotation(self)
        }, 6000);
    };


     var _addGalleryActions = function(self) {
        var clist = $('<ul></ul>')
            .attr('id', 'topgallerycommands')
            .appendTo(self.g['cnt']);

        for (var j=0; j<self.g['frames'].length; j++) {
            (function(j) {
                $('<li><a></a></li>').
                    find('a').
                    attr('href', '#').
                    html('photo ' + j).
                    bind('click', function() {
                        clearInterval(self.autorotation);
                        _gotophoto(self, j);

                        return false;
                    }).
                    toggleClass('current', j == self.currentphoto).
                    end().
                    appendTo(clist);
                })(j);
        };

        self.g['links'] = $('a', $('#topgallerycommands'));
       // console.log(self.g['links']);
    };

    var _gotophoto = function(self, i) {
        //var fadeoutindex = (i === 0)?  self.g['frames'].length - 1 : i-1;

        $(self.g['frames'][self.currentphoto]).fadeOut(800, function() { });
        $(self.g['frames'][i]).fadeIn(800);
        self.currentphoto = i;
        //console.log(i);
        $('a.current', $('#topgallerycommands')).each(function() { $(this).removeClass('current'); });
        $(self.g['links'][i]).addClass('current');


    };

    /* funzione init */
    (function(self) {
        if (self.g['cnt'] = $(idGallery)) {
            self.g['frames'] = $('div', self.g['cnt']);
            if (self.g['frames'] < 2) return false;
            //console.log(self.g['frames']);
            //console.log(self.g['cnt']);
            _addGalleryActions(self);
             self.autorotation = setTimeout(function() { _autoRotation(self) }, 4000);
        };
    })(this);

};

jQueryBottomGallery = 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);
};

