// Configuration
//

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = '../genericJS/overlay/loading.gif';		
var closeButton = '../genericJS/overlay/close.gif';
var logoImage = '../genericJS/overlay/tfs-small.gif';
var overImage = '../genericJS/overlay/overlay.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;
	var bodyObj =document.body;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = bodyObj.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (bodyObj.scrollHeight > bodyObj.offsetHeight){ // all but Explorer Mac
		xScroll = bodyObj.scrollWidth;
		yScroll = bodyObj.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = bodyObj.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;
}


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}






//
// showLightbox()
//
function showLightbox()
{

	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLogoImage = document.getElementById('logoImage');
	var objCaption = document.getElementById('overCaption');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - 126) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}
		if (objLogoImage && objLoadingImage ) {
		objLogoImage.style.top = ((parseInt(objLoadingImage.style.top)  - parseInt(objLoadingImage.height) - 50) + 'px');
		objLogoImage.style.left = (((arrayPageSize[0] - 20 - 100) / 2) + 'px');
		//objLogoImage.style.display = 'block';
	}

	if (objCaption && objLoadingImage) {
		objCaption.style.color="white";
		objCaption.style.fontSize="20";
		objCaption.style.top = ((parseInt(objLoadingImage.style.top)   + parseInt(objLoadingImage.height) + 40 ) + 'px');
	    objCaption.style.left = (((arrayPageSize[0] - 20 -300) / 2) + 'px');
		objCaption.style.display = 'block';
	}
	
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';



	// 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";
        }*/
	
	 document.body.scroll="no"	;
}

//
// hideLightbox()
//
function hideLightbox(doc)
{
	// get objects
	objOverlay = document.getElementById('overlay');
	// 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";
	}*/
  document.body.scroll="auto"	;
	
}




//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{

	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.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '5';
	objOverlay.style.left = '5';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	// preload and create loader image
	var imgPreloader = new Image();
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
        objLoadingImage.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImage);
		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs
		return false;
	}
	imgPreloader.src = loadingImage;
	/*var imgPrelogo = new Image();
	imgPrelogo.onload=function(){
		var objLogoImage = document.createElement("img");
		objLogoImage.src = logoImage;
		objLogoImage.setAttribute('id','logoImage');
		objLogoImage.style.position = 'absolute';
		objLogoImage.style.zIndex = '150';
        objLogoImage.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLogoImage);
		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs
		return false;
	}
	imgPrelogo.src = logoImage;
	*/
	var objOverCaption = document.createElement("div");
	objOverCaption.setAttribute('id','overCaption');
	objOverCaption.style.display = 'none';
	objOverCaption.style.position = 'absolute';
	objOverCaption.style.zIndex = '151';
 //objOverCaption.style.width = '100%';
	objOverCaption.style.align = 'center';
	objOverCaption.innerHTML="Espere un momento, procesando su solicitud...";
	objOverlay.appendChild(objOverCaption);
}



function initWaitbox(doc)
{
	
	var objOverWait =  document.getElementById(doc + 'overWait');
	if(!objOverWait)
	var objOverWait = document.createElement("div");
	//objOverWait.style.background="#778899";
	objOverWait.style.color="#D00D17";
	objOverWait.setAttribute('id',doc + 'overWait');
	objOverWait.onclick = function () {hideWaitbox(doc); return false;}
	//objOverWait.style.visibility = 'hidden';
	objOverWait.style.display = 'none';
	objOverWait.style.position = 'relative';
	objOverWait.style.top = '20';
	objOverWait.style.left = '40';
	//objOverWait.style.verticalAlign = 'bottom';
	objOverWait.innerHTML="Un momento...";
	//objOverWait.style.zIndex = '90';
 	//objOverWait.style.width = '100';
	doc.insertBefore(objOverWait, doc.lastChild);
}



function showWaitbox(doc)
{

	// prep objects
	var objWait = document.getElementById(doc + 'overWait');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
		// set height of Overlay to take up whole page and show
	//objWait.style.height = '20px';
	objWait.style.display = 'block';
	//objWait.style.visibility = "visible";
	
	// Hide select boxes as they will 'peek' through the image in IE
		selects = doc.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }
		inputs = doc.getElementsByTagName("input");
        for (i = 0; i != inputs.length; i++) {
                //inputs[i].disabled = true;
        }
	// document.body.scroll="no"	;
}

//
// hideLightbox()
//
function hideWaitbox(doc)
{
	// get objects
	objWait = document.getElementById(doc + 'overWait');
	// hide lightbox and overlay
	//objOverWait.style.visibility = 'hidden';
	objWait.style.display = 'none';
	// make select boxes visible
	selects = doc.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
		
	}
	inputs = doc.getElementsByTagName("input");
        for (i = 0; i != inputs.length; i++) {
                inputs[i].disabled = null;
        }
}

function proteccionCapa(capa){
	if(findObj(capa)){
	initWaitbox(findObj(capa));
	showWaitbox(findObj(capa));
	}
}