/**
 * Prudsys call functionality bundled in here 
 */
var Prudsys = {
	//: all the post requests	
	post_requests : [],
	//: has the dom:loaded event fired yet?
	dom_loaded : false,
	//: class set to element while request is pending
	css_class_loading : false,
	//: class set to element when request is complete
	css_class_complete : false,
	//: save callbacks by ids
	callbacks : [],
	
	//: init the common parameters and set listener to dom loaded event
	initialize : function(){
		this.post_requests = [];
		this.dom_loaded = false;
		this.css_class_loading = 'service-loading';
		this.css_class_complete = 'service-complete';
		this.callbacks = [];
		
		Event.observe(document, 'dom:loaded', function(){Prudsys.domLoaded();});
	},
	
	/* 
	 * PUBLIC FUNCTIONS 
	 */
	
	//: clear all request queues
	clear : function() {
		this.clearPost();
	},
	
	//: add a post request
	add : function( $id, $service, $params, $callback ) {
		if ($params == null) $params = [];
		if ($callback == null) $callback = false;
		
		var requestObj = {id: $id, service: $service, callparams: $params, callback: $callback};
		if (!this.dom_loaded)
			this.post_requests.push(requestObj);
		else
			//: short wire if dom has already loaded
			this.submitPost([requestObj]);
	},
	
	/* 
	 * PROTECTED FUNCTIONS (pseudo) 
	 */
	
	//: clear the post queue
	clearPost : function() {
		this.post_requests.clear();
	},
	
	/* SUBMISSION */
	
	//: submit all added requests
	submit : function() {
		//: submit post requests and clear the queue
		this.submitPost(this.post_requests);
		this.clearPost();
	},
	
	//: submit given params as post call
	submitPost : function(params){
		
		params = params.filter(function(req){
			return (!!$(req.id) || req.id == null);
		});
		
		for (var i=0; i<params.length; i++){
			if (params[i].id != null) {
				//: set loading class to each element
				this.setLoadingStatus(params[i].id);
				//: write callback to callback array
				this.addCallback(params[i].id, params[i].callback);
			}
		}
		
		if(params.length > 0) {
			new Ajax.Request('/prudsys/', {
				method: 'post',
				parameters: 'data='+Object.toJSON(params),
				onComplete: this.processResponse
			});
		}
	},
	
	/* CALLBACKS */
	
	//: append a callback for a given object id
	addCallback : function(id, callback) {
		if (typeof(callback) == 'function') {
			if (typeof(this.callbacks[id]) !== 'object') {
				this.callbacks[id] = [];
			}
			this.callbacks[id].push(callback);
		}
	},
	
	//: trigger callbacks for a given id
	triggerCallback : function (id, reqObject, remove) {
		if (remove != false) remove = true;
		if (typeof(this.callbacks[id]) !== 'undefined') {
			//: trigger all callbacks
			this.callbacks[id].each(function(eachObj){
				try {
					eachObj(reqObject);
				} catch (e) {}
			});
			//: remove callback from queue
			if (remove) delete(this.callbacks[id]);
		}
	},
	
	/* ELEMENT STATUS */
	
	//: set the configured loading class to the object with the given id
	setLoadingStatus : function(id) {
		this.cleanStatus(id);
		$(id).addClassName(this.css_class_loading);
	},
	
	//: set the configured complete class to the object with the given id
	setCompleteStatus : function (id) {
		this.cleanStatus(id);
		$(id).addClassName(this.css_class_complete);
	},
	
	//: remove any status classes
	cleanStatus : function (id) {
		$(id).removeClassName(this.css_class_loading);
		$(id).removeClassName(this.css_class_complete);
	},
	
	/* EVENTS */
	
	//: triggered if dom has loaded
	domLoaded : function (e) {
		this.dom_loaded = true;
		//: submit all requests added
		this.submit();
	},
	
	//: default callback for request response
	processResponse : function (transport) {
		if (transport.readyState == 4) {
			var resp = transport.responseText.evalJSON(true);
			for (var s in resp) {
				if ($(resp[s].target) && resp[s].count > 0 && resp[s].list != false) {
					$(resp[s].target).update(resp[s].list);
					
					if (resp[s].template == 'vertical') {
						new prudsysSlider(0, resp[s].count, resp[s].target);
					} else if (resp[s].template == 'horizontal_sliding') {
						new EndlessSlider(resp[s].target);
					}
				} else if ($(resp[s].target)) {
					//: nothing do display, so hide the target area, if it exists
					$(resp[s].target).hide();
				}
			}
		}
	}
};

//: initialize the prudsys service class
Prudsys.initialize();
