
var VideoGalleryClass = Class.create();
VideoGalleryClass.prototype = 
{
    pageDimensions : {
        width : null,
        height : null
    },
    
    initialize: function(total) 
    {
        this._getPageDimensions();
        
        this.total = total;
        this.player = null;
        this.current = 0;
        this.albumOverlay = $('BusyOverlay');
        this.albumView = $('galleryAlbumView');
        this.prev = $('galleryPreviewPrev'); 
        this.next = $('galleryPreviewNext'); 
        this.madeby = $('pageMadeby'); 
        
        Event.observe(this.prev, 'click', this.clickPrevNext.bindAsEventListener(this)  );
        Event.observe(this.next, 'click', this.clickPrevNext.bindAsEventListener(this)  );
        
    },

    showVideo: function(id, updateAlbumElements)
    {
        this.current = id;

        this.updateView( id );

        if(updateAlbumElements)
            this.toggleAlbumElements(true);

        this.updateVideo();
    },
    
    updateVideo: function()
    {   
        if(!this.player)
            setTimeout(function updt_fnc(){ videoGallery.updateVideo(); } , 100);
    
        var item = $('item_' + this.current ); 
        var vsrc = item.readAttribute('vsrc');

        $('galleryView_Info').update( item.innerHTML );
        
        if(this.player)
        {
            this.player.sendEvent('STOP'); 
            this.player.sendEvent('LOAD', vsrc); 
            this.player.sendEvent('START');
        }
    },
    
    getPlayerObject: function()
    {
        if(navigator.appName.indexOf("Microsoft") != -1) {
            return window['player1'];
        } else {
            return document['player1'];
        }
    },
    
    toggleAlbumElements: function(show)
    {
        if(show) {
            //this.albumView.show();
            this.albumView.style.visibility = "visible";
            this.albumView.style.position = 'relative';                   
            this.albumView.style.left = "auto";
            this.albumView.style.top = "auto";
            
            this._getPageDimensions();
            this.albumOverlay.style.height = this.pageDimensions.height+'px';
            this.albumOverlay.show();
            this.madeby.hide();
            this.albumView.scrollTo();  
             }
        else {
            this.getPlayerObject().sendEvent('STOP');
                
            this.player = null;
            this.albumOverlay.hide();
            
            //this.albumView.hide();
            this.albumView.style.visibility="hidden"; 
            this.albumView.style.position = 'absolute';             
            this.albumView.style.left = '-9000px';
            this.albumView.style.top = '-9000px';
             
            
            this.madeby.show();     
            }                  
    },
    
    updateView: function( id )
    { 
        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();
        }
    },
    
    clickPrevNext: function( event )
    {
        var id = event.target.readAttribute('pid');
        this.showVideo( id, 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;
        }
    } 
}