//////////////////////////////////////////////
// ColumnBrowser Object VERSION_0.8
//////////////////////////////////////////////
//
//The light box control is a way to view 
// content in a popup window. It is currently 
// only used to show Newsfilm thumbnail 
// images, but can be used to display any 
// content that may be desired, including 
// forms and interactive elements.
//
//////////////////////////////////////////////

var LightBox = Class.create();
LightBox.prototype = {

rsid: '',
isOpen: false,
//
// Constructor
initialize: function(rsid)
	{
		this.rsid = rsid;
		
		this.cb = Builder.node("div", {id:'LBclose', onclick:"document.fire('LightBox:Hide'); return false;" });
		this.bg = Builder.node("div", {id:'LBb', style:'display:none'});
		this.lb = Builder.node("div", {id:'LB', style:'display:none'},
							   						[
														Builder.node("div", {id:'LBcont'},
																	 				[
																					 	Builder.node("div", {id:'LBtitle'},
																									 		[
																											 	this.cb,
																												Builder.node("div", {id:'LBtitletext'})
																											]
																									 ),
																					 	Builder.node("div", {id:'LBscont'})																						
																					]
																	 )
													]
							   );

		
		
		
		if ($(rsid))
		{					
			// Append to main surface
			$(rsid).appendChild(this.bg);
			$(rsid).appendChild(this.lb);
			
			$('LBb').style.height = $(rsid).clientHeight + "px";			
			
			$('LB').style.marginTop = (($('LB').clientHeight - $('LBscont').offsetHeight)/2) + "px";
		}
		
		document.observe('LightBox:Show',				this.Open.bindAsEventListener(this));
		document.observe('LightBox:ShowImage',	this.OpenImage.bindAsEventListener(this));
		document.observe('LightBox:Hide',				this.Close.bindAsEventListener(this));
	
		//Event.observe(this.cb, "click", this.Close.bindAsEventListener(this));
		Event.observe(this.bg, "click", this.Close.bindAsEventListener(this));
	
	},
		

//Open the light box with a passed object to append as the
//child element.  can be just an image url
Open: function(e)
	{
		var params = e.memo;
		//Hide the video player, as any embed controls 
		//can screw up the light box
		document.fire('VideoPlayer:Hide');
		
		this.isOpen = true;
		$('LBb').style.height = $(this.rsid).clientHeight + "px";
		$('LBb').style.width = $(this.rsid).clientWidth + "px";
		
		$('LBtitletext').update(params.title);
		
		$('LBscont').update();
		$('LBscont').appendChild(params.childNode);
		$('LBscont').style.height = "";
		
		  var scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		  }			
		  
		  
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }	
	  
	  $('LB').setStyle({top: "-2000px"});
		$('LB').style.display ="";
	  var t = (scrOfY +((myHeight - $('LBcont').offsetHeight)/2));
		var l = (myWidth - $('LBcont').offsetWidth)/2;
		$('LB').style.display ="none";
		$('LB').setStyle({top: t + "px", left: l + "px"});
		
		Effect.Appear('LBb', 
					  	{
							duration: 0.6,
							from: 0,
							to: 0.75/*,
							
							afterFinish: function()
										{
											$('LB').setStyle({top: "-2000px"});
											$('LB').style.display ="";
											var t = (scrOfY +((myHeight - $('LBcont').offsetHeight)/2));
											var l = (myWidth - $('LBcont').offsetWidth)/2;
											$('LB').style.display ="none";
											$('LB').setStyle({top: t + "px", left: l + "px"});
											new Effect.Unfold('LB');									
										}*/
						}
					  );
		
		Effect.Appear('LB', {duration: 0.6, from: 0, to: 1});
		

		
		
		
	
	},
	
//Open an image from a url
OpenImage: function(e)
	{
		var params = e.memo;
		params.childNode = Builder.node('div', {style:'height:400px'} ,'Loading Image');
		this.Open(params);
		
		var imageUrl = params.url;	
		
		var imgElt = new Image();
		var thisObj = this;
		//Load an image in the background
		imgElt.onload = function() { thisObj.ImageOnLoad(imgElt); }
		imgElt.src = imageUrl;
	},
	
//display the image, and resize the light box
ImageOnLoad: function(imgObj)
	{
		$('LBscont').update(imgObj);			
		$('LBscont').setStyle({height:imgObj.height + 'px', width:imgObj.width + 'px'});
		
		var scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
	  }			
		  
		  
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }		
		
		$('LB').style.display = '';
		var t = (scrOfY +((myHeight - $('LBcont').offsetHeight)/2));
		var l = (myWidth - $('LBcont').offsetWidth)/2;
		
		$('LB').style.display ="none";
		$('LB').setStyle({top: t + "px", left: l + "px"});
		$('LB').style.display = "";
	},

//Close the lightbox
Close: function(e)
	 {
		 if (e)
			 Event.stop(e);
		 if (this.isOpen)
		 {
			 
			 document.fire('VideoPlayer:Show');
			 
			 this.isOpen = false;
		 	Effect.Fade('LBb',{duration: 0.5, from: 0.75, to: 0});
		 	Effect.Fade('LB',{duration: 0.5});
		 }
	 }

}
