// Mis modificaciones para ser utilizado a través de un IFRAME con están marcadas con //**

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImagen = 'img/loading.gif';		
var closeButtonn = 'img/close.gif';		

//
// getPaginaScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPaginaScroll(){

	var yyScroll;

	if (self.pageYOffset) {
		yyScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yyScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yyScroll = document.body.scrollTop;
	}

	arrPageScroll = new Array('',yyScroll) 
	return arrPageScroll;
}

//
// getPaginaSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPaginaSize(){
	
	var xxScroll, yyScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xxScroll = document.body.scrollWidth;
		yyScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xxScroll = document.body.scrollWidth;
		yyScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xxScroll = document.body.offsetWidth;
		yyScroll = 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(yyScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yyScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xxScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xxScroll;
	}


	arrPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrPageSize;
}


//
// pausear(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 pausear(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKeyy(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKeyy(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}


//
// listenKeyy()
//
function listenKeyy () {	document.onkeypress = getKeyy; }
	

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLinkk, objCapp)
{
	// prep objects
	var objOverlayy = document.getElementById('overlayy');
	var objLightboxx = document.getElementById('lightboxx');
	var objKaptionn = document.getElementById('lightboxxCaption');
	var objImagen = document.getElementById('lightboxxImage');
	var objLoadingImagen = document.getElementById('loadingImagen');
	var objLightboxxDetails = document.getElementById('lightboxxDetails');
	var arrPageSize = getPaginaSize();
	var arrPageScroll = getPaginaScroll();

	// center loadingImagen if it exists
	if (objLoadingImagen) {
		objLoadingImagen.style.top = (arrPageScroll[1] + ((arrPageSize[3] - 35 - objLoadingImagen.height) / 2) + 'px');
		objLoadingImagen.style.left = (((arrPageSize[0] - 20 - objLoadingImagen.width) / 2) + 'px');
		objLoadingImagen.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlayy.style.height = (arrPageSize[1] + 'px');
	objOverlayy.style.display = 'block';

	// preload image
	imgPreloadd = new Image();

	imgPreloadd.onload=function(){

//**		objImagen.src = objLinkk.href;
		objImagen.src = objLinkk;

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrPageScroll[1] + ((arrPageSize[3] - 35 - imgPreloadd.height) / 2);
		var lightboxLeft = ((arrPageSize[0] - 20 - imgPreloadd.width) / 2);
		
		objLightboxx.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightboxx.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		objLightboxxDetails.style.width = imgPreloadd.width + 'px';


//**		if(objLinkk.getAttribute('title')){
			objKaptionn.style.display = 'block';
			objKaptionn.style.width = imgPreloadd.width + 'px';

//**
			if (objCapp != '') {
				objKaptionn.innerHTML = objCapp;
			} else {
				objKaptionn.innerHTML = 'Ampliaci&oacute;n';
			}

//**			objKaptionn.innerHTML = objLinkk.getAttribute('title');

//**		} else {
//**			objKaptionn.style.display = 'none';
//**		}
		
		// A small pausear 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){
			pausear(250);
		} 

		if (objLoadingImagen) {	objLoadingImagen.style.display = 'none'; }

		// 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";
        }

	
		objLightboxx.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrPageSize = getPaginaSize();
		objOverlayy.style.height = (arrPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKeyy();

		return false;
	}

//**	imgPreloadd.src = objLinkk.href;
	imgPreloadd.src = objLinkk;
	
}





//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlayy = document.getElementById('overlayy');
	objLightboxx = document.getElementById('lightboxx');

	// hide lightbox and overlay
	objOverlayy.style.display = 'none';
	objLightboxx.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 = '';
}




//
// 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()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchorss = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchorss.length; i++){
		var anchorr = anchorss[i];

		if (anchorr.getAttribute("href") && (anchorr.getAttribute("rel") == "lightboxx")){
			anchorr.onclick = function () {showLightbox(this); return false;}
		}
	}

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImagen" /></a>
	//	</div>
	// <div id="lightbox">
	//		<a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
	//			<img id="closeButtonn" />		
	//			<img id="lightboxImage" />
	//		</a>
	//		<div id="lightboxDetails">
	//			<div id="lightboxCaption"></div>
	//			<div id="keyboardMsg"></div>
	//		</div>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlayy = document.createElement("div");
	objOverlayy.setAttribute('id','overlayy');
	objOverlayy.onclick = function () {hideLightbox(); return false;}
	objOverlayy.style.display = 'none';
	objOverlayy.style.position = 'absolute';
	objOverlayy.style.top = '0';
	objOverlayy.style.left = '0';
	objOverlayy.style.zIndex = '90';
 	objOverlayy.style.width = '100%';
	objBody.insertBefore(objOverlayy, objBody.firstChild);
	
	var arrPageSize = getPaginaSize();
	var arrPageScroll = getPaginaScroll();

	// preload and create loader image
	var imgPreloadder = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloadder.onload=function(){

		var objLoadingImagenLink = document.createElement("a");
		objLoadingImagenLink.setAttribute('href','#');
		objLoadingImagenLink.onclick = function () {hideLightbox(); return false;}
		objOverlayy.appendChild(objLoadingImagenLink);
		
		var objLoadingImagen = document.createElement("img");
		objLoadingImagen.src = loadingImagen;
		objLoadingImagen.setAttribute('id','loadingImagen');
		objLoadingImagen.style.position = 'absolute';
		objLoadingImagen.style.zIndex = '150';
		objLoadingImagenLink.appendChild(objLoadingImagen);

		imgPreloadder.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloadder.src = loadingImagen;

	// create lightbox div, same note about styles as above
	var objLightboxx = document.createElement("div");
	objLightboxx.setAttribute('id','lightboxx');
	objLightboxx.style.display = 'none';
	objLightboxx.style.position = 'absolute';
	objLightboxx.style.zIndex = '100';	
	objBody.insertBefore(objLightboxx, objOverlayy.nextSibling);
	
	// create link
	var objLinkk = document.createElement("a");
	objLinkk.setAttribute('href','#');
	objLinkk.setAttribute('title','Clic para cerrar');
	objLinkk.onclick = function () {hideLightbox(); return false;}
	objLightboxx.appendChild(objLinkk);

	// preload and create close button image
	var imgPreloaddCloseButton = new Image();

	// if close button image found, 
	imgPreloaddCloseButton.onload=function(){

		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButtonn;
		objCloseButton.setAttribute('id','closeButtonn');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLinkk.appendChild(objCloseButton);

		return false;
	}

	imgPreloaddCloseButton.src = closeButtonn;

	// create image
	var objImagen = document.createElement("img");
	objImagen.setAttribute('id','lightboxxImage');
	objLinkk.appendChild(objImagen);
	
	// create details div, a container for the caption and keyboard message
	var objLightboxxDetails = document.createElement("div");
	objLightboxxDetails.setAttribute('id','lightboxxDetails');
	objLightboxx.appendChild(objLightboxxDetails);

	// create caption
	var objKaptionn = document.createElement("div");
	objKaptionn.setAttribute('id','lightboxxCaption');
	objKaptionn.style.display = 'none';
	objLightboxxDetails.appendChild(objKaptionn);

	// create keyboard message
	var objKeyboardMsgg = document.createElement("div");
	objKeyboardMsgg.setAttribute('id','keyboardMsgg');
	objKeyboardMsgg.innerHTML = 'presione <a href="#" onclick="hideLightbox(); return false;"><kbd>x</kbd></a> para cerrar';
	objLightboxxDetails.appendChild(objKeyboardMsgg);

}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initLightbox);	// run initLightbox onLoad
