﻿///<summary>
///Loads the rss from blog.bgmaps.com and reads the content for category новости
///</summary>

var BlogRSSReaderControl = Class.create({
	initialize: function(options) {
		this.options = Object.extend({
			container: null,
			gateway: "BlogRSSReader",
			maxCount: 3
		}, options || {});

		if (!$(this.options.container))
			throw new MissingDOMElement("Missing container for the Recent panel");

		this.readRSS();
	},
	readRSS: function() {
		new Ajax.Request(this.options.gateway, {
			method: "GET",
			onComplete: function(req) {
				this.writeContent(req.responseXML);
			} .bind(this)
		});
	},
	writeContent: function(xml) {
		var items = this.parseItems(xml.getElementsByTagName("item"));
		this.fillContainer(items);
	},
	fillContainer: function(items) {
		for (var i = 0; i < items.length; i++)
			this.fillItem(items[i]);
	},
	fillItem: function(item) {
		newElement("a", newElement("div", this.options.container), { href: item.url, target: "_blank" }).appendChild(document.createTextNode(item.title));
	},
	parseItems: function(list) {
		var items = [];
		var maxCount = Math.min(this.options.maxCount, list.length);
		for (var i = 0; i < maxCount; i++)
			items.push(this.parseItem(list[i]));

		return items;
	},
	parseItem: function(xmlitem) {
		return {
			title: xmlitem.getElementsByTagName("title")[0].childNodes[0].nodeValue,
			url: xmlitem.getElementsByTagName("link")[0].childNodes[0].nodeValue
		};
	}

});

generateExceptions("MissingDOMElement");
