//
// getPageScroll()
// Returns array with x,y page scroll values.
//
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;
	}

	var arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
//
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){
		var pageHeight = windowHeight;
	} else { 
		var pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		var pageWidth = windowWidth;
	} else {
		var pageWidth = xScroll;
	}


	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// showLightbox()
//
function showLightbox(objLink){
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

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


		wsID = objLink.rel;
		moveFO(objImage.id);
		

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - flash_h) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - flash_w) / 2);

		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


		objLightboxDetails.style.width = 'auto';
		
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}

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

	
		objLightbox.style.display = 'block';

		// 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');

	
}

function hideLightbox(){
	// get objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	var selects = document.getElementsByTagName("select");
    for (var i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	// remove flash movie
	var obj = document.getElementById("WSplayerLight");
	obj.parentNode.removeChild(obj);
	
	initLightbox()
}

function initLightbox(){
	if (!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("id") == "lightboxlink"){
			if(anchor.childNodes[1]){
				anchor.href = anchor.childNodes[0].src || anchor.childNodes[1].src;
			}
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}
	
	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 = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '999998';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '999999';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
	// create link obj
	var objLink = document.createElement("a");
	objLink.setAttribute('title','Click to close');
	objLightbox.appendChild(objLink);

	// create image
	objImage = document.createElement("div");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);
	
	// create details div, a container for the caption and keyboard message
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);

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

	// create close message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	objKeyboardMsg.innerHTML = '<a href="javascript:" onclick="hideLightbox(); return false;">Click to close</a> ';
	
	objLightboxDetails.appendChild(objKeyboardMsg);


}

function getParam(obj,_nm){
	if (!obj)
	        return "";
	var c = obj.childNodes;
	var cl = c.length;
	for (var i = 0; i < cl; i++) {
		if (c[i].nodeType == 1 && c[i].nodeName.toLowerCase() == "object") {
			c = c[i].childNodes;
			cl = c.length;
			i = 0;
		}     
		if (c[i].nodeType == 1 && c[i].nodeName.toLowerCase() == "param" && c[i].getAttribute("name") == _nm) { 
			return c[i].getAttribute("value"); 
		}	       
	}
}

function moveFO(div_nm){
	var obj = document.getElementById("WSplayer");
	var swfPath = getParam(obj,"movie")
	flash_w = obj.width;
	flash_h = obj.height;
	var flvrs = getParam(obj,"FlashVars");
	flvrs = flvrs.replace(/WSplayer/g,"WSplayerLight");
	var params = {
	  wmode: "transparent",
	  allowScriptAccess:"always",
	  swliveconnect:"true",
	  allowFullScreen:"true",
	  quality:"high",
	  FlashVars:flvrs
	};
	var flashvars = {};
	var attributes = {
	  id: "WSplayerLight",
	  name: "WSplayerLight"
	};
	swfobject.embedSWF(swfPath, div_nm, flash_w, flash_h, "8.0.0","expressInstall.swf", flashvars, params, attributes);
}

function flashEvent(e){
	if(e.event == "playerIsReady" && e.flashObjectID=="WSplayerLight"){
		swfobject.getObjectById(e.flashObjectID).JS_PlayVideoNum(wsID);
	}
}

window.onload=initLightbox;

