var results;

function playVideo(id) {
	var vp = document.getElementById('videoplayer');
	vp.innerHTML = '<span></span>'; // wipe it clean
	document.getElementById('overlay').style.display = vp.style.display = 'block';

	var player = GvideoSearch.createPlayer(results[id], "playerwidget");

	var close = document.createElement("div");
	var ahref = document.createElement("a");
	ahref.setAttribute("href", "javascript:void(0);");
	ahref.setAttribute("id", "close");
	ahref.onclick = clearPlayer;
	ahref.appendChild(document.createTextNode("close"));
	close.appendChild(ahref);
	
	vp.replaceChild(player, vp.firstChild);
	vp.appendChild(close);
}

GSearch.setOnLoadCallback(function() {
	doSearch();
	setupOverlay();
});

function doSearch(narrow) {
	var term, escapedterm;
	
	if (narrow) {
		term = narrow;
		escapedterm = escape(term);
	} else {
		term = 'most recent videos';
		escapedterm = '';
	}
	var videolink = '<a href="http://video.google.com/videosearch?q=user%3A%22Google+engEDU%22+' + escapedterm + '&num=10&so=1&start=0" title="Jump out to Google Video">';
	
	var videoSearch = new GvideoSearch();
	videoSearch.setResultOrder(GSearch.ORDER_BY_DATE);
	videoSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
	videoSearch.setNoHtmlGeneration();
	videoSearch.setSearchCompleteCallback(null, function() {
		var output;
		if (videoSearch.results.length == 0) { 
			output = '<div id="searchresults"><p>No results found for: ' + term + '</p><p>Try again, or check out the <a href="javascript:void(0)" onclick="doSearch(); return false;">most recent tech talk videos</a></div>';
		} else {
			output = '<div id="searchresults">Search Results for: ' + videolink + term + '</a></div><table width="100%">';
			results = videoSearch.results;
			var totalVideos = videoSearch.results.length;
			for (var x=0; x < totalVideos; x++) {
				var result = videoSearch.results[x];
				var nicerDate = new Date();
				nicerDate.setTime(Date.parse(result.published));
				var duration = parseInt(result.duration / 60)
				var dateFormatted = (nicerDate.getMonth()+1) + '/' + nicerDate.getDate() + '/' + nicerDate.getFullYear();
				var snippet = stripToAbstract(result.content);
				var playUrl = '<a href="javascript:void(0);" onclick="playVideo(' + x + ');">';
				var entry = '<td width="50%" style="padding: 5px;"><div class="videoentry">' + playUrl + '<img src="' + result.tbUrl + '" alt="' + result.titleNoFormatting + ' Video" width="129" height="110" border="0" align="left" style="margin: 5px" /></a><div class="name">' + playUrl + result.titleNoFormatting + '</a></div><div style="font-size: xx-small; color: #999; padding: 5px;">Recorded on ' + dateFormatted + ' <a class="ongv" href="' + result.url + '" title="View the video over at Google Video" target="_blank">view on GV</a><br />Duration: ' + duration + ' mins</div><p class="videosnippet">' + snippet + '</p></div></td>';
				
				if ( (x % 2) == 0) {
					output += '<tr>' + entry;
				} else {
					output += entry + '</tr>';
				}
			}
			if (totalVideos % 2 == 1) { // odd, so a tr is left over
				output += '</tr>';
			}
			output += '</table><div>' + videolink + 'View all results for: ' + term + ' &raquo;</a></div>';
		}
		document.getElementById('videoresults').innerHTML = output;
	});
	var searchterm = 'user:"Google engEDU"';
	if (narrow) {
		searchterm = searchterm + ' ' + narrow;
	}
	videoSearch.execute(searchterm);
};

function setupOverlay() {
	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){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if (yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if (xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	var overlay = document.getElementById('overlay');
	overlay.style.height = pageHeight;
	overlay.style.width = pageWidth;
	//overlay.onclick = clearPlayer;
}

function stripToAbstract(snippet) {
	var index = snippet.indexOf('ABSTRACT');
	return (index > 0) ? snippet.substring(index + 8) : snippet;
}

function clearPlayer() {
	document.getElementById('overlay').style.display = document.getElementById('videoplayer').style.display = 'none';
}
