function closeVideoWindow (e)
{
	var obj = null;
	if ( e.srcElement != null )
	{
		obj = e.srcElement;
	}
	else
	{
		obj = e.target;
	}
	if ( obj != null )
	{
		removeMessageArea ( "vidmessage", null );
	}
}
function setupVideoWindowHandlers()
{
	var myWindow = document.getElementById("vidmessage");
	if ( myWindow != null )
	{
		var vidLinks = myWindow.getElementsByTagName("a");
		if ( vidLinks != null )
		{
			for ( var i = 0; i < vidLinks.length; i++ )
			{
				if ( vidLinks[i].id.indexOf("vselect") != -1 )
				{
					vidLinks[i].onclick = function () { return false; };
					addEvent ( vidLinks[i], "click", closeVideoWindow, false );
					addEvent ( vidLinks[i], "click", overrideNavigation, false );
				}
			}
		}
	}
}

function showVideo ( XML )
{
	var XML = (XML) ? XML : null;
	if ( XML != null )
	{
		var fixtureId = "";
		var raceId = "";
		var divisionId = "";
		var entryId = "";
		var type = "result";
		var horseStr = "";
		var loggedIn = false;
		var videoDiv = "videooverlay";
		var videoInfo = "";
		var listHTML = "";

		var myRoot  = XML.documentElement;
		var xRecords = myRoot.getElementsByTagName("records")[0];
		if ( xRecords != null && xRecords.firstChild != null )
		{
			var xQuery = xRecords.firstChild;
			while ( xQuery )
			{
				if ( xQuery.nodeName == "#text" )
				{
					xQuery = xQuery.nextSibling;
					if ( xQuery == null )
					{
						break;
					}
				}
				var myLabel = '';
				var myValue = '';
				
				var xLabel = xQuery.getElementsByTagName("qlabel")[0];
				if ( xLabel != null && xLabel.firstChild != null )
				{
					myLabel = xLabel.firstChild.nodeValue;
				}
				var xValue = xQuery.getElementsByTagName("qvalue")[0];
				if ( xValue != null && xValue.firstChild != null )
				{
					myValue = xValue.firstChild.nodeValue;
				}
				
				if ( myLabel != "" && myValue != "" )
				{
					switch ( myLabel )
					{
						case "entry_id":
							entryId = myValue;
							break;
							
						case "fixture_id":
							fixtureId = myValue;
							break
							
						case "race_id":
							raceId = myValue;
							break;
							
						case "division":
							divisionId = myValue;
							break;
							
						case "horse_name":
						case "horse_id":
							horseStr = myLabel + "=" + myValue;
							break;
							
						case "listhtml":
							listHTML = myValue;
							break;
							
						case "logged_in":
							if ( myValue == "1" )
							{
								loggedIn = true;
							}
							break;
							
						case "videodiv":
							videoDiv = myValue;
							break;
							
						case "ordervideo":
							videoInfo = myValue;
							break;
							
						default:
							break;
					}
				}
				xQuery = xQuery.nextSibling;
			}
			/*
			 * Now we have the values needed, build the screen for the display
			 */
			var myBody = document.getElementsByTagName("body")[0];
			if ( myBody != null )
			{
				var msgAreaHeight = 100;
				if ( document.height )
				{
					msgAreaHeight = document.height;
				}
				if ( window.innerHeight )
				{
					if ( msgAreaHeight < window.outerHeight )
					{
						msgAreaHeight = window.outerHeight;
					}
				}
				else
				{
					if ( msgAreaHeight < document.body.clientHeight )
					{
						msgAreaHeight = document.body.clientHeight;
					}
				}
				/*
				 * Create the 'grey-out' overlay
				 */
				overlay = document.createElement( "div" );
				overlay.setAttribute ( "id", "sysoverlay" );
				overlay.style.height = "2400px";
	
				/*
				 * Create the container window and 
				 * adjust the top position
				 */
				area = document.createElement( "div" );
				//area.setAttribute ( "id", "sysmessage" );
				area.setAttribute ( "id", "vidmessage" );
				area.className = "videowindow";
				/*
				var boxTop = 250;
				if ( entryId != "" )
				{
					var objId = "lselect_showvideo_" + entryId;
					var myObj = document.getElementById(objId);
					if ( myObj != null )
					{
						var pos = findPos ( myObj );
						if ( pos != null )
						{
							boxTop = pos[1] - 300;
							if ( boxTop < 0 )
							{
								boxTop = 200;
							}
						}
					}
				}
				*/
				area.style.top = "30px";
				area.setAttribute("top", "30px" );
				/*
				 * Create the window header
				 */
				var videoHdr = document.createElement("div");
				videoHdr.className = "videoheader";
				videoHdr.appendChild(document.createTextNode("Archive Video Replays"));
				/*
				 * Create the video container
				 */
				var msgArea = document.createElement("div");
				msgArea.className = "videocontainer"
				msgArea.id = "videoarea";
				/*
				 * Create the area for the result listing
				 */
				var listArea = document.createElement("div");
				listArea.className = "vidoeresultlist";
				listArea.innerHTML = listHTML;
				/*
				 * Create the area for the video / sales info
				 */
				var infoArea = document.createElement("div");
				infoArea.className = "videoinfo";
				infoArea.innerHTML = videoInfo;
				/*
				 * Create the window footer; 
				 * The "close window" form and
				 * and append the form to the footer.
				 */
				var footer = document.createElement("p");
				footer.style.margin = "0";
				footer.setAttribute("margin", "0" );
				footer.style.padding = "0";
				footer.style.background = "#065500";
				
				var myForm = document.createElement("form");
				myForm.className = "closevideoform";
				myForm.setAttribute ( "method", "post" );
				myForm.setAttribute ( "action", "#" );
				myForm.onsubmit = function () { return false };
				var myButton = document.createElement ( "input" );
				myButton.setAttribute ( "type", "submit" );
				myButton.setAttribute ( "id", "closebutton" );
				myButton.id = "closebutton";
				myButton.value = "Hide Video";
				//myButton.setAttribute ("margin-left", "100px");
				myButton.onclick = function () { removeMessageArea ( "vidmessage", null ); return false; };
				myForm.appendChild(myButton);
				myClear = document.createElement("hr");
				myClear.className = "clear";
				
				footer.appendChild(myForm);
				/*
				 * Add stuff to window
				 */
				area.appendChild (videoHdr);
				area.appendChild(msgArea);
				area.appendChild(listArea);
				area.appendChild(infoArea);
				area.appendChild(myClear);
				area.appendChild(footer);
				/*
				 * Add to the document
				 */
				myBody.appendChild(overlay);
				myBody.appendChild(area);
				setupVideoWindowHandlers ();
				/*
				 * Now show the video
				 */
				if ( loggedIn )
				{
					flash_movie_display(fixtureId, raceId, divisionId, msgArea.id, type, horseStr );
				}
				else
				{
					flash_movie_display_short(fixtureId, raceId, divisionId, msgArea.id, type, horseStr );
				}
			}
		}
	}
}

