
//var Blog = Class.create();
//Blog.prototype = {
var Blog = Class.create({
  initialize: function(path, bid) {
    this.path = path;
	this.backend = path + 'backend/edit/';
	this.bid = bid;
	this.changes = new Array;
	this.cb = null;
  },

  setTags: function(tagstr) {
	this.changes.push(['tag', 'get', 'str='+tagstr]);
  },

  setCol: function(cid) {
	this.changes.push(['col', 'get', 'cid='+cid]);
  },

  setTitle: function(text) {
	this.changes.push(['title', 'get', 'title='+text]);
  },

  setDesc: function(text) {
	this.changes.push(['desc', 'post', 'desc='+text]);
  },

  doRequest: function(cmd, type, pars) {
	var obj = this;
	var handler = function(req) {
		var data = JSON.parse(req.responseText);
		obj.doSubmit();
	};

	var url = 'submitBlog.php';	
	if (type == 'get') {
		pars += '&cmd='+cmd+'&bid='+this.bid;
	}	
	else if (type == 'post') {
		url += '?cmd='+cmd+'&bid='+this.bid;
	}

	var req = new Ajax.Request(this.backend + url,
		{ method: type, parameters: pars, onComplete: handler});
  },

  doSubmit: function() {
	if (this.changes.length == 0) {
		this.cb();
	}
	else {
		var tmp = this.changes.pop();
		this.doRequest(tmp[0], tmp[1], tmp[2]);
	}
  },

  submit: function(cb) {
	this.cb = cb;
	this.doSubmit();
  }
});

