/**
 * @contributor Matthew Foster
 * @date 		February 4th 2007
 * @purpose 	To allow for a short cut to document.createElement with an implementation of parameterism.
 */
$C = function (name, options){
				return Element.extend(Object.extend(document.createElement(name || "div"), options || {})); 
			}
/**
 * @contributor Matthew Foster
 * @date 		February 4th 2007
 * @purpose 	This class should be considered an "abstract" it is never intended to be a concrete class, notice there is no initialize function.
 *				This class is intended to be extended from, allowing an overwritable yet base functions for processing ajax requests.  The proposed advantage to this structure
 *				Is that during debugging all of your requests are being processed in the same area, thus allowing an easier target for reviewing ajax data.  As well as handling
 *				more of what I believe to be the monotoneous routines involved in sending ajax requests.
 */			
Ajax.Application = {}

Ajax.Application.Base = Class.create(); 
Ajax.Application.Cache = Class.create();

Object.extend(Ajax.Application.Base.prototype,
						{
							method : "post",
							
							sendRequest : function(dto, cb){
								var p = this.compileDTO(dto);
										
								
								new Ajax.Request(this.url,
													{
														parameters   : p,
														method		 : this.method,
														onComplete   : this.receiveRequest.bind(this, cb),
														onFailure    : this.ajaxFailure.bind(this),
														onException  : this.ajaxException.bind(this)
													}
												);					
							},
							
							receiveRequest : function(cb, eAja){
								
								cb(eAja);
										
							},
							ajaxFailure : function(){
								if(typeof console == "object")
									console.log("Ajax Request failed args = %o ", arguments);
								else
									alert("Ajax request has failed. Application integrity has been compromised.");
							},
							ajaxException : function(){
								
								if(typeof console == "object")
									console.log("ajax exception occured args = %o", arguments);
								
								
							},
							compileDTO : function(dto){
								
								if(typeof dto == "object")
									return $H(dto).toQueryString();
								else if(typeof dto == "string")
									return dto;
								else
									throw { message : "object sent to sendRequest was invalid type.  Type = " +typeof dto };
										
								
							}
						
						
						}
			);

/**
 * @author Matthew Foster
 * @date   April 24th 2007
 * @purpose	This class is an extension of the base class with added functionality to allow for transparent caching.  The idea is to save the query string as a key in a hash object
 * 			and if that query string has already been sent for it will already have an ajax response and it will simply pass that immediately to the call back function
 */
Object.extend(Object.extend(Ajax.Application.Cache.prototype, Ajax.Application.Base.prototype),
					{
						getCache : function(){
							
							if(!this.requestCache)
								return this.requestCache = {};
							
							return this.requestCache;
						
						},
						setCache : function(obj){
						
							if(!this.requestCache)
								this.requestCache = obj;
							else
								Object.extend(this.requestCache, obj || {});
						
						},
						sendRequest : function(dto, cb){
							
							var cache = this.getCache();
							
							var key = this.compileDTO(dto);
							
							if(!cache[key]){
								var cachedCallBack = this.registerCache.bind(this, cb, key);
								Ajax.Application.Base.prototype.sendRequest.apply(this, [key, cachedCallBack]);
							}
							else
								cb(cache[key]);
						},
						registerCache : function(cb, key, eAja){
							var cache = {};
							
							cache[key] = eAja;
							
							this.setCache(cache);
							
							cb(eAja);
							
						}				
					}
			);
