var maxH = 70;
var maxHs = 50;

var firstCommentImgSrcShow = "images/show_first_comment.gif";
var firstCommentImgSrcHide = "images/hide_first_comment.gif";
var commentImgSrcShow = "images/show_comment.gif";
var commentImgSrcHide = "images/hide_comment.gif";

function showComment(divId) {
	var div = document.getElementById(divId);
	div.style.display = "block";
	div.style.overflow = "hidden";
	if (div.className == "commentSmall") {
		imgSrc = commentImgSrcHide;
		slideDown(divId, 0, true);
	}		
	else {
		imgSrc = firstCommentImgSrcHide;
		slideDown(divId, 0, false);
	}
	var id = parseInt(divId.substr(3));
	var img = document.getElementById("img"+id);
	img.onclick = function () { hideComment(divId); }
	img.src = imgSrc;
	img.title = "Hide comment";
}

function hideComment(divId) {
	var div = document.getElementById(divId);
	div.style.overflow = "hidden";
	var imgSrc;
	if (div.className == "commentSmall") {
		imgSrc = commentImgSrcShow;
		slideUp(divId, maxHs, true);
	}		
	else {
		imgSrc = firstCommentImgSrcShow;
		slideUp(divId, maxH, false);
	}		
	var id = parseInt(divId.substr(3));
	var img = document.getElementById("img"+id);
	img.onclick = function () { showComment(divId); }
	img.src = imgSrc;
	img.title = "Show comment";
}

function slideUp(divId, height, isSmall) {
	var div = document.getElementById(divId);
	div.style.height = height+"px";
	var dec = 1;
	if (isSmall) {
		dec = 5;
	}		
	else {
		dec = 10;
	}
	var newHeight = (height - dec);
	if (newHeight >= 0)
		setTimeout("slideUp('"+divId+"', "+newHeight+", "+isSmall+")", 10);
	else {
		div.style.display = "none";
	}
}

function slideDown(divId, height, isSmall) {
	var div = document.getElementById(divId);
	div.style.height = height+"px";
	var limit = maxH;
	var inc = 1;
	if (isSmall) {
		limit = maxHs;
		inc = 5;
	}		
	else {
		limit = maxH;
		inc = 10;
	}
	var newHeight = (height + inc);
	if (newHeight <= limit)
		setTimeout("slideDown('"+divId+"', "+newHeight+", "+isSmall+")", 10);
	else 
		div.style.overflow = "auto";
}

function loadInTextarea(divId) {
	var div = document.getElementById(divId);
	div.onclick = function () { return false; }
	div.style.height = "150px";
	var comment = div.innerHTML;
	var id = parseInt(divId.substr(3));
	div.innerHTML = '<textarea id="t'+id+'" class="insideComment">'+comment+'</textarea>';
	div.innerHTML += '<br><br><span onClick="javascript:cancelSubmit('+divId+');" class="editComment">cancel</span>';
}

function cancelSubmit(div) { //porquê div e não divId?!
	var divId = div.id;
	var id = parseInt(divId.substr(3));
	div.style.height = "110px";
	var textarea = document.getElementById("t"+id);
	var comment = textarea.value;
	div.innerHTML = comment;
	
}