
var PhotoGalleryClass = Class.create();
PhotoGalleryClass.prototype = 
{
    pageDimensions : {
        width : null,
        height : null
    },
    
    initialize: function(total) 
    {
        this._getPageDimensions();
        
        this.total = total;
        this.albumOverlay = $('BusyOverlay');
        this.albumView = $('galleryAlbumView');
        this.view = $('galleryView');
        this.prev = $('galleryPreviewPrev'); 
        this.next = $('galleryPreviewNext'); 
        this.vertical = $('galleryViewVertical'); 
        this.madeby = $('pageMadeby'); 
        
        Event.observe(this.prev, 'click', this.showPhotoPreview.bindAsEventListener(this)  );
        Event.observe(this.next, 'click', this.showPhotoPreview.bindAsEventListener(this)  );
        
    },

    show: function(img, id, vertical, updateAlbumElements)
    {
        this.view.src = img;   
        this.updateView( id, vertical );

        if(updateAlbumElements)
            this.toggleAlbumElements(true);
    },
    
    toggleAlbumElements: function(show)
    {
        if(show) {
            this.albumView.show();  
            this.madeby.hide();                   
            this._getPageDimensions();
            this.albumOverlay.style.height = this.pageDimensions.height+'px';
            this.albumOverlay.show();
            this.albumView.scrollTo();
             }
        else {
            this.albumOverlay.hide();
            this.madeby.show();
            this.albumView.hide(); }
    },
    
    updateView: function( id, vert )
    { 
        this.vertical.removeClassName( (vert==1?'viewHor':'viewVert'));
        this.vertical.addClassName( (vert==1?'viewVert':'viewHor') );
            
        id = parseInt(id);
        if (id - 1) {
          this.prev.show();
          this.prev.writeAttribute('pid', (id-1));
        } else {
          this.prev.hide();
        }
        
        if (id<this.total) {
          this.next.show();      
          this.next.writeAttribute('pid', (id+1));
        } else {
          this.next.hide();
        }
    },
    
    showPhotoPreview: function( event )
    {
        var id = event.target.readAttribute('pid');
        var item = $('item_'+id);
        this.show( item.readAttribute('psrc') , id, item.readAttribute('pvert'), false);
    },
    
    //  Get the actual page size
    _getPageDimensions : function() {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {    
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ 
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { 
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;
        if (self.innerHeight) {    
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { 
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { 
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }    

        if(yScroll < windowHeight){
            this.pageDimensions.height = windowHeight;
        } else { 
            this.pageDimensions.height = yScroll;
        }

        if(xScroll < windowWidth){    
            this.pageDimensions.width = windowWidth;
        } else {
            this.pageDimensions.width = xScroll;
        }
    } 
}