
XWK.RenderPipe = function() {
	this.delay = 40;
	this.list = new Array();
	this.interval = null;
	this.status = 0;
}




var _pt = XWK.RenderPipe.prototype = {};

_pt.stop = function() {
	clearInterval(this.interval);
	this.status = 0;
}

_pt.start = function(force) {
	this.stop();
	this.interval = setInterval("XWK.PIPE.render()",this.delay);
	this.status = 1;
	if (force)
		this.render();
}

_pt.addObject = function(obj,force) {
	for(var i=0; i<this.list.length; i++) {
		if (this.list[i] == obj) {
			obj.id = i;
			if (force)
				this.prozess(obj.id);
			return false;
		}
	}
	obj.id = this.list.length;
	this.list.push(obj);
	if (this.status == 0)
		this.start();
	if (force)
		this.prozess(obj.id);
	return true;

}

_pt.render = function() {
	XWK.trace("Rendering...."+this.list.length);
	var res;
	var obj;
	for (var i=0; i<this.list.length; i++) {
		if (!this.prozess(i))
			i--;
	}
	if (this.list.length == 0)
		this.stop();
}

_pt.prozess = function(i) {
	var obj = this.list[i];
	obj.id = i;
	obj.count++;
	if(obj.count>0 && obj.count%obj.every == 0) {
		if(!XWK.sendCallback(obj.func,obj.funcArgs)) {
			obj.id = null;
			this.list.splice(i,1);
			XWK.sendCallback(obj.callback,obj.callbackArgs);
			return false;
		} 
	}
	return true;
}

_pt.removeObj = function(obj) {
	var i = obj.id;
	obj.id = null;
	this.list.splice(i,1);
}



XWK.RenderObject = function () {
	this.func = arguments[0];
	this.funcArgs = new Array(this).concat(arguments[1]);
	this.callback = arguments[2];
	this.callbackArgs = arguments[3];
	this.id = null;
	this.every = 1;
	this.count = 0;
}

var _pt = XWK.RenderObject.prototype = {};
_pt.update = function () {
	return sendCallback(this.func,this.funcArgs);
}

_pt.setCallback = function (func) {
	this.callback = func;
	this.callbackArgs = arguments.slice(1);
}

_pt.delayStart = function (sec) {
	if (sec < 0)
		this.count = Math.round(sec);
	else 
		this.count = Math.round(-sec*25);
}

_pt.kill = function() {
	this.count = 0;
	this.func = null;
	this.funcArgs = null;
	this.callback = null;
	this.callbackArgs = null;
}


_pt.drop = function(force) {
	this.func = null;
	this.funcArgs = null;
	if (force) {
		sendCallBack(this.callback,this.callbackArgs);
		this.callback = null;
		this.callbackArgs = null;
	}
}

_pt.removeFromPipe = function() {
	PIPE.removeObj(this);
}


_pt.addToPipe = function(force) {
    if (force == null) force = true;
     XWK.PIPE.addObject(this,force);
}


XWK.PIPE = new XWK.RenderPipe();

