/*  wx_layerx envine version 2.0	    

    This file is part of the WidgetX library
    http://www.dynamic-core.net
*/

function wx_layerx(w,h,speed,contents){
	this.id = "wx_layerx"+(wx_layerx.Count++)
	this.object = this.id + "Obj"
	eval(this.object + "=this")

	this.layers = new Array()
	this.setSize(w||0,h||0)

	this.speed = speed||0
	this.setHTML(contents||'')
	this.setVisible(false)

	return this
}
wx_layerx.Count=0
wx_layerx.prototype = new DynLayer
wx_layerx.prototype.timerid = null
wx_layerx.prototype.running = false
wx_layerx.prototype.getSubClass = function() { return wx_layerx }
wx_layerx.prototype.add = function(speed,contents) {
	var i=this.layers.length
	this.layers[i]=new wx_layerx(this.w<<3,this.h,speed||0,contents||'')
	this.layers[i].moveTo(0,0)	
	this.layers[i].setVisible(true)

	this.addChild(this.layers[i])

	return this.layers[i]
}
wx_layerx.prototype.wx_layerxsetHTML=DynLayer.prototype.setHTML
wx_layerx.prototype.setHTML = function(contents) { this.wx_layerxsetHTML(contents+contents) }
wx_layerx.prototype.start = function () { 
	this.stop()
	if (is.ns) this.timerid = setTimeout(this.object+'.update()',20)
	else this.timerid = setInterval(this.object+'.update()',20) 
	this.running=true
}
wx_layerx.prototype.stop = function () { 
	if(this.running)
		if (is.ns) clearTimeout(this.timerid)
		else clearInterval(this.timerid)
	this.running = false
}
wx_layerx.prototype.update = function () {
	al=this.layers.length
	for (var i=0; i<al; i+=1) {
		this.layers[i].x+=this.layers[i].speed

		if (this.layers[i].speed>0) {
			if (this.layers[i].x>=0) this.layers[i].x-=this.layers[i].w>>1
		} else {
			if (this.layers[i].x<-(this.layers[i].w>>1)) this.layers[i].x+=this.layers[i].w>>1
		}

		if (is.ns) this.layers[i].css.left = this.layers[i].x
		else this.layers[i].css.pixelLeft = this.layers[i].x
	}
	if (is.ns) this.timerid = setTimeout(this.object+'.update()',20)
}
wx_layerx.prototype.remove = function() { this.deleteFromParent() }
