//-------------------- Goojet JQuery PLugins ---------------------------------//
$.fn.ajaxSubmit = function(callback) {
	var params = {};
    $(this)
    	.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
    	.filter(":enabled")
	    .each(function() {
	    	params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	    });

	$.post(this[0].getAttribute("action") + "?call=ajax", params, function(data){
		callback(data);
	},"json");
	
	return this;
}

//------------------------------- Goojet object helper -------------------------------//

function GJAjax() {
	
	/**
	 * PRIVATE
	 * replace the div by data
	 */
	var replace = function(data) {
		 if (data.js_replace) {
			var id = data.js_replace.id;
			var html = data.js_replace.html;
			var blink = data.js_replace.blink;
			$("#"+id).replaceWith(html);
			if (blink) {
				//animate 
				$("#"+id)[0].style.backgroundColor="#FFB700";
				$("#"+id).animate({backgroundColor:"#FFFFFF"}, 2000);
			}
			return id;
		 }
	}
	
	/**
	 * PRIVATE
	 * replace the content of the div
	 */
	var inner_replace = function(data) {
		 if (data.js_inner_replace) {
			var id = data.js_inner_replace.id;
			var html = data.js_inner_replace.html;
			var blink = data.js_inner_replace.blink;
			$("#"+id).html(html);
			if (blink) {
				//animate 
				$("#"+id)[0].style.backgroundColor="#FFB700";
				$("#"+id).animate({backgroundColor:"#FFFFFF"}, 2000);
			}
			return id;
		 }
	}
	
	/**
	 * PRIVATE
	 * replace the content of the div
	 */
	var append = function(data) {
		 if (data.js_append) {
			var id = data.js_append.id;
			var html = data.js_append.html;
			$("#"+id).append(html);
			return id;
		 }
	}
	
	/**
	 * PRIVATE
	 * prepend the content of the div
	 */
	var prepend = function(data) {
		 if (data.js_prepend) {
			var id = data.js_prepend.id;
			var html = data.js_prepend.html;
			$("#"+id).prepend(html);
			return id;
		 }
	}
	
	/**
	 * PRIVATE
	 * remove a div in the result
	 * @param data
	 * @return
	 */
	var remove = function(data) {
		if (data.js_remove) {
			var id = data.js_remove.id;
			$("#"+id).animate({height:0, opacity:'hide'}, function(data){
				$("#"+id).remove();
			})
		}
	}
	
	/**
	 * Send a request to the server
	 */
	this.postRequest = function(url, data, callback) {
		$.getJSON(url, data,
			function(data){
				var id;
				id = replace(data);
				id = id || inner_replace(data);
				id = id || append(data);
				id = id || prepend(data);
				id = id || remove(data);
				if (callback) {
					callback(data, id);
				}
			}
		);
	}
	
	/**
	 * Send the form passed as parameter in a jsonRequest 
	 */
	this.postForm = function(/* String */ form, callback) {
		$("#" + form).ajaxSubmit(function(data){
			var id;
			id = replace(data);
			id = id || inner_replace(data);
			id = id || append(data);
			id = id || prepend(data);
			id = id || remove(data);
			if (typeof(callback) == "function") {
				callback(data, id);
			};
		});
	}
	
	/**
	 * Open a dialog box (modal) with the div represent by id
	 */
	this.openModal = function(/*String*/ id) {
		new GoojetModal(id).open();
	}
	
	this.postRequestAndOpenModal = function(url, data, callback) {
		this.postRequest(url, data, function(data, id){
			gjAjax.openModal(id)
		})
	}
	

}

var gjAjax = new GJAjax;
var modal;
function GoojetModal(id) {
	this.id = id;
	this.open = function() {
		modal = $("#" + this.id).modal( {
			onOpen :this.show,
			position : [ "15%",],
			opacity :50,
			onClose : this.close
		});
	};
	
	this.close = function(dialog) {
		dialog.data[0].style.visibility = "hidden";
		dialog.container.animate({height: "0"}, function() {
			dialog.overlay.fadeOut(200, function() {
				$.modal.close();
			});
		});
	};
	
	this.show = function(dialog) {
		// Prepare dialog
		dialog.overlay.fadeIn(200);
		dialog.data[0].style.display = "block";
		var height = $('#simplemodal-container').height();
		dialog.container[0].style.height="0px";
		dialog.container.animate({height: height});
	};
	
	$(document).bind('keydown.simplemodal', function (e) {
	    if (e.keyCode == 27) { // ESC
	        $.modal.close();
	    }
	});
	
	
};


