



function RSS_Feeder(url) {
	if(!url || typeof url!="string" || url.toLowerCase().indexOf("http")<0) {
		if(typeof console != "undefined") console.log("Bad URL: "+url);
		return null;
	}
	
	this.url = encodeURIComponent(url);
	//proxy to avoid xhr cross-domain security issues
	this.proxy = "/proxy.cfm";
	this.proxyParam = "url";
	this.onXMLLoad = null;
	this.jsObj = null;
	this.xml = null;
	
	return this;
}

RSS_Feeder.prototype.elem = function(tag, attribs, text) { // string, object, string
	var el = null;
	var iebug = false;
	if(tag.toLowerCase()=='img' && typeof attribs.src != 'undefined')
		try {el = document.createElement("<img src='"+attribs.src+"'>"); iebug=true; }
		catch(e) { el = document.createElement(tag); }
	else el = document.createElement(tag);
	if(!el) return null;
	
	if(attribs) for(var i in attribs) {
		if(iebug && i.toLowerCase()=='src') continue;
		if(i.toLowerCase()=="class") el.className = attribs[i];
		else el.setAttribute(i, attribs[i]);
	}
	if(text) el.innerHTML = text;
	return el;
};

RSS_Feeder.prototype.buildXHR = function() {
    try { return new XMLHttpRequest(); }
	catch(e) {
	    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e2) {
		    try { return new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e3) {
		        alert("Your browser doesn't support ajax.");
                return null;
		    }
	    }
    }
};

RSS_Feeder.prototype.buildXML = function(str) {
	var xml = null;
	if (window.DOMParser) {
	  xml=new DOMParser();
	  xml = xml.parseFromString(str, "text/xml");
	} else {
	  xml=new ActiveXObject("Microsoft.XMLDOM");
	  xml.async="false";
	  xml.loadXML(str);
	}
	
	return xml;
};

RSS_Feeder.prototype.getXML = function() {
	var xhr = this.buildXHR();
	var url = window.location.protocol+"//"+window.location.host+this.proxy+"?"+this.proxyParam+"="+this.url;
	
	xhr.master = this;
	xhr.onreadystatechange = function() {
		if(this.readyState==4) {
			this.master.xml = this.master.buildXML(this.responseText);
			this.master.jsObj = XML2js(this.master.xml);
			if(this.master.onXMLLoad) this.master.onXMLLoad();
		}
	};
	
	xhr.open("GET", url);
	xhr.send(null);
};

function XML2js(node) {
	var obj = {};
	var childs = node.childNodes || [];
	var len = childs.length;
	
	for(var i=0; i<len; i++) {
		var c = childs[i];
		if(c.nodeType==3 || c.nodeType==4) {
			var txt = c.textContent || c.text;
			if(obj["$__text"]) {
				if(typeof obj["$__text"] == "object" && obj["$__text"].length) obj["$__text"].push(txt);
				else {
					// create the array and push our txt
					var tmp = obj["$__text"];
					obj["$__text"] = [];
					obj["$__text"].push(tmp);
					obj["$__text"].push(txt);
				}
			} else obj["$__text"] = txt;
		} else if(c.childNodes && c.childNodes.length) {
			var data = XML2js(c);
			if(obj[c.tagName]) {
				if(typeof obj[c.tagName] == "object" && obj[c.tagName].length) obj[c.tagName].push(data);
				else {
					// create the array and push our data
					var tmp = obj[c.tagName];
					obj[c.tagName] = [];
					obj[c.tagName].push(tmp);
					obj[c.tagName].push(data);
				}
			} else obj[c.tagName] = data;
		}
	}
	
	if(obj["$__text"]) {
		if(typeof obj["$__text"] == "string" && len == 1) obj = obj["$__text"];
		else if(obj["$__text"].length == len && obj["$__text"].join) obj = obj["$__text"].join("");
		else if(obj["$__text"].join) obj["$__text"] = obj["$__text"].join("");
	}
	return obj;
}


