// Query used for getVideo request
var g_query = "category:news";

// The global variable for the AOLVideoSearch object used in this application.
var g_aolVideos = null;

// Function getVideos() submits the given query to the AOL video search engine.
function getVideos (query)
{ 
	document.getElementById ("video_thumbnail").src = "images/no_video.gif";
	g_aolVideos.getVideos (query);
}

// Function playVideo() opens a new browser window to load the specified videoUrl.
function playVideo (videoUrl, id)
{
	window.open (videoUrl, "", "width=800,height=800,location=no,menubar=no,resizable=yes,scrollbars=yes");
}

// Function handleUpdate() handles all response messages from the AOLVideoSearchAPI.  Whenever the AOLVideoSearchAPI
// receives a response to an AJAX request, it fires the onupdate event.  This method handles each onupdate event.
function handleUpdate (methodName)
{
	refreshResults (g_aolVideos);
}

// Function handleLoad() handles the onload event, which is fired when the initialization of the AOLVideoSearch object is complete.
function handleLoad (reloadStateFlag)
{
	getVideos (g_query);
}

// Function handleError() handles all errors thrown by the AOLVideoSearch API.  It displays an alert box with the
// error code and error message.
function handleError (errorCode, errorMessage)
{
	alert ("ERROR: Code " + errorCode + "; " + errorMessage);
}

// Function refreshResults() takes a the AOL Video Search object as an argument and use the data sets in this object
// to display a clickable thumbnail and title for each video search result.  This function also displays the provided
// title above the search results and the submitted query in the search box.
function refreshResults (g_aolVideos)
{
	var videoLoaded = false;
	if (g_aolVideos.VideoSet && g_aolVideos.VideoSet.Video)
	{
		var videoSet = g_aolVideos.VideoSet;
		if (videoSet.Video.length > 0)
		{
			var video = videoSet.Video [0];
			if (video)
			{
				var seconds = video.runtime % 60;
				if (seconds < 10)
					seconds = "0" + seconds;
				var timeStr = Math.floor (video.runtime / 60) + ":" + seconds;
				var videoRef = "javascript:playVideo(\'" + video.videoUrl + "\'," + video.id+"); void(0);";
				videoLoaded = true;
				document.getElementById ("video_title").innerHTML = video.title;
				document.getElementById ("video_description").innerHTML = video.description;
				document.getElementById ("video_time").innerHTML = "Video Length "  + timeStr;
				document.getElementById ("video_thumbnail").src = video.thumbnailUrl;
				document.getElementById ("video_watch_tn").href = videoRef;
				document.getElementById ("video_watch").href = videoRef;
			}
		}
	}
	
	if (! videoLoaded)
	{
		document.getElementById ("video_thumbnail").src = "images/no_video.gif";
	}
}

// Function VSLoad() is called when the containing web page has completed loading.  This function instantiates a single AOLVideoSearch
// object, sets the state of the object, attaches the appropriate event handlers to this object, and then initializes the object. 
function VSLoad ()
{
	g_aolVideos = new AOLVideoSearch ('a7bagxtzynuhv3ait');
	g_aolVideos.results = 1;
	g_aolVideos.attachEvent ('onerror', 'handleError (errorCode, errorMessage);');
	g_aolVideos.attachEvent ('onupdate', 'handleUpdate (methodName);');
	g_aolVideos.attachEvent ('onload', 'handleLoad (reloadStateFlag);');
	g_aolVideos.initialize ();
}
