function comments(holder, bid, path)
{
	this.layer = $(holder);
	this.bid = bid;  
	this.path = path;
	this.popup = null;
	this.submitForm = submitForm;
	this.reply = reply;
	this.modify = modify;
	this.newcom = newcom;
	this.refresh = refresh;
}

function reply(cid) {
	var backend = this.path + 'backend/comments/';
	this.popup = new Popup(this.path, backend+'form.php?t=0&cid='+cid+'&p='+this.path);
	this.popup.show();
}
function modify(cid) {
	var backend = this.path + 'backend/comments/';
	this.popup = new Popup(this.path, backend+'form.php?t=1&cid='+cid+'&p='+this.path);
	this.popup.show();
}
function newcom() {
	var backend = this.path + 'backend/comments/';
	this.popup = new Popup(this.path, backend+'form.php?t=2&bid='+this.bid+'&p='+this.path);
	this.popup.show();
}

function submitForm(elms)
{
	var wait_popup = new Popup(this.path, this.path+'backend/spinner.php?s=Posting%20comment...&p='+this.path);
	wait_popup.show();

	var obj = this;
	var pars = '';
	for(i = 0; i < elms.length - 1; i++)
		pars += elms[i] +'='+$F(elms[i]) + '&';	
	pars += elms[i] +'='+$F(elms[i]);	


	var handler = function(req) {
		var res = JSON.parse(req.responseText);
		
		wait_popup.close();
		if (res.result == true) {
			obj.popup.close();
			obj.popup = null;
			obj.refresh();
		}
		else {
			alert("Posting failed, sorry");
		}
	}
	var eHandler = function(req) {
		wait_popup.close();
		this.popup.close();
		this.popup = null;
	}

    var req = new Ajax.Request(this.path+'backend/comments/submit.php',
        { method: 'post', postBody: pars, 
            onComplete: handler, onError: eHandler});

}

function refresh()
{
	var wait_popup = new Popup(this.path, this.path+'backend/spinner.php?s=Refreshing%20comments...&p='+this.path);
	wait_popup.show();

	var obj = this;
	var handler = function(req) {
		obj.layer.innerHTML = req.responseText;
		wait_popup.close();
		obj.layer.innerHTML.evalScripts();
	}

	var backend = this.path+'backend/';
	var pars = 'bid='+this.bid+'&p='+this.path + '&dummy='+Math.floor(Math.random()*10000);
    var req = new Ajax.Request(backend+'getComments.php',
        { method: 'get', parameters: pars,
            onComplete: handler});
}

