var pageWidth;
var pageHeight;

var isIE = (document.getElementById && document.all)?true:false;
var isNS4 = (document.layers)?true:false;
var isNS6 = (document.getElementById && !document.all)?true:false;

function doBackGroundStuff()
{    
    var img_name = new Array(
        "bg1",
	"bg2",
        "bg3",
        "bg5"
    );

    var l = img_name.length;
    var rnd_no = Math.floor(l * Math.random());

    var imagePath = '/bgimg/' + img_name[rnd_no] + '.jpg';
   
    document.getElementById("background_image").src = imagePath;
}

function myGetElementById (id)
{
    if ( isIE )
    {
	var str = "document.all('" + id + "')";
	var o = eval(str);
	return o;
    }

    return document.getElementById(id);
}

function updateBackground (id)
{
    /*var obj = myGetElementById(id);
    if ( obj )
    {
	var newBgObj = myGetElementById("PAGE_BACKGROUND");
	var bgObj = myGetElementById("background_image");
    
	if ( newBgObj && bgObj )
	{
	    //var src = newBgObj.getAttribute("src");
	    var src = "/fanbase_access/images_store/tomjones/authorised/Site Backgrounds/" + randomImage;
	    
	    if ( src )
	    {
		setBackground (src,
		    newBgObj.getAttribute("width"),
		    newBgObj.getAttribute("height"));
	    }
	}
    }*/
}

function setBackground (url, width, height)
{
    var obj = myGetElementById ('background_image');
    if ( obj )
    {
	obj.onload = function () { 
	    if ( width ) this.style.width = width;
	    if ( height ) this.style.height = height;
	    findDimensions();
	}
	obj.src = url;
    }
}

function findDimensions ()
{
    var frameWidth;
    var frameHeight;
    var obj;

    if (self.innerWidth)
    {
	frameWidth = self.innerWidth;
	frameHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
	frameWidth = document.documentElement.clientWidth;
	frameHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
	frameWidth = document.body.clientWidth;
	frameHeight = document.body.clientHeight;
    }
    else
	return;

    pageWidth = frameWidth;
    pageHeight = frameHeight;

    if ( (obj = myGetElementById('background')) )
    {
	//============================================
    //alert("setting background to " + pageWidth + "px wide, and " + pageHeight + "px tall");
	obj.style.width = frameWidth;
	obj.style.height = frameHeight;
    }

    if ( (obj = myGetElementById('background_image')) )
    {
	var iWidth = obj.width; 
	var iHeight = obj.height;
    //=========================
	//alert("setting background_image to " + iWidth + "px wide, and " + iHeight + "px tall (with ratio)");
	var iRatio = iWidth / iHeight;
	var fRatio = frameWidth / frameHeight;	

	obj.style.top = obj.style.left = 0;

	if ( iRatio < fRatio ) 
	{
	    obj.style.width = frameWidth;
	    obj.style.height = frameWidth / iRatio;
	} 
	else 
	{
	    obj.style.height = frameHeight;
	    obj.style.width = frameHeight * iRatio;
	}
    }

    doBackGroundStuff();
	//alert("page is " + pageWidth + "px wide, and " + pageHeight + "px tall");
}