var jsReady = false;
var req;

var hst = window.location.host;
var hostm = (hst.match(/ztemdev/)) ? "http://" + hst.replace(/.*?\./,"zcm.") + "/" : "http://" + hst.replace(/.*?\./,"m.") + "/";
var hostwww = (hst.match(/ztemdev/)) ? "http://" + hst.replace(/.*?\./,"zc.") + "/" : "http://" + hst.replace(/.*?\./,"www.") + "/";

if (navigator.platform.indexOf('Mac')==-1 && navigator.platform.indexOf('Win')==-1 && navigator.platform.indexOf('Linux')==-1) {
	//alert(hostm);
	//assume mobile???
	window.location = hostm;
}

var xDoc;
xDoc = loadXMLDoc("xml/videos.xml");

function isReady()
{
	return jsReady;
}

function pageInit()
{
	jsReady = true;
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function asPlayVideo(vid) {
	//alert(navigator.userAgent);
	if (navigator.userAgent.indexOf("IE") != -1 && navigator.userAgent.indexOf("9.") == -1) {
		thisMovie("hires").asPlayVideo(vid);
	} else {
		document.getElementById('zazavid').pause();
		var ttl = getNode(xDoc, "//hires[@id='"+vid+"']/@title").value;
		var ogv = getNode(xDoc, "//hires[@id='"+vid+"']/@ogv").value;
		var h264 = getNode(xDoc, "//hires[@id='"+vid+"']/@path").value;
		var iphone = getNode(xDoc, "//hires[@id='"+vid+"']/@iphone").value;
		//alert(ttl);
		document.getElementById('player').innerHTML = '<h3>Now Playing: ' + ttl + '</h3>\n<video id="zazavid" width="784" controls="controls" autoplay="autoplay">\n' + //height="432"
		'<source src="' + iphone + '">\n' +
		'<source src="' + ogv + '">\n' +
		'<source src="' + h264 + '">\n' +
		'</video>';
		//document.getElementById('linkvideo').play();
		//alert(document.getElementById('player').innerHTML);
	}
}

function jsAlert (str) {
	alert("" + str);
}

function loadXMLDoc(url) {
	req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	if(req) {
		//req.onreadystatechange = processReqChange; // syncronous (ajax???)
		req.open("GET", url, false); // asyncronous
		req.send("");
		lXml = req.responseXML;
		req = null;
		return lXml;
	} else {
		alert("Could not create XML object with your browser.");
		req = null;
		return -1;
	}
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function getNode(oXML, xPath) {
	// only return first node????
	if (window.ActiveXObject) {
		oXML.setProperty("SelectionLanguage","XPath");
		var nodes=oXML.selectNodes(xPath);
		var result=nodes[0];
	} else if (document.implementation && document.implementation.createDocument) {
		var nodes=oXML.evaluate(xPath, oXML, null, XPathResult.ANY_TYPE, null);
		var result=nodes.iterateNext();
	} else {
		alert('Your browser cannot handle this script');
		return -1;
	}
	return result;
}

function getMouseLoc(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	return {x: posx, y: posy};
}
