//
// Picture scroller plug-in for CMS. Programmed by Roel van Mastbergen.
//

if(!picscroller_instances) var picscroller_instances = new Array();

function picscroller(id, blockwidth, blockheight, blockspacing, imagelist) {

	this.id = id;
	this.blockwidth = blockwidth;
	this.blockspacing = blockspacing;
	this.elements = false;
	this.wait = 200;
	this.shift = 0;
	this.imageurls = imagelist.split('\\');
	this.images = new Array();
	this.imagesdisplayed = 0;
	this.preload_timeout = 0;
	this.stage = 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);
		if(!d) return false;
		var 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.stage0 = function() {

		//
		// Create elements!
		//
		var i;
		var pos = 0;
		var code = "";
		var d = document.getElementById("plugin_picscroller_" + this.id);
		if(!d) return false;

		for(i in this.imageurls) {

			var settings = this.imageurls[i].split(',');
			var url = settings[1];

			this.imageurls[i] = settings[0];

			//
			// Preload image
			//
			this.images[i] = new Image();
			this.images[i].src = this.imageurls[i];

			//
			// Write some HTML code
			//
			code += '<div class="plugin_picscroller_block" style="display:none;background-color:#000000;position:absolute;left:'+pos+'px;width:' + this.blockwidth + 'px;height:' + blockheight + 'px;background-image:url(&quot;' + this.imageurls[i] + '&quot;);">';
			code += '<a href="' + url + '" style="display:block;width:' + this.blockwidth + 'px;height:' + blockheight + 'px;line-height:' + blockheight + 'px;"></a>';
			code += '</div>';
			pos += this.blockwidth + this.blockspacing;
		}

		d.innerHTML += code;

		this.stage++;
	}


	this.stage1 = function() {
		//
		// Try to list all the animated elements in an array
		//
		this.elements = this.getblocks();
		if(this.elements && this.elements.length) {
			this.stage++;

			var i;
			for(i=0; i<this.elements.length; i++) {
				opacity_set(this.elements[i], 0.01);
				this.elements[i].style.display = "block";
			}
		}
	}


	this.stage2 = function() {

		//
		// Show all images one by one
		//
		var currentindex = this.imagesdisplayed;

		this.preload_timeout++;
		if(this.preload_timeout >= 1) {
			opacity_set(this.elements[currentindex], 1.0);

			this.imagesdisplayed++;
			this.preload_timeout = 0;

			if(this.imagesdisplayed >= this.elements.length) {
				//
				// Showing all images
				//
				this.stage++;
				return;
			}
		}

	}


	this.stage3 = function() {

		//
		// 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 i, b, x;
		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;
		}
	}



	this.update = function() {

		//
		// Always auto-update opacity
		//
		var i, b, x, o;
		if(this.elements && this.stage > 2) {
			// Adjust opacity
			for(i=0; i<this.elements.length; i++) {
				b = this.elements[i];
				if(b.style.display != "none") {
					// x = parseInt(b.style.left);
					o = opacity_get(b);
					if(o < 1) opacity_set(b, o + 0.1);
				}
			}
		}


		switch(this.stage) {
			case 0:
				this.stage0();
				return;
			case 1:
				this.stage1();
				return;
			case 2:
				this.stage2();
				return;
			case 3:
				this.stage3();
				return;
		}
	}


	window.setInterval("picscroller_instances["+this.objid+"].update()", 40);
}



