//Twitter Site Widget
//(c) 2009 - Grant Galitz
//Modified for American Yoga
function Twitter(username, element, url) {
	this.sElement = element;
	this.sUser = username;
	this.sName = username;
	this.sImg = "";
	this.sFollowing = "";
	this.sTime = "";
	this.sUpdated = "";
	this.sText = "";
	this.sURL = url;
	this.oHandle = null;
	this.nLastPost = 0;
	this.aText = new Array();
	this.oDOM = null;
	this.oTime = new Date();
	this.startTime = this.oTime.getTime();
	this.sanityCheck();
}
Twitter.prototype.doSynchronousQuery = function () {
	this.oHandle.open("GET", this.sURL + "?t=" + this.startTime, false);
	this.oHandle.send(null);
}
Twitter.prototype.sanityCheck = function () {
	if (typeof document.getElementById(this.sElement) != "undefined") {
		if (this.checkXMLHttpRequest()) {
			if (this.oHandle != null) {
				this.doSynchronousQuery();
				this.doConvertText();
			}
		}
	}
	else {
		alert("Ajax Error: The (x)html document is missing an id to match to.");
	}
}
Twitter.prototype.checkXMLHttpRequest = function () {
	if (typeof window.XMLHttpRequest != "undefined") {
		this.oHandle = new window.XMLHttpRequest;
	}
	else {
		//Fallback, if IE7+ has native xmlhttprequest support turned off.
		try {
			this.oHandle = new ActiveXObject("Msxml2.XMLHTTP.6.0");
		}
		catch (error) {
			try {
				this.oHandle = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			}
			catch (error) {
				try {
					this.oHandle = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (error) {
					try {
						//Should never get to this, unless browser is something like IE5.
						this.oHandle = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (error) {	//Should only fail for really old browsers (Some Pre-2006).
						this.errorMessage("Ajax isn\'t supported on your system (\"" + error.message + "\") for the data in ");
						return false;
					}
				}
			}
		}
	}
	return true;
}
Twitter.prototype.errorMessage = function (msg) {
	var error = document.createElement("span");
	TextNode(error, msg + " \"" + this.sURL + "\".");
	document.getElementById(this.sElement).appendChild(error);
}
Twitter.prototype.findValue = function (elementName, oDom) {
	if (oDom.getElementsByTagName(elementName) != null) {
		if (typeof oDom.getElementsByTagName(elementName)[0] != "undefined") {
			if (typeof oDom.getElementsByTagName(elementName)[0].firstChild != "undefined") {
				if (typeof oDom.getElementsByTagName(elementName)[0].firstChild.data != "undefined") {
					return oDom.getElementsByTagName(elementName)[0].firstChild.data;
				}
			}
		}
	}
	return "";
}
Twitter.prototype.nonEmpty = function (string) {
	if (typeof string != "undefined") {
		if (string.length > 0) {
			return true;
		}
	}
	return false;
}
Twitter.prototype.doConvertText = function () {
	if (this.oHandle.responseXML != null) {
		//Use some of these properties later if required...
		this.oDOM = this.oHandle.responseXML;
		this.sUser = this.findValue("screen_name", this.oDOM.documentElement);
		this.sName = this.findValue("name", this.oDOM.documentElement);
		this.sImg =  this.findValue("profile_image_url", this.oDOM.documentElement);
		this.sFollowing = this.findValue("followers_count", this.oDOM.documentElement);
		if (this.nonEmpty(this.sUser) && this.nonEmpty(this.sName) && this.nonEmpty(this.sImg) && this.nonEmpty(this.sFollowing)) {
			var aStatus = this.oDOM.documentElement.getElementsByTagName("status");
			if (aStatus.length == 1) {
				this.sTime = this.findValue("created_at", aStatus[0]);
				this.sText = this.findValue("text", aStatus[0]);
				if (this.nonEmpty(this.sTime) && this.nonEmpty(this.sText)) {
					this.nLastPost = Date.parse(this.sTime);
					if (this.nLastPost > 0) {
						this.oTime.setTime(this.nLastPost);
					}
					this.renderTwitter();
				}
				else {
					this.errorMessage("One or more child element values in the status element sub-tree were found to be empty in ");
				}
			}
			else {
				this.errorMessage("The twitter status element could not be found in ");
			}
		}
		else {
			this.errorMessage("One or more values were found to be empty in ");
		}
	}
	else {
		this.errorMessage("Could not fetch data from ");
	}
}
Twitter.prototype.renderTwitter = function () {
	var twitterBox = document.createElement("div");
	twitterBox.id = "twitter_wrapper";
	var middle = document.createElement("div");
	middle.id = "twitter_text_wrap";
	twitterStatus = document.createElement("a");
	twitterStatus.setAttribute("href", "http://www.twitter.com/" + this.sUser);
	twitterStatus.setAttribute("target", "_blank");
	TextNode(twitterStatus, this.sText);
	middle.appendChild(twitterStatus);
	twitterBox.appendChild(middle);
	var last = document.createElement("div");
	last.id = "twitter_box_bottom";
	img = document.createElement("div");
	img.setAttribute("id", "twitter_icon");
	last.appendChild(img);
	twitterBox.appendChild(last);
	var clear = document.createElement("div");
	clear.id = "clear";
	twitterBox.appendChild(clear);
	document.getElementById(this.sElement).appendChild(twitterBox);
}