function findTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop
		if(!ie){
			while (obj = obj.offsetParent) {
				curtop += obj.offsetTop
			}
		}
	}
	return curtop;
}

function windowHeight(){
if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } 
else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } 
else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }

	return myHeight;
}

function scrolledHeight(){
var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && document.body.scrollTop ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }

	return scrOfY;
}

function goToElem(elem){
	t = findTop(elem);
	scrH = scrolledHeight();
	winH = windowHeight();

//	alert(scrH + ", " + winH + ", " + t);
	if((scrH + winH < t + 50) || (scrH > t)){
//		alert("moving to "+(t-50));
		window.scrollTo(0, t-50);
	}	
}

function toggle_comments(msgid){
	if(document.getElementById(msgid+"_comments").style.display == "none")
		show_comments(msgid);
	else
		hide_comments(msgid);
}

function show_comments_and_post(msgid){
	sndReq("comments.php?ajax="+msgid+"_comments&msgid="+msgid+"&show_comment_form=1");
//	slideDown_after_ajax(msgid+"_comments");
	elem = document.getElementById(msgid+"_comments");
	elem.style.display = "block";
}

function show_comments(msgid){
	sndReq("comments.php?ajax="+msgid+"_comments&msgid="+msgid);
//	slideDown_after_ajax(msgid+"_comments");
	elem = document.getElementById(msgid+"_comments");
	elem.style.display="block";
}

function commentsPage(msgid, pagenum){
	sndReq("comments.php?ajax="+msgid+"_comments&msgid="+msgid+"&p="+pagenum);
	elem = document.getElementById(msgid+"_comments");
	goToElem(elem);
}

function slideDown_after_ajax(id){
	if(http.readyState==0 || http.readyState==4){
		Effect.SlideDown(id);
	}	
	else{
		setTimeout("slideDown_after_ajax('"+id+"')", 10);
	}	
}

function post_comment(msgid){
	elem = document.getElementById(msgid+"_comment_form");
	elem.style.display="block";
	goToElem(elem);
}

function hide_comments(msgid){
//	Effect.SlideUp(msgid+"_comments");
	document.getElementById(msgid+"_comments").style.display="none";
	elem = document.getElementById(msgid+"_message");
	goToElem(elem);
}

function preview_comment(formname){
	form = document.forms[formname];
	user = form.user.value;
	msgid = form.msgid.value;
	comment = form.comment.value;

	comment = comment.replace(new RegExp( "\\n", "g" ), "<br>");

	comment_form_div = document.getElementById(msgid+"_comment_form");
	preview_div = document.getElementById(msgid+"comment_preview");
	if(preview_div){
		comment_form_div.parentNode.removeChild(preview_div);
	}
	preview_div = document.createElement("div");
	preview_div.setAttribute("id", msgid+"comment_preview");
	preview_div.setAttribute("style", "padding: 0 15px;");
	preview_comment_div = document.createElement("div");
	preview_comment_div.setAttribute("class", "comment");
	preview_comment_div.innerHTML = comment;
	preview_comment_info = document.createElement("div");
	preview_comment_info.setAttribute("class", "comment_info");
	preview_comment_info.innerHTML = "By "+user;
	preview_comment_info.innerHTML += " <i>now</i>.";

	preview_comment_div.appendChild(preview_comment_info);
	preview_div.appendChild(preview_comment_div);

	preview_div.innerHTML = "<center><h4>-- Preview --</h4></center>" + preview_div.innerHTML;

	comment_form_div.parentNode.appendChild(preview_div);
	goToElem(preview_div);
}

function submit_comment(formname){
	form = document.forms[formname];
	user = form.user.value;
	msgid = form.msgid.value;
	comment = form.comment.value;

	user = escape(encodeURIComponent(user));
	msgid = escape(encodeURIComponent(msgid));
	comment = escape(encodeURIComponent(comment));

	document.getElementById(msgid+'_comments').innerHTML += "<h3>Submitting...</h3>";
	http.open('post', 'submit_comment.php');
	http.onreadystatechange = handleResponse;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	for(i=0; i<loading_divs.length; i++){
		document.getElementById(loading_divs[i]).style.display="block";
	}
	http.send('user='+user+'&msgid='+msgid+'&comment='+comment);
}

function cancel_comment(formname){
	form = document.forms[formname];
	msgid = form.msgid.value;

	elem = document.getElementById(msgid+"_comment_form");
	elem.style.display = "none";
	form.comment.value = "";


	elem = document.getElementById(msgid+"_comments");
	goToElem(elem);
}

function delete_comment(cid){
	sndReq("delete_comment.php?cid="+cid);
}
