/**
 * Ajax Newsletter overlay for zeitgeist
 */

var YoutubeOverlay = Class.create({

	appname: 	'YoutubeOverlay',
	init: 		false, 

	initialize: function()
	{                   
		                      
		this.init 		= true; 
		                                                                     
	    this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
	    this.showPlayer 		= this.showPlayer.bindAsEventListener(this); 
		                     
		//this.addContainers();      
		this.bindObservers();       
	},
	
	bindObservers: function()
	{
	},


	showPlayer: function( youtube_key )
	{                
		if(slideshow) {            
			slideshow.disableKeyboardNav(); 
		}
		this.enableKeyboardNav();                                                                                    
                                                    
		this.showLoading();                     
		
		var arrayPageSize = this.getPageSize();
        $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' }); 
        var ap = new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8});                                          
		    
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden'; });

		var player_code = '<div class="box-text" style="width: 720px;"><div id="imprint" style="width: 720px;">';
		player_code += '<div onclick="_youtube.hidePlayer()" id="close">» close</div>';
		player_code += '<object style="height: 344px; width: 425px">';
		player_code += '<param name="movie" value="http://www.youtube.com/v/'+ youtube_key +'?version=3&autoplay=1&hd=1">';
		player_code += '<param name="allowFullScreen" value="true">';
		player_code += '<param name="allowScriptAccess" value="always">';
		player_code += '<embed src="http://www.youtube.com/v/'+ youtube_key +'?version=3&autoplay=1&hd=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="720" height="430"></object>';
		player_code += '</div></div>';

		$('overlaycontent').update(player_code);                                                          
        
		// show black background
//		$$('#overlaycontent .box-text')[0].setStyle( { width:'434px' } );
 

		// show content
		var arrayPageScroll = document.viewport.getScrollOffsets();
        var overlayTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var overlayLeft = arrayPageScroll[0];
        $('overlaycontent').setStyle({ top: overlayTop + 'px' }); 
		var ap2 = new Effect.Appear('overlaycontent', { duration: 0.2, delay: 0.6}); 
		      
        /*
var parameters = { form:'subscribe' };          
		var aj = new Ajax.Updater('overlaycontent', this.url_form, { 
			method: 'post',
			parameters: parameters,
			onComplete: function()
			{
				this.showOverlay( true ); 
			}.bind(this) 
		});
		*/
	},

	hidePlayer: function() 
	{	       
		if(slideshow) {
			slideshow.enableKeyboardNav(); 
		}
		this.disableKeyboardNav();
        
		$('overlaycontent').hide();
		$('overlaycontent').innerHTML = '';
        var fd = new Effect.Fade('overlay', { duration: 0.8 });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible'; });
    },

	showLoading: function()
	{                                                                                   
		//var ap = new Effect.Appear('overlaycontent', { duration: 0.2, delay: 0.6});   
		if( $('overlaycontent') ) {                                                       
			$('overlaycontent').update('<img src="fileadmin/templates/images/ajax-loader.gif" class="loading" />');  
			$('overlaycontent').show();
		}
	},
	
	hideLoading: function()
	{
		//$('overlaycontent').hide();                                                                                                        
	},   

	//
    //  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];
	}, 
	
	//
    //  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 ( keycode == escapeKey ){
            this.hideForm();
        }
    },

  	// helpers
	debug: function(msg)
	{
		if(window.console) {
			window.console.log(msg);//this.appname+': '+  
		}
	}
	
});
