// tarragon2.assanka.com


Inferno = {
	reqid: 1,
	requests:[],
	host:'http://inferno.t.ft.com',
	apipath:'/api3',
	replyto:null,
	reporting:null,
	config:{},
	user: null,
	linksByRef:{},
	linksByURI:{},
	comments:{},
	domReady: false,
	JSON: {},
	hooks:{
		preDOMChange:function(content, contid, itemid, mode, sortindex){},
		postDOMChange:function(content, contid, itemid, mode, sortindex){},
		load:function(){}
	},

	init:function() {
		
		// Backwards compatibility
		if (typeof infernositeid != 'undefined') InfernoConfig.site = infernositeid;

		// Quit if siteid has not yet been specified - init can be called again by user code once config is done
		if (typeof InfernoConfig == 'undefined' || typeof InfernoConfig.site == 'undefined') return false;

		// Backwards compatibility
		if (typeof InfernoConfig == 'undefined') InfernoConfig = {};
		if (typeof infernothiscontent != 'undefined') InfernoConfig.thiscontent=infernothiscontent;
		if (typeof InfernoConfig.ops == 'undefined') InfernoConfig.ops = [];
		if (typeof infernocontent != 'undefined') InfernoConfig.ops[InfernoConfig.ops.length] = {method:'editcontent', args: Inferno.JSON.parse(infernocontent)};
		if (typeof infernouser != 'undefined') InfernoConfig.user = Inferno.JSON.parse(infernouser);
		
		// Defaults
		if (typeof InfernoConfig.commentcontainer == 'undefined') InfernoConfig.commentcontainer = 'inferno-comments';
		if (typeof InfernoConfig.inputuicontainer == 'undefined') InfernoConfig.inputuicontainer = 'inferno-input';
		if (typeof InfernoConfig.commentlinktext == 'undefined') InfernoConfig.commentlinktext = 'comment';
		if (typeof InfernoConfig.thiscontent == 'undefined') InfernoConfig.thiscontent = location.pathname;

		// Set document.domain to highest level
		var d = document.domain.split('.');
		document.domain = d[d.length-2]+'.'+d[d.length-1];

		// Use custom host if specified
		if (typeof InfernoConfig.host != 'undefined') Inferno.host = InfernoConfig.host;

		// Set up custom event hooks
		if ('hooks' in InfernoConfig) {
			for (h in InfernoConfig.hooks) Inferno.addFunctionHook(InfernoConfig.hooks[h], h);
		}
		
		// Compile initial API request
		for (var i=0, s=InfernoConfig.ops.length; i<s; i++) {
			Inferno.queueOperation(InfernoConfig.ops[i].method, InfernoConfig.ops[i].args);
		}
		if (Inferno.el(InfernoConfig.commentcontainer) || Inferno.el(InfernoConfig.inputuicontainer)) {
			if (InfernoConfig.user || document.cookie.indexOf('InfernoUser') != -1) Inferno.queueOperation('getUserDetail');
			Inferno.queueOperation('createContent', {ref:InfernoConfig.thiscontent, uri:location.pathname, title:document.title});
			Inferno.queueOperation('getConfig', {ref:InfernoConfig.thiscontent});
		}
		var links = document.getElementsByTagName("a"), refs = [], uris = [], s = '';
		for (var i=(links.length-1); i>=0; --i) {
			if (links[i].innerHTML.toLowerCase() == InfernoConfig.commentlinktext) {
				if (s = links[i].getAttribute('infernoref')) {
					if (typeof Inferno.linksByRef[s] == 'undefined') {
						refs[refs.length] = s;
						Inferno.linksByRef[s] = [];
					}
					Inferno.linksByRef[s][Inferno.linksByRef[s].length] = links[i];
				} else {
					s = links[i].href.replace(/\/?#.*$/, "").replace(/^https?:\/\/[^\/]+/, "");
					if (typeof Inferno.linksByURI[s] == 'undefined') {
						uris[uris.length] = s;
						Inferno.linksByURI[s] = [];
					}
					Inferno.linksByURI[s][Inferno.linksByURI[s].length] = links[i];
				}
			}
		}
		if (refs.length) Inferno.queueOperation("getMultiCounts", {'refs':refs});
		if (uris.length) Inferno.queueOperation("getMultiCounts", {'uris':uris});
		if (Inferno.el(InfernoConfig.commentcontainer) || Inferno.el(InfernoConfig.inputuicontainer)) {
			Inferno.queueOperation('getCSS', {ref:InfernoConfig.thiscontent});
		}
		if (Inferno.el(InfernoConfig.commentcontainer)) {
			Inferno.queueOperation('getListWrapperUI', {ref:InfernoConfig.thiscontent});
			listcomopts = {ref:InfernoConfig.thiscontent};
			if (typeof InfernoConfig.overrides != 'undefined' && typeof InfernoConfig.overrides.sort != 'undefined') {
				listcomopts.sort = InfernoConfig.overrides.sort;
			}
			Inferno.queueOperation('listComments', listcomopts);
		}
		if (Inferno.el(InfernoConfig.inputuicontainer)) {
			Inferno.queueOperation('getInputUI', {ref:InfernoConfig.thiscontent});
		}

		// Send API request
		if (Inferno.requests[Inferno.reqid]) Inferno.sendRequest();
	},

	queueOperation: function(method, args) {
		if (!args) args = {};
		if (typeof Inferno.requests[Inferno.reqid] == 'undefined') Inferno.requests[Inferno.reqid] = [];
		Inferno.requests[Inferno.reqid][Inferno.requests[Inferno.reqid].length] = {method:method,args:args};
	},

	sendRequest: function() {
		if (!InfernoConfig.site) throw("Site ID not specified");
		var req = {site: InfernoConfig.site, output:{format:'iframe', jsonp:'parent.Inferno.receiveResponse', themed:1,data:1}, operations:[]};
		if (Inferno.user && typeof Inferno.user.id != 'undefined') req.user = Inferno.user.token;
		else if (typeof InfernoConfig.user != 'undefined') req.user = InfernoConfig.user;
		Inferno.queueOperation('echo', {key:'reqid', val:Inferno.reqid});
		req.operations = Inferno.requests[Inferno.reqid];
		var reqdata = Inferno.JSON.stringify(req, function(key, value) {
			if (typeof this[key] == 'object' && Object.prototype.toString.apply(this[key]) === '[object Array]') {
				return this[key];
			} else {
				return value;
			}
		});
		var html = "<form method='POST' name='infernofrm"+Inferno.reqid+"' action='"+Inferno.host+Inferno.apipath+"' target='infernoifr"+Inferno.reqid+"'>";
		var enc = encodeURIComponent(reqdata).replace(/\n/g,"%0A").replace(/&/g, "%26").replace(/'/g, "%27");
		html += "<input type='hidden' name='request' value='"+enc+"' />";
		html += "</form><iframe src='"+Inferno.host+"/blank.html' class='infernoiframe' width='10' height='10' frameborder='0' name='infernoifr"+Inferno.reqid+"' id='infernoifr"+Inferno.reqid+"'></iframe>";
		var reqdiv = document.createElement("DIV");
		reqdiv.id = "infernoreq"+Inferno.reqid;
		reqdiv.innerHTML = html;
		document.body.appendChild(reqdiv);
		try { reqdiv.firstChild.submit(); } catch(e) {};
		Inferno.reqid++;
	},

	receiveResponse: function(data) {
		if (typeof data.status != 'undefined' && data.status=='error'){
			if (data.code == 0) alert(data.message);
			//console.log("API ERROR", data.code, data.message);
			return false;
		}
		if (typeof data.length == 'undefined' || !data.length || typeof data[data.length-1].reqid == 'undefined') throw "Unable to parse API response";
		var thisreq = data[data.length-1].reqid;
		for (var i in data) {
			var req = Inferno.requests[thisreq][i], resp = data[i];
			//console.log(req.method, resp);
			if (typeof resp.status != 'undefined' && resp.status=='error') {
				if (resp.code==0) {
					alert(resp.message);
					Inferno.enabletextarea();
				} else {
					//console.log("API ERROR", resp.code, resp.message);
				}
				if (req.method=='reportAbuse') Inferno.cancelAbuseReport();					
			} else if (req.method=='getMultiCounts') {
				if (req.args.uris) {
					var links = Inferno.linksByURI;
					var reqrefs = req.args.uris;
				} else {
					var links = Inferno.linksByRef;
					var reqrefs = req.args.refs;
				}
				for (r in resp) {
					if (typeof resp[r].rendered != 'undefined') {
						if (typeof links[reqrefs[r]] != 'undefined') {
							for (l in links[reqrefs[r]]) links[reqrefs[r]][l].innerHTML = resp[r].rendered;
						}
					}
				}
			} else if (req.method=='getListWrapperUI') {
				Inferno.dom(resp.rendered, InfernoConfig.commentcontainer, false, 'add');
				Inferno.prepUI();
			} else if (req.method=='listComments') {
				Inferno.dom('', 'inferno-commentlist');
				for (r in resp) {
					Inferno.dom(resp[r].rendered, 'inferno-commentlist', 'inferno-item'+resp[r].id, 'add', resp[r].sortindex);
					Inferno.comments[resp[r].id] = resp[r];
				}
				Inferno.renumberComments();
				Inferno.showHideModOpts();

				// Scroll to anchor comment, if specified on query string
				var loc = String(document.location);
				var idx = loc.indexOf('#comment-');
				if (idx>0) {
					var cmtid = loc.substring(idx+9);
					var cmtobj = Inferno.el("inferno-item"+cmtid);
					if (cmtobj) window.scrollTo(0, cmtobj.offsetTop);
				}
			} else if (req.method=='newComment') {
				Inferno.dom(resp.rendered, 'inferno-commentlist', 'infernocomment'+resp.id, 'add', resp.sortindex);
				Inferno.comments[resp.id] = resp;
				Inferno.renumberComments();
				Inferno.showHideModOpts();
				Inferno.enabletextarea();
				Inferno.el('inferno-postform-txtcommment').value = Inferno.config.deftext
			} else if (req.method=='getConfig') {
				Inferno.config = resp;
				if (typeof InfernoConfig.overrides != 'undefined' && typeof InfernoConfig.overrides.sort != 'undefined') {
					Inferno.config.sort = InfernoConfig.overrides.sort;
				}
				Inferno.prepUI();
			} else if (req.method=='getUserDetail') {
				Inferno.user = resp;
				Inferno.showHideModOpts();
			} else if (req.method=='getInputUI') {
				Inferno.dom(resp.rendered, InfernoConfig.inputuicontainer, false, 'add');
				Inferno.enabletextarea();
			} else if (req.method=='getCSS') {
				var styleElement = document.createElement("style");
				styleElement.type = "text/css";
				styleElement.id = "inferno-css";
				if (styleElement.styleSheet) {
					styleElement.styleSheet.cssText = resp.rendered;
				} else {
					styleElement.appendChild(document.createTextNode(resp.rendered));
				}
				document.getElementsByTagName("head")[0].appendChild(styleElement);
			} else if (req.method=='getModOpts') {
				Inferno.el('inferno-optmenu').innerHTML = resp.rendered;
			} else if (req.method=='openCommenting' || req.method=='closeCommenting') {
				// No action
			} else if (req.method=='rejectComment' || req.method=='queueForModeration') {
				Inferno.hideCommentOpts();
				Inferno.dom(null, 'inferno-item'+req.args.id, null, 'remove');
			} else if (req.method=='reportAbuse') {
				if (Inferno.reporting) {
					alert("Thank you for your abuse report.  The comment has been queued to be examined by a member of our moderation team");
					Inferno.cancelAbuseReport();
					Inferno.reporting = null;
				}
			} else if (req.method=='editComment') {
				Inferno.dom(resp.rendered, 'inferno-commentlist', 'inferno-item'+resp.id, 'add', resp.sortindex);
				Inferno.comments[resp.id] = resp;
				Inferno.renumberComments();
				Inferno.showHideModOpts();
			} else if (req.method=='vote') {
				//TODO
			}
		}
		if (thisreq==1) Inferno.hooks.load();
	},

	prepUI: function() {
		if (Inferno.el('inferno-sortopts')) {
			Inferno.hide(Inferno.el('inferno-sortopts').childNodes);
			Inferno.show('inferno-sort-'+Inferno.config.sort);
		}
		if (Inferno.el('inferno-contentopts')) {
			Inferno.hide(Inferno.el('inferno-contentopts').childNodes);
			if (Inferno.user && Inferno.user.canmoderate) {
				if ((!Inferno.config.expdate || Inferno.config.expdate > Inferno.time()) && (!Inferno.config.embdate || Inferno.config.embdate < Inferno.time())) {
					Inferno.show('inferno-contentopts-open');
					Inferno.el('inferno-contentopts-open').getElementsByTagName('a')[0].onclick = function() {
						Inferno.queueOperation('closeCommenting', {ref:Inferno.config.ref});
						Inferno.queueOperation('getConfig', {ref:Inferno.config.ref});
						Inferno.sendRequest();
					};
				} else {
					Inferno.show('inferno-contentopts-closed');
					Inferno.el('inferno-contentopts-closed').getElementsByTagName('a')[0].onclick = function() {
						Inferno.queueOperation('openCommenting', {ref:Inferno.config.ref});
						Inferno.queueOperation('getConfig', {ref:Inferno.config.ref});
						Inferno.sendRequest();
					};
				}
			}
		}
		if (typeof Inferno.config.reportabuse != "undefined" && Inferno.config.reportabuse && !Inferno.el('inferno-recaptchalib')) {
			var scripttag = document.createElement("SCRIPT");
			scripttag.id = "inferno-recaptchalib";
			scripttag.type = "text/javascript";
			scripttag.src = "http://api.recaptcha.net/js/recaptcha_ajax.js";
			document.getElementsByTagName("HEAD")[0].appendChild(scripttag);
		}
		if (typeof Inferno.config.sort != "undefined") Inferno.show('inferno-sort-'+Inferno.config.sort);
		Inferno.hide('inferno-reportform');
		Inferno.hide('inferno-editcommentform');
		Inferno.hide('inferno-optmenu');
	},

	clearReq: function(ifr) {
		var div = ifr.frameElement.parentNode;
		if (div.tagName!='DIV') return;
		div.parentNode.removeChild(div);
	},

	showHideModOpts: function() {
		var els = Inferno.digForClass(Inferno.el(InfernoConfig.commentcontainer), 'inferno-optslink');
		for (var i = 0, s=els.length; i<s; i++) {
			if (Inferno.user && Inferno.user.canmoderate) {
				Inferno.show(els[i], 'inline');
			} else {
				Inferno.hide(els[i]);
			}
			els[i].onclick = function() { Inferno.showCommentOpts(this) };
		}
	},

	dom:function(content, contid, dedupid, mode, sortindex) {
		if (dedupid && Inferno.el(dedupid)) remove(Inferno.el(dedupid));
		if (!Inferno.el(contid)) return false;
		Inferno.hooks.preDOMChange(content, contid, dedupid, mode, sortindex);
		if (mode=="add") {
			if (Inferno.el(contid).hasChildNodes()) {
				isascending = (Inferno.config.sort == 'dateasc' || Inferno.config.sort == 'ratingasc');
				var inserted = false;
				var items = Inferno.el(contid).childNodes;
				for (var i=0, s=items.length; i<s; i++) {
					if ((typeof items[i].getAttribute == 'function' || typeof items[i].getAttribute == 'object') && ((items[i].getAttribute('sortindex') > sortindex && isascending) || (items[i].getAttribute('sortindex') < sortindex && !isascending))) {
						insert(Inferno.el(contid), content, items[i]);
						inserted = true;
						break;
					}
				}
				if (!inserted) insert(Inferno.el(contid), content);
			} else {
				Inferno.el(contid).innerHTML = content;
			}
		} else if (mode=="remove") {
			remove(Inferno.el(contid));
		} else {
			Inferno.el(contid).innerHTML = content;
		}
		Inferno.hooks.postDOMChange(content, contid, dedupid, mode, sortindex);
		return true;

		function insert(parent, html, insertposition) {
			var fr = document.createDocumentFragment();
			var ol = document.createElement('OL');
			ol.innerHTML = html;
			var els = ol.childNodes;
			for (var i=0, s=els.length; i<s; i++) fr.appendChild(els[i]);
			if (insertposition) parent.insertBefore(fr, insertposition);
			else parent.appendChild(fr);
		}
		function remove(target) {
			target.parentNode.removeChild(target);
		}
	},

	anontoggle: function(chk) {
		if (chk.checked) {
			Inferno.hide('inferno-postform-realauthor');
			Inferno.show('inferno-postform-customauthor', 'inline');
			Inferno.el('inferno-postform-customauthor-txtauthor').focus();
		} else {
			Inferno.hide('inferno-postform-customauthor');
			Inferno.show('inferno-postform-realauthor', 'inline');
		}
	},

	renumberComments: function() {
		if (!Inferno.el('inferno-commentlist')) return false;
		isascending = (Inferno.config.sort == 'dateasc' || Inferno.config.sort == 'ratingasc');
		var comments = Inferno.el('inferno-commentlist').childNodes;
		var numcomments = comments.length;
		var num = (isascending) ? 1 : numcomments;
		for (var i=0; i<numcomments; i++) {
			var cnts = Inferno.digForClass(comments[i], 'inferno-commentnum');
			if (cnts.length) cnts[0].innerHTML = num;
			num = (isascending) ? num+1 : num-1;
		}
		if (!numcomments) {
			Inferno.show('inferno-nocommentsyet');
			Inferno.hide('inferno-sortopts');
		} else { 
			Inferno.hide('inferno-nocommentsyet');
			Inferno.show('inferno-sortopts');
		}
	},

	reloadComments: function(sort) {
		Inferno.dom('', 'inferno-commentlist');
		var params = {ref:Inferno.config.ref};
		if (sort) params.sort = sort;
		Inferno.queueOperation("listComments", params);
		Inferno.sendRequest();
	},

	sortComments: function(sort) {
		if (Inferno.user) Inferno.queueOperation("setUserSortPreference", {sort:sort});
		Inferno.hide(Inferno.el('inferno-sortopts').childNodes);
		Inferno.show('inferno-sort-'+sort);
		Inferno.config.sort = sort;
		Inferno.reloadComments(sort);
	},

	newComment:function() {
		var ta = Inferno.el("inferno-postform-txtcommment"), chkmask = 0, name = '', e = null;
		if (e = Inferno.el("inferno-postform-customauthor-checkbox")) chkmask = (e.checked) ? 1 : 0;
		if (e = Inferno.el("inferno-postform-customauthor-txtauthor")) name = e.value;
		if (ta.value.length > 10000) {
			alert('Your comment is too long.  Please keep your remarks under 10,000 characters in length');
			return false;
		}
		ta.value = ta.value.replace(/^[\s-\.\,\:\;\_\`]+/, '').replace(/[\s-\,\:\;\_\`]+$/, '');
		if (ta.value == Inferno.config.deftext || !ta.value.length) {
			alert('Please type a comment first.');
			ta.focus();
			return false;
		}
		ta.blur();
		ta.disabled=true;
		Inferno.el("inferno-postform-btnsubmit").disabled = true;
		var params = {ref:Inferno.config.ref, mask:chkmask, name:name, comment:ta.value};
		if (Inferno.replyto) {
			params.replyto = Inferno.replyto;
			Inferno.replyto = null;
			Inferno.el(InfernoConfig.inputuicontainer).appendChild(Inferno.el('inferno-postform'));
		}
		Inferno.queueOperation('newComment', params);
		Inferno.sendRequest();
		return false;
	},
	
	showReply:function(el) {
		var id = Inferno.getID(el);
		if (Inferno.replyto != id) {
			Inferno.replyto = id;
			Inferno.el('inferno-item'+id).appendChild(Inferno.el('inferno-postform'));
		}
		Inferno.enabletextarea();
	},

	enabletextarea: function() {
		if (Inferno.el("inferno-postform-txtcommment")) {
			var tb = Inferno.el("inferno-postform-txtcommment");
			tb.onclick = function() { if (this.value==Inferno.config.deftext) this.value = ''; }
			tb.onblur = function() { if (this.value=='') this.value = Inferno.config.deftext; }
			tb.disabled = false;
			if (tb.className=='growable' && window.XMLHttpRequest) {
				var frm = Inferno.el('inferno-postform-form');
				tb.style.overflow = 'hidden';
				tb.rows = 5;
				tb.onkeyup = function() {
					var tb = Inferno.el('inferno-postform-txtcommment');
					var lh = tb.clientHeight / tb.rows;
					while (tb.scrollHeight > tb.clientHeight && !window.opera && tb.rows < 30) {
						tb.style.overflow = 'hidden';
						tb.rows += 1;
					}
					if (tb.scrollHeight > tb.clientHeight) tb.style.overflow = 'auto';
				}
				tb.onkeyup();
			}
		}
		if (Inferno.el("inferno-postform-btnsubmit")) {
			Inferno.el("inferno-postform-btnsubmit").disabled = false;
		}
	},

	vote:function(el, dir) {
		var id = Inferno.getID(el);
		Inferno.queueOperation("vote", {dir:dir, id:id});
		Inferno.sendRequest();
	},

	showCommentOpts: function(comment) {
		comment.blur();
		while (comment = comment.parentNode) if (comment.id.indexOf('inferno-item') == 0) break;
		var cid = comment.id.substr(12);
		Inferno.show('inferno-optmenu');
		Inferno.el('inferno-optmenu').innerHTML = "...";
		comment.appendChild(Inferno.el('inferno-optmenu'));
		Inferno.queueOperation('getModOpts', {id:cid});
		Inferno.sendRequest();
	},

	hideCommentOpts: function() {
		Inferno.el(InfernoConfig.commentcontainer).appendChild(Inferno.el('inferno-optmenu'));
		Inferno.hide('inferno-optmenu');
	},

	ban:function(user, level, el) {
		var id = Inferno.getID(el);
		Inferno.queueOperation("banUser", {id:user, level:level});
		Inferno.queueOperation("getModOpts", {id:id});
		Inferno.sendRequest();
	},

	ratelimit:function(user, el) {
		var id = Inferno.getID(el);
		var limit = prompt("Enter a rate limit in comments per hour", "");
		if (!limit) pph = 0;
		Inferno.queueOperation("rateLimitUser", {id:user, pph:limit});
		Inferno.queueOperation("getModOpts", {id:id});
		Inferno.sendRequest();
	},

	rejectcomment:function(el) {
		var id = Inferno.getID(el);
		if (!confirm("Reject this comment: are you sure?")) return false;
		Inferno.queueOperation("rejectComment", {id:id});
		Inferno.sendRequest();
	},

	queuecomment:function(el) {
		var id = Inferno.getID(el);
		if (!confirm("Queue this comment for moderation: are you sure? This will hide the comment.")) return false;
		Inferno.queueOperation("queueForModeration", {id:id});
		Inferno.sendRequest();
	},

	showAbuseReport: function(el) {
		var id = Inferno.getID(el);
		if (Inferno.reporting) {
			Inferno.show("inferno-comtext"+Inferno.reporting);
		}
		var cmt = Inferno.el("inferno-comtext"+id);
		var rpt = Inferno.el("inferno-reportform");
		Inferno.hide(cmt);
		Inferno.show(rpt);
		cmt.parentNode.insertBefore(rpt, cmt.nextSibling);
		Inferno.hide('inferno-reportform-inprog');
		Inferno.show('inferno-reportform-buttons');
		Inferno.reporting = id;
		Inferno.hideCommentOpts();
		setTimeout(function() {
			Recaptcha.create(Inferno.config.recaptchapublic, "inferno-reportform-recaptcha", {theme: "clean", callback: Recaptcha.focus_response_field});
		}, 50);
	},

	cancelAbuseReport: function(id) {
		if (Inferno.reporting) {
			Inferno.show("inferno-comtext"+Inferno.reporting);
			Inferno.hide("inferno-reportform");
			Inferno.reporting = null;
		}
	},

	sendAbuseReport: function() {
		if (Inferno.reporting) {
			Inferno.queueOperation('reportAbuse', {id:Inferno.reporting,rc:Recaptcha.get_challenge(),rr:Recaptcha.get_response()});
			Inferno.sendRequest();
			Inferno.hide("inferno-reportform-buttons");
			Inferno.show("inferno-reportform-inprog");
		}
		return false;
	},

	showEditComment:function(el) {
		var id = Inferno.getID(el);
		var cmt = Inferno.el("inferno-comtext"+id);
		var frm = Inferno.el("inferno-editcommentform");
		Inferno.hide(cmt);
		Inferno.show(frm);
		cmt.parentNode.insertBefore(frm, cmt.nextSibling);
		Inferno.el("inferno-editcommentform-txtcomment").value = Inferno.comments[id].comment;
		Inferno.el("inferno-editcommentform-txtcomment").focus();
		Inferno.hideCommentOpts();
	},
	
	cancelEditComment:function(el) {
		var id = Inferno.getID(el);
		Inferno.hide("inferno-editcommentform");
		Inferno.show("inferno-comtext"+id);
		Inferno.el(InfernoConfig.commentcontainer).appendChild(Inferno.el('inferno-editcommentform'));
	},

	saveEditComment:function(el) {
		var id = Inferno.getID(el);
		Inferno.queueOperation("editComment", {id:id, comment:Inferno.el("inferno-editcommentform-txtcomment").value});
		Inferno.sendRequest();
	},



	getID: function(el) {
		el.blur();
		while (el = el.parentNode) if (el.id.indexOf('inferno-item') == 0) break;
		return el.id.substr(12);
	},

	el: function(id) {
		return document.getElementById(id);
	},

	hide: function(id) {
		if (Inferno.isNL(id)) for (var i=0, s=id.length; i<s; i++) Inferno.hide(id[i]);
		else if (typeof id.nodeType != 'undefined') {
			if (id.nodeType == 3) return false;
			id.style.display = 'none';
		}
		else if (Inferno.el(id)) Inferno.el(id).style.display = 'none';
	},

	show: function(id, style) {
		if (Inferno.isNL(id)) for (var i=0, s=id.length; i<s; i++) Inferno.show(id[i], style);
		else if (typeof id.nodeType != 'undefined') {
			if (id.nodeType == 3) return false;
			id.style.display = (style) ? style : 'block';
		}
		else if (Inferno.el(id)) Inferno.el(id).style.display = (style) ? style : 'block';
	},

	isNL: function(el) {
		return (typeof el.nodeType == 'undefined' && typeof el.length == 'number' && (typeof el.item == 'string' || typeof el.item == 'function'));
	},

	digForClass: function(root, targetclass) {
		if(!root) root = document.getElementsByTagName("body")[0];
		var a = [];
		var re = new RegExp('\\b' + targetclass + '\\b');
		var els = root.getElementsByTagName("*");
		for (var i=0,j=els.length; i<j; i++) if(re.test(els[i].className)) a.push(els[i]);
		return a;
	},

	addFunctionHook: function(funcRef, hook) {
		Function.prototype.andThen=function(g) {
			var f=this;
			return function() {
				a = arguments;
				f.apply(f,a);
				g.apply(g,a);
			}
		};
		if (typeof funcRef == 'function') Inferno.hooks[hook] = (Inferno.hooks[hook]).andThen(funcRef);
	},

	time: function() {
		var d = new Date();
		return Math.round(d.getTime()/1000);
	}
};


(function(){function f(n){return n<10?"0"+n:n}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf()}}
var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];
return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+string+'"'}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==="object"&&typeof value.toJSON==="function"){value=value.toJSON(key)}if(typeof rep==="function"){value=rep.call(holder,key,value)}switch(typeof value){case"string":return quote(value);case"number":return isFinite(value)?String(value):"null";case"boolean":case"null":return String(value);case"object":if(!value){return"null"}gap+=indent;partial=[];
if(Object.prototype.toString.apply(value)==="[object Array]"){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||"null"}v=partial.length===0?"[]":gap?"[\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"]":"["+partial.join(",")+"]";gap=mind;return v}if(rep&&typeof rep==="object"){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==="string"){v=str(k,value);
if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}v=partial.length===0?"{}":gap?"{\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"}":"{"+partial.join(",")+"}";gap=mind;return v}}Inferno.JSON.stringify=function(value,replacer,space){var i;gap="";indent="";
if(typeof space==="number"){for(i=0;i<space;i+=1){indent+=" "}}else{if(typeof space==="string"){indent=space}}rep=replacer;if(replacer&&typeof replacer!=="function"&&(typeof replacer!=="object"||typeof replacer.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":value})};Inferno.JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];
if(value&&typeof value==="object"){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v}else{delete value[k]}}}}return reviver.call(holder,key,value)}cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){
return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}());


	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = Inferno.init;
	} else {
		window.onload = function() {
			if (oldonload) oldonload();
			Inferno.init();
		}
	}
	