var frontpageZoomMainPhotoDone = false;

function mydebug (message) // x
{ // x
    if ($defined(console)) // x
    { // x
        console.debug(message); // x
    } // x
} // x

var leftSideBarWidth = 220;
var rightSideBarWidth = 220;

function centreTheBanner()
{
    if ($('h1title'))
    {
    	var bannerImageWidth2 = 400; // 800x150
    	var signBoardLeft = 219; // measured off the bg image
    	var signBoardWidth2 = 375/2; // measured off the bg image
        var w2 = window.getWidth() / 2;
        var left = ((w2 > bannerImageWidth2) ? w2 - bannerImageWidth2 : 0).toInt();
        var h1titleimg = $('h1title').getElements('img')[0];
        var width = parseInt(h1titleimg.getProperty('width'));
        var h1imgpadleft = signBoardWidth2 - (width / 2);

        mydebug("centreTheBanner " + left + " " + h1imgpadleft); // x

        $('header').style.backgroundPosition = left + "px 0";
        $('h1title').style.left = (signBoardLeft + left) + "px";
        h1titleimg.style.paddingLeft = h1imgpadleft + "px";
        // x $('topNavigation').style.marginLeft = left + "px";
        // x $('bottomNavigation').style.marginLeft = left + "px";
    }
}

function frontpageZoomMainPhoto()
{
    if (frontpageZoomMainPhotoDone)
    {
        return;
    }

    var mainPhoto = $('mainPhoto');
    if (mainPhoto)
    {
    	//
    	// This is hardwired for 768x576 photos.
    	//
        var box = mainPhoto.parentNode.parentNode;
        // subtract a value determined empirically (approx width of both sidebars)
        var bothSidebars = leftSideBarWidth + rightSideBarWidth;
        var w = window.getWidth() - bothSidebars;
        var border = 20;
        var allowedWidth = 768 + border;
        if (w < allowedWidth)
        {
            // shrink to fit
        	w -= border;
            var h = (w * 3) / 4;
            mydebug("mainPhoto scaled to " + w + "x" + h); // x
            mainPhoto.width = w;
            mainPhoto.height = h;
            box.style.width = (w + 10) + "px";
            box.style.height = (h + 10) + "px";
            box.parentNode.style.paddingLeft = "0";
        }
        else
        {
            // position centrally
            mainPhoto.width = 768;
            mainPhoto.height = 576;
            box.style.width = allowedWidth + "px";
            box.style.height = "586px";
            var r = (w - allowedWidth) / 2;
            mydebug("mainPhoto moved right to " + r); // x
            box.parentNode.style.paddingLeft = r + "px";
        }
        mainPhoto.style.display = "inline";
    }
//    frontpageZoomMainPhotoDone = true;
}

function fettleTheLayout()
{
    if ($defined(console)) // x
    { // x
        console.time("fettleTheLayout"); // x
    } // x
    centreTheBanner();
    frontpageZoomMainPhoto();
    if ($defined(console)) // x
    { // x
        console.timeEnd("fettleTheLayout"); // x
    } // x
}

window.addEvent('domready', frontpageZoomMainPhoto);
window.addEvent('resize', fettleTheLayout);
