var ov = [];
var prices = [];
var select2 = [];
var opts;
var prices = [];
var optsArrayLength;

var usableBrowser = ( document.createElement && document.getElementsByTagName );

window.onload = function ()
{
	if ( usableBrowser )
	{
		document.getElementById( 'options' ).style.display = 'none';

		if ( document.getElementById( 'optionsform' ) )
		{
			setupOptions();
		}

		document.getElementById( 'options' ).style.display = 'block';


		if ( document.getElementById( 'otherviews' ) )
		{
			setupRollover();
		}
	}
}


function setupRollover()
{
	mainImageSrc = document.getElementById( 'mainimageimg' ).src;

	mainImage = new Image();
	mainImage.src = mainImageSrc;

	i = 0;
	while ( i < 3 )
	{
		thisImage = document.getElementById( 'ov'+i );
		if ( thisImage )
		{
			thisImage.parentNode.href = '#';
			thisImage.onmouseover = showOther;
			thisImage.onmouseout = showMain;

			thisImage.style.cursor = 'default';

			src = thisImage.src ;
			ov[i] = new Image();
			ov[i].src = src.replace( 'thumbnails' , 'resized' );
		}
		i++;
	}
}

function showOther()
{
	i = this.id.slice( -1 );
	document.getElementById( 'mainimageimg' ).src = ov[i].src;
}

function showMain( e )
{
	document.getElementById( 'mainimageimg' ).src = mainImage.src;
}



function setupOptions()
{
	opts = document.getElementById( 'options' );
	optsJson = opts.childNodes[ 0 ].nodeValue;
	if ( optsJson && ( optsJson.charAt( 1 ) !== '{' ) )
	{
		optsJson = opts.childNodes[ 1 ].nodeValue;
	}
	var optsArray = eval( '(' + optsJson + ')' );

	optsArrayLength = 0;
	for ( opt in optsArray )
	{
		optsArrayLength++;
	}

	document.getElementById( 'optionsform' ).removeChild( document.getElementById( 'optionstable' ) );
	document.getElementById( 'optionsform' ).removeChild( document.getElementById( 'addtobasket' ) );

	newopts = document.createElement( 'div' );
	newopts.id = 'optspb';
	newopts.innerHTML = "<div id='optsprice'><span id='currentprice'> </span></div>"+
						"<input type='hidden' name='js' value='js' />"+
						"<input id='optsbutton' value='Add To Cart' type='image' src='/bijou-artwork/AddToCart-25.png' onclick='doSubmit()' />";
	document.getElementById( 'optionsform' ).appendChild( newopts );

	select1 = document.createElement( 'select' );
	select1.id = 'select1';
	select1.onchange = opt1change;

	if ( optsArrayLength > 1 )
	{
		for ( option1 in optsArray )
		{
			var thisOpt = document.createElement( 'option' );
			thisOpt.value = option1
			thisOpt.innerHTML = ucWords( option1 );
			select1.appendChild( thisOpt );
		}
		document.getElementById( 'optspb' ).parentNode.insertBefore( select1 , document.getElementById( 'optspb' ) );

		for ( option1 in optsArray )
		{
			select2[ option1 ] = document.createElement( 'select' );
			select2[ option1 ].id = 'select2';
			select2[ option1 ].name = 'option';
			select2[ option1 ].onchange = opt2change;
			subopts = optsArray[ option1 ];
			for ( option2 in subopts )
			{
				var thisOpt = document.createElement( 'option' );
				thisOpt.value = subopts[ option2 ].id;
				thisOpt.innerHTML = subopts[ option2 ].subtype;
				select2[ option1 ].appendChild( thisOpt );
				prices[ subopts[ option2 ].id ] = subopts[ option2 ].price;
			}
		}

			document.getElementById( 'optspb' ).parentNode.insertBefore( select2[ document.getElementById( 'select1' ).value ] , document.getElementById( 'optspb' ) );
	}
	else
	{
		for ( option1 in optsArray )
		{
			select2[ 0 ] = document.createElement( 'select' );
			select2[ 0 ].id = 'select2';
			select2[ 0 ].name = 'option';
			select2[ 0 ].onchange = opt2change;
			subopts = optsArray[ option1 ];
			for ( option2 in subopts )
			{
				var thisOpt = document.createElement( 'option' );
				thisOpt.value = subopts[ option2 ].id;
				thisOpt.innerHTML = subopts[ option2 ].subtype;
				select2[ 0 ].appendChild( thisOpt );
				prices[ subopts[ option2 ].id ] = subopts[ option2 ].price;
			}
		}

		document.getElementById( 'optspb' ).parentNode.insertBefore( select2[ 0 ] , document.getElementById( 'optspb' ) );
	}

	priceNow = prices[ document.getElementById( 'select2' ).value ] / 100;
	document.getElementById( 'currentprice' ).innerHTML = "&pound;" + priceNow.toFixed( 2 );
}


function opt1change()
{
	document.getElementById( 'select2' ).parentNode.removeChild( document.getElementById( 'select2' ) );
	if ( document.getElementById( 'select1' ) )
	{
		document.getElementById( 'optspb' ).parentNode.insertBefore( select2[ document.getElementById( 'select1' ).value ] , document.getElementById( 'optspb' ) );
	}
	else
	{
		document.getElementById( 'optspb' ).parentNode.insertBefore( select2[ 0 ] , document.getElementById( 'optspb' ) );
	}

	opt2change();
}


function opt2change()
{
	priceNow = prices[ document.getElementById( 'select2' ).value ] / 100;
	document.getElementById( 'currentprice' ).innerHTML = "&pound;" + priceNow.toFixed( 2 );

}

function doSubmit()
{
}


function ucWords( str )
{
   arrStr = str.split( ' ' );
   var strOut = '';

   for ( i = 0 ; i < arrStr.length ; i++ )
   {
       firstChar = arrStr[ i ].substring( 0 , 1 );
       remainChar = arrStr[ i ].substring( 1 );

       firstChar = firstChar.toUpperCase();
       remainChar = remainChar.toLowerCase();

       strOut += firstChar + remainChar + ' ';
   }
   return strOut.substring( 0 , strOut.length - 1 );
}

