C4AR.Lightbox = function()
{
	
	var YUE = YAHOO.util.Event;
	var YUD = YAHOO.util.Dom;
	
	var colour = '000000';
		
	return {
		
		init: function(link_id)
		{
		  this.root = document.getElementsByTagName('body')[0];
			this.link = document.getElementById(link_id);
      
			this.setup();
			this.attachEvents();
		},
		
		setup: function()
		{
	    var region = YUD.getRegion(this.root);
			var width = YUD.getDocumentWidth();
			var height = parseInt(YUD.getDocumentHeight() * 2, 10); // YUD methods to get document height don't really work
			
			this.maskPanel = document.createElement('div');
			this.maskPanel.id = 'lightbox_mask_panel';
			this.maskPanel.style.margin = '0 auto';            
			
			this.insertPlayer();  
			
		  // this.maskPanel.appendChild(this.btn_close);
		
		  this.root.appendChild(this.maskPanel);      
			
		this.maskBG = document.createElement('div');
		this.maskBG.id = 'lightbox_mask_bg';
		this.maskBG.style.width = width + 'px';
		this.maskBG.style.height = height + 'px';
				
		YUD.insertBefore(this.maskBG, this.maskPanel);
				
		},
	
		insertPlayer: function()
		{
      var html = '<embed src="http://services.brightcove.com/services/viewer/federated_f8/'+ this.link.getAttribute('rel') +'"'+
                 'bgcolor="#000000"'+ 
                 'flashvars="playerId='+ this.link.getAttribute('rel')  +'&amp;viewerSecureGatewayURL=https://services.brightcove.com/services/amfgateway&amp;servicesURL=http://services.brightcove.com/services&amp;cdnURL=http://admin.brightcove.com&amp;domain=embed&amp;autoStart=false&amp;"'+
                 'base="http://admin.brightcove.com"'+ 
                 'name="flashObj"'+ 
                 'width="486"'+
                 'height="412"'+
                 'seamlesstabbing="false"'+
                 'type="application/x-shockwave-flash"'+
                 'swliveconnect="true"'+ 
                 'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"/>'
                 		  
		  this.maskPanel.innerHTML = html;

      // attach close button and add event
		  var p = this.maskPanel.firstChild;
		  this.btn_close = document.createElement('img');
      this.btn_close.id = 'btn_close';
      this.btn_close.src = 'images/icons/close.gif';
      this.btn_close.alt = 'Close';
		  YUD.insertBefore( this.btn_close, p);
		  YUE.on(this.btn_close, 'click', this.hide, this);	

		},
		
		attachEvents: function()
		{
			YUE.on(this.maskBG, 'click', this.hide, this);	
											
			var link = document.getElementById('video_link', 'a', this.root);			
			YUE.on(link, 'click', this.show, this); 			
		},
		
				
		positionPanel: function()
		{		
		  var region = YUD.getRegion(this.maskPanel);
		
			var panel_width = (region.right - region.left) > 100 ? (region.right - region.left) : 680;

			var left = Math.round((YUD.getViewportWidth() / 2) - (panel_width / 2)) + 'px';
			var top = YUD.getDocumentScrollTop() + 75 + 'px';

			this.maskPanel.style.left = left;
			this.maskPanel.style.top  = top;
		},
		
		show: function(e, that)
		{
		  // deal with DOM references and event targets
		  
		  var target = YUE.getTarget(e) ? YUE.getTarget(e) : e;
		  
		  if(!that.maskPanel.innerHTML)
		  {
    		that.insertPlayer();
		  }
		    
			that.maskBG.style.display = 'block';
			that.maskPanel.style.display = 'block';
			that.positionPanel();
						
			var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {opacity: { to: 0.95 } }); 
			var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {opacity: { to: 1 } }); 
			anim1.animate();
			anim2.animate();
						
			YUE.preventDefault(e);
		},
		
		hide: function(e, that)
		{
			var anim1 = new YAHOO.util.ColorAnim(that.maskBG, {opacity: { to: 0 } }); 
			var anim2 = new YAHOO.util.ColorAnim(that.maskPanel, {opacity: { to: 0 } });
			anim1.onComplete.subscribe(function(){that.maskBG.style.display = 'none';});
			anim1.onComplete.subscribe(function(){that.maskPanel.style.display = 'none';});
			anim1.animate();
			anim2.animate();
			
		  that.maskPanel.innerHTML = '';
		}
		
	};
}();

YAHOO.util.Event.onAvailable('c4footerLinks', C4AR.Lightbox.init, "video_link", C4AR.Lightbox);