window.onload = function() {
	window.NewsFeed = new NewsFeedClass("http://commental.org/feeds/news.xml");		//http://www.xml.com/cs/xml/query/q/19
	delete NewsFeedClass;
	
	window.onCharResize = function(nw,nh,ow,oh) {
		NewsFeed.Adjust();
	};
	
	window.CharResizeDetector();
};

function el(id) { return document.getElementById(id); };

window.CharResizeDetector = function() {
	var ch = document.createElement("span");
	ch.style.visibility = "hidden";
	ch.style.padding = "0px";
	ch.style.margin = "0px";
	ch.style.border = "none";
	ch.innerHTML = "m";
	document.body.appendChild(ch);
	window.testChar = ch;
	setInterval("window.CheckCharacterDimensions();", 50);
	
	window.CheckCharacterDimensions = function(disableEvents) {
		var ch = window.testChar;
		var oh = window.charHeight;
		var ow = window.charWidth;
		window.charHeight = window.testChar.offsetHeight;
		window.charWidth = window.testChar.offsetWidth;
		if((window.charWidth!=ow || window.charHeight!=oh) && disableEvents!=true)
			window.onCharResize(window.charWidth,window.charHeight,ow,oh);
	};

	window.CheckCharacterDimensions();
};











/* News Feed: */

function NewsFeedClass(feed_url) {
	this.area = el("newsfeed_area");
	this.inner = el("newsfeed_inner");
	this.feedDisplay = this.inner.getElementsByTagName("ul")[0];
	this.title = el("newsfeed_title");
	this.titletext = el("newsfeed_titletext");
	this.feedIcon = el("newsfeed_rss_icon");
	this.refreshButton = el("newsfeed_refresh_icon");
	this.statusarea = el("newsfeed_statusarea");
	this.feed = null;
	this.status = null;
	this.busy = false;
	this.xdr = CrossDomainRequest;
	
	for(var x in this)
		if(this[x]!=null && this[x].nodeName!=null && this[x].nodeName.length>0)
			this[x].parentNewsFeed = this;
	
	this.refreshButton.onclick=function(){ this.parentNewsFeed.Refresh(); };
	this.feedIcon.onclick=function(){ this.parentNewsFeed.Subscribe(); };
	
	if(feed_url!=null)
		this.feed=feed_url;
	this.LoadFeed(feed_url);
}

NewsFeedClass.prototype.Adjust = function() {
	this.inner.style.height = ( el("footer").offsetTop-this.area.offsetTop - 40 )+"px";
};

NewsFeedClass.prototype.setTitle = function(newtitle) {
	if(newtitle==null)
		newtitle = "";
	this.titletext.innerHTML = newtitle;
};

NewsFeedClass.prototype.setStatus = function(status,message) {
	switch(status.toLowerCase()) {
		case "loading":
			this.statusarea.innerHTML = "loading...";
			this.statusarea.style.display = "block";
			this.area.className += " loading";
		break;
		case "complete":
			this.statusarea.style.display = "none";
			this.statusarea.innerHTML = "";
			this.area.className = this.area.className.substring(0,this.area.className.indexOf(" loading")) + this.area.className.substring(this.area.className.indexOf(" loading")+8, this.area.className.length);
		break;
	}
};

NewsFeedClass.prototype.Subscribe = function() {
	this.subscription_cover = document.createElement("div");
	this.subscription_cover.style.cssText = "position:absolute; display:block; width:100%; height:100%; top:0px; left:0px; cursor:pointer; background:#000000; z-index:997; opacity:0.7;"+((document.all&&parseFloat(navigator.appVersion)<7)?" filter:alpha(opacity=70);":"");
	this.subscription_cover.parentNewsFeed = this;
	this.subscription_cover.onclick = function() { this.parentNewsFeed.subscription_frame.Close(); };
	document.body.appendChild(this.subscription_cover);
	
	this.subscription_frame = document.createElement("iframe");
	this.subscription_frame.style.cssText = "position:absolute; display:block; width:90%; height:90%; top:5%; left:5%; background:#222222; border:2px solid #CC6600; z-index:998;";
	this.subscription_frame.parentNewsFeed = this;
	this.subscription_frame.Close = function() {
		//this.parentNewsFeed.Refresh();
		document.body.removeChild(this.parentNewsFeed.subscription_frame);
		document.body.removeChild(this.parentNewsFeed.subscription_closeIcon);
		document.body.removeChild(this.parentNewsFeed.subscription_cover);
	};
	this.subscription_frame.src = this.feed;
	document.body.appendChild(this.subscription_frame);
	
	this.subscription_closeIcon = document.createElement("div");
	this.subscription_closeIcon.style.cssText = "position:absolute; display:block; height:1.3em; width:1.3em; cursor:pointer; text-align:center; top:5%; right:5%; margin-top:-0.8em; margin-right:-0.8em; border:1px outset #CC2222; color:#FFFFFF; font-weight:bold; background:#880000; z-index:999;";
	this.subscription_closeIcon.innerHTML = "X";
	this.subscription_closeIcon.parentNewsFeed = this;
	this.subscription_closeIcon.onclick = function() { this.parentNewsFeed.subscription_frame.Close(); };
	document.body.appendChild(this.subscription_closeIcon);
	
	this.Refresh();
};

NewsFeedClass.prototype.Refresh = function() {
	this.LoadFeed(this.feed);
};

NewsFeedClass.prototype.LoadFeed = function(feed_url) {
	if(this.busy==true || feed_url==null || feed_url=="undefined")
		return false;
	this.busy = true;
	this.feed = feed_url;
	this.setStatus("loading");
	var data = new this.xdr( "http://commental.org/jsproxy/?url="+encodeURIComponent(this.feed.replace(/^(http|https|ftp)\:\/\//gim,'$1|[:\/\/]|'))+"&sid="+Math.floor(Math.random()*99999999) );
	data.AddEventListener("onload",function(data,request){
		request.NewsFeed.LoadFeedCallback(data);
	});
	data.NewsFeed = this;
	data.Send();
	return true;
};

NewsFeedClass.prototype.LoadFeedCallback = function(data) {
	var iHTML = "";
	var doc = this.getXML(data);
	doc = doc.getElementsByTagName("rss")[0];
	doc = doc.getElementsByTagName("channel")[0];
	for(var x in doc.getElementsByTagName("item")) {
		var node = doc.getElementsByTagName("item")[x];
		if(node.nodeName!=null && node.nodeName.toLowerCase()=="item") {
			var title = null;
			var link = null;
			var description = null;
			for(var c in node.childNodes) {
				var nn = node.childNodes[c].nodeName;
				if(nn!=null) {
					nn = nn.toLowerCase();
					if(nn=="title")
						title = node.childNodes[c].data;
					else if(nn=="link")
						link = node.childNodes[c].nodeValue;
					else if(nn=="description")
						description = node.childNodes[c].innerHTML;
				}
			}
			iHTML += "<li>"+description.substring(0,24)+"...</li>";
		}
	}
	this.feedDisplay.innerHTML = iHTML;
	for(var x in this.feedDisplay.getElementsByTagName("li")) {
		var li = this.feedDisplay.getElementsByTagName("li")[x];
		li.onmouseover = function(){ this.className += " hover"; };
		li.onmouseout = function(){ this.className = this.className.substring(0,this.className.indexOf(" hover")) + this.className.substring(this.className.indexOf(" hover")+6, this.className.length); };
	}
	this.setStatus("complete");
	this.busy = false;
};

NewsFeedClass.prototype.getXML = function(text) {
	var frame = document.createElement("iframe");
	frame.style.visibility = "hidden";
	frame.style.border = "none";
	frame.style.height = "0px";
	frame.style.width = "0px";
	document.body.appendChild(frame);
	var doc = frame.contentDocument?frame.contentDocument:frame.document;

	doc.open();
	doc.write("<meta name=\"Content-type\" description=\"application/xml\" />\n"+text);
	doc.close();

	var xml = frame.contentWindow.document;
	document.body.removeChild(frame);
	return xml;
};























window.xdr_onloadold = window.onload ? window.onload : function(){};
window.onload = function() {
	if(window.location.href.lastIndexOf("#")!=window.location.href.length-1)
		window.location.href = window.location.href+"#";
	if(window.location.href.lastIndexOf("##")!=window.location.href.length-2)
		window.location.href = window.location.href+"#";
	window.xdr_onloadold();
};

function CrossDomainRequest(url,onload,onerror,maxattempts) {
	if(window._CrossDomainRequests==null)
		window._CrossDomainRequests = new Array();
	window._CrossDomainRequests.push(this);
	this.url = url;
	this.onload = new Array();
	this.onerror = new Array();

	this.xdrid = Math.floor(Math.random()*99999999999);
	
	this.maxattempts = maxattempts!=null?maxattempts:2;
	this.attempts = 0;
	
	if(onload!=null)
		this.AddEventListener("onload",onload);
	if(onerror!=null)
		this.AddEventListener("onerror",onerror);
}

CrossDomainRequest.prototype.Send = function() {
	this.attempts++;
	this.request_tail = "&__xdrid="+this.xdrid+"&__xdr_self="+encodeURIComponent(window.location.href.replace(/^(http|https|ftp)\:\/\//gim,'$1|[:\/\/]|'));
	this.frame = document.createElement("iframe");
	this.frame.id = "CrossDomainRequest_frame_"+Math.floor(Math.random()*9999999);
	this.frame.setAttribute("id","CrossDomainRequest_frame_"+Math.floor(Math.random()*9999999));
	this.frame.style.cssText = "visibility:hidden; height:0px; width:0px; border:none; background:none;";
	this.frame.style.visibility = "hidden";
	this.frame.style.height = "0px";
	this.frame.style.width = "0px";
	this.frame.style.border = "none";
	this.frame.req = this;
	this.frame.xdrid = this.xdrid;
	this.frame.request_tail = this.request_tail;
	document.body.appendChild(this.frame);
	this.frame.frame_onloadevent = function() {
		var rval = null;
		var loc = window.location.href;
		var start = loc.indexOf("#__xdr_"+this.xdrid+"=");
		var startlen = ("#__xdr_"+this.xdrid+"=").length;
		if(start > -1) {
			var end = loc.indexOf("#__xdr_",start+startlen);
			if(end <= start+startlen)
				end = loc.length;
			rval = unescape(loc.substring(start+startlen, end));
			window.location.href = loc.substring(0, loc.indexOf("#__xdr_"+this.xdrid+"=")) + loc.substring(end, loc.length)+"#";
		}
		else if(this.req.attempts <= this.req.maxattempts) {
			setTimeout("document.body.removeChild(document.getElementById('"+this.id+"'));", 1);
			this.req.Send();
			return false;
		}
		if(rval!=null && rval.substring(rval.length-1,rval.length)=="#")
			rval = rval.substring(0,rval.length-1);
		if(rval!=null)
			for(var x in this.req.onload)
				this.req.onload[x](rval,this.req);
		else
			for(var x in this.req.onerror)
				this.req.onerror[x]("error while requesting "+this.req.url , this.req);
		setTimeout("document.body.removeChild(document.getElementById('"+this.id+"'));", 1);
		this.req.Destroy();
		var loc = window.location.href;
		if(loc.indexOf("#__xdr") > -1 && window._CrossDomainRequests.length==0)
			window.location.href = loc.substring(0,loc.indexOf("#__xdr"))+"#";
	};
	//this.frame.onload = function(){ CrossDomainRequest_IELoad(this); };
	this.frame.waitforload = setInterval("if(window.location.href.indexOf(\"#__xdr_"+this.xdrid+"=\")>-1) CrossDomainRequest_IELoad(document.getElementById('"+this.frame.id+"'));", 10);
	this.frame.onerror = function() {
		throw("Request Failed. "+this.req.url);
		this.req.Destroy();
	};
	this.frame.src = this.url+this.request_tail;
};

CrossDomainRequest_IELoad = function(iframe) {
	if(iframe.waitforload!=null) {
		clearInterval(iframe.waitforload);
		iframe.waitforload = null;
		setTimeout("document.getElementById('"+iframe.id+"').frame_onloadevent();", 60);
	}
};

CrossDomainRequest.prototype.Destroy = function() {
	setTimeout("if(document.getElementById('"+this.frame.id+"')) document.body.removeChild(document.getElementById('"+this.frame.id+"'));", 1);
	var a = new Array();
	for(var x in window._CrossDomainRequests)
		if(window._CrossDomainRequests[x]!=this)
			a[x] = window._CrossDomainRequests[x];
	window._CrossDomainRequests = a;
	delete a;
	try{
		if(!(document.all && parseFloat(navigator.appVersion)<7))
			delete this;
	} catch(err){ }
};

CrossDomainRequest.prototype.AddEventListener = function(event,func) {
	var arr;
	if(event=="load" || event=="onload")
		arr = this.onload;
	else if(event=="error" || event=="onerror")
		arr = this.onerror;
	var ind = arr.length;
	arr[ind] = func;
	return ind;
};

CrossDomainRequest.prototype.RemoveEventListener = function(event,index,func) {
	var rval = false;
	var a=new Array();
	if(event=="load" || event=="onload") {
		for(var x in this.onload)
			if(x!=index && this.onload[x]!=func)
				a[x] = this.onload[x];
		rval = this.onload.length!=a.length;
		this.onload = a;
	}
	else if(event=="error" || event=="onerror") {
		for(var x in this.onerror)
			if(x!=index && this.onerror[x]!=func)
				a[x] = this.onerror[x];
		rval = this.onerror.length!=a.length;
		this.onerror = a;
	}
	delete a;
	return rval;
};





