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

var article = {
	OTHER_MAX_ITEMS: 6,

	newIframe: function (className) {
		var iframe = document.createElement ("iframe");
		iframe.setAttribute ("scrolling", "no");
		iframe.setAttribute ("frameborder", "0");
		if (className)
			iframe.className = className;
		return iframe;
	},

	srvClickPrint: function () {
		window.print ();
	},

	srvShareHide: function (srv) {
		if (srv.className != "button sunk")
			return false;

		SHARETHIS.shareables[0].popup ();
		document.getElementById ("side-ads").style.visibility = "visible";
		srv.className = "button";
		return true;
	},

	srvClickShare: function (evt, srv) {
		if (!evt) evt = window.event;
		var srv = document.getElementById ("artsrv-share");
		if (!article.srvShareHide (srv)) {
			document.getElementById ("side-ads").style.visibility = "hidden";
			srv.className = "button sunk";
			SHARETHIS.shareables[0].popup (evt);
		}
	},

	srvMailHide: function (srv) {
		if (srv.className != "button sunk")
			return false;

		document.getElementById ("article-sendto").style.display = "none";
		SHARETHIS.showEmbeds ();
		document.getElementById ("side-ads").style.visibility = "visible";
		srv.className = "button";
		return true;
	},

	srvClickMail: function () {
		var srv = document.getElementById ("artsrv-mail");
		if (!article.srvMailHide (srv)) {
			article.srvCommentHide (document.getElementById ("artsrv-comment"));
			article.srvShareHide (document.getElementById ("artsrv-share"));
			SHARETHIS.hideEmbeds ();
			document.getElementById ("side-ads").style.visibility = "hidden";
			srv.className = "button sunk";
			var sendto = document.getElementById ("article-sendto");
			sendto.style.display = "block";
			if (!sendto.firstChild)
				sendto.appendChild (article.newIframe ());
			if (sendto.firstChild.src.indexOf ("sendto") < 0)
				sendto.firstChild.src = window.location.toString () + '&action=sendto';
		}
	},

	srvCommentHide: function (srv) {
		if (srv.className != "button sunk")
			return false;

		document.getElementById ("article-addcomment").style.display = "none";
		SHARETHIS.showEmbeds ();
		document.getElementById ("side-ads").style.visibility = "visible";
		srv.className = "button";
		return true;
	},

	srvClickComment: function () {
		var srv = document.getElementById ("artsrv-comment");
		if (!article.srvCommentHide (srv)) {
			article.srvShareHide (document.getElementById ("artsrv-share"));
			article.srvMailHide (document.getElementById ("artsrv-mail"));
			SHARETHIS.hideEmbeds ();
			document.getElementById ("side-ads").style.visibility = "hidden";
			srv.className = "button sunk";
			var comment = document.getElementById ("article-addcomment");
			comment.style.display = "block";
			if (!comment.firstChild)
				comment.appendChild (article.newIframe ());
			if (comment.firstChild.src.indexOf ("addcomment") < 0)
				comment.firstChild.src = window.location.toString () + '&action=addcomment';
		}
	},

	srvReplyCommentHide: function (srv, id_comentario, bp) {
		if (srv.className.indexOf ("sunk") == -1)
			return false;
		var comment = document.getElementById (id_comentario);
		comment.style.display = "none";
		comment.firstChild.style.display = "none";
		comment.removeChild(comment.firstChild);
		comment.appendChild (article.newIframe());
		comment.firstChild.className = 'reply';
		SHARETHIS.showEmbeds ();
                if (bp == 'rb') {
			srv.className = "button reply";
        	        srv.firstChild.nodeValue = "Responder a este comentario";
		} else {
			srv.className = "button";
        	        srv.firstChild.nodeValue = "Comentar esta nota";
		}
		return true;
	},

	srvClickReplyComment: function (srv, id_comentario, bp) {
		var replybutton = bp + id_comentario;
		var srv = document.getElementById (replybutton);
		var trapid = document.getElementById ("renderComments");
                var renderCommentsVal = bp + id_comentario;
                if (trapid.className != 0 && trapid.className != renderCommentsVal) {
			var otherbutton = trapid.className;
			var othersrv = document.getElementById (otherbutton);
			var trapidVal = trapid.className;
			if (trapidVal.match('rb')) {
	                        trapidVal = trapidVal.replace('rb', '');
				cbp = 'rb';
			} else {
	                        trapidVal = trapidVal.replace('cb', '');
				cbp = 'cb';
			}
			article.srvReplyCommentHide (othersrv, trapidVal, cbp);
			trapid.className = 0;
		}

		if (!article.srvReplyCommentHide (srv, id_comentario, bp)) {
			trapid.className = bp + id_comentario;
                        if (bp == 'rb') {
                        	srv.className = "button reply sunk";
                        	srv.firstChild.nodeValue = "Cerrar";
			} else {
				srv.className = "button sunk";
                        	srv.firstChild.nodeValue = "Cerrar";
			}
			var comment = document.getElementById (id_comentario);
			comment.style.display = "block";
			if (bp == 'rb')
				var idcomentariostr = '&id_comentario=' + id_comentario;
			else
				var idcomentariostr = '';
			if (!comment.firstChild)
				comment.appendChild (article.newIframe ("reply"));
			if (comment.firstChild.src.indexOf ("addcomment") < 0) {
				comment.firstChild.src = window.location.toString () + '&action=addcomment' + idcomentariostr;
	                        comment.firstChild.style.display = "block";
                        }
                } else
			trapid.className = 0;
	}, 


	srvClickFont: function (evt, srv) {
		var textDiv = document.getElementById ("article-text");
		var font;
		
		switch (dodo.getComputedStyle (textDiv, "font-size")) {
		case "16px": size = "18px"; break;
		case "18px": size = "20px"; break;
		case "20px": size = "22px"; break;
		case "22px": 
			size = "16px";
			switch (textDiv.style.fontFamily.substr (0, 3)) {
			case "Hel":
				font = 'Times, "Times New Roman", serif';
				break;
			case "Tim":
			default:
				font = "Helvetica, Arial, sans-serif";
			}
		}

		textDiv.style.fontSize = size;
		dodo.cookieSet ("textSize", size, 999);
		if (font) {
			textDiv.style.fontFamily = font;
			dodo.cookieSet ("textFont", font, 999);
		}
	}, 

	setCols1: function (srv) {
		var text = document.getElementById ("article-text");
		for (var i = 0, node; node = text.childNodes[i]; i++)
			if (node.className && node.className.indexOf ("col") > -1)
				node.className = "col";
		srv.style.display = "none";
		document.getElementById ("artsrv-cols2").style.display = "inline";
	}, 

	srvClickCols1: function (evt, srv) {
		article.setCols1 (srv);
		dodo.cookieSet ("textCols", "1", 999);
	}, 

	srvClickCols2: function (evt, srv) {
		var text = document.getElementById ("article-text");
		var col = 1;
		for (var i = 0, node; node = text.childNodes[i]; i++)
			if (node.className && node.className.indexOf ("col") > -1)
				node.className = "col col" + col++;
		srv.style.display = "none";
		document.getElementById ("artsrv-cols1").style.display = "inline";

		dodo.cookieSet ("textCols", "2", 999);
	}, 

	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);
		}
	}, 

	onLoad: function () {
/*		lupa.setup (document.getElementById ("article-cont")); */

		article.args = dodo.argsParse (document.location);
		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 srv = document.getElementById ("artsrv-cols1");
		if (!document.getElementById ("artsrv-cols2")) { // El documento es de sólo una columna.
			srv.className = "button-disable";
			srv.onclick = function () { return false; };
		} else {
			if (dodo.cookieGet ("textCols") == "1")
				article.setCols1 (srv);
		}

		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;

		SHARETHIS.shareables[0].attachButton (document.getElementById ("artsrv-share"));
		document.getElementById ("artsrv-share").onclick = article.srvClickShare;

		var cbmaincomment = document.getElementById ("cbmaincomment");
		if (comments.checkDate (document.location.toString ())) {
			cbmaincomment.style.display = "inline";
			cbmaincomment.onclick = function () { 
				article.srvClickReplyComment (this, 'maincomment', 'cb');
				return false;
			};
		}
	}
}

dodo.onLoadPush (article.onLoad);
