//
// Create proper-derivable "class".
//
// Version: 1.2
//

function newClass(parent, prop) {
  // Dynamically create class constructor.
  var clazz = function() {
    // Stupid JS need exactly one "operator new" calling for parent
    // constructor just after class definition.
    if (clazz.preparing) return delete(clazz.preparing);
    // Call custom constructor.
    if (clazz.constr) {
      this.constructor = clazz; // we need it!
      clazz.constr.apply(this, arguments);
    }
  }
  clazz.prototype = {}; // no prototype by default
  if (parent) {
    parent.preparing = true;
    clazz.prototype = new parent;
    clazz.prototype.constructor = parent;
    clazz.constr = parent; // BY DEFAULT - parent constructor
  }
  if (prop) {
    var cname = "constructor";
    for (var k in prop) {
      if (k != cname) clazz.prototype[k] = prop[k];
    }
    if (prop[cname] && prop[cname] != Object)
      clazz.constr = prop[cname];
  }
  return clazz;
}


WidgetBase = newClass(null, {
  constructor: function() {
  },

	onMouseClickDouble: function(div)
	{
		var widget = $(div);
		var form = $(this.form)
		form.values(widget.data())
		document.selection.empty()
		this.bind = div

		this.window = WindowManager.show(this.type, null, null)
		this.window.eventCallback(
			function(ev){	if (ev == 'onload')	form.values(widget.data()) }
		)
		return false;
	},

	onCreate: function(div){
		this.ai = new AJAXInteraction(
			BlogEditor.script+"/ajax",
			function(content){
				$(div).css("background","")
				$(div).html(content)
				$(dragHelper.lastChild).html(content)
				$(dragHelper.lastChild).css("background","")
			}
		)

		$(div).css("background","url('"+BlogEditor.url+"/widgets/loader.gif') no-repeat middle center")
		this.ai.doPost("b="+BlogEditor.domain+"&widget="+escape(this.type));
    return div;
	},

	saveCode: function(div)
	{
		var code = $(div).attr('_data');
		return code || '';
	},

	event: function(ev)
	{
		if (!this.window)
			return;

		if (ev == 'close'){
			this.window.close();
		}
		else
		if (ev == 'ok'){
			var widget = $('div #'+this.bind.id);
			var form = $(this.form)
			this.reload(form.values());
			this.window.close();
		}
		return true;
	},

	reload: function(hash)
	{
		var widget = $('div #'+this.bind.id);

		widget.attr("prevwidth", widget.css("width"))
		widget.attr("prevheight", widget.css("height"))

		widget.width(widget[0].offsetWidth)
		widget.height(widget[0].offsetHeight)

		widget.fadeOut(300)
					.stop()
					.html("")					
					.css("background","url('"+BlogEditor.url+"/widgets/loader.gif') no-repeat middle center")

		this.ai = new AJAXInteraction(
			BlogEditor.script+"/ajax",
			function(response){
				widget.hide()
					.css("background","")
					.width(widget.attr("prevwidth"))
					.height(widget.attr("prevheight"))
					.html(response)					
					.data(hash)
					.fadeIn(300)
			}
		)

		this.ai.doPost("b="+BlogEditor.domain+"&widget="+escape(this.type)+"&data="+packHash(hash));
	}

});
