/*
	Video Embedding Script
	
	Some code taken from:
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com
	
*/

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
var video = null;

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


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


	function getVideo (videoURL, video)
	{
		video = video + "=";

		if (videoURL.length > 0)
		{
		    startIndex = videoURL.indexOf(video);

		    if (startIndex > -1)
			{
				startIndex += video.length;	
				endIndex = videoURL.indexOf("&" , startIndex);

				if (endIndex == -1)
				{	
					endIndex = videoURL.length;
				}
	
				return unescape(videoURL.substring(startIndex, endIndex));
		    }
		    else
		    {
				return "";
		    }
		}
	}



function vem_showVideoBox(videoUrl)
{
	objLightBox = document.createElement("div");
	objLightBox.style.visible = "false";
	objLightBox.style.position = "absolute";
	
	// Determine appropriate video position & size based on browser
	
	posY = getPageScroll()[1];
	
	bodyHeight = getPageSize()[3];
	bodyWidth = getPageSize()[0]-35;
	
	vidWidth = bodyWidth * .54;
	vidHeight = vidWidth * .7;
	
	vidLeft = (bodyWidth / 2) - (vidWidth / 2);
	vidTop = posY + (bodyHeight / 2) - (vidHeight / 2);
	
	objLightBox.style.left = vidLeft+"px";
	objLightBox.style.top =  vidTop+"px";
	
	objLightBox.style.width = vidWidth+"px";
	objLightBox.style.height = vidHeight+"px";
	
	
	objLightBox.id = "lightbox";
	
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	
	objOverlay.style.fontSize = "10pt";
	objOverlay.style.color = "#ffffff";
	objOverlay.style.textAlign = "center";
	
	objOverlay.onclick = function () {
		objOverlay.style.display = 'none';
		objLightBox.style.display = 'none';
		objLightBox.parentNode.removeChild(objLightBox);
	}
	
	document.getElementsByTagName("body").item(0).appendChild(objOverlay);
	document.getElementsByTagName("body").item(0).appendChild(objLightBox);
	
	objOverlay.appendChild(document.createTextNode("Click anywhere outside the video to close."));
	
	flashembed("lightbox", "http://www.piematrix.com/video/flowplayer/FlowPlayerLight.swf", {config: {   
	    autoPlay: false, 				
		autoBuffering: true,
		initialScale: 'scale', 
	    useNativeFullScreen: true ,
		usePlayOverlay: true,
	    playList: [{ url: videoUrl, overlayId: 'lightbox', loop: false}]
	}});
	
	
	
	objLightBox.style.background = "#ffffff";
	objLightBox.style.visible = "true";
	objLightBox.style.margin = "5px";
	objLightBox.style.zIndex = "100";
}