// video file name
var flvId = location.href.split("flvId=")[1];

// if none choose first
if(flvId == "" || flvId == "undefined" || flvId == null) {
	flvId = "1";
}

// default variables
var contentNode;
var flvPath;
var videoPath;
var ccPath;
var videoImg;
var belowVideoContent = "";
var thumbString = "";
var theVideo = "";
var httpRequest = false;

function makeRequest(url) {
	try {
		// for mozilla, safari, browsers
		if (window.XMLHttpRequest) {
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType("text/xml");
			}
		}
		// for ie
		else if (window.ActiveXObject) {
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!httpRequest) {
			alert("Cannot create XMLHTTP Instance.");
			return false;
		}
		// call function getData()
		httpRequest.onreadystatechange = getData;
		httpRequest.open('GET', url, true);
		httpRequest.send(null);
	}
	catch(e){debug!=-1?alert(e):"";}
}

function getData() {
	try{
		if(httpRequest.readyState == 0) {}
		if(httpRequest.readyState == 1) {}
		if(httpRequest.readyState == 2) {}
		if(httpRequest.readyState == 3) {}
		if(httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				contentNode= httpRequest.responseXML.getElementsByTagName("content");
				process();
			}
			else {
				alert("There was a problem with the request.");
			}
		}
	}
	catch(e) {
		alert(e);
	}
}

function process() {

	var items = contentNode[0].getElementsByTagName("item");
	
	function getVal(num, nodeName){
			var v = items[num].getElementsByTagName(nodeName)[0].firstChild.nodeValue;
			return v;
	}
	for (k = 0; k < items.length; k++) {
		
		if (String(flvId) == String(items[k].getAttribute("id"))) {
			
			videoPath         = getVal(k,"videoPath");
			ccPath	          = getVal(k,"ccPath");
			videoImg          = getVal(k,"videoImg");
			belowVideoContent = getVal(k, "belowVideoContent");
			
			flvPath = videoPath + "^" + ccPath + "^" + videoImg;

		}
		
		//constructing thumb string
		thumbString += '<table border="0" cellpadding="2" cellspacing="0" class="thumbContainer"><tr><td width="10%">';
		thumbString += '<a href="?flvId=' + items[k].getAttribute('id') + '">';
		thumbString += '<img style="padding-bottom:1px;" border="0" id="thumb2" class="home-thumbnail" src="' + getVal(k,'thumbImg') + '" title="' + getVal(k,'thumbTitle') + '"/>';
		thumbString += '</a></td>';
		thumbString += '<td class="thumbHeader">';
		thumbString += '<h3>' + getVal(k, 'thumbTitle') + '</h3></td></tr>';
		thumbString += '<tr><td colspan="2" class="thumbDescription"><p>' + getVal(k, 'thumbDes') + '</p></td></tr>';			
		thumbString += '</table>';
	}
	
	// now that all objects are loaded, update page
	renderVideoPlayer("home-video");
	updatedBelowVideoContent();
	buildThumbs();
}

function updatedBelowVideoContent() {
	if ((belowVideoContent != "") && (belowVideoContent != "undefined")) {
		document.getElementById('home-video-entry').innerHTML = belowVideoContent;
	}
}

function buildThumbs() {
	if ((thumbString != "") && (thumbString != "undefined")) {
		document.getElementById('thumbContent').innerHTML = thumbString;
	}
}

function renderVideoPlayer(divID) {
 	
	var sVid = new SWFObject("flvplayer.swf", "flvplayer", 345, 213, "8");
	sVid.addParam("allowfullscreen", "true");
	sVid.addParam("allowScriptAccess", "sameDomain");
	sVid.addParam("play", "true");
	sVid.addParam("loop", "true");
	sVid.addParam("menu", "true");
	sVid.addParam("quality", "high");	
	sVid.addParam("scale", "exactfit");
	sVid.addParam("align", "middle");
	sVid.addParam("wmode", "window");
	sVid.addParam("bgcolor", "#000000");
	sVid.addParam("flashvars", "myFlashVar=" + flvPath);
	sVid.write(divID);
}
