/*
	Simple random image selector for css backgrounds
	to use:
	
	bg = new bgSelect(<Object ID>, <image URL 1>, <image URL 2>, ... <image URL n>);
	bg.selectImage();

*/
function bgSelect(ID, list)
{
	this.obj = null;
	this.imgList = new Array();

	this.addImage = function()
	{
		for (var i=0; i<arguments.length; i++)
		{
			this.imgList[this.imgList.length] = "url("+arguments[i] + ")";
		}
	}

	this.selectImage = function ()
	{
		var idx = Math.floor(Math.random() * this.imgList.length);
		this.obj.style.backgroundImage = this.imgList[idx];
	}

	this.find = function (theObj, theDoc)
	{
		var p, i, foundObj;
		
		if(!theDoc) theDoc = document;
		if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
		{
			theDoc = parent.frames[theObj.substring(p+1)].document;
			theObj = theObj.substring(0,p);
		}
		if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
		for (i=0; !foundObj && i < theDoc.forms.length; i++) 
			foundObj = theDoc.forms[i][theObj];
		for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
			foundObj = findObj(theObj,theDoc.layers[i].document);
		if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
		
		return foundObj;
	}

	if ( !(this.obj = this.find(ID)) || !this.obj.style )
	{
		alert("#" + ID + " not found or has no style property");
	} else
	{
		if (arguments.length > 1)
		{
			for (var i=1; i<arguments.length; i++)
			{
				this.addImage(arguments[i]);
			}
		}
	}
}
