
function VapautumassaLoader(sId, sSource, sLang) {
	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       = 1;
	this.m_sLang           = (sLang != null && sLang.length > 0) ? sLang : "fi";
	this.CreateRSSFeed();
	this.GetXML();

	var oRSSFeed = this;
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", function() { oRSSFeed.GetXML(); }, false)
	} else if (navigator.appName.indexOf("Microsoft") != -1) {
		document.write("<script type=\"text/javascript\" id=\"CLT_" + this.m_sId + "\" defer=\"defer\" src=\"/scripts/empty.js\"><\/script>");
		var contentloadtag = document.getElementById("CLT_" + this.m_sId)
		contentloadtag.onreadystatechange = function() {
			if (this.readyState == "complete" || this.readyState == "loaded") {
				oRSSFeed.GetXML();
			}
		}
	} else if (/WebKit/i.test(navigator.userAgent)) {
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(_timer)
				oRSSFeed.GetXML();
			}
		}, 10);
	}
}

VapautumassaLoader.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) {}
		}
	}
}

VapautumassaLoader.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 ("https:" == document.location.protocol)
				this.m_oRSSFeedUrl = this.m_oRSSFeedUrl.replace("http://", "https://");				
			this.m_oPlaceholder.innerHTML = "<img src=\"/images/loading.gif\" alt=\"Ladataan\"/>";
			this.m_oRSSFeed.onreadystatechange = function() { oRSSFeed.Initialize(); }
			this.m_oRSSFeed.open("GET", this.m_oRSSFeedUrl, true);
			this.m_oRSSFeed.send(null);
		}
	}
}

VapautumassaLoader.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("kohde");
			if (this.m_aItems.length == 0) { //If no itema are found in returned content
				var AErrors = sXML.getElementsByTagName("error");
				if (AErrors.length == 0)
					this.m_oPlaceholder.innerHTML += "<div class='errorS'>Ei tietoja</div>";
				else
					this.m_oPlaceholder.innerHTML += "<div class='errorS'>Virhe!</div>";
				return;
			}
			this.m_oPlaceholder.innerHTML = "";
			this.DisplayItemList();
			if(this.m_oPlaceholder.innerHTML.length == 0)
				this.m_oPlaceholder.innerHTML += "<div>Virhe</div>";
		} else { // If there was an error while requesting content
			this.m_oPlaceholder.innerHTML = "<div class='error'>Virhe!</div>";
		}
	}
}

VapautumassaLoader.prototype.GetChildValueByName = function(oParent, sChildName) {
	var sValue  = "";
	var oAChild = oParent.getElementsByTagName(sChildName)
	if (oAChild && oAChild.length > 0)
		sValue = (oAChild[0].firstChild) ? oAChild[0].firstChild.nodeValue : oAChild[0].nodeValue;
	return sValue;
}

VapautumassaLoader.prototype.DisplayItemList = function() {
	var sItemList = "";
	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 bVuokra = (this.m_aItems[i].attributes.length > 0 && this.m_aItems[i].attributes[0].name == "vuokra");
		var bVapaa = (this.m_aItems[i].attributes.length > 0 && this.m_aItems[i].attributes[0].name == "vapaa");
		var sId = this.GetChildValueByName(this.m_aItems[i], "id");
		var sHId = this.GetChildValueByName(this.m_aItems[i], "hid");
		var oAImages = this.m_aItems[i].getElementsByTagName("image");
		var sImage = this.GetChildValueByName(this.m_aItems[i], "markkinointikuva");
		var sValmVuosi = this.GetChildValueByName(this.m_aItems[i], "valmistumisvuosi");

		if (sImage && sImage.length == 0 && oAImages && oAImages.length > 0)
			sImage = "/images/kohdekuvat/?item=" + oAImages[0].firstChild.nodeValue + "&width=400";
		else
			sImage = "/images/kohdekuvat/?item=" + sImage + "&width=400";

		if (sImage.length > 0) {
			var oImago = document.getElementById("vapautumassa_imago");
			var oImagoHdr = document.getElementById("vapautumassa_header");
			if (oImago && oImagoHdr) {
				oImago.style.backgroundImage = "url(" + sImage + ")";
				//oImagoHdr.src                = "/images/imagobanner/?text=" + this.GetChildValueByName(this.m_aItems[i], "ilmoituspvm");
				oImagoHdr.src = "/images/imagobanner/?text=" + this.GetChildValueByName(this.m_aItems[i], "vapautumispvm");
				oImagoHdr.style.display = "block"
			}
		}

		sItemList += "<p style=\"margin-bottom: 0px;\"><strong>" + this.GetChildValueByName(this.m_aItems[i], "kaupunki") + ", " + this.GetChildValueByName(this.m_aItems[i], "alue") + ", " + this.GetChildValueByName(this.m_aItems[i], "osoite") + "</strong></p>";
		sItemList += "<p style=\"margin-top: 0px;\"><strong>" + this.GetChildValueByName(this.m_aItems[i], "rakennustyyppi") + ", " + this.GetChildValueByName(this.m_aItems[i], "huoneistotyyppi") + ", " + this.GetChildValueByName(this.m_aItems[i], "pintaala") + " m<sup>2</sup></strong></p>";
		//sItemList += "<h3 style=\"margin-top: 0px;\">" + this.GetChildValueByName(this.m_aItems[i], "rakennustyyppi") + ((sValmVuosi != null && sValmVuosi.length > 0 && sValmVuosi != "null") ? ", valmistumisvuosi " + sValmVuosi : "") + "</h3>";

		sItemList += "<ul class=\"links\">";
		sItemList += "<li><a href=\"/asunnot/asuntohaku/?id=" + sId + ((sHId.length > 0) ? "&hid=" + sHId : "") + ((bVuokra) ? "&v=1" : "") + "\">";
		switch (this.m_sLang) {
			case "en":
				sItemList += "Read more";
				break;
			case "sv":
				sItemList += "Läs mer";
				break;
			default:
				sItemList += "Lue lisää";
				break;
		}
		sItemList += "</a></li>";
		//sItemList += "<li class=\"double\"><a href=\"/asunnot/asuntohaku/\">Kaikki vapaat asunnot</a></li>";
		sItemList += "</ul>";
		sItemList += "<p><a href=\"/asunnot/asuntohaku/\"><img src=\"/images/Vapaat-ja-vapautumassa.jpg\" alt=\"Vapaat ja vapautumassa olevat asunnot\"></a></p>";

	}
	this.m_oPlaceholder.innerHTML += sItemList;
}
