// Requires vizwoz.js
Blobs = 
{
	blobs: new Array,
	current_blob_control: null,
	controls_css_class: null,

	initBlob: function(controls_css_class, images_css_class, parent)
	{
		// Used to make sure the correct elemtn is clicked.
		this.controls_css_class = controls_css_class;

		// get blob controls
		var blob_controls = $$(controls_css_class, 'a', parent);

		// Get blob images
		var blobs = $$(images_css_class, null, parent);

		// Randomise the blob order, except for the first, adserved one. 
		var ad_served = blobs.shift();
		blobs.sort(function() {return 0.5 - Math.random()});
		blobs = new Array(ad_served).concat(blobs);

		// for each control
		for(var i in blob_controls)
		{
			// Get corresponding blobs
			this.blobs[blob_controls[i]] = blobs[i];

			// ensure all blobs are hidden
			blobs[i].style.display = 'none';
		}

		// Set the current blob to the first control
		this.current_blob_control = blob_controls[0];
		this.current_blob_control.className += ' selected';
		this.blobs[this.current_blob_control].style.display = 'block';

		// Add 'click' event
		addEvent($(parent), 'click', delegateHandler(this, this.handle, true));
	},

	handle: function(e)
	{
		// Make sure a control was clicked
		if(!e.target.className || e.target.className != this.controls_css_class) return;

		// Same button was clicked
		if(this.current_blob_control == e.target) return;

		this.current_blob_control.className = 'blob_control';
		this.blobs[this.current_blob_control].style.display = 'none';

		this.current_blob_control = e.target;

		this.current_blob_control.className += ' selected';
		this.blobs[this.current_blob_control].style.display = 'block';
	}
}