/*
 *  Fabrizio Calderan @ h-art
 *  rev. 2008.04.16
 */

function PhotoGallery() {

    var pg          = null;
    var pgcontainer = null;
    var pgWindow    = 0;

    var pgpictures  = [];
    var currpicture = 1;
    var picturesize = {
        width       : 0,
        height      : 0
    }

    var nextLink, prevLink = null;
    var slideFx     = null;
    var pgMutex     = true;

    this.initGallery = function() {
        pgpictures  = pg.getElements('img');
        pglinks     = pg.getElements('a');

        currlink    = 1;
        totlink     = pgpictures.length;

    };

    this.setClasses = function() {
        pg.addClass('js');
        pgcontainer         = pg.getElements('div')[0];
        pgcontainer.setStyle('width', (pgpictures.length * picturesize.width)+2);
        slideFx = new Fx.Morph(pgcontainer, {duration: '500', transition: Fx.Transitions.Quart.easeOut});

    };

    this.createLinks = function() {

        prevLink = new Element('a');
        prevLink.addClass('prevlink');
        prevLink.href = '#';
        prevLink.innerHTML = 'previous photo';
        // prevLink.setStyle('visibility', 'hidden');
        prevLink.injectBefore(pg);

        nextLink = new Element('a');
        nextLink.addClass('nextlink');
        nextLink.href = '#';
        nextLink.innerHTML = 'next photo';
        nextLink.injectAfter(pg);


        prevLink.onclick = this.prevClick;
        nextLink.onclick = this.nextClick;
    }


    this.nextClick = function() {
        if (pgMutex) {
            prevLink.setStyle('visibility', 'visible');

            if (currpicture <= pgpictures.length - pgWindow) {
                pgMutex = false;
                var offset = -(currpicture * (picturesize.width - 1));

                slideFx.start({'margin-left': offset}).chain(function() {
                    pgMutex = true;
                    ++currpicture;
                });
            }
        }

        if ((currpicture) == pgpictures.length - pgWindow) {
            this.setStyle('visibility', 'hidden');
        }

        return false;
    };

    this.prevClick = function() {

        if (pgMutex) {
            nextLink.setStyle('visibility', 'visible');

            if (currpicture > 1) {
                pgMutex = false;
                var offset = -((--currpicture - 1) * (picturesize.width - 1));
                slideFx.start({'margin-left': offset}).chain(function() {
                    pgMutex = true;
                });
            }
        };


        if (currpicture == 1) {
            this.setStyle('visibility', 'hidden');
        };

        return false;
    };



    this.init = function(idpg, width, height) {
        if ($(idpg)) {
            pg = $(idpg);
            picturesize.width   = width;
            picturesize.height  = height;

            this.initGallery();
            this.setClasses();
            this.createLinks();
        }
    };

    this.setWindow = function(window) {
        pgWindow        = window;
    };
}
