function SelectControl(tag, ident){

    var elements = document.getElementsByTagName(tag);

    for(i = 0;i < elements.length; i++)
    {
        var name = elements[i].getAttribute("id");

        if ( name != null && name.indexOf(ident) != -1 )
        {
            return elements[i];
        }
    }
}

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;
}


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;
}


function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}


function listenKey () {	document.onkeypress = getKey; }
	
function showPreloadLightbox( ){
    var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objOverlay = SelectControl('div','BgLightbox');
	var objCarregando = SelectControl('div', 'CarregandoLightBox');
    var objLightbox = SelectControl('div', 'ContentLightBox');
    
	objOverlay.style.width = (arrayPageSize[0] + 'px');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	objOverlay.style.display = "block";
	objCarregando.style.display = "block";
    objLightbox.style.display = "block";
    objLightbox.style.width = "1px";
    objLightbox.style.height = "1px";
    objLightbox.style.overflow = "hidden";
   
	var lightboxTop = 0 + ((arrayPageSize[3] - 35 - objCarregando.offsetHeight) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - objCarregando.offsetWidth) / 2);
		
	objCarregando.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objCarregando.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";		

	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "hidden";
    }	

}
	
function showContentLightbox(width, height )
{
   
    var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var objOverlay = SelectControl('div', 'BgLightbox');
	var objLightbox = SelectControl('div', 'ContentLightBox');
	var objConteudoModalCentral = SelectControl('div', 'Modal-Aba-Cen');
    var objCarregando = SelectControl('div', 'CarregandoLightBox');
		
	// set height of Overlay to take up whole page and show
	objOverlay.style.width = (arrayPageSize[0] + 'px');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = "block";
	objLightbox.style.display = "block";
	objCarregando.style.display = "none";
       
    // Definindo o Tamanho do ObjLightbox baseado no tamanho passado como parāmetro, somando 26px das bordas
    objLightbox.style.width = (width + 26) + "px";	
	objLightbox.style.height = (height + 26)   + "px";		
	
	objConteudoModalCentral.style.width = (width + 26) + "px";	
	objConteudoModalCentral.style.width = (width + 26) + "px";	

	
	// Definindo a altura das bordas (filhos) do objConteudoModalCentral baseado no tamanho dado 
	// ao objConteudoModal que é definido no css
	for(var i=0; i < objConteudoModalCentral.childNodes.length; i++)
	{   
	    if(objConteudoModalCentral.childNodes[i].nodeName == "SPAN")
	    {
	        objConteudoModalCentral.childNodes[i].style.height = (height) + "px";
	    }
	}	  
    
	// center lightbox and make sure that the top and left values are not negative
	// and the image placed outside the viewport
	var lightboxTop = 0 + ((arrayPageSize[3] - 35 - objLightbox.offsetHeight) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - objLightbox.offsetWidth) / 2);
	
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";		
			
	// A small pause between the image loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		pause(250);
	} 
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "hidden";
    }
    
    //ShowSelectLightBox(objConteudoModal);
	// After image is loaded, update the overlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	// Check for 'x' keypress
	listenKey();

	return false;
}

// Libera todos os SELECT que estiver dentro do LightBox
function ShowSelectLightBox(obj){
    for(var i=0; i<obj.childNodes.length; i++)
    {
        if(obj.childNodes[i].nodeName == "SELECT")
        {
            obj.childNodes[i].style.visibility = "visible";
        }
        
        if(obj.childNodes[i].length != 0)
        {
            ShowSelectLightBox(obj.childNodes[i]);
        }
        
    }
}

function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('BgLightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	
	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}