var tempmsg;
var lastedit = '';
var lang = new Array('Update', 'Cancel', 'Loading...', 'Saving...');
var safedElm = new Array ('postedit','updatePost','cancelUpdate');

function editPost(id) {
	tempmsg = $("#postmsg" + id).html();
	$.ajax({type:'POST',
		url:'ajax.server.php',
		data:{'action':'get','id':id,'utf':1},
		dataType:'xml',
		success:function(xml){
			lang[0] = $("update", xml).text();
			lang[1] = $("cancel", xml).text();
			lang[2] = $("saving", xml).text()
			$("#info" + id).css({'display':'none'});
			if ($('lastedit',xml))
				lastedit = '';
			
			pewidth = $('#postmsg' + id)[0].offsetWidth - 10; //10 = padding-left + padding-right of postmsg
			peheight = $('#postmsg' + id)[0].offsetHeight - 30;
			if (peheight < 200) //min postedit height
				peheight = 200;
			
			if (peheight > 400) //max postedit height
				pheight = 400;
			
			$("#postmsg" + id).html("<textarea id='postedit' style='width:"+pewidth+"px; height: "+peheight+"px'>"+$("message_tags",xml).text()+"</textarea><input type=button onclick='updatePost("+id+")' value='"+lang[0]+"' id='updatePost'> <input type=button onclick='cancelEdit("+id+")' value='"+lang[1]+"' id='cancelUpdate'>");
			$('#pedit'+id+' a').show();
			$('#busy'+id).hide();
			
		}	
	})
	if ($("#info" + id).html() != '')
		lang[2] = $('#info' + id).html();
	$('#info'+id).css({'display':'inline'});
	$('#pedit'+id+' a').hide();
	$('#pedit'+id).prepend("<img id=\"busy"+id+"\" src=\"img/busy.gif\" alt=\"loading\" />");
}


function updatePost(id) {
	dhelper(safedElm, true);
	$("#info" + id)[0].style.display = "inline";
	$("#info" + id).html(lang[3]);	
	$.ajax({type:'POST',
		url:'ajax.server.php',
		data:{'action':'update','id':id,'message':$("#postedit")[0].value,'utf':1},
		dataType:'text',
		success:function(text){
			dhelper(safedElm, false);
			$("#info" + id)[0].style.display = "none";
			$("#info" + id).html(lang[2]);
			$("#postmsg" + id).html(text);
		}	
	})
}

function cancelEdit(id) {
	msg = tempmsg;
	if (lastedit != '')
		msg += lastedit;
	$("#postmsg"+id).html(msg);
}

function dhelper(dom, state) {
	for (i in dom) 
		$('#'+dom[i])[0].disabled = state;
}
