//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
window.getPageScroll = function(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (parent.document.documentElement && parent.document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = parent.document.documentElement.scrollTop;
	} else if (parent.document.body) {// all other Explorers
		yScroll = parent.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
//
window.getPageSize = function(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = parent.document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;		
	} else if (parent.document.body.scrollHeight > parent.document.body.offsetHeight){ // all but Explorer Mac
		xScroll = parent.document.body.scrollWidth;
		yScroll = parent.document.body.scrollHeight;		
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari		
		xScroll = parent.document.body.offsetWidth;
		yScroll = parent.document.body.offsetHeight;		
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;		
	} else if (parent.document.documentElement && parent.document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = parent.document.documentElement.clientWidth;
		windowHeight = parent.document.documentElement.clientHeight;
	} else if (parent.document.body) { // other Explorers
		windowWidth = parent.document.body.clientWidth;
		windowHeight = parent.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
//
window.pause = function(numberMillis){
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightboxcart.
//
window.getKey = function(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightboxcart(); }
}


//
// listenKey()
//
window.listenKey = function() {	parent.document.onkeypress = getKey; }
	

//
// showLightboxcart()
// Preloads images. Pleaces new image in lightboxcart then centers and displays.
//
window.showLightboxcart = function(objLink){

	//Hidden some elements
        document.getElementById('ad-top').style.visibility = 'hidden';
	document.getElementById("main-sideside-narrow").style.visibility = 'hidden';
	document.getElementById("portadita-img").style.visibility = 'hidden';
	document.getElementById("idcolumnasad").style.visibility = 'hidden';

	// prep objects
	var objOverlaycart = parent.document.getElementById('overlaycart');	
	var objLightboxcart = parent.document.getElementById('lightboxcart');
	var objLoadingImage = parent.document.getElementById('loadingImage');	
	
	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 - objLoadingImage.width) / 2) + 'px');		
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlaycart to take up whole page and show
	objOverlaycart.style.height = (arrayPageSize[1] + 'px');
	objOverlaycart.style.display = 'block';

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function(){
		//objImage.src = objLink.name;

		// center lightboxcart and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxcartTop = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2) / 6);
		var lightboxcartLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);		
		
		objLightboxcart.style.top = (lightboxcartTop < 0) ? "0px" : lightboxcartTop + "px";
		objLightboxcart.style.left = (lightboxcartLeft < 0) ? "0px" : lightboxcartLeft + "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);
		} 

		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

		// Hide select boxes as they will 'peek' through the image in IE
		selects = parent.document.getElementsByTagName("select");
                for (i = 0; i != selects.length; i++) {
          	      selects[i].style.visibility = "hidden";
        	}

	
		objLightboxcart.style.display = 'block';

		// After image is loaded, update the overlaycart height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlaycart.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		return false;
	}

	imgPreload.src = objLink.name;
	
}

//
// hideLightboxcart()
//
window.hideLightboxcart = function()
{
	//Show some elements
        document.getElementById('ad-top').style.visibility = 'visible';
	document.getElementById("main-sideside-narrow").style.visibility = 'visible';
	document.getElementById("portadita-img").style.visibility = 'visible';
	document.getElementById("idcolumnasad").style.visibility = 'visible';

	// get objects
	objOverlaycart = parent.document.getElementById('overlaycart');
	objLightboxcart = parent.document.getElementById('lightboxcart');

	// hide lightboxcart and overlaycart
	objOverlaycart.style.display = 'none';
	objLightboxcart.style.display = 'none';

	// make select boxes visible
	selects = parent.document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	parent.document.onkeypress = '';
}




//
// initLightboxcart()
// Function runs on window load, going through link tags looking for rel="lightboxcart".
// These links receive onclick events that enable the lightboxcart 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 overlaycart pattern and the inline image.
//
window.initLightboxcart = function(idc)
{	
	if (!parent.document.getElementsByTagName){ return; }

	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
        for (var i=0; i<anchors.length; i++){
                var anchor = anchors[i];
                if (anchor.getAttribute("name") && (anchor.getAttribute("rel") == "lightboxcart")){
                        anchor.onclick = function () {
                            showLightboxcart(this);
                            return false;
                        }
                }
        }
	
	var objBody = parent.document.getElementsByTagName("body").item(0);
	
	// create overlaycart div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlaycart = parent.document.createElement("div");
	objOverlaycart.setAttribute('id','overlaycart');
	objOverlaycart.onclick = function () {hideLightboxcart(); return false;}
	objOverlaycart.style.display = 'none';
	objOverlaycart.style.position = 'absolute';
	objOverlaycart.style.top = '0';
	objOverlaycart.style.left = '0';
	objOverlaycart.style.zIndex = '90';
 	objOverlaycart.style.width = '100%';
	objBody.insertBefore(objOverlaycart, 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 lightboxcart and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = parent.document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightboxcart(); return false;}
		objOverlaycart.appendChild(objLoadingImageLink);
		
		var objLoadingImage = parent.document.createElement("img");
		objLoadingImage.src = "http://www.jornada.unam.mx/v8.0/images/media/loading.gif";
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = "http://www.jornada.unam.mx/v8.0/images/media/loading.gif";

	// create lightboxcart div, same note about styles as above
	var objLightboxcart = parent.document.createElement("div");
	objLightboxcart.setAttribute('id','lightboxcart');
	objLightboxcart.style.display = 'none';
	objLightboxcart.style.position = 'absolute';
	objLightboxcart.style.zIndex = '100';
        var objLightIframeC = document.createElement("iframe");
        objLightIframeC.setAttribute('id','lightboxcarIframe');
        objLightIframeC.setAttribute('width','500px');
        objLightIframeC.setAttribute('height','570px');
        objLightIframeC.setAttribute('marginwidth','0');
        objLightIframeC.setAttribute('marginheight','0');
        objLightIframeC.setAttribute('scrolling','no');
        objLightIframeC.setAttribute('frameborder','0');
        objLightIframeC.setAttribute('src','http://www.jornada.unam.mx/v8.0/cgi/cartoons.php?itemcarton=' + idc);
        objLightboxcart.appendChild(objLightIframeC);
	objBody.insertBefore(objLightboxcart, objOverlaycart.nextSibling);
	
	// create link
	var objLink = parent.document.createElement("a");
	objLink.setAttribute('href','javascript:void(0)');
	objLink.setAttribute('title','Cerrar');
        objLink.style.display = 'block';        
	objLink.onclick = function () {hideLightboxcart(); return false;}
	objLightboxcart.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){

		var objCloseButton = parent.document.createElement("img");
		objCloseButton.src = "http://www.jornada.unam.mx/v8.0/images/media/close.gif";
		objCloseButton.setAttribute('id','closeButton');
		objLink.appendChild(objCloseButton);

		return false;
	}

	imgPreloadCloseButton.src = "http://www.jornada.unam.mx/v8.0/images/media/close.gif";
}
