// first ensure a placeholder image is displayed to avoid one image appearing, then rapidly disappearing (looks rubbish!)
var placeholderImg = "http://collections.europarchive.org/tna/20090805045019/http://www.nationalarchives.gov.uk/images/home-img/placeholder.gif";
var placeholderAlt = "Bringing history to life through UK government records";
var theString;
theString = "<img src=\"" + placeholderImg + "\" alt=\"" + placeholderAlt + "\" width=\"640\" height=\"188\" />";
document.write( theString );

function addLoadEvent(func) {
    // executes JavaScript functions after browser window has loaded
    // another copy of this function is used in hdr-search-utils.js
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function doContentRotate() {
    var contentContainer // div containing the content to be rotated
    var contentTitle // title attribute of contentContainer
    var contentImage // the image to be changed
    var contentText // the text to be changed
    var imgArray = Array("http://collections.europarchive.org/tna/20090805045019/http://www.nationalarchives.gov.uk/images/home-img/bg-laptop.jpg", "http://collections.europarchive.org/tna/20090805045019/http://www.nationalarchives.gov.uk/images/home-img/bg-book.jpg", "http://collections.europarchive.org/tna/20090805045019/http://www.nationalarchives.gov.uk/images/home-img/bg-domesday.jpg", "http://collections.europarchive.org/tna/20090805045019/http://www.nationalarchives.gov.uk/images/home-img/bg-repository.jpg");
    var txtArray = Array("1,000 years of government information,<br />from parchment to websites", "Shaping the way UK government<br />information is managed", "Bringing history to life through UK<br />government records");
    
    contentContainer = document.getElementById("branding");
    if (contentContainer) {
        contentTitle = contentContainer.getAttribute("title");
        contentImage = contentContainer.getElementsByTagName("img")[0];
        contentText = contentContainer.getElementsByTagName("h1")[0];
        if (contentImage && contentText) {
            var newImg = imgArray[randomContentSelect(imgArray.length)];
            var newTxt = txtArray[randomContentSelect(txtArray.length)];
            contentText.innerHTML = newTxt;
            if (contentTitle) {
                // contentText contains a break - this needs to be removed for use as a title
                contentTitle = stripHTML(newTxt);
                contentContainer.setAttribute("title", contentTitle);
                contentImage.setAttribute("alt", contentTitle);
            }
            contentImage.src = newImg;
        }
    }
}

function randomContentSelect(arrLength) {
    var arrIndex;
    
    arrIndex = Math.floor(Math.random(Math.random()) * arrLength );
    return arrIndex;
}
function stripHTML(theString) {
    return theString.replace(/<br \/>/, " ");
}

addLoadEvent(doContentRotate);

