/**
* Sliding gallery
* v. 1.1
*
* @author Aurimas Baubkus, Dalius Šidlauskas
*/

var Gallery = new Class.create();

Gallery.prototype = {
  
    thumbs: null,
    photos: null,
    phoros_gal: null,
    current: null,
    total: null,
    root: null,
    active: null,
  
    initialize: function() {
        this.thumbs = new Array();
        this.photos = new Array();
        this.photos_gal = new Array();
        this.current = 0;
        this.total = -1;
        this.active = 1;
        this.initAutocenter();
    },
  
    reset: function() {
        this.photos = new Array();
        this.thumbs = new Array();
        this.photos_gal = new Array();
        this.current = 0;
        this.total = -1;
    },
  
    addPhoto: function(thumb, photo) {
        this.thumbs.push(thumb);
        this.photos.push(photo);
        this.total++;
    },
  
    leftMove: function() {
        this.current--;
        this.changePhotos();
    },
  
    rightMove: function() {
        this.current++;
        this.changePhotos();
    },
  
    changePhotos: function() {
        if (this.current < 0) {
            this.current = this.total;
        } else if (this.current > (this.total)) {
            this.current = 0;
            this.resetPhotos();
            return;
        }

        $('img_1').src = this.thumbs[this.current];
        $('img_1_a').stopObserving('click');
        $('img_1_a').observe('click', function(event) {
            objGallery.changePhoto(objGallery.current);
            return false;
        });
        if (this.current + 1 > this.total) {
            $('img_2').src = this.thumbs[this.total-this.current];
            $('img_2_a').stopObserving('click');
            $('img_2_a').observe('click', function(event) {
                objGallery.changePhoto(objGallery.current-objGallery.total);
                return false;
            });
        } else {
            $('img_2').src = this.thumbs[this.current+1];
            $('img_2_a').stopObserving('click');
            $('img_2_a').observe('click', function(event) {
                objGallery.changePhoto(objGallery.current + 1);
                return false;
            });
        }

        if (this.current + 2 > this.total) {
            $('img_3').src = this.thumbs[(this.current+1)-this.total];
            $('img_3_a').stopObserving('click');
            $('img_3_a').observe('click', function(event) {
                objGallery.changePhoto((objGallery.current+1)-objGallery.total);
                return false;
            });
        } else {
            $('img_3').src = this.thumbs[this.current+2];
            $('img_3_a').stopObserving('click');
            $('img_3_a').observe('click', function(event) {
                objGallery.changePhoto(objGallery.current + 2);
                return false;
            });
        }
    },
  
    resetPhotos: function() {
        var number = 1;
        for (var i = 0; i < 3; i++) {
      
            $('img_' + number).src = this.thumbs[i];
            this.addValues(number, i);
            ++number;
        }
    },
  
    addValues: function(id, photo_id) {
        $('img_' + id + '_a').stopObserving('click');
        $('img_' + id + '_a').observe('click', function(event) {
            objGallery.changePhoto(photo_id);
            return false;
        });
    },
  
    changePhoto: function(id) {
        /*
        $('full_img').setStyle({
            left : '0px', top : '0px'
        });
        */
        $('full_img').src = this.photos[id];
    },


    initAutocenter: function() {
        $('full_img').observe('load', function(event) {
            var left = 0;
            var top = 0;

            
            var parent  = $('full_img').up().getDimensions();
            var image   = $('full_img').getDimensions();
            if (parent.width > image.width) {
                left = (parent.width - image.width) / 2;
                
            }
            if (parent.height > image.height) {
                top = (parent.height - image.height) / 2;
                
            }
            $('full_img').setStyle({left : left + 'px'});
            $('full_img').setStyle({top : top + 'px'});
        });
    }
};
