var kohana = {

	//get site_domain based on the current document URL
	site_domain : document.URL.substring(0,document.URL.lastIndexOf("/wi3/")+1) + "wi3/",
	//site_domain : "http://localhost/w/kohana/wi3/", //for localhost
	//site_domain : "http://fabricasapiens.nl/projecten/wi3/wi3/", //for fabricasapiens.nl
	//site_domain : "http://wiedrie.nl/wi3/", //for wiedrie.nl

	request : function(controller, args) {
		//tell user request is pending...
		if ($.purr) {
			$('<div class="notice"><div class="notice-body">' + 'busy...' + '</div></div>').purr( { isSticky: false, removeTimer: 2000 } );
		} else {
			//nothing. Alerts are frustrating
		}
		if (!args) { args = {}; }
		$.post(this.site_domain + controller, args,
		  function(data){
			
			/* there are three categories here, handled in order:
			data.scriptsbefore, which is an object with javascript that will be executed before everything else
			data.dom, which will contain an object with a few different types of dom-editing:
				- remove (removes a certain html element)
				- fill (fill an html element with some content)
				- copy (copy html element to another html element)
				- append (append some content to an html element)
				- prepend (append some content to an html element)
				these types contain objects with jquery selectors along with a parameter, depending on the type of action you choose
				(for example the destination div in the case of copy)
			data.responses, which is an object with a few key-value pairs
			data.scriptsafter, which is an object javascript that will be executed after everything else
			data.alert, which will alert a message in Purr style.
			*/
			
			for(var index in data.scriptsbefore) {
				eval(data.scriptsbefore[index]);
			}
			
			for(var type in data.dom) {
				if (type == "remove" || type == "delete") {
					for(var selector in data.dom[type]) {
						$(data.dom[type][selector]).remove();
					}
				} else if (type == "fill") {
					for(var selector in data.dom[type]) {
						$(selector).html(data.dom[type][selector]);
					}
				} else if (type == "copy") {
					for(var selector in data.dom[type]) {
						$(selector).replaceWith($(data.dom[type][selector]).html());
					}
				} else if (type == "append") {
					for(var selector in data.dom[type]) {
						$(selector).append(data.dom[type][selector]);
					}
				} else if (type == "prepend") {
					for(var selector in data.dom[type]) {	
						$(selector).prepend(data.dom[type][selector]);
					}
				}
			}
			
			for(var index in data.scriptsafter) {
				eval(data.scriptsafter[index]);
			}
			
			if (data.alert) {
				if ($.purr) {
					$('<div class="notice"><div class="notice-body">' + data.alert + '</div></div>').purr( { isSticky: false } );
				} else {
					alert(data.alert);
				}
			}

		  }
		  , "json"
		 );
	}

}

