/*
name: jsonp
version: 8-14-08
author: Dan Kantor
/////////////////////
dependencies:
1. moo-12-all.js
////////////////////
////////////////////
events:
1. onComplete
////////////////////
///////////////////////////////////////////////////////////////
*/
var JsonP = new Class({
	Implements : [Options, Events],
	options: {
		url : null,
		params : {},
		cacheBust : true,
		callBackKey : null,
		onComplete : Class.empty,
		timeout : 10000,
		disposed : false
	},
	initialize: function(options){
		this.setOptions(options);
		if (this.options.cacheBust){
			this.options.params.cacheBust = Math.random(); 	
		}
		var i = JsonP.index++;
		var r = "c"+i;
		this.createNew(r);
		this.options.params[this.options.callBackKey] = "JsonP.requestors."+r+".callback";
		this.makeUrl();
	},
	makeUrl : function(){
		var qs = $H(this.options.params).toQueryString();
		this.url = this.options.url+"?"+qs;
	},
	request : function(){
		if (!this.options.callBackKey){
			this.fireEvent("onComplete", null);
		} else {
			this.script = new Asset.javascript(this.url);
			this.clean.delay(this.options.timeout, this, null);
		}
	},
	createNew : function(i){
		JsonP.requestors[i] = {
			obj : this,
			callback : function(json){
				this.obj.clean(json)
			}
		}
	},
	clean : function(object){
		try {
			if (!this.disposed) {
				this.script.dispose();
				this.disposed = true;
				this.fireEvent("onComplete", object);
			}
		} catch (e) {
			this.fireEvent("onComplete", null);
		}
	}
});
JsonP.requestors = {};
JsonP.index = 0;

