/** constructor **/
function BackgroundImage( imagesArray, imageDirectory, elementID )
{
	if( document.getElementById )
	{
		this.imagesArray    = imagesArray;
		this.imageDirectory = imageDirectory;
		this.elementID      = elementID;

		this.init();
	}
}




/** instance method **/
// method init
function BackgroundImage_init()
{
	this.setBackground()
}
BackgroundImage.prototype.init = BackgroundImage_init;


// method setBackground
function BackgroundImage_setBackground()
{
	var backgroundImage = this.getImage();

	document.writeln( '<style type="text/css">' );
	document.writeln( '<!--' );
	document.writeln( '#' + this.elementID + '{' );
	document.writeln( 'background-image:url("' + backgroundImage + '");' );
	document.writeln( '}' );
	document.writeln( '-->' )
	document.writeln( '<\/style>' );
}
BackgroundImage.prototype.setBackground = BackgroundImage_setBackground;


// method getImage
function BackgroundImage_getImage()
{
	var imageIndex   = this.getIndex();

	var imageSource  = this.imageDirectory + this.imagesArray[imageIndex];
	var displayImage = new Image();
	displayImage.src = imageSource;

	return imageSource;
}
BackgroundImage.prototype.getImage = BackgroundImage_getImage;


// method getIndex
function BackgroundImage_getIndex()
{
	var index;
	var tempIndex;
	var nextIndex;
	var indexValue;
	var cookieName   = 'imageIndex';
	var imagesLength = this.imagesArray.length;

	if( ( document.cookie ).indexOf( cookieName ) != -1 )
	{
		var cookiesArray = ( document.cookie ).split( ';' );

		for( var i = 0; i < cookiesArray.length; i++ )
		{
			if( cookiesArray[i].indexOf( cookieName ) != -1 )
			{
				indexValue = cookiesArray[i].split( '=' )[1];
				index      = parseInt( indexValue );
				nextIndex  = ( index + 1 < imagesLength ) ? index + 1 : 0;
				document.cookie = cookieName + '=' + nextIndex;
				break;
			}
		}
	}
	else
	{
		document.cookie = 'imageIndex=0';

		if( ( document.cookie ).indexOf( cookieName ) != -1 )
		{
			var cookiesArray = ( document.cookie ).split( ';' );

			for( var i = 0; i < cookiesArray.length; i++ )
			{
				if( cookiesArray[i].indexOf( cookieName ) != -1 )
				{
					indexValue = cookiesArray[i].split( '=' )[1];
					index      = parseInt( indexValue );
					nextIndex  = ( index + 1 < imagesLength ) ? index + 1 : 0;
					document.cookie = cookieName + '=' + nextIndex;
					break;
				}
			}
		}
		else
		{
			tempIndex = Math.floor( Math.random() * ( imagesLength ) );
			index     = ( tempIndex < imagesLength ) ? tempIndex : 0;
		}
	}
	return index;
}
BackgroundImage.prototype.getIndex = BackgroundImage_getIndex;

