// JavaScript Document
var holderheight;
var holderwidth;
var hide;
	holderheight = 243;
	holderwidth = 411;
	
	

function createdisplay(holderwidthf,holderheightf,ajax,url,autohide){
	opacity = 0.8;

if(holderwidthf == undefined){
	holderwidth=800
}else{
	holderwidth=holderwidthf;
}
	
if(holderheightf == undefined){
	holderheight=600
}else{
	holderheight=holderheightf;
}

if(autohide == undefined){
	autohide=true;
}

if(ajax == undefined){
	ajax=0
}

if(!$('dOverlay')){ // create the divs
	overlay = new Element('div', {'id': 'dOverlay'}).injectInside(document.body);
	overlay.setStyles({'top': window.getScrollTop(), 'height': window.getScrollHeight(), 'opacity':'0'});	
	
	screenwidth = $('dOverlay').clientWidth;
	screenheight = window.getHeight();
	
	holder = new Element('div', {'id': 'dHolder'}).injectInside(document.body);
	
	$('dHolder').setStyles({
	//								 'height':0, 
	//								 'width':0
									 'height':holderheight, 
									 'width':holderwidth
									 });
	$('dHolder').setStyles({
									 'top':((screenheight-holder.getStyle('height').toInt())/2+'px'), 
									 'left':((screenwidth-holder.getStyle('width').toInt())/2+'px'),
									 'overflow':'hidden',
									 'opacity':0
									 });
}else{
	
	resizeoverlay()
}// end create the divs



$('dOverlay').addEvent('click', function(e) { //finds 'id=dOverlay' and add an onclick event
		e = new Event(e).stop();
		if(window.styleActive == 0){	
			if(this.style.opacity >= opacity){
				clearTimeout(hide);
														
					holderfx.start({
			//				'height':0, 
			//				'width':0,
			//				'top':((screenheight)/2+'px'),
			//				'left':((screenwidth)/2+'px')
							'opacity':0
					}).chain(function(){
							var d = detectMacXFF(); //mac firefox fix
							if (d) {
								$('dOverlay').style.visibility = "hidden";
							}else{
								overlayfx.start(opacity,0);
							}
					});
				}
		}//end of if
});
		
var overlayfx = new Fx.Style('dOverlay', 'opacity', {
							 duration:200, 
							 wait:true, 
							 onStart: function()
							 	{	
									//alert('1');
									window.styleActive = 1;
									$('dOverlay').removeEvent('click'), function(e)
									{ e = new Event(e).stop(); }
							 	}  
							 });

var holderfx = new Fx.Styles('dHolder', {duration:200, wait:true, onComplete: function(){ window.styleActive = 0; }} );


overlayfx.start(0,opacity).chain(function(){
		// executes immediately after completion of above effect
		holderfx.start({
						'opacity':1
		});
});



	el = 'dHolder';
	
if(ajax > 0){
	$(el).addClass('loading');
	loadajax(url,el);
}


overlayfx.start ({
			'margin-left': 5,
			'background-color': '#666',
			color: '#ff8'
});

if(autohide == true){
hide=setTimeout('autohide()',4000);
}
}

function loadajax(url,ele){
	
	loadpage = new Ajax(url, {
									method: 'get',
									update: $(ele),
									evalScripts: true,  //for the scroll bar in the new page loaded
									onComplete: function(){
										$(ele).removeClass('loading');
									}											
	});
	
	loadpage.request();
}

function findshow(){
			$ES('.show').addEvent('click', function(e) { //find all 'class=show' and add an onclick event
					e = new Event(e).stop();
					holderheight = '700';
					holderwidth = '550';
					createdisplay(holderwidth,holderheight,1,this.href,false);
				});
	}

function resizeoverlay(){
if($('dOverlay')){ // create the divs
	$('dOverlay').setStyles({'top': 0, 'height': window.getScrollHeight()});	
	var holderfx = new Fx.Styles('dHolder', {duration:500, wait:true});
	
	if( typeof( window.innerHeight ) == 'number' ) {
		newwidth = ($('dOverlay').getStyle('width').toInt()-holderwidth)/2+'px'
	} else if( document.documentElement &&( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		newwidth = (document.documentElement.clientWidth-holderwidth)/2+'px'
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		newwidth = (document.body.clientWidth-holderwidth)/2+'px'
	}
	if($('dHolder')){
	if($('dHolder').getStyle('width').toInt() > 0){
	holderfx.start({
						'height': holderheight, 
						'width': holderwidth,
						'top':((window.getHeight()-holderheight)/2+'px'),
						'left':(newwidth)
	});
	}
	}
}
}


function closedisplay(){
if($('dOverlay').style.opacity >= opacity){
var overlayfx = new Fx.Style('dOverlay', 'opacity', {
							 duration:200, 
							 wait:true, 
							 onStart: function()
							 	{	
									//alert('2');
									window.styleActive = 1;
									$('dOverlay').removeEvent('click'), function(e)
									{ e = new Event(e).stop(); }
							 	}  
							 });
var holderfx = new Fx.Styles('dHolder', {duration:500, wait:true, onComplete: function(){ window.styleActive = 0; }} );

		if(window.styleActive == 0){
		holderfx.start({
//						'height':0, 
//						'width':0,
//						'top':((screenheight)/2+'px'),
//						'left':((screenwidth)/2+'px'),
						'opacity':0
		}).chain(function(){
			var d = detectMacXFF(); //mac firefox fix
			if (d) {
				$('dOverlay').style.visibility = "hidden";
			}else{
				overlayfx.start(opacity,0);
			}
		});
		}//end of if
}
}

function autohide(){
	
if($('dOverlay').style.opacity >= opacity){
var overlayfx = new Fx.Style('dOverlay', 'opacity', {
							 duration:200, 
							 wait:true,
							 onStart: function()
							 	{	
									//alert('3');
									window.styleActive = 1;
									$('dOverlay').removeEvent('click'), function(e)
									{ e = new Event(e).stop(); }
							 	}  
							 });

var holderfx = new Fx.Styles('dHolder', {duration:200, wait:true, onComplete: function(){ window.styleActive = 0; }} );

				var d = detectMacXFF();
				if (d) {
					var overlayfx = new Fx.Styles('dOverlay', {
									duration:200, 
									wait:true,
									onComplete:function(){
									$('dOverlay').style.visibility = "hidden";
									}
									});
					
					overlayfx.start({'background-color':'#ffffff'});
						$('dHolder').style.visibility = "hidden";
						
				}else{
						holderfx.start({// hack for big active
								'opacity':0
							}).chain(function(){
								overlayfx.start(opacity,0);
							});
			}
}
}


function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

window.addEvent('domready', findshow);
window.onresize = resizeoverlay;
