var 	Event = YAHOO.util.Event,
			Dom   = YAHOO.util.Dom,
			lang  = YAHOO.lang,
			connect = YAHOO.util.Connect;

//var Updater = function(config) {	
var Updater = function() {	
	//this.el = Dom.get(config.el);
	//this.form = config.form;
	//this.url = config.url;
	//this.params = config.params;
	//this.jsonCallback = { callback: config.jsonCallback, scope: config.scope};
	var instance = this;
	this.callback = {
		cache: false,
		success: instance.handleResponse,
		failure: function(o) {
			this.updateCursor('pointer');
			this.busy = false;
			alert("There was a problem connecting to our server. Please verify that you are connected to the internet and try again.");
		},
		scope: instance
	};
	this.busy = false;
	
};

Updater.prototype = {
	execScripts: function(html) {
		var hd = document.getElementsByTagName("head")[0];
        var re = /(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig;
        var srcRe = /\ssrc=([\'\"])(.*?)\1/i;
        var typeRe = /\stype=([\'\"])(.*?)\1/i;
        
        var match;
        while(match = re.exec(html)){
            var attrs = match[1];
            var srcMatch = attrs ? attrs.match(srcRe) : false;
            if(srcMatch && srcMatch[2]){
               var s = document.createElement("script");
               s.src = srcMatch[2];
               var typeMatch = attrs.match(typeRe);
               if(typeMatch && typeMatch[2]){
                   s.type = typeMatch[2];
               }
               hd.appendChild(s);
            }else if(match[2] && match[2].length > 0){
                if(window.execScript) {
                   window.execScript(match[2]);
                } else {
                   window.eval(match[2]);
                }
            }
        }
	},
	updateCursor: function(prop) {
		if(lang.isValue(this.callingEl)) {
			for(var i in this.callingEl) {
				Dom.get(this.callingEl[i]).style.cursor = prop;
			}
		}
	},
	request: function(config) {
		if(this.busy)
			return;
		
		this.el = Dom.get(config.el);
		if(lang.isValue(config.callingEl)) {
			this.callingEl = config.callingEl;	
			this.updateCursor('wait');
		}
			
		if(lang.isValue(config.form)) {
			var form = Dom.get(config.form);			
			connect.setForm(form);			
			connect.asyncRequest(form.method, form.action, this.callback, null); 
		} else if(lang.isValue(config.params)) {
			connect.asyncRequest('POST', config.url, this.callback, params);
		} else {
			connect.asyncRequest('GET', config.url, this.callback, null);
		} 
		this.busy = true;
	},
	handleResponse: function(response) {
		this.updateCursor('default');
		
		this.busy = false;
		if(/text\/json/ig.test(response.getResponseHeader['Content-Type'])) {
			var obj = null;
			try {
				obj = eval(response.responseText);
			} catch(e) { }
			this.jsonCallback.callback.call(this.jsonCallback.scope, obj);
			return;
		} else {
			this.updateElement(response.responseText);
		}
	},	
	updateElement: function(html) {
		//var html = content.responseText;	
		
		//this.el.innerHTML = html.replace(re, "");
		this.el.innerHTML = html;        
		this.el.style.display = 'block';
		this.execScripts(html);
	}	
};



