﻿// first ensure a placeholder image is displayed to avoid one image appearing, then rapidly disappearing (looks rubbish!)
var placeholderImg = "/images/home/placeholder.gif";
var placeholderAlt = "Bringing history to life through UK government records";
var educationString;
var recordsString;
var infoManString;
// create the strings to replace the noscript on the html page
educationString = "<img src=\"" + placeholderImg + "\" alt=\"" + placeholderAlt + "\" width=\"280\" height=\"290\" />";
recordsString = "<img src=\"" + placeholderImg + "\" alt=\"" + placeholderAlt + "\" width=\"280\" height=\"290\" />";
infoManString = "<img src=\"" + placeholderImg + "\" alt=\"" + placeholderAlt + "\" width=\"280\" height=\"290\" />";

// create the paths to the code to be replaced in each section's list item
var temp = document.getElementById( "education-branding" );
// image is the first a href tag in the list
var educationBranding = temp.getElementsByTagName('a')[0];
// text is the third a href tag in the list
var educationTextArea = temp.getElementsByTagName('a')[2];
var temp = document.getElementById( "records-branding" );
var recordsBranding = temp.getElementsByTagName('a')[0];
var recordsTextArea = temp.getElementsByTagName('a')[2];
var temp = document.getElementById( "info-management-branding" );
var infoManBranding = temp.getElementsByTagName('a')[0];
var infoManTextArea = temp.getElementsByTagName('a')[2];
// replace the static image with a placeholder
educationBranding.innerHTML = educationString;
recordsBranding.innerHTML = recordsString;
infoManBranding.innerHTML = infoManString;
// write the text to the page again
educationTextArea.innerHTML = educationTextArea.innerHTML;
recordsTextArea.innerHTML = recordsTextArea.innerHTML;
infoManTextArea.innerHTML = infoManTextArea.innerHTML;
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 familyMember; // in this case, the Homepage
    var xmlDoc;
    var temp;
    var educationImgArray = new Array();
    var recordsImgArray = new Array();
    var infoManagementImgArray = new Array();
    var educationTextArray = new Array();
    var recordsTextArray = new Array();
    var infoManagementTextArray = new Array();
    var txtArray = new Array();
    var outputString = "";
    
    familyMember = "homepage";
    
    //contentContainer = document.getElementById("banner");
    // get XML in
    // modern, decent, non-IE6 browsers
      if ( window.XMLHttpRequest ) {
          xmlDoc = new window.XMLHttpRequest();
          xmlDoc.open( "GET", "/xml/rotate-details.xml", false );
          xmlDoc.send( "" );
          xmlDoc=xmlDoc.responseXML;
      }
      
      // IE 6
      else try {
          xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
          xmlDoc.async = false;
          xmlDoc.load( "/xml/rotate-details.xml" );
      }
      
      // catch unknown browser error - prevent a JavaScript error being thrown
      catch(e) {
          // something bizarre has happened - run away
      }
  
      temp = xmlDoc.getElementsByTagName( "listItem" );
      //instatiate the extra counter for the 3 different image arrays for images loop
      var j=0;
      var l=0;
      var m=0;
      // sort through the xml file and pull out any image or text items - then place into the correct array
      for( i=0; i<temp.length; i++ ) {
          // select all list items with the same family as the page
          if( temp[i].getElementsByTagName( "category" )[0].childNodes[0].nodeValue.toLowerCase() == familyMember ) {
              
              if (  temp[i].getElementsByTagName( "type" )[0].childNodes[0].nodeValue.toLowerCase() == "image" ) {
                  
                  // IF statement for each Education homepage image
                  if (  temp[i].getElementsByTagName( "educationImg" )[0].childNodes[0].nodeValue ) {
                      
                  //add to imgArray array
                  educationImgArray[j] = temp[i];
                  // find out if this image has intro text attached to it
                  var element = temp[i].getElementsByTagName( "educationText" )[0];
                  
                          if ( element.hasChildNodes() ) {
                            // if there, add to imgArray array
                              educationTextArray[j] = temp[i].getElementsByTagName( "educationText" )[0].childNodes[0].nodeValue;
                        }
                        else {
                            // else add default from top of JS file
                            educationTextArray[j] = educationTextArea.innerHTML;                    
                        }
                        
                  //increase the count of the new image array
                  j++;
                  
                  }
                  
                  // IF statement for each Records homepage image
                  if (  temp[i].getElementsByTagName( "recordsImg" )[0].childNodes[0].nodeValue ) {
                      
                  //add to imgArray array
                  recordsImgArray[l] = temp[i];    
                  // find out if this image has intro text attached to it
                  var element = temp[i].getElementsByTagName( "recordsText" )[0];
                  
                          if ( element.hasChildNodes() ) {
                            // if there, add to imgArray array
                              recordsTextArray[l] = temp[i].getElementsByTagName( "recordsText" )[0].childNodes[0].nodeValue;
                        }
                        else {
                            // else add default from top of JS file
                            recordsTextArray[l] = recordsTextArea.innerHTML;                    
                        }
                        
                  //increase the count of the new image array
                  l++;
                  }
                  
                  // IF statement for each Info Management homepage image
                  if (  temp[i].getElementsByTagName( "infoManagementImg" )[0].childNodes[0].nodeValue ) {
                  //add to imgArray array
                  infoManagementImgArray[m] = temp[i];
                  // find out if this image has intro text attached to it
                  var element = temp[i].getElementsByTagName( "infoManagementText" )[0];
                  
                          if ( element.hasChildNodes() ) {
                            // if there, add to imgArray array
                              infoManagementTextArray[m] = temp[i].getElementsByTagName( "infoManagementText" )[0].childNodes[0].nodeValue;
                        }
                        else {
                            // else add default from top of JS file
                            infoManagementTextArray[m] = infoManTextArea.innerHTML;                    
                        }
                        
                  //increase the count of the new image array
                  m++;
                  }
              }
          }
          
      }
      
      // find lowest complete "set"
      var lowestNumber = Math.min(Math.min(educationImgArray.length, recordsImgArray.length),infoManagementImgArray.length);
      // generate random number using the length of the lowest complete set
      var randomNumber = randomContentSelect(lowestNumber);
      //alert(randomNumber);
      
        // write the image string for each image into the page
        educationBranding.innerHTML = "<img src=\"" + educationImgArray[randomNumber].getElementsByTagName( "educationImg" )[0].childNodes[0].nodeValue + "\" alt=\"" + educationImgArray[randomNumber].getElementsByTagName( "educationAlt" )[0].childNodes[0].nodeValue + "\" width=\"280\" height=\"290\" />";
        recordsBranding.innerHTML = "<img src=\"" + recordsImgArray[randomNumber].getElementsByTagName( "recordsImg" )[0].childNodes[0].nodeValue + "\" alt=\"" + educationImgArray[randomNumber].getElementsByTagName( "recordsAlt" )[0].childNodes[0].nodeValue + "\" width=\"280\" height=\"290\" />";
        infoManBranding.innerHTML = "<img src=\"" + infoManagementImgArray[randomNumber].getElementsByTagName( "infoManagementImg" )[0].childNodes[0].nodeValue + "\" alt=\"" + educationImgArray[randomNumber].getElementsByTagName( "infoManagementAlt" )[0].childNodes[0].nodeValue + "\" width=\"280\" height=\"290\" />";
        
        // write the text string for the intro text into the page
        educationTextArea.innerHTML = educationTextArray[randomNumber];
        recordsTextArea.innerHTML = recordsTextArray[randomNumber];
        infoManTextArea.innerHTML = infoManagementTextArray[randomNumber];
}
function randomContentSelect(arrLength) {
    var arrIndex;
    
    arrIndex = Math.floor(Math.random(Math.random()) * arrLength );
    return arrIndex;
}
function stripHTML(theString) {
    return theString.replace(/<br \/>/, " ");
}
addLoadEvent(doContentRotate);
