/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Script featured on Dynamic Drive code library Jan 24th, 06':
	http://www.dynamicdrive.com

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
	Table of Contents
	-----------------
	Configuration
	
	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- hideLightbox()
	- initLightbox()
	- addLoadEvent()
	
	Function Calls
	- addLoadEvent(initLightbox)

*/



//
// Configuration
//

// If you would like to use a custom loading image or close button reference them in the next two lines.
var imgOverlay = '/img/blackdot.gif';
var imgOverlayContent = '/img/overlaycontent.png';
var imgFechar = '/img/fechar.png';	

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// hideLightbox()
//
function hideLightbox(strField)
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	if( strField != "" || strField != false) {
//		alert(document.getElementById(strField).type);
		if( document.getElementById(strField).type == "select-one" )
			document.getElementById(strField).focus();
		else
			document.getElementById(strField).select();
	}

}

//
// hideLightbox()
//
function hideLightconfirm(strForm,strWindow,strConfirm)
{

	objForm = document.getElementById(strForm);
	
	
//	alert(strForm);
//	alert(objForm.id);
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	
	if( strConfirm == "true" ) {

		if( strForm == "ajaxpost" ){
			ajaxpost(objForm.action,strWindow,"POST",objForm.id);
		} else if (strForm == "close" ){
			window.close();
		} else {
			objWindow = eval(strWindow);
			objWindow.setForms(objForm.id);
			objWindow.load('ajax',objForm.action,'','POST');
		}
	} else {
		return false;
	}	
}


// lightbox()
// função que retorna uma mensagem totalmente customizável
// Informando: titulo, mensagem e campo pra retornar o foco
function lightboxalert(strTitulo,strMsg, strCommand)
{

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objBody = document.getElementsByTagName("body").item(0);

	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '9990';
 	objOverlay.style.width = '100%';
//	objOverlay.style.background = 'url('+imgOverlay+')';
	objOverlay.style.display = 'block';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objBody.insertBefore(objOverlay, objBody.firstChild);

	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 200) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 300) / 2);

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.position	= 'absolute';
	objLightbox.style.zIndex 	= '9991';	
	objLightbox.style.border	= '2px solid #163F51';
	objLightbox.style.width 	= '300px';
	objLightbox.style.padding 	= '2px';
	objLightbox.innerHTML 		= "<div align='center' class='titulo'>"+strTitulo+"</div><br><div align='justify' class='texto'>"+strMsg+"</div><br><br><center><input id='ok' value='    OK    ' type='button' style='width:85px;' class='botao' onkeydown=\"javascript:"+strCommand+";hideLightbox(false);\" onclick=\"javascript:"+strCommand+";hideLightbox(false);\"><script>document.getElementById('ok').focus();document.getElementById('ok').select();</script></center>";
	objLightbox.style.top 		= (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left 		= (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objLightbox.style.display 	= 'block';
//	objLightbox.style.background= 'url('+imgOverlayContent+')';
	objLightbox.style.background = "#F6F6F6";

	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
}


// lightbox()
// função que retorna uma mensagem totalmente customizável
// Informando: titulo, mensagem e campo pra retornar o foco
function lightbox(strTitulo,strMsg, strField)
{

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objBody = document.getElementsByTagName("body").item(0);

	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '9992';
 	objOverlay.style.width = '100%';
//	objOverlay.style.background = 'url('+imgOverlay+')';
	objOverlay.style.display = 'block';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objBody.insertBefore(objOverlay, objBody.firstChild);

	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 200) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 300) / 2);

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.position	= 'absolute';
	objLightbox.style.zIndex 	= '9993';	
	objLightbox.style.border	= '2px solid #163F51';
	objLightbox.style.width 	= '300px';
	objLightbox.style.padding 	= '2px';
	objLightbox.innerHTML 		= "<div align='center' class='titulo'>"+strTitulo+"</div><br><div align='justify' class='texto'>"+strMsg+"</div><br><br><center><input value='    OK    ' type='button' class='botao' onkeydown=hideLightbox('"+strField+"') onClick=hideLightbox('"+strField+"')></center>";
	objLightbox.style.top 		= (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left 		= (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objLightbox.style.display 	= 'block';
//	objLightbox.style.background= 'url('+imgOverlayContent+')';
	objLightbox.style.background = "#F6F6F6";

	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
}

// lightconfirm()
// função que retorna uma mensagem de confirmação totalmente customizável
// Informando: titulo, mensagem
function lightconfirm(strForm,strWindow,strTitulo,strMsg)
{

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '9994';
 	objOverlay.style.width = '100%';
//	objOverlay.style.background = 'url('+imgOverlay+')';
	objOverlay.style.display = 'block';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 200) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 300) / 2);
		
	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '9995';	
	objLightbox.style.border = '2px solid #163F51';
	objLightbox.style.width = '300px';
	objLightbox.style.padding = '2px';
	objLightbox.innerHTML = "<div align='center' class='titulo'>"+strTitulo+"</div><br><div align='justify' class='texto'>"+strMsg+"</div><br><br><center><input value='    OK    ' type='button' class='botao' style='width:85px;' onClick=hideLightconfirm('"+strForm+"','"+strWindow+"','true')>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input value='    Cancelar    ' type='button' class='botao' style='width:85px;' onkeydown=hideLightconfirm('"+strForm+"','"+strWindow+"','false') onClick=hideLightconfirm('"+strForm+"','"+strWindow+"','false')></center>";
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objLightbox.style.display = 'block';
//	objLightbox.style.background = 'url('+imgOverlayContent+')';
	objLightbox.style.background = "#F6F6F6";
	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
}


// lightbox()
// função que retorna uma mensagem totalmente customizável
// Informando: titulo, mensagem e campo pra retornar o foco
function lightboxContent(strId,strWidth,strTitulo,strContent)
{

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objBody = document.getElementsByTagName("body").item(0);

	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id',strId+'_overlay');
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '9996';
 	objOverlay.style.width = '100%';
//	objOverlay.style.background = 'url('+imgOverlay+')';
	objOverlay.style.display = 'block';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objBody.insertBefore(objOverlay, objBody.firstChild);

	var lightboxTop = 35;
	var lightboxLeft = ((arrayPageSize[0] - 725) / 2);

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id',strId+'_lightbox');
	objLightbox.style.position	= 'absolute';
	objLightbox.style.zIndex 	= '9997';	
	objLightbox.style.border	= '2px solid #163F51';
	objLightbox.style.width 	= strWidth;
	objLightbox.style.padding 	= '2px';
	objLightbox.innerHTML 		= "<div style=\"float:right; padding-top:1px;\"><a href=\"javascript:hideLightboxContent('"+strId+"')\"><img src=\""+imgFechar+"\" alt=\"Fechar Janela\" border=\"0\"></a></div><div align=\"center\" id=\"barra_azul\" onmousedown=\"javascript:setupdrag('"+strId+"')\">"+strTitulo+"</div><br>"+strContent+"</div>";
	objLightbox.style.top 		= (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left 		= (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objLightbox.style.display 	= 'block';
//	objLightbox.style.background= 'url('+imgOverlayContent+')';
	objLightbox.style.background = "#F6F6F6";

	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
}
function hideLightboxContent(strId)
{
	// get objects
	objOverlay = document.getElementById(strId+'_overlay');
	objLightbox = document.getElementById(strId+'_lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

}