/*
 *			JSON Updater 
 *			rev 0.0.3
 *			(c)	2007 LaziAList Studio
 *
 *			WARNING ! This module required PROTOTYPE.JS
 */
 
 /// v 0.0.2 Added form post function
 /// v 0.0.3 Added Confirm function
 /// v 0.0.4 Added _xAJAX initialization
 /// v 0.0.5 Added EvalScripts !
JSONUpdater = function () {
}
JSONUpdater.prototype = {

	sendRequest:function(sURL,sParams,sUserActions,sActionsAtResponse) {
//		this.sActionsAtResponse=typeof(sActionsAtResponse)!="undefined"?sActionsAtResponse:"SERVER_SPECEFIC";
		this.sUserActions=sUserActions;
		if (typeof(sParams)!='undefined') {
			sParams+='&ajax=1';
		}
		else {
			sParams='&ajax=1';
		}
		if (typeof( Ajax)!='undefined') {
		var AjaxRequest = new Ajax.Request(
			sURL, 
			{
				method: 'post', 
				parameters: sParams, 
				onComplete: this.waitRequest.bindAsEventListener(this)
			});
		} else if (typeof( jQuery.ajax)!='undefined') {
		jQuery.ajax( {
			url:sURL, 
		    type:'post', 
			data:sParams, 
			complete: this.waitJQueryRequest}
			);
		}
		
	},
	
	sendForm:function(sURL,sFormName,sUserActions,sActionsAtResponse) {
		var params='';
    	params+=Form.serialize(sFormName);
		params+='&ajax=1';
    	this.sendRequest(sURL,params,sUserActions,sActionsAtResponse);
	},
	/*
	 *		wait until request is completed and call runJSON function
	 */
	waitJQueryRequest:function(uResponse,status) {
		if ( status == "success") {
		 if (typeof( jQuery.ajax)!='undefined') {
				eval( "JSON = " +uResponse.responseText );
			}	
		switch (this.bActionsAtResponse) {
			case "USER_SPECEFIC":
				this.runJSON(this.sUserActions,JSON);				
			break;
			default :
				jsonData=JSON;				
					
					  // Execute all the scripts inside of the newly-injected HTML
				//uResponse.responseText.evalScripts();
				eval(JSON.ONLOAD);
			break;
		}
		}		
	},
	waitRequest:function (uResponse) {
		if (typeof( Ajax)!='undefined') {
			var JSON=uResponse.responseText.evalJSON();
		}
		switch (this.bActionsAtResponse) {
			case "USER_SPECEFIC":
				this.runJSON(this.sUserActions,JSON);				
			break;
			default :
				this.runJSON(JSON.ONLOAD,JSON);				
			break;
		}
//		prop(JSON);
		for (i in JSON) 
		if (typeof(JSON[i])!='function') {
			JSON[i].evalScripts();
		}
	},
	/*
		$('id').innerHTML=jsonData.
	*/
	runJSON : function(sActions,uData) {
//		prop(sActions);
		jsonData=uData;
		eval(sActions);
	},
	Confirm : function (sText,sActions) {
		if (confirm(sText)) {
			eval(sActions);
		}
	}
}

var _xAjax = new JSONUpdater();



