var last_elem_touched = null;
var first_view = true;

function $(x){return document.getElementById(x);}

function showForm(targetID) {	
	if($("comment-form-portable").parentNode == $("comment-form" + targetID) && $("comment-form-portable").getAttribute("STATE") != "0"){				
		$("comment-form-portable").style.display="none";
		$("comment-form-portable").setAttribute("STATE","0");
		$("show-form" + targetID).style.backgroundPosition = "0 0";
		$("commentUserNamePortable").value = "";
		$("commentUserEmailPortable").value = "";
		$("commentTextPortable").value = "";
		$("commentCaptchaAnswerPortable").value = "";		
		last_elem_touched = $("show-form" + targetID);
	}else if( $("comment-form-portable").getAttribute("STATE") == "0" && $("comment-form-portable").parentNode == $("comment-form" + targetID) ){		
		$("parentIdPortable").value = targetID;
		$("comment-form-portable").style.display="block";
		$("comment-form-portable").setAttribute("STATE","1");
		$("show-form" + targetID).style.backgroundPosition = "0 -10px";
		if ($("comment-error-message") && !first_view) {
			$("comment-error-message").parentNode.removeChild($("comment-error-message"));
		}		
		last_elem_touched = $("show-form" + targetID);
	}else{
		if ($("comment-error-message") && !first_view) {
			$("comment-error-message").parentNode.removeChild($("comment-error-message"));
		}
		first_view = false;
		$("parentIdPortable").value = targetID;
		$("comment-form-portable").style.display="block";
		$("comment-form" + targetID).appendChild($("comment-form-portable"));
		$("show-form" + targetID).style.backgroundPosition = "0 -10px";		
		if(last_elem_touched){
			last_elem_touched.style.backgroundPosition = "0 0";
		}
		last_elem_touched = $("show-form" + targetID);		
	}
}

function showThread(oid){
	var t = $("thread" + oid);
	if(t.getAttribute("STATE") == "1"){
		t.style.display = "none";
		t.setAttribute("STATE","0");
		$("show-thread" + oid).replaceChild(document.createTextNode("Show "),$("show-thread" + oid).firstChild);
		$("show-thread" + oid).style.backgroundPosition = "0 0";
	}else{
		t.style.display = "block";
		t.setAttribute("STATE","1");
		$("show-thread" + oid).replaceChild(document.createTextNode("Hide "),$("show-thread" + oid).firstChild);
		$("show-thread" + oid).style.backgroundPosition = "0 -14px";
	}
}

function handleCommentSubmit(o){		
	var id = o.form.getAttribute("id");
	var pid = o.form.parentNode.getAttribute("id");		 		
	o.form.action = "http://" + location.host + location.pathname + "?commentid=" + (pid == "comment-form-static" ? "-1" : o.form.parentNode.parentNode.getAttribute("id").split("-form")[1]);		
	setTimeout("$(\"" + id + "\").submit();",100);
	return false;
}

function skipToComment(){
	if(/(\?|&)commentid=/.test(location.href)){
		var id = location.href.replace(/^.*(\?|&)commentid=((\-)?[\d]+).*$/, "$2"); 				
		if (comment_success) {
			location = "#comments-anchor"
		} else if(!comment_success && id == "-1") {					
			location = "#commentForm"
		} else {
			var oid = id;							
			while(/^thread[\d]+$/.test($("comment" + oid).parentNode.getAttribute("id"))){					
				oid = $("comment" + oid).parentNode.getAttribute("id").replace(/^thread([\d]+)$/, "$1");
				showThread(oid);
			}
			location = "#anchor" + id;
			if(!comment_success){
				showForm(id);
			}
		}
	}
}

window.onload = skipToComment;