/*
	Lightboxvideos- JS
*/

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'http://www.jornada.unam.mx/v8.0/images/media/loading.gif';
var closeButton = 'http://www.jornada.unam.mx/v8.0/images/media/close.gif';

//
// 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;
}

//
// 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;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightboxvideos.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightboxvideos(); }
}


//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	
//
// showLightboxvideos()
// Preloads images. Pleaces new image in lightboxvideos then centers and displays.
//

function showLightboxvideos(indice)
{

	document.getElementById('videoboxclick').style.display = '';
        document.getElementById('main-sideside-narrow').style.visibility = 'hidden';
        document.getElementById('portadita-img').style.visibility = 'hidden';
        document.getElementById('ad-top').style.visibility = 'hidden';        

       	var minivideo = document.getElementById('lightminiboxvideos');
        var littlebox = document.getElementById('videoshow');

        if (minivideo != null)
	    littlebox.removeChild(minivideo);

	// prep objects
	var objOverlayvideos = document.getElementById('overlayvideos');
	var objLightboxvideos = document.getElementById('lightboxvideos');
	var objCaption = document.getElementById('lightboxvideosCaption');
	var objImage = document.getElementById('lightboxvideosImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxvideosDetails = document.getElementById('lightboxvideosDetails');

	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';
	}


	objOverlayvideos.style.height = (arrayPageSize[1] + 'px');
	objOverlayvideos.style.display = 'block';
	
	imgPreload = new Image();	

	var lightboxvideosTop = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 418) / 2) / 6);
	var lightboxvideosLeft = ((arrayPageSize[0] - 20 - 522) / 2);		
		
	objLightboxvideos.style.top = (lightboxvideosTop < 0) ? "0px" : lightboxvideosTop + "px";
	objLightboxvideos.style.left = (lightboxvideosLeft < 0) ? "0px" : lightboxvideosLeft + "px";

	objLightboxvideosDetails.style.width = '522px';

	if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
	
	objImage.setAttribute('width','522');
	objImage.setAttribute('height','418');	
	objImage.setAttribute('type','application/x-shockwave-flash');
	objImage.setAttribute('data',VIDEOS[indice].swf);
	objImage.style.visibility = 'visible';

	objLightboxvideos.style.display = 'block';
}

//
// hideLightboxvideos()
//

function hideLightboxvideos()
{

	//document.getElementById("idTrVideos").style.display = 'block';
	//document.getElementById("idTrPortadita").style.display = 'block';

        document.getElementById('main-sideside-narrow').style.visibility = 'visible';
        document.getElementById('portadita-img').style.visibility = 'visible';
        document.getElementById('ad-top').style.visibility = 'visible';        

	// get objects
	objOverlayvideos = document.getElementById('overlayvideos');
	objLightboxvideos = document.getElementById('lightboxvideos');

	// hide lightboxvideos and overlayvideos
	objOverlayvideos.style.display = 'none';
	objLightboxvideos.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 = '';
}


//
// initLightboxvideos()
// Function runs on window load, going through link tags looking for rel="lightboxvideos".
// These links receive onclick events that enable the lightboxvideos 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 overlayvideos pattern and the inline image.
//
function initLightboxvideos()
{
			
	if (!document.getElementsByTagName){ return; }
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlayvideos div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlayvideos = document.createElement("div");
	objOverlayvideos.setAttribute('id','overlayvideos');
	objOverlayvideos.onclick = function () {hideLightboxvideos(); return false;}
	objOverlayvideos.style.display = 'none';
	objOverlayvideos.style.position = 'absolute';
	objOverlayvideos.style.top = '0';
	objOverlayvideos.style.left = '0';
	objOverlayvideos.style.zIndex = '90';
 	objOverlayvideos.style.width = '100%';
	objBody.insertBefore(objOverlayvideos, 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 lightboxvideos and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.setAttribute('title','Cerrar');
		objLoadingImageLink.onclick = function () {hideLightboxvideos(); return false;}
		objOverlayvideos.appendChild(objLoadingImageLink);		
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		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 = loadingImage;

	// create lightboxvideos div, same note about styles as above
	var objLightboxvideos = document.createElement("div");
	objLightboxvideos.setAttribute('id','lightboxvideos');
	objLightboxvideos.style.display = 'none';
	objLightboxvideos.style.position = 'absolute';
	objLightboxvideos.style.zIndex = '100';	
	objBody.insertBefore(objLightboxvideos, objOverlayvideos.nextSibling);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','javascript:void(0)');
	//objLink.setAttribute('title','Cerrar');
	//objLink.onclick = function () {hideLightboxvideos(); return false;}
	objLightboxvideos.appendChild(objLink);

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

	var objCloseButton = document.createElement("img");
        objCloseButton.src = ""; 
	objCloseButton.src = "http://www.jornada.unam.mx/v8.0/images/media/close.gif";
	objCloseButton.setAttribute('id','closeButton');
	objCloseButton.style.position = 'absolute';
	objCloseButton.style.top = '-7px';
	objCloseButton.style.right = '-5px';
	objCloseButton.style.zIndex = '200';
	objCloseButton.onclick = function () {hideLightboxvideos(); return false;}
	objLink.appendChild(objCloseButton);	
	
	var objImage = document.createElement("object");
	objImage.setAttribute('id','lightboxvideosImage');
	objLink.appendChild(objImage);

	// create details div, a container for the caption and keyboard message
	var objLightboxvideosDetails = document.createElement("div");
	objLightboxvideosDetails.setAttribute('id','lightboxvideosDetails');
	objLightboxvideos.appendChild(objLightboxvideosDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxvideosCaption');
	objCaption.style.display = 'none';
	objLightboxvideosDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	objKeyboardMsg.innerHTML = '<a href="#" onclick="hideLightboxvideos(); return false;"><kbd></kbd></a>';
	objLightboxvideosDetails.appendChild(objKeyboardMsg);
}
