var commentsObj = {};
commentsObj.onCurrentRowChanged = function(n,data){

}

function reloadCode(){
	var d = Math.random() * 1000;
	Spry.$$('img#imageCode')[0].src = Spry.$$('img#imageCode')[0].src +'&k='+ d;
}

function setCommentForm(id){
	var row = commentsListDs.getRowByID(id);
	Spry.Utils.setInnerHTML('comment',row['comment']);
	Spry.$$('input#commentid').setProperty('value',row['comment/@id']);
	Spry.$$('input#node').setProperty('value',nodeId);
	window.location.hash= "form";
}
function onComment(req){
	var msg = null;
	var response = Spry.Utils.urlComponentToObject(req.xhRequest.responseText);
	if(response.resp == 'true'){
		msg = response.msg;
		commentsListDs.loadData();
	}else if(response.resp == 'false'){
		msg = response.error;
	}
	
	alert(msg);
}

function submitComment(form){
	Spry.$$('input#node').setAttribute('value',nodeId);
	// form fields description structure
	var a_fields = {
		'comment' : {'l':'Comments','r':true,'t':'commentsLabel','mn':0,'max':255}
		,'code' : {'l':'Code','r':true,'f':'alphanum'}
	},
	o_config = {
		//'to_disable' : ['submitBttn'],
		'alert' : 1
	}
	
	var v = new validator(form.id, a_fields, o_config);
	if(v.exec())
		Spry.Utils.submitForm(form.id,onComment,{method:'POST', url:'includes/widgets/comments/set_comment.php'});
	form.reset();
	return false;
}
function loadCommentsForm(div){
	Spry.Utils.updateContent(div,'includes/widgets/comments/comments_form.php');
}
var params = (typeof params == 'undefined')? (Spry.Utils.getLocationParamsAsObject()) : params;
var nodeId = (typeof nodeId == 'undefined')? (params.id? params.id : null) : nodeId; // gets the refid whose comments are for
var commentsURL = "includes/widgets/comments/get_comments.php?node="+nodeId;
var commentsListDs = new Spry.Data.XMLDataSet(commentsURL, "comments/node", {useCache:false});
commentsListDs.setColumnType("created", "date");
commentsListDs.addObserver(commentsObj);