Js.widget.toolTip = function(sel) {
	this.element = Jrun.pick(sel, "fieldset label");
	this.init();
	return this;
};
Js.widget.toolTip.prototype = {
	init: function() {
		jQuery(this.element).click(function() {
			var object = $(this);
			var title = object.html();
			title = Jrun.stripTags(title);

			var obj = jQuery(":input" , object.parent());
			var klas = Jrun.pick(obj.attr('className'), obj.attr('class'), '');
			klass = klas.split(/ /g);
			msg = [];
			var req = 0;

			for(var i = 0; i < klass.length; i++) {
				if(klass[i] == 'required') {
					msg.reverse();
					msg.push('This field is <strong>required</strong>');
					msg.reverse();
					req++;
				} else if(klass[i] == 'string') {
					msg.push('Input type alpha-numeric');
				} else if(klass[i] == 'email') {
					msg.push('Input type e-mail');
				} else if(klass[i] == 'integer' || klass[i] == 'number') {
					msg.push('input number');
				} else if(klass[i] == 'date') {
					msg.push('input date (dd-mm-yyyy)');
				} else if(klass[i].match(/^(min|max|exact)\-(\d{1,100})$/)) {
					var type = RegExp.$1;
					var digit = RegExp.$2;
					var val = "";
					if(type == 'min') {
						val = "minimum";
					} else if(type == 'max') {
						val = "maximum"
					} else {
						val = "correct";
					}
					msg.push('Type of input needed ' + val + ' ' + digit + ' characters');
				}
			}
			if(msg.length == 0) {
				msg.push('This field is <strong>not required</strong>');
			} else if(req == 0) {
				msg.reverse();
				msg.push('This field is <strong>not required</strong>');
				msg.reverse();
			}
			var fmsg = [];
			for(var x = 0; x < msg.length; x++) {
				fmsg.push((x+1) + '. ' + msg[x]);
			}

			Js.widget.notice.note({title: title, message: fmsg.join('<br />')});
		}).css("cursor", "help");
	}
};

Js.ext.formSubmit = function(id, url, overlay, callback) {
	this.id = Jrun.pick(id, null);
	this.url = Jrun.pick(url, null);
	this.overlay = Jrun.pick(overlay, true);
	this.callback = Jrun.pick(callback, false);
	this.debug = true;

	if(!!this.id && !!this.url) {
		this.init();
	}
};
Js.ext.formSubmit.prototype = {
	init: function() {
		var that = this;
		jQuery(that.id).submit(function(e) {
			var thisObject = jQuery(this);
			var submitted = thisObject.data("submitted");

			if (!submitted || submitted === false) {

				thisObject.data("submitted", true);

				var form = new Js.ext.form();
				var params = form.validate(this);

				if (!!params) {
					jQuery.ajax({
						type: "POST",
						url: that.url,
						data: params,
						beforeSend: function(){
							if (that.overlay == true) {
								Js.widget.overlay.activate();
							}
						},
						success: function(reply){
							thisObject.data("submitted", false);

							if (Jrun.isfunction(that.callback)) {
								that.callback(reply);
							}

							if (that.overlay == true) {
								Js.widget.overlay.deactivate();
							}

							if (that.debug === false) {
								Js.debug.log(reply);
							}
							Js.parse.response.init(reply);
						},
						onError: function() {
							thisObject.data("submitted", false);
						}
					});
				} else {
					thisObject.data("submitted", false);
				}
			}
			return false;
		});
	}
};

Js.ext.buttonSubmit = function(__) {
	this.id = Jrun.pick(__.id, null);
	this.url = Jrun.pick(__.url, null);
	this.overlay = Jrun.pick(__.overlay, true);
	this.callback = Jrun.pick(__.callback, false);
	this.beforeSend = Jrun.pick(__.beforeSend, false);
	this.button = Jrun.pick(__.button, null);
	this.debug = true;

	if(!!this.id && !!this.url && this.button) {
		this.init();
	}
};
Js.ext.buttonSubmit.prototype = {
	init: function() {
		var that = this;
		jQuery(that.button).click(function() {
			var thisObject = jQuery(this);
			var submitted = thisObject.data("submitted");

			if (!submitted || submitted === false) {
				thisObject.data("submitted", true);

				if (Jrun.isfunction(that.beforeSend)) {
					that.beforeSend();
				}


				var form = new Js.ext.form();
				var params = form.validate(that.id);

				if (!!params) {
					jQuery.ajax({
						type: "POST",
						url: that.url,
						data: params,
						beforeSend: function(){
							if (that.overlay == true) {
								Js.widget.overlay.activate();
							}
						},
						success: function(reply){
							thisObject.data("submitted", false);

							if (Jrun.isfunction(that.callback)) {
								that.callback(reply);
							}

							if (that.overlay == true) {
								Js.widget.overlay.deactivate();
							}

							if (that.debug === false) {
								Js.debug.log(reply);
							}
							Js.parse.response.init(reply);
						},
						onError: function(){
							thisObject.data("submitted", false);
						}
					});
				} else {
					thisObject.data("submitted", false);
				}
			}
			return false;
		});
	}
};

Js.widget.activity = function(elem) {
	this.bg = "#fff";
	this.object = null;
	this.element = Jrun.pick(elem, "#overlay");
	this.box = null;
	this.opacity = 0;
	this.boxWidth = 220;
	this.boxHeight = 19;
	this.status = 0;
};
Js.widget.activity.prototype = {
	init: function() {
		this.object = jQuery(this.element).css({background: this.bg}).fadeTo("slow", 0.01);
	},
	activate: function() {
		if(this.status == 0) {
			this.object.css({display: "block"}).fadeTo("normal", this.opacity);
			var t = Js.util.dimension.page.middle(this.boxWidth, this.boxHeight);
			if(Jrun.isset(this.box)) {
				this.box.css({
					top: "130px",
					left: t[1] + "px"
				});
				$("#loading_place").html('<img align="absmiddle" src="' + Path.base + 'images/indicator.gif" title="log keluar">');
			}
			/* we the time out for the procession to 20 seconds. Just in case the connection hangs */
			setTimeout("Js.widget.overlay.deactivate()",(20*1000));
		}
		this.status++;
	},
	loadImage: function() {
		this.box = $("<img/>").attr({
			src: Path.base + "images/indicator.gif"
		}).css({
			position: "absolute",
			width: this.boxWidth + "px",
			height: this.boxHeight + "px",
			zIndex: 9879
		}).appendTo(this.object);
	},
	deactivate: function() {
		if(this.status == 1) {
			this.object.fadeTo("normal", 0, function() {
				jQuery(this).css({display: "none"});
				$("#loading_place").html("");
			});
		}
		this.status--;

		this.status = (this.status < 0 ? 0 : this.status);
	}
};



jQuery(function() {
	Js.widget.overlay = new Js.widget.activity;
	Js.widget.overlay.init();
	Js.widget.overlay.loadImage();
});