LightboxOptions = Object.extend({
    fileBottomNavCloseImage: '/images/closelabel.gif',

    overlayOpacity: 0.8,   // controls transparency of shadow overlay

    borderSize: 10         //if you adjust the padding in the CSS, you will need to update this variable

}, window.LightboxOptions || {});

// -----------------------------------------------------------------------------------

var Lightbox = Class.create();

Lightbox.prototype = {
    imageArray: [],
    activeImage: undefined,
    
    // initialize()
    // Constructor runs on completion of the DOM loading. Calls updateImageList and then
    // the function inserts html at the bottom of the page which is used to display the shadow 
    // overlay and the image container.
    //
    initialize: function() {    
    	var playerButton = $('player-button');
    	if (!playerButton)
    		return;

        var size = 1;
        var objBody = $$('body')[0];

        objBody.appendChild(Builder.node('div',{id:'overlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'lightbox'}, [
            Builder.node('div',{id:'outerImageContainer'}, 
                Builder.node('div',{id:'imageContainer'}, [
                    Builder.node('a',{id:'player', href: '/video/kontur.flv'})
                ])
            ),
            Builder.node('div', {id:'imageDataContainer'},
                Builder.node('div',{id:'imageData'}, [
                    Builder.node('div',{id:'bottomNav'},
                        Builder.node('a',{id:'bottomNavClose', href: '#' },
                            Builder.node('img', { src: LightboxOptions.fileBottomNavCloseImage })
                        )
                    )
                ])
            )
        ]));


        $('lightbox').hide();
		$('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('outerImageContainer').setStyle({ width: size, height: size });
		$('bottomNavClose').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
		playerButton.linkify(this.start.bindAsEventListener(this));

        var th = this;
        (function(){
            var ids = 
                'overlay player lightbox outerImageContainer imageContainer lightboxImage hoverNav prevLink nextLink loading loadingLink ' + 
                'imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();
    },
    initPlayer: function() {
		var body = $(document.body);
//		this.player.setStyle({
//			left: this.left
//		});
		this.player.setStyle({
//			top: this.lightbox.getStyle('top'),
			left: this.lightbox.getStyle('left')
		});
		this.outerImageContainer.setStyle({
			width: (parseInt(this.player.getStyle('width'))+20)+'px',
			height: (parseInt(this.player.getStyle('height'))+20)+'px'
		});
		this.imageDataContainer.setStyle({
			width: (parseInt(this.player.getStyle('width'))+20)+'px'
		});
		flowplayer("player", "/flowplayer/flowplayer-3.1.3.swf").onLoad( function() { this.setVolume(20); });
		this.playerInitialized = true;
	},
    start: function(e) {    

        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
        

        // stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

        this.overlay.show();
        this.overlay.setOpacity(LightboxOptions.overlayOpacity);

        // calculate top and left offset for the lightbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var lightboxLeft = arrayPageScroll[0];
        this.lightbox.setStyle({
        	top: lightboxTop + 'px',
        	left: lightboxLeft + 'px'
        }).show();
        if (!this.playerInitialized) {
			this.initPlayer();
        }
        this.player.down().style.visibility = 'visible';
        flowplayer().show();
        
        this.enableKeyboardNav();
        e && e.stop();
    },

    //
    //  enableKeyboardNav()
    //
    enableKeyboardNav: function() {
        document.observe('keydown', this.keyboardAction); 
    },

    //
    //  disableKeyboardNav()
    //
    disableKeyboardNav: function() {
        document.stopObserving('keydown', this.keyboardAction); 
    },

    //
    //  keyboardAction()
    //
    keyboardAction: function(event) {
        var keycode = event.keyCode;

        var escapeKey;
        if (event.DOM_VK_ESCAPE) {  // mozilla
            escapeKey = event.DOM_VK_ESCAPE;
        } else { // ie
            escapeKey = 27;
        }

        var key = String.fromCharCode(keycode).toLowerCase();
        
        if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
            this.end();
        }
    },

    //
    //  end()
    //
    end: function() {
        this.disableKeyboardNav();
        flowplayer().pause();
		flowplayer().hide();
        this.lightbox.hide();
        this.overlay.hide();
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
    },

    //
    //  getPageSize()
    //
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}

document.observe('dom:loaded', function () { new Lightbox(); });
