// This file, when included on a page, installs an onload handler that
// scans links on the page for YouTube references.   If any are found, 
// It creates a hidden div to contain a YouTube embedded player that is
// raised whenever the original link is clicked.

// Additionally, if the page's query string includes video=expand, an inline viewer will be placed on the page following the link.

// Several CSS selectors are used to style the resulting div.
// 		a.videopop span { styles the close button }
// 		a.videopop span:hover
// 		a.videopop .vpopframe
// 		a.videopop .vpopframe .closelink

if (window.addEventListener) // W3C standard
{
	window.addEventListener('load', setupVideoPop, false); // NB **not** 'onload'
} 
else if (window.attachEvent) // Microsoft
{
	window.attachEvent('onload', setupVideoPop);
}

// from http://stackoverflow.com/questions/2907482/getting-querystring-by-javascript
function getQueryStrings() { 
	  var assoc  = {};
	  var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
	  var queryString = location.search.substring(1); 
	  var keyValues = queryString.split('&'); 

	  for(var i in keyValues) { 
	    var key = keyValues[i].split('='); 
	    if (key.length > 1) assoc[decode(key[0])] = decode(key[1]); 
	  } 

	  return assoc; 
	} 

function vpopClick(theDiv, e) {
	// Called from clicks on the original link
	//	theDiv: the "popup" div associated with this link.
	//	e: event
	//	If the div is displayed, hide it, otherwise show it.
	// todo: tell YouTube player to start playing
	var fw = theDiv;
	if (fw) {
		if (fw.style.display == "block") {
			fw.style.display = "none";
		} else {
			fw.style.display = "block";
		}
	}
}

function vpopDismiss(theDiv, e) {
	// Called from the "close" link in the popup div.
	// theDiv: the "popup" div that we are closing
	// e: event
	// todo: tell YouTube player to stop playing
	
	var fw = theDiv;
	if (fw) {
		fw.style.display = "none";
	}
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return(true);
}

// Helper functions to create closures for onclick functions
// Needed to create a closure context for the individual elements being created.
function mkClick(ele) {
	return function(e) { vpopClick(ele, e); return false; };
}

function mkDismiss(ele) {
	return function(e) { vpopDismiss(ele, e); return false; };
}

function attachYouTubePop(vLink, videoId, inline) {
	// Create a video display div at the end of the link
	var vDiv = document.createElement("div");
	vDiv.className = "vpopframe";
	vDiv.id = "videoObj" + videoId;

	// Span to hold the "X" to close the video window
	var vSpanClose = document.createElement("span");
	vSpanClose.className = "closelink";
	vSpanClose.onclick = mkDismiss(vDiv);
	vSpanClose.appendChild(document.createTextNode("X"));
	if (!inline) vDiv.appendChild(vSpanClose);

	var frameObj = document.createElement("iframe");
	frameObj.className="youtube-player";
	frameObj.type = "text/html";
	frameObj.width="480";
	frameObj.height="385";
	frameObj.src = "http://www.youtube.com/embed/" + videoId + 
	"?rel=0&amp;showinfo=0&amp;showsearch=0;&amp;autoplay=0";
	frameObj.frameborder = "0";

	if (inline) {
		vLink.href="#";		// Make the link inoperative
		vLink.appendChild(frameObj);
	} else {
	 	vDiv.appendChild(frameObj);
		vLink.onclick = mkClick(vDiv); 
		vLink.appendChild(vDiv);
		vLink.className = "videopop";
	}
}

function setupVideoPop() {
	// Runs when page loads.
	
	// Does querystring ask for expanded videos?
	var qs = getQueryStrings();
	var myQSvideo = qs["video"]; 
	if (myQSvideo && (myQSvideo != "")) myQSvideo=myQSvideo.toLowerCase();

	// Grab all links on the page
	var linkList = document.getElementsByTagName("a");
	var urlPattern = new RegExp("\.youtube\.com/");

	for (var i=0; i <linkList.length; i++) {
		if (urlPattern.test(linkList[i].href)) {
			// from the youtube URL, take the string after "watch=" and build our embedded URL
			// assume that anything after the first "=" and up to an optional "?" is what we want.
			var thePos = linkList[i].href.indexOf("=");
			var endPos = linkList[i].href.substr(thePos+1).indexOf("&");
			if (endPos < 0) endPos = linkList[i].href.substr(thePos+1).length;

			var videoId = linkList[i].href.substr(thePos+1, endPos);
			
			attachYouTubePop(linkList[i], videoId, (myQSvideo == "expand"));
		}
	}
}

