
function NewsLoader(sId, sSource, iMaxItems, bShowDesc, bAdaptive, iMaxChars) {
	this.m_sId             = sId;
	this.m_oPlaceholder    = document.getElementById(sId);
	this.m_oRSSFeedUrl     = sSource;
	this.m_oRSSFeed        = null;
	this.m_aItems          = null;
	this.m_iItemIndex      = 0;
	this.m_iMaxItems       = (iMaxItems >= 0) ? iMaxItems : 0;
	this.m_iMaxChars       = 150;
	this.m_bIsList         = (bShowDesc);
	this.m_bIsAdaptive     = (bAdaptive);
	this.CreateRSSFeed();
	this.GetXML();
}

NewsLoader.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) {}
		}
	}
}

NewsLoader.prototype.GetXML = function() {
	if(this.m_oRSSFeed){
		if(!this.m_oPlaceholder) {
			document.write("<div id='" + this.m_sId + "'></div>");
			this.m_oPlaceholder = document.getElementById(this.m_sId);
		}
		if(this.m_oPlaceholder) {
			var oRSSFeed = this
			if (this.m_bIsList)
				this.m_oPlaceholder.innerHTML = "<span class=\"loader\">Haetaan uutisia...<br/><img src=\"/images/bar90.gif\" alt=\"Haetaan uutisia\"/></span>";
			else
				this.m_oPlaceholder.innerHTML = "Haetaan uutisia...";
			this.m_oRSSFeed.onreadystatechange = function() { oRSSFeed.Initialize(); }
			this.m_oRSSFeed.open("GET", "/usercontrols/newsloader.aspx?id=" + this.m_sId + "&url=" + this.m_oRSSFeedUrl, true);
			this.m_oRSSFeed.send(null);
		}
	}
}

NewsLoader.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 itema are found in returned content
				this.m_oPlaceholder.innerHTML = "<p class='note'>Ei tiedotteita</p>";
				return;
			}
			this.m_oPlaceholder.innerHTML = "";
			this.DisplayItemList();
			this.m_oPlaceholder.innerHTML += "";
		} else { // If there was an error while requesting content
			this.m_oPlaceholder.innerHTML = "<p class='errorS'>Virhe tiedotteiden haussa</div>";
		}
	}
}

NewsLoader.prototype.GetChildValueByName = function(oParent, sChildName, iMaxLength) {
	var sValue = "";
	var oAChild = oParent.getElementsByTagName(sChildName)
	if (oAChild && oAChild.length > 0)
		sValue = (oAChild[0].firstChild) ? oAChild[0].firstChild.nodeValue : oAChild[0].nodeValue;
	if (iMaxLength > 0 && sValue.length > iMaxLength - 3)
		sValue = sValue.substr(0, iMaxLength - 1) + "...";
	return sValue;
}

NewsLoader.prototype.ConvertToDate = function(sDate) {
	try {
		var sADate = sDate.split(" ");
		//if (sADate.length == 4) {
			var oDate = new Date(Date.parse(sADate[2] + " " + sADate[1] + ", " + sADate[3]));
			return oDate.getDate() + "." + (oDate.getMonth() + 1) + ".";
			//return oDate.getFullYear();
		//}
	} catch (e) { }
	return sDate;
}

NewsLoader.prototype.GetQueryParameterValue = function(sPara) {
	var sValue = "";
	var sUrl = parent.document.URL;
	var iIndex = sUrl.indexOf(sPara + "=");
	if (iIndex > 0) {
		var sUrlPart       = sUrl.slice(iIndex + sPara.length + 1);
		var iNextParaIndex = sUrlPart.indexOf("&");
		sValue = (iNextParaIndex > 0) ? sUrlPart.substr(0, iNextParaIndex) : sUrlPart;
	}
	return sValue;
}

NewsLoader.prototype.DisplayItemList = function() {
	var sItemList = "";
	var sReqId = this.GetQueryParameterValue("nid");
	if (this.m_iMaxItems == 0 || (this.m_iMaxItems > 0 && this.m_aItems.length < this.m_iMaxItems))
		this.m_iMaxItems = this.m_aItems.length
	sItemList += "<ul class=\"" + ((this.m_bIsList) ? "list" : "rss") + "\">";
	var iChars = 0;
	for (var i = 0; i < this.m_iMaxItems; i++) {
		var sLinkTxt = this.GetChildValueByName(this.m_aItems[i], "title");
		var sLinkUrl = this.GetChildValueByName(this.m_aItems[i], "link");
		iChars += sLinkTxt.length;
		if(!this.m_bIsAdaptive || this.m_iMaxChars == 0 || iChars < this.m_iMaxChars) {
			//sItemList += "<li><a href=\"" + sLinkUrl + "\" target=\"_blank\" class=\"title\">" + this.GetChildValueByName(this.m_aItems[i], "title") + " <span class=\"date\">" + this.ConvertToDate(this.GetChildValueByName(this.m_aItems[i], "pubDate")) + "</span></a>";
			sItemList += "<li><a href=\"" + sLinkUrl + "\" target=\"_blank\" class=\"title\">" + this.GetChildValueByName(this.m_aItems[i], "title") + "</span></a>";
			if (this.m_bIsList)
				sItemList += "<div class=\"desc\">" + this.GetChildValueByName(this.m_aItems[i], "description", 50) + " <a href=\"" + sLinkUrl + "\" target=\"_blank\" class=\"more\">Lue lisää</a></div>";
			sItemList += "</li>";
		} else {
			break;
		}
	}
	sItemList += "</ul>";
	this.m_oPlaceholder.innerHTML += sItemList;
}
