if(typeof mini_news === 'undefined') var mini_news = {};

mini_news.utils = {};

/***************************************			
			
	callbacks

****************************************/
	
mini_news.utils.defineCallbacks = function(obj, names) {
    for(var i = 0, l = names.length ; i < l; i++) {
        (function() {
            var name = names[i];
            obj['on' + name] = function(func, scope) {
                this['_' + name.toLowerCase ()] = function() {
                    if(scope) return (scope[func] ? scope[func] : scope.func);
                    else return (window[func] ? window[func] : (window.func? window.func : func));
                }();
				return this;
            };
        })();
    };
};



/***************************************			
			
	script feeds

****************************************/
mini_news.JSONScriptFeed = function(url, callback, callbackName, timeout) {

	this._url = url;
	this._id = mini_news.JSONScriptFeed.gui++;
	this._callback = (callback || '__onload__' + this._id);
	this._callbackName = callbackName || 'callback';
	this._timeout = ((timeout || 30) * 1000);
	
	return this;
};
mini_news.JSONScriptFeed.gui = 0;

mini_news.JSONScriptFeed.prototype.abort = function() {
    this._clean();
    if(this._abort) this._abort();
	return this;
};

mini_news.JSONScriptFeed.prototype.load = function(url) {
    this._url = url || this._url;
    if(this._start) this._start();

    var script = document.createElement('script');
    script.id = this._id;
    script.src = this._url + (this._url.indexOf('?') === -1 ? "?" : "&") + this._callbackName + "=" + this._callback;
	
    var _self = this;

	var func = window[this._callback] = function(data) {
	
		if(typeof _self._success === 'function') {
			_self._success(data);
			//_self._clean();
			if(_self._complete) _self._complete();
			clearTimeout(_self.timer);
		}    
		else {
			setTimeout(function() {
				func(data);
			}, 100);
		};
	};        

    this.timer = setTimeout(function() {
       	_self._clean();
        if(_self._error) _self._error();
        else throw "JSON Script loading aborted";
    }, this._timeout);
   
    document.getElementsByTagName('head')[0].appendChild(script);
	return this;
};

mini_news.JSONScriptFeed.prototype._clean = function() {
	window[this._callback] = null;
    try {delete window[this._callback]}catch(e){};
    document.getElementsByTagName('head')[0].removeChild(document.getElementById(this._id));
	return this;
};
           
mini_news.utils.defineCallbacks(mini_news.JSONScriptFeed.prototype, ['Start', 'Success', 'Complete', 'Error', 'Abort']); 