var generic = generic || {};
 
generic.RediTemplate = Class.create( Template, {
	initialize: function ( template, pattern ) {
		this.template = template?template:'';
		this.readyState = template?1:0;
		this.pattern = pattern?pattern:Template.Pattern; 
		this.queue = new Array();		
		return;
	},
	load: function(template) {
	    this.template = template.toString(); 
	    this.readyState = 1; 
	    this.onReadyState();
	}, 
	evaluateCallback: function (options) {  
	    this.options = {
	      object:       {},
	      callback: 	function () {}
	    };
	    Object.extend(this.options, options || { });  
	    if (this.readyState) {   
			this.options.callback(this.evaluate(this.options.object));
	    } else { 
			this.queue.push({
				qtype: 'callback',
				obj: this.options.object,
				fnc: this.options.callback
			});
	     }
	     return;		
	},
	onReadyState: function () {
		while (q = this.queue.shift()) {
			var object = q.obj;
			var qtype = q.qtype;
			var callback = q.fnc;
			var elm;
			callback(this.evaluate(object)); 
		}
	}
});	

/*
 WDR:
 Some new options to the get() method:
 
 urlparams -- a js hash of stuff to be added to the query string. 
    simple example:

    generic.templatefactory.get({
        path: '/templates/edit-address-jsdata.tmpl',
        urlparams: {
            ADDRESS_ID: options.addrid
        }
    }).evaluateCallback({
        ...blah blah... 
    });

    this results in the url 'http://(domain)/templates/edit-address-jsdata.tmpl?ADDRESS_ID=88888'
    (assuming 88888 is the value of "options.addrid")
    
    From there, the perl side is plain old $request->param('ADDRESS_ID') or whatever...
    
 method -- HTTP method to use.  default is "GET", but you can override as "POST" if desired.
 
 NOTE - The default get() params use the "GET" http method and no query string.
 This leverages the browser cache to save the gotten file for subsequent calls.
 Therefore, understand that using the urlparams and method options may bypass
 the browser cache, which may or may not be your intent.

*/

generic.TemplateFactory = Class.create( Hash, {  
	get: function (params) {  
		var key = params.path;
        var query = params.query;
		var forceReload = params.forceReload || false; 
		var templateString = params.templateString || false;
		if (typeof this._object[key] != "undefined" && !forceReload && !query) {  
			return this._object[key];
		}
		
		this._object[key] = new generic.RediTemplate();  
		
		if (templateString) {
			this._object[key].load(templateString); 
			return this._object[key];
		} 
		
		var url = key;
        if (query) {
            var q = $H(query);
            var queryString = q.toQueryString();
            url += "?" + queryString;
        }
		var tAjax = new Ajax.Request(url, {
		    method: params.method || 'get',
            parameters: params.urlparams,
		    onSuccess: function(transport) {   
		        this._object[key].load(transport.responseText);
		    }.bind(this)
		});
		return this._object[key];	
	}	
});
generic.templatefactory = new generic.TemplateFactory();
