
//requires Dean Edwards addEvent.js

//from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var LightBox={
  maskingDiv:null,
  loadingDiv:null,
  windows:[], //this is to keep track when a "modal" pops a modal of it's own
  windowIdx:-1,
  closeCurrentWindow: function(ele){ 
      //force window to close 		
	  if(LightBox.windowIdx>-1){
	  	document.body.removeChild(LightBox.windows[LightBox.windowIdx]);	  		
		LightBox.maskingDiv.style.display='none';
		LightBox.windows.pop();
		LightBox.windowIdx--;
		if(LightBox.windowIdx==-1){
			LightBox.maskingDiv.style.display='none';
		}
			//we need to re-enable the next modal		
	  }
	  if(LightBox.loadingDiv){
			LightBox.loadingDiv.style.display="none";
	  }
  },
  closeCurrentWindowListener: function(e){  
	   
	  	if(!e) e=window.event;
	  	//check to see if the close button was clicked or escape was pressed	  	
	  	if(e.type=='click' ){
	  		LightBox.closeCurrentWindow();
	  	}else if(e.keyCode==27){
	  		LightBox.closeCurrentWindow();
	  	}
  },
  reposition:function(){	  
		var viewportwidth  = f_clientWidth();
		var viewportheight = f_clientHeight();
		for (var i=0;i<LightBox.windows.length;i++){
			var top=Math.floor((viewportheight-parseInt(LightBox.windows[i].style.height))/2);
			top+=f_scrollTop();
			var left=Math.floor((viewportwidth-parseInt(LightBox.windows[i].style.width))/2);		
			LightBox.windows[i].style.top=((top>0)?top:0)+"px";
			LightBox.windows[i].style.left=((left>0)?left:0)+"px";
		}
  },
  openWindow: function(props){
  		if(!this.maskingDiv){
  			this.maskingDiv=document.createElement("DIV");
			this.maskingDiv.className="lw_mask";
			if(document.all)
				this.maskingDiv.style.height=document.body.offsetHeight+"px";
			else
				this.maskingDiv.style.height=document.height+"px";
  			document.body.appendChild(this.maskingDiv);
  			this.maskingDiv.id="light_box_window";  	  	
  		    addEvent(window,"keyup",LightBox.closeCurrentWindowListener);
  		    addEvent(window,"resize",LightBox.reposition);
  			addEvent(this.maskingDiv,"click",LightBox.closeCurrentWindowListener);
  			//addEvent(this.maskingDiv,"keypress",LightBox.closeCurrentWindowListener);

  		}else{
  			this.maskingDiv.style.display='block';
  		}
  		
 		//var viewportwidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		//var viewportheight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		var viewportwidth  = f_clientWidth();
		var viewportheight = f_clientHeight();
		
  		//show the popup
  		var win=document.createElement("div"); //window container
  		win.className=props.className;  		
  		
  		var contentArea=document.createElement(((props.window.iframe)?"iframe":"div"));
  		
  		//do we have a titlebar?
  		if(props.titlebar){
  			var title=document.createElement("div");
  			title.style.width=props.width;
  			title.style.height=props.titlebar.height;  		
  			title.className=props.titlebar.className
  			var tt=document.createElement("div");
  				tt.className=(props.titlebar.title.className)?props.titlebar.title.className:'lw_titlebar_text';
  				tt.innerHTML=props.titlebar.title.content;
  				title.appendChild(tt);

  			var closeLink=document.createElement("a");
  	  			closeLink.className="lw_close_btn";
  	  			closeLink.onclick=LightBox.closeCurrentWindowListener;

  			var closeIm=document.createElement("img");
  				closeIm.src=props.titlebar.button.src;  				
  				closeIm.className=(props.titlebar.button.className)?props.titlebar.button.className:"lw_close_btn_img";
  				closeIm.alt="Close";
  				closeIm.title="Close";
  			
  			var closeButtonText=null;
  			var pre=false;
  			var post=false;
  			if(props.titlebar.button.text){
  				closeButtonText=document.createElement("span");
  				
  				closeButtonText.appendChild(document.createTextNode(props.titlebar.button.text.value));
  				closeButtonText.className=props.titlebar.button.text.className;
  				closeIm.className=(props.titlebar.button.text.className)?props.titlebar.button.text.className:"lw_close_btn_text";
  				pre=(props.titlebar.button.text.location=="before");
  				post=!pre;
  			}
  			
  			if(pre) closeLink.appendChild(closeButtonText);
  			closeLink.appendChild(closeIm);
  			if(post) closeLink.appendChild(closeButtonText);
  			
  			title.appendChild(closeLink);
  			title.appendChild(tt);
  			
  				
  		  	win.appendChild(title);
  		}
  		  		
  		
  		if(props.window.iframe){
  			contentArea.className=props.window.className;
  			contentArea.frameBorder=0;
  		}
  		
  		//the content area of the pop up
  		contentArea.style.width=props.width;
  		contentArea.style.height=props.window.height;  		
  		contentArea.className=props.window.className;
  		contentArea.id="lightwindow_fr";
  		  		
  		var load=false;
  		if(props.window.loading){  			
  			if(!this.loadingDiv){
  				this.loadingDiv=document.createElement("div");  		
  				document.body.appendChild(this.loadingDiv);
  				this.loadingDiv.style.position='absolute';
  				this.loadingDiv.style.display='none';
  				this.loadingDiv.id="lw_loading";  				
  				//addEvent(document,"keypress",LightBox.closeCurrentWindowListener);
  	  		}
  			
  			this.loadingDiv.className=(props.window.loading.className)?props.window.loading.className:"";
  			this.loadingDiv.style.width=(props.window.loading.width)?props.window.loading.width:"300px";
  			this.loadingDiv.style.height=(props.window.loading.height)?props.window.loading.height:"300px";
  			this.loadingDiv.innerHTML=props.window.loading.content;
  			load=true;
  		}
  		//append the 
  		win.appendChild(contentArea);
  		
  	
  		
  		var l=Math.floor((viewportwidth-parseInt(props.width))/2);
  		win.style.left=((l>0)?l:0)+"px";
  		
  		var t=0;
  		var h=0;
  		
  		if(props.titlebar){
  			t=Math.floor((viewportheight-parseInt(props.window.height)-parseInt(props.titlebar.height))/2);
  			h=parseInt(props.titlebar.height)+parseInt(props.window.height);
  		}else{
  			t=Math.floor((viewportheight-parseInt(props.window.height))/2);
  			h=parseInt(props.window.height)
  		}
  		
  		t=(t>0)?t:0
  		t+=f_scrollTop();//always add the scroll top so we are working with the top of teh screen not the page  		
  		win.style.top=t+"px";  		  	
  		win.style.height=h+"px";
  		win.style.width=props.width;
  		document.body.appendChild(win);
  		
  		if(load){
  			t=Math.floor((viewportheight-parseInt(props.window.loading.height))/2);
  	  		t=(t>0)?t:0;
   	    	t+=f_scrollTop();

  			h=parseInt(this.loadingDiv.style.height);
  	  		l=Math.floor((viewportwidth-parseInt(props.window.loading.width))/2);  	  		

  			this.loadingDiv.style.top=t+"px"; 
  			this.loadingDiv.style.left=((l>0)?l:0)+"px";
  			this.loadingDiv.style.display='block';
  			win.style.visibility='hidden';
  			contentArea.style.visibility='hidden';
  		}
  		//if there is a modal open we need to disable it 1st!
  		
  		this.windows.push(win);
  		this.windowIdx++;  		
  		
  		//load the content
  		if(props.window.iframe){
  		  contentArea.src=props.window.url;
  		}else{
  			if(typeof(props.window.content)=="String"){
  				contentArea.innerHTML=props.window.content;
  			}else{
  				contentArea.innerHTML=props.window.content();
  			}
  		}

		this.maskingDiv.focus();
  },
  loaded: function(flag){
	  if (flag) this.reposition();
	  this.loadingDiv.style.display="none";	  
	  this.windows[0].style.visibility='visible';
	  for(var i=0;i<this.windows[0].childNodes.length;i++){
		  if(this.windows[0].childNodes[i].id=="lightwindow_fr"){
			  this.windows[0].childNodes[i].style.visibility='visible';
		  }
	  }	  
  }
};

function popWin(pageUrl,altWidth,altHeight){

	var HTML='';	

	var h='576px';
	var w='662px';
	if(altHeight) h=altHeight;
	if(altWidth) w=altWidth;


	var props={
	        'width':w,
	        'className':'lw_window',
	        titlebar:{
				'height':"32px",
				'className':'lw_titlebar',
				'button':{
					'src':"/assets/media/close.jpg",
					'className':'lw_close_btn_img',
					'text':{
						'value':'Close',
						'location':'before',
						'className':'lw_close_btn_text'
					}					
				},
				'title':{
					'className':'lw_titlebar_text',
					'content':HTML
				}
			},
	        window:{
	        	'height':h,
	        	'className':'lw_content',
	        	'iframe':true,
	        	'url':pageUrl,
	        	'content':null
	        }        
	     }
		 LightBox.openWindow(props);	

}

