/**
 * Class to handle Ajax calls
 */
var AjaxRequest = Class.create({
	initialize: function(controller, method, pageId) {
		this.controller = controller;
		this.method = method;
		this.pageId = pageId;
		
		// get ajax url params
		this.urlParams = new Hash();
		var urlSplit = String(document.location).split('#');
		if(urlSplit[1]){
			var paramPairs = urlSplit[1].split('&');
			for (var i=0 ; i<paramPairs.length ; i++) {
				var paramPair = paramPairs[i].split('=');
				this.urlParams.set(paramPair[0], paramPair[1]);
			}
		}
 	},

	getUrlParam: function(key) {
		return this.urlParams.get(key);
	},

	getController: function() {
    	return this.controller;
	},
	
	/**
	 * Call an ajax method and execute the callback sent in a json object
	 * 
	 * @param ajaxmethod: ajax method to be called
	 * @param params: parameters that will be passed on to ajax method
	 * @param httpmethod: get|post
	 * @param async: true|false
	 * @param browserHistory: true|false keep get params in browser url. allow browser functionalities (back, refresh, ...)
	 */
	call: function(ajaxmethod, params, httpmethod, async, browserHistory) {		

		if(async == undefined ){
			async =  true;
		}
		if(browserHistory == undefined ){
			browserHistory =  false;
		}
		
		var url	='/ajax/'+ajaxmethod+'/ctrl/'+this.controller+'/method/'+this.method+'/pageid/'+this.pageId;

		/* default http method is 'get' */ 
		if (httpmethod === undefined) {
			httpmethod = 'get';
		}
		
		/* if method is 'get', append key / values in the url */
		var urlSplit = String(document.location).split('#');
		var newUrl = urlSplit[0];
		
		if (httpmethod == 'get') {
			var i=0;
			for(key in params) {
				if (i==0) {
					newUrl = newUrl+"#"+key+"="+params[key];
				} else {
					newUrl = newUrl+"&"+key+"="+params[key];
				}
				url += "/" + key + "/" + params[key];
				i++;
			}
			params = undefined;
		}
		if (browserHistory == true) {
			window.location = newUrl;
		}

		new Ajax.Request(url, {
		  method: httpmethod,
		  parameters: params,
		  asynchronous: async,
		  onSuccess: function(transport) {
		  	var result = transport.responseText.evalJSON(true);
		  	eval(result.callback);
		  }
		});
	},
	
	/**
	 * Call an ajax method and display the result in the specified div
	 * 
	 * @param ajaxmethod: ajax method to be called
	 * @param elemId: id of the element where data is displayed
	 * @param params: parameters that will be passed on to ajax method
	 * @param httpmethod: get|post
	 * @param browserHistory: true|false keep get params in browser url. allow browser functionalities (back, refresh, ...)
	 */
	callUpdate: function(ajaxmethod, elemId, params, httpmethod, browserHistory) {
		
		var url	='/ajax/'+ajaxmethod+'/ctrl/'+this.controller+'/method/'+this.method+'/pageid/'+this.pageId;
		
		/* default http method is 'get' */
		if (httpmethod === undefined) {
			httpmethod = 'get';
		}
		if(browserHistory == undefined ){
			browserHistory =  false;
		}
		
		var urlSplit = String(document.location).split('#');
		var newUrl = urlSplit[0];

		if (httpmethod == 'get') {
			var i=0;
			for(key in params) {
				if (i==0) {
					newUrl = newUrl+"#"+key+"="+params[key];
				} else {
					newUrl = newUrl+"&"+key+"="+params[key];
				}
				url += "/" + key + "/" + params[key];
				i++;
			}
			params = undefined;
		}
		if (browserHistory == true) {
			window.location = newUrl;
		}

		new Ajax.Request(url, {
		  method: httpmethod,
		  parameters: params,
		  asynchronous: false,
		  onSuccess: function(transport) {
		  	var result = transport.responseText;
		  	ajaxRequest.replace(elemId, result);
		  }
		});
	},
	
	increment: function(elemId) {
		$(elemId).innerHTML = $(elemId).innerHTML * 1 + 1;
	},
	
	decrement: function(elemId) {
		$(elemId).innerHTML = $(elemId).innerHTML * 1 - 1;
	},
	
	showAndHide: function(elemId, msg, duration) {
		
		if (duration == undefined) {
			duration = 2.0;
		}
		
		$(elemId).show();
		$(elemId).innerHTML = msg;
		Effect.Fade(elemId, { duration: 2.0 });
	},
	
	replace: function(elemId, msg) {
		$(elemId).innerHTML = msg;
	},
	
	clearBox: function(elemId){
		$(elemId).value="";
	},
	
	setCss: function(elemId, classCss){
		$(elemId).className = classCss;
	},
	
	location: function(url){
		window.location = url;
	},
	
	display: function(elemId){
		$(elemId).toggle();
	},
	
	updateHref: function(elemId, url) {
		$(elemId).href = url;
	}
	
});
	
