
var DynamicloadScriptCount = 0;
function DynamicloadScript(url, interval, callback) {
	this.url = url || null;
	this.interval = interval || null;
	this.script = null;
	this.isNoCache = true;
	this.cacheTime = 1;
	this.id = "DynamicloadScriptId" + DynamicloadScriptCount++;
}

DynamicloadScript.prototype.setProperties = function (props) {
	for (var key in props) {
		if (typeof this[key] != "undefined") this[key] = props[key];
		else throw new Error("Invalid Property [" + key + "]");
	}
	return this;
};

DynamicloadScript.prototype.read = function () {
	this._detach();
	this._attach();
	return this;
};

DynamicloadScript.prototype.setInterval = function (interval) {
	this.interval = interval || this.interval;
	if (!this.interval) return;
	var that = this;
//	that.read();
//	if (that.callback) that.callback();
	this.timerId = setInterval(function () {
		that.read();
	}, this.interval);
};

DynamicloadScript.prototype._attach = function () {
	var url = this.url;
	if (this.isNoCache) {
		var noCache = Math.round((new Date()).getTime() / this.cacheTime);
		url += this.url.indexOf('?') > 0 ? '&' : '?' + noCache;
//alert(noCache + ":" + (new Date()).getTime());
	}
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src  = url;
//console.log("dynamicload:" + url);
	script.id   = this.id;
	script.className = "DynamicloadScriptTransport";
	this.element = script;
	document.getElementsByTagName("head")[0].appendChild(script);
};

DynamicloadScript.prototype._detach = function () {
	var script = document.getElementById(this.id);
	if (this.element && script) {
		this.element.parentNode.removeChild(this.element);
		this.element = null;
	}
};

DynamicloadScript.prototype.addOnloadListener = function () {
	var that = this;
	var listener = function () {
		that.setInterval();
		Event.stopObserving(window, 'load', listener, false);
	};
	Event.observe(window, 'load', listener, false);
	return this;
};


