//var ventriloURL = "http://127.0.0.1/HecilAgent/ventrilo.js";
var ventriloURL = "https://www.typefrag.com/Ventrilo-Status/Status.aspx?ID=5c23ba77-c0c4-4ec5-941a-2050e79d2b22";

$(document).ready(function() {
	// Cufon font replacement
	//Cufon.now();
	Cufon.replace("#Header h1", { fontFamily: "Windsong", textShadow: "#333 2px 2px" });
	Cufon.replace("#Header h2", { fontFamily: "Windsong", textShadow: "#333 1px 1px" });
	Cufon.replace("#Content h2", { fontFamily: "Romance Fatal Serif", textShadow: "white 1px 1px" });
	Cufon.replace("#Content h3", { fontFamily: "Romance Fatal Serif", textShadow: "white 1px 1px" });

	// get Ventrilo status
	if ($("#Ventrilo").length > 0) {
		if (!getVentriloStatus()) {
			$("#Ventrilo").append("<h4>Unable to get ventrilo status</h4>");
		}
	}

	// get Codemasters LOTRO Twitter feed
	if ($("#CodemastersTwitterFeed").length > 0) {
		$("#CodemastersTwitterFeed").getTwitter({
			userName: "LOTRO_Europe",
			numTweets: 10,
			loaderText: "Loading tweets...",
			slideIn: true,
			showHeading: true,
			headingText: "LOTRO Europe",
			showProfileLink: true
		});
	}

	// add lightbox
	$(".lightbox a").attr("rel", "lightbox");

	// set lightbox images
	$("a[rel=lightbox]").lightBox({
		imageBlank:    "images/lightbox/blank.gif",
		imageLoading:  "images/lightbox/loading.gif",
		imageBtnClose: "images/lightbox/close.gif",
		imageBtnPrev:  "images/lightbox/prev.gif",
		imageBtnNext:  "images/lightbox/next.gif"
	});

	// fade gallery images on mouseover/mouseout
	$("#Gallery img").bind("mouseover", function() {
		$(this).fadeTo(500, 1);
	}).bind("mouseout", function() {
		$(this).fadeTo(500, .8);
	});
});

// bogus function to prevent error reporting
var TFVS_StatusUpdateList = { Add: function() { return null } }

// add DOMParser function if not defined (IE)
if (typeof DOMParser == "undefined") {
	DOMParser = function () {}
	DOMParser.prototype.parseFromString = function (str, contentType) {
		if (typeof ActiveXObject != "undefined") {
			var d = new ActiveXObject("MSXML.DomDocument");
			d.loadXML(str);
			return d;
		} else if (typeof XMLHttpRequest != "undefined") {
			var req = new XMLHttpRequest;
			req.open("GET", "data:" + (contentType || "application/xml") + ";charset=utf-8," + encodeURIComponent(str), false);
			if (req.overrideMimeType) {
				req.overrideMimeType(contentType);
			}
			req.send(null);
			return req.responseXML;
		}
	}
}

function getVentriloStatus() {
	$.getScript(ventriloURL, function() {
		if (TFVS_statusUpdate == null) return false;
		// convert string to XML
		xml = (new DOMParser()).parseFromString(TFVS_statusUpdate.xmlData, "text/xml");

		// make a jQuery object out of the XML
		$xml = $(xml);

		// get server info
		ventriloInfo = {
			hostname: $xml.find("TFVS").attr("N"),
			/* port: $xml.find("TFVS").attr("P"),*/
			uptime: $xml.find("TFVS").attr("UT")
		}

		// get uptime
		var uptime = new Object;
		var x, y;
		x = ventriloInfo.uptime.split(":");
		y = x[0].split(".");
		uptime = { days: y[0], hours: y[1], minutes: x[1], seconds: x[2] }

		$("#Ventrilo").append("<p>Uptime: " + uptime.days + " days, " + uptime.hours + " hours, " + uptime.minutes + " minutes and " + uptime.seconds + " seconds.</p>");
		$("#Ventrilo").append("<h4>" + ventriloInfo.hostname + "</h4>");
		$("#Ventrilo").append("<ul id=\"DefaultChannel\">");

		// get users in default channel
		$xml.find("TFVS").children("US").children("U").each(function(i) {
			$("#DefaultChannel").append("<li title=\"Online for: " + $(this).attr("TO") + ", Ping: " + $(this).attr("PI") + "ms\">" + $(this).attr("N") + "</li>");
		});

		// get channels
		$xml.find("CHS").children("CH").each(function(i) {
			// append channel heading and list element
			$("#DefaultChannel").append("<h4>" + $(this).attr("N") + "</h4>");
			$("#DefaultChannel").append("<ul id=\"channel" + i + "\">");

			// get users in this channel
			$(this).find("US").children("U").each(function() {
				$("#channel" + i).append("<li title=\"Online for: " + $(this).attr("TO") + ", Ping: " + $(this).attr("PI") + "ms\">" + $(this).attr("N") + "</li>");
			});
		});
	});

	return true;
}

