if(!picscroller_instances) var picscroller_instances = new Array();

function picscroller(id, blockwidth, blockspacing){

	this.id = id;
	this.blockwidth = blockwidth;
	this.blockspacing = blockspacing;
	this.elements = false;
	this.wait = 200;
	this.shift = 0;

	// Keep track of objects
	this.objid = picscroller_instances.length;
	picscroller_instances[this.objid] = this;



	this.getblocks = function(){
		var retnode = new Array();
		var myclass = new RegExp('\\bplugin_picscroller_block\\b');
		var d = document.getElementById("plugin_picscroller_" + this.id);
		var elem;
		if(!d) return false;
		elem = d.getElementsByTagName('div');
		for(var i=0; i<elem.length; i++){
			var classes = elem[i].className;
			if (myclass.test(classes)) retnode.push(elem[i]);
		}
		return retnode;
	};


	this.update = function(){

		if(!this.elements){
			this.elements = this.getblocks();
			if(!this.elements) return;
		}

		var i, b, x, o;

		if(this.elements){
			// Adjust opacity
			for(i=0; i<this.elements.length; i++){
				b = this.elements[i];
				x = parseInt(b.style.left);

				o = opacity_get(b);
				if(o < 1) opacity_set(b, o + 0.1);
			}
		}


		// Idle a while
		if(this.wait){
			--this.wait;
			if(!this.wait){
				this.shift = (this.blockwidth + this.blockspacing);

				// Sometimes the element list is incomplete, so re-get it now
				this.elements = this.getblocks();
			}
			return;
		}



		// Move
		var shift = this.shift;
		if(shift > 20) shift = 20;

		for(i=0; i<this.elements.length; i++){
			b = this.elements[i];
			x = parseInt(b.style.left);

			x -= shift;
			if(x < -this.blockwidth){
				x += this.elements.length * (this.blockwidth + this.blockspacing);
				opacity_set(b, 0);
			}
			b.style.left = x + "px";
		}

		this.shift -= shift;
		if(!this.shift){
			this.wait = 200;
		}
	}


	window.setInterval("picscroller_instances["+this.objid+"].update()", 40);
}

