
function RSSObject(sId, sSource) {
	this.m_sId             = sId;
	this.m_oPlaceholder    = document.getElementById(sId);
	this.m_oRSSFeedUrl     = sSource;
	this.m_oRSSFeed        = null;
	this.m_oWaitTimer      = null;	
	this.m_iWaitTimerPhase = 0;	
	this.m_aItems          = null;	
	this.m_iItemIndex      = 0;		
	this.m_bShowChnlTitle  = false;	// Show channel title (with link if specified)?
	this.m_bShowChnlDesc   = false;	// Show channel description?
	this.m_bShowImage      = false;	// Show content provider logo/image if available?
	this.m_bShowDate       = true;	// Show date?
	this.m_bShowTime       = false;	// Show time?
	this.m_bShowItemDesc   = false;	// Show item description if available?
	this.m_bPopupLinks     = true;	// Open links to a new window?
	this.m_iMaxItems       = 5;		// Number of items to show in the listing (0 = all)
	this.m_iTitleMaxLength = 50;	// Maximum item title length (0 = don't clip)
	this.m_iDescMaxLength  = 50;	// Maximum item description length (0 = don't clip)
	this.CreateRSSFeed();
	this.GetXML();
}

RSSObject.prototype.CreateRSSFeed = function() {
	if(window.XMLHttpRequest){ // Mozilla, Safari, Opera
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) {}

		this.m_oRSSFeed = new XMLHttpRequest();
		if(this.m_oRSSFeed.overrideMimeType)
			this.m_oRSSFeed.overrideMimeType('text/xml');
	} else if(window.ActiveXObject) { // IE
		try {
			this.m_oRSSFeed  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.m_oRSSFeed = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
}

RSSObject.prototype.GetXML = function() {
	if(this.m_oRSSFeed){
		if(!this.m_oPlaceholder) {
			document.write("<div id='" + this.m_sId + "' class='rss'></div>");
			this.m_oPlaceholder = document.getElementById(this.m_sId);
		}
		if(this.m_oPlaceholder) {
			var oRSSFeed = this
			this.m_oPlaceholder.innerHTML = "<div class='message'>Haetaan RSS-syöte<span id='" + this.m_sId + "_Timer'></span></div>";
			this.m_oWaitTimer = setInterval(function(){oRSSFeed.DisplayTimer();}, 150);
			this.m_oRSSFeed.onreadystatechange = function() { oRSSFeed.Initialize(); }
			this.m_oRSSFeed.open("GET", this.m_oRSSFeedUrl, true);
			this.m_oRSSFeed.send(null);
		}
	}
}

RSSObject.prototype.DisplayTimer = function() {
	var oTimer = document.getElementById(this.m_sId + "_Timer");
	if(oTimer) {
		oTimer.innerHTML += ">";
		if(oTimer.innerText.length > 3)
			oTimer.innerHTML = "";
		return;
	}
	clearInterval(this.m_oWaitTimer);
}

RSSObject.prototype.Initialize = function() {
	if (this.m_oRSSFeed.readyState == 4) { // If request of content completed
		if (this.m_oRSSFeed.status == 200) { // If request was successful
			var sXML = this.m_oRSSFeed.responseXML;
			this.m_aItems = sXML.getElementsByTagName("item");
			if(this.m_aItems.length == 0) { //If no <item> elements found in returned content
				this.m_oPlaceholder.innerHTML = "<div class='error'>Ei sisältöä!</div>";
				//return;
			}
			this.m_oPlaceholder.innerHTML = "";
			// Show channeltitle
			if(this.m_bShowChnlTitle) {
				var aTitles = sXML.getElementsByTagName("title");
				var aLinks  = sXML.getElementsByTagName("link");
				if(aTitles.length > 0 && aTitles[0].firstChild) {
					this.m_oPlaceholder.innerHTML += "";
					if(aLinks.length > 0 && aLinks[0].firstChild)
						this.m_oPlaceholder.innerHTML += "<div class='header'><a href='" + aLinks[0].firstChild.nodeValue + "' target='" + ((this.m_bPopupLinks) ? "_blank" : "") + "' title='" + aTitles[0].firstChild.nodeValue + "'>" + aTitles[0].firstChild.nodeValue + "<a></div>";
					else
						this.m_oPlaceholder.innerHTML += "<div class='header'>" + aTitles[0].firstChild.nodeValue + "</div>";
				}
			}
			// Show channel description
			if(this.m_bShowChnlDesc) {
				var aChnlDescs = sXML.getElementsByTagName("description");
				if(aChnlDescs.length > 0)
					this.m_oPlaceholder.innerHTML += "<div class='description'>" + aChnlDescs[0].firstChild.nodeValue + "</div>";
			}
			// Show content provider logo/image
			if(this.m_bShowImage) {
				var aImages = sXML.getElementsByTagName("image");
				if(aImages.length > 0) {
					var aURLs = aImages[0].getElementsByTagName("url");
					if(aURLs.length > 0 && aURLs[0].firstChild) {
						var aLinks  = aImages[0].getElementsByTagName("link");
						var aDescs  = aImages[0].getElementsByTagName("description");
						var aTitles = aImages[0].getElementsByTagName("title");
						var sLink   = (aLinks.length > 0 &&  aLinks[0].firstChild) ? aLinks[0].firstChild.nodeValue : "";
						var sDesc   = (aDescs.length > 0 && aDescs[0].firstChild) ? aDescs[0].firstChild.nodeValue : "";
						var sTitle  = (aTitles.length > 0 && aTitles[0].firstChild) ? aTitles[0].firstChild.nodeValue : "";
						if(sLink && sLink != "")
							this.m_oPlaceholder.innerHTML += "<div class='image'><a href='" + sLink + "' target='" + ((this.m_bPopupLinks) ? "_blank" : "") + "' title='" + sDesc + "'/><img src='" + aURLs[0].firstChild.nodeValue + "' border='0' alt='" + sTitle + "' /></a></div>";
						else
							this.m_oPlaceholder.innerHTML += "<div class='image'><img src='" + aURLs[0].firstChild.nodeValue + "' border='0' alt='" + sTitle + "' /></div>";
					}
				}
			}
			this.DisplayItemList();
			this.m_oPlaceholder.innerHTML += "";
		} else { // If there was an error while requesting content
			this.m_oPlaceholder.innerHTML = "<div class='error'>Virhe RSS-syötteen haussa! " + this.m_oRSSFeedUrl + " > " + this.m_oRSSFeed.responseText + "</div>";
			//this.m_oPlaceholder.innerHTML = "<div class='error'>Virhe RSS-syötteen haussa!</div>";
		}
	}
}

RSSObject.prototype.DisplayItemList = function() {	
	var sItemList = "<table cellpadding='0' cellspacing='0' class='items'>";
	if(this.m_iMaxItems > 0 && this.m_aItems.length < this.m_iMaxItems)
		this.m_iMaxItems = this.m_aItems.length
	
	for(var i = 0; i < this.m_iMaxItems; i++){
		var aTitles = this.m_aItems[i].getElementsByTagName("title");
		var aLinks  = this.m_aItems[i].getElementsByTagName("link");
		var aDescs  = this.m_aItems[i].getElementsByTagName("description");
		var aDates  = this.m_aItems[i].getElementsByTagName("pubDate");
		if(aTitles.length > 0 && aLinks.length > 0 && (!this.m_bShowItemDesc || aDescs.length > 0) && ((!this.m_bShowDate && !this.m_bShowTime) || aDates.length > 0)) {
			sItemList += "<tr valign='top' class='item'>" + this.FormatDate(((aDates[0].firstChild) ? aDates[0].firstChild.nodeValue : "")) + "<td><div class='itemTitle'><a href='" + ((aLinks[0].firstChild) ? aLinks[0].firstChild.nodeValue : "#") + "' target='" + ((this.m_bPopupLinks) ? "_blank" : "") + "' title='" + ((aTitles[0].firstChild) ? aTitles[0].firstChild.nodeValue : "") + "'>" + this.FormatTitle(((aTitles[0].firstChild) ? aTitles[0].firstChild.nodeValue : "")) + "</a></div>" + this.FormatDesc(((aDescs[0].firstChild) ? aDescs[0].firstChild.nodeValue : "")) + "</td></tr>";
		}
	}
	sItemList += "</table>";
	this.m_oPlaceholder.innerHTML += sItemList;
}

RSSObject.prototype.FormatTitle = function(sTitle) {
	if(sTitle.length > 0) {
		if(this.m_iTitleMaxLength > 0 && sTitle.length > this.m_iTitleMaxLength - 3)
			sTitle = sTitle.substring(0, this.m_iTitleMaxLength - 3) + "...";
	}
	return sTitle;
}

RSSObject.prototype.FormatDate = function(sDate) {
	var sResult = "";
	if((this.m_bShowDate || this.m_bShowTime) && sDate.length > 0) {
		if(sDate.indexOf(" ") > 0) {
			var aDateTime = sDate.split(" ");
			sResult = "<td class='itemDate'>";
			if(this.m_bShowDate)
				sResult += aDateTime[0] + " ";
			if(this.m_bShowTime)
				sResult += aDateTime[1];
			sResult += "</td>";
		}
	}
	return  sResult;;
}

RSSObject.prototype.FormatDesc = function(sDesc) {
	if(this.m_bShowItemDesc && sDesc.length > 0) {
		if(this.m_iDescMaxLength > 0 && sDesc.length > this.m_iDescMaxLength - 3)
			sDesc = sDesc.substring(0, this.m_iDescMaxLength - 3) + "...";
		return "<div class='itemDesc'>" + sDesc + "</div>";
	}
	return "";
}
