// -*- mode: javascript; coding: utf-8 -*-

var article = {
	OTHER_MAX_ITEMS: 6,

	otherRender: function (xml) {

		var args = article.args;		

		var sectCode = args.article.substr (args.article.length - 3);
		var items = xml.getElementsByTagName ("Item");
		var idx = [];
		var pos;
		for (var i = 0, item; item = items[i]; i++) {
			var id = item.getAttribute ("id");
			if (id == args.article)
				pos = idx.length;
			if (args.section == "opinion") {
				if (id.substr (3, 1) == "o" || 
				    id.substr (3, 1) == "a")
					idx.push (i);
			} else if (id.substr (id.length - 3) == sectCode)
				idx.push (i);
		}
		var start = pos - (article.OTHER_MAX_ITEMS / 2) + 1;
		if (start + article.OTHER_MAX_ITEMS >= idx.length)
			start = idx.length - article.OTHER_MAX_ITEMS;
		if (start < 0)
			start = 0;
		start = idx[start];

		document.getElementById ("other-articles").innerHTML = 
			sumario.renderSectTitle (args.section, "Otras notas de " + sumario.SECT_LABELS[args.section]) +
			sumario.renderSectItems (xml, args.section, start, article.OTHER_MAX_ITEMS, 
						 (args.section == "opinion")? new RegExp ('(?:Opinion|Analysis)'): undefined) +
			sumario.renderPleca ();
	}, 

	dirOnLoad: function (req) {
		if (req.readyState == 4) {
			if (req.status == 200 || req.status == 0)
				article.otherRender (req.responseXML);
			dodo.finishTasks (1);
		}
	}, 
        
        getSectArt: function (){
		var args = {};
		args.section = comments.getSect();
	        args.article = comments.getArt();
		return args;
        },
	onLoad: function () {
		srvices.init ();
		lupa.setup (document.getElementById ("article-cont"));
		article.args = dodo.argsParse (document.location);
                if (article.args == undefined)
			article.args = article.getSectArt();

		switch (article.args.section) {
		case "edito":
		case "correo":
			break;
		default:
			dodo.startTasks (1, "feed-ultimas.rss");
			document.getElementById ("ultimas").style.display = "block";
			rss.load (document.getElementById ("ultimas-list"), "/feed-ultimas.rss");

			if (ARTICLE_DIR && ARTICLE_DIR.length) {
				dodo.startTasks (1, ARTICLE_DIR);
				var req = dodo.httpReqGet ();
				dodo.httpReqLoad (req, ARTICLE_DIR, function () { article.dirOnLoad (req); });
			}
		}

		var textDiv = document.getElementById ("article-text");
		var size = dodo.cookieGet ("textSize");
		if (size)
			textDiv.style.fontSize = size;
		var font = dodo.cookieGet ("textFont");
		if (font)
			textDiv.style.fontFamily = font;
	}
}

dodo.onLoadPush (article.onLoad);

