var bb_lightwindow = Class.create();
function log()
{
	return;
	var message = "";
	for( var i = 0; i < arguments.length; i++ )
	{
		message += arguments[ i ] + " ";
	}
	if( console )
	{
		console.log( message );
	}
	else
	{
		// IE < 8, no Firebug, (Opera, Safari)?
		alert( message );
	}
}
bb_lightwindow.prototype = {

	initialize : function(){
		this.x = null;
		this.y = null;
		this.ajexURL;
		this.ajexDIV;
		this.fullScreen = true;
		this.defaultBorder = 10;
		this.defaultWidth = 150;
		this.defaultHeight = 90;
		this.defaultBorder = 10;
		this.titleBarHeight = 30;
		this.backdropOpacity = .7;
		this.growSpeed = .5;
		this.loadFade = 1;
		this.contentURL = '#';
		this.browserWidth = 0;
		this.browserHeight = 0;
		this.openInIframe = false;
		this.contentLoaded = false;
		this.morphComplete = false;
		this.divIds = ["bb_lightwindow_loading","bb_lightwindow_backdrop", "bb_lightwindow_title_bar",  "bb_lightwindow_title_bar_close", 
		"bb_lightwindow_close","bb_lightwindow_content", "bb_lightwindow_container", "bb_lightwindow_span", "bb_lightwindow"];
		
		this.windowDivFormat = '<div id="bb_lightwindow">'+
					'<div id="bb_lightwindow_title_bar">'+
						'<div id="bb_lightwindow_close">'+
							'<span id="bb_lightwindow_span"></span>'+
								'<a id="bb_lightwindow_title_bar_close" class="bb_lightwindow_close_link"> close </a>'+
						'</div>'+
						'</div>'+
					'<div id="bb_lightwindow_content">'+
					'<div id="bb_lightwindow_loading"><p> &nbsp; Loading Please Wait...</p></div>'+
					'<div id="bb_lightwindow_ajax"></div>'+
					'</div>'+
				'</div>';
		this._insertLightwindowDivs();
		this._getBrowserDimensions();
		this._getTitleBarHeight();
		this._hideLightwindowElements();				
	},
	
	loadLightWindow:function (URL, inIframe, ajexDIV,ajexURL, x, y)
	{
		this.openInIframe = inIframe;
		this.contentURL = URL;
		this.ajexURL = ajexURL;
		this.ajexDIV = ajexDIV;
		this._getBrowserDimensions();
		this.x = x;
		this.y = y;
		if(this.x == null)
		{
			this.fullScreen = true;
		}
		else
		{
			this.fullScreen = false;
		}
		this._showLightwindowElements();	
		this._morphContainerSize();
		window.onresize = function(){
			this._getBrowserDimensions();
			this._resizeWindow();
		}.bind(this);
 //this._morphContainerSize(x,y);
		
	},
	
	
	closeLightWindow:function()
	{
		$('bb_lightwindow_content').innerHTML = '<div id="bb_lightwindow_loading"><p> &nbsp; Loading Please Wait...</p></div><div id="bb_lightwindow_ajax"></div>';
		if($('bb_lightwindow_iframe')){
			Element.remove($('bb_lightwindow_iframe'));
		}		
		if($('bb_lightwindow_content'))
			{
			$('bb_lightwindow_content').setStyle({
					border:'25px solid transparent'
			});
		}
		if(this.ajexURL  != null && this.ajexDIV != null) this._refreshRequest();
		this._hideLightwindowElements();
		this.openInIframe = false;
		this.contentLoaded = false;
		this.morphComplete = false;
		window.onresize = null;
	},
	
	_resizeWindow:function()
	{
		var t = this.defaultBorder;
		var l = this.defaultBorder;
		if(!this.fullScreen){			
			t=Math.max((this.browserHeight - this.y)/2, 0);
			l=Math.max((this.browserWidth - this.x)/2 , 0);
		}
		else{
			$('bb_lightwindow_content').setStyle({
				border: 'none'
			});
			this.x = this.browserWidth - 2*t;
			this.y = this.browserHeight - 2*l - this.titleBarHeight;
		}	
		//$('bb_lightwindow_container').morph('width: '+this.x+'px; height:'+this.y+'px; top: '+t+'px; left: '+l+'px;',{duration:.05, queue:'end'});
		$('bb_lightwindow_container').setStyle({
				width: this.x  +'px',
				height: this.y +'px',
				left: l +'px',
				top: t +'px'
			});			
	},
	
	_morphContainerSize:function()
	{
		var t = this.defaultBorder;
		var l = this.defaultBorder;
		if(!this.fullScreen){			
			t=Math.max((this.browserHeight - this.y)/2, 0);
			l=Math.max((this.browserWidth - this.x)/2 , 0);
		}
		else{
			$('bb_lightwindow_content').setStyle({
				border: 'none'
			});
			this.x = this.browserWidth - 2*t;
			this.y = this.browserHeight - 2*l - this.titleBarHeight;
		}			
		if($('bb_lightwindow_container')){
			
			if($('bb_lightwindow_ajax'))
			{
				$('bb_lightwindow_ajax').setStyle({
					display:'none'
				});
			}
			if($('bb_lightwindow_iframe'))
			{
				$('bb_lightwindow_iframe').setStyle({
					display:'none'
				});
			}
			$('bb_lightwindow_container').setStyle({
				width: this.defaultWidth +'px',
				height: this.defaultHeight +'px',
				left:(this.browserWidth-this.defaultWidth)/2+'px',
				top:(this.browserHeight-this.defaultHeight)/2+'px'
			});			
			$('bb_lightwindow_container').morph('width: '+this.x+'px; height:'+this.y+'px; top: '+t+'px; left: '+l+'px;',{duration:this.growSpeed, queue:'front', 
			afterFinish:function(){
				this.morphComplete = true;
				if(this.openInIframe == true)
				{
					this._setFillContent();
				}
				else
				{
					this._requestContentHTML();
				}
				this._loadingComplete();}.bind(this)
				});
		}
	},
		
	_loadingComplete:function()
	{
		if(this.morphComplete && this.contentLoaded){
			if($('bb_lightwindow_iframe'))
			{
				$('bb_lightwindow_iframe').appear({duration:this.loadFade});
			}
			else
			{
				$('bb_lightwindow_ajax').appear({duration:this.loadFade});
			}
			
			$('bb_lightwindow_loading').fade({duration:this.loadFade});	
		}			
	},
	
	_getTitleBarHeight:function()
	{
		var str = $('bb_lightwindow_title_bar').getStyle('height');
		str  = str.replace('px','');
		this.titleBarHeight = str * 1;
		
	},
	
	_insertLightwindowDivs : function () //adds needed divs to the browser
	{
		var bb_lightwindow = document.createElement('div');
		bb_lightwindow.setAttribute('id', 'bb_lightwindow_container');
		bb_lightwindow.innerHTML = this.windowDivFormat;
		
		var backdrop = Element.extend(document.createElement('div'));
		backdrop.setAttribute('id', 'bb_lightwindow_backdrop');
		
		backdrop.setStyle({
				opacity: this.backdropOpacity,
				backgroundColor: '#000000',
				backgroundRepeat: 'repeat',
				height: '100%'
			});
		
			
			
		if (typeof XMLHttpRequest == "undefined") //IF browser is IE6 need to shrink window a bit this a hack and could be done better
		{
			backdrop.setStyle({
				width: '99.6%'
			});	
		}
		
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(backdrop);
		body.appendChild(bb_lightwindow);	
		
		if ($('bb_lightwindow_title_bar_close')) {			
			Event.observe('bb_lightwindow_title_bar_close', 'click', this.closeLightWindow.bindAsEventListener(this));
			$('bb_lightwindow_title_bar_close').onclick = function() {return false;};
		}
	},
	
	_hideLightwindowElements : function()
	{
		this.divIds.each(function(id) {
			if($(id)){
				$(id).style.display = 'inline';		
				$(id).style.visibility = 'hidden';
			}
		});
	},
	
	_showLightwindowElements : function()
	{
		this.divIds.each(function(id) {
			if($(id)){
				$(id).style.display = 'block';		
				$(id).style.visibility = 'visible';
			}				
		});
	},
	
	_getBrowserDimensions : function() {
		this.browserWidth = document.viewport.getWidth();
		this.browserHeight = document.viewport.getHeight();
		if (typeof XMLHttpRequest == "undefined") //IF browser is IE6 need to shrink window a bit this a hack and could be done better
		{
			this.browserWidth = document.viewport.getWidth() - 33;			
		}		
	},
			
	
	
	_setFillContent : function() //creates an i-frame of given url content in lightwindow
	{
		if($('bb_lightwindow_content')){
			$('bb_lightwindow_content').innerHTML += '<iframe id="bb_lightwindow_iframe" src="'+this.contentURL+'" width="100%" height="100%">';		
		}
		this.contentLoaded = true;
		this._loadingComplete();
	},
	
	_requestContentHTML : function()
	{
		var ajaxRequest = new Ajax.Request(this.contentURL, {
			method: 'get',
			parameters: '' ,
			onComplete:function(response){
				if($('bb_lightwindow_ajax'))
				{ 
					this.contentLoaded = true;
					$('bb_lightwindow_ajax').innerHTML += response.responseText;
					this._loadingComplete();
				}
			}.bind(this)
		});	
	},
	
	_refreshRequest: function()
	{
		var i = 0;
		for(i = 0; i < this.ajexURL.length;i++)
		{
			parent.myAjaxLoader.makeRequestAndSub(this.ajexURL[i] ,this.ajexDIV[i]);
		}
	}
};

Event.observe(window, 'load', bbLightwindowInit, false); 
var myBBlw = null;
function bbLightwindowInit() {
	myBBlw = new bb_lightwindow();
}
