<!--
/* This script is Copyright and cannot be used without written permission from Bright Spells Ltd */
var browserDisplaySize
var banners
var imagesToResize
var rowsWithImages
var bannerInterval = 50
var bannerSpacing = 3

function load()
{
 if ( isBrowserDisplaySizeRequired() ) browserDisplaySize = getBrowserDisplaySize()
 setupBannersStage1()
 resizeImagesStage1()
 setupBannersStage2()
 resizeImagesStage2()
 if ( banners != null || imagesToResize != null ) window.onresize = resizeevent
}

function isBrowserDisplaySizeRequired()
{
 if ( anyImagesRequireResize() ) return true
 if ( anyBanners() ) return true
 return false
}

function anyImagesRequireResize()
{
 for ( var index = 0; index < document.images.length; index++ )
 {
  var anImage = document.images[index]
  if ( anImage.className == "fitWidth" )
  {
   var imageContainer = anImage
   while ( imageContainer.tagName != "TD" )
   {
    imageContainer = imageContainer.parentNode
    if ( imageContainer == null ) break
   }
   if ( imageContainer == null ) continue
  }
  else if ( anImage.className != "fitScreenImage" ) continue
  return true
 }
 return false
}

function anyBanners()
{
 return anyBannersRecurse(document.body)
}

function anyBannersRecurse(anObject)
{
 if ( anObject.className == "banner" ) return true
 else
 {
  var childNodes = anObject.childNodes
  for ( var i = 0; i < childNodes.length; i++ )
  {
   if ( anyBannersRecurse(childNodes[i]) ) return true
  }
 }
 return false
}

function setupBannersStage1()
{
 setupBannersRecurse(document.body)
}

function setupBannersRecurse(anObject)
{
 if ( anObject.className == "banner" ) setupBannerStage1(anObject)
 else
 {
  var childNodes = anObject.childNodes
  for ( var i = 0; i < childNodes.length; i++ ) setupBannersRecurse(childNodes[i])
 }
}

function setupBannerStage1(banner)
{
 var bannerDetails = new Object
 bannerDetails.initialBannerObject = banner
 bannerDetails.bannerHTML = banner.innerHTML
 if ( banners == null ) banners = new Array
 banners[banners.length] = bannerDetails
 banner.innerHTML = ""
}

function setupBannersStage2()
{
 if ( banners == null ) return
 for ( var i = 0; i < banners.length; i++ ) setupBannerStage2(banners[i])
 setInterval(moveBanners,bannerInterval)
}

function setupBannerStage2(bannerDetails)
{
 var banner = bannerDetails.initialBannerObject
  
 var outerBanner = document.createElement("div")
 outerBanner.style.border = "0px"
 outerBanner.style.margin = "0px"
 outerBanner.style.padding = "0px"
 outerBanner.style.overflow = "hidden"
 var bannerInitialWidth = banner.offsetWidth
 var bannerInitialHeight = banner.offsetHeight
 outerBanner.style.width = String(bannerInitialWidth)+"px"
 banner.insertBefore(outerBanner,null)
 var bannerNewWidth = banner.offsetWidth
 outerBanner.style.width = String(bannerInitialWidth-(bannerNewWidth-bannerInitialWidth))+"px"

 bannerDetails.bannerObject = document.createElement("div")
 bannerDetails.bannerObject.style.border = "0px"
 bannerDetails.bannerObject.style.margin = "0px"
 bannerDetails.bannerObject.style.padding = "0px"
 bannerDetails.bannerObject.style.width = outerBanner.style.width
 bannerDetails.bannerObject.style.overflow = "hidden"
 outerBanner.insertBefore(bannerDetails.bannerObject,null)

 bannerDetails.bannerObject.innerHTML="." 
// var actualBannerInitialOffsetWidth = banner.offsetWidth
 var actualBannerInitialOffsetWidth = bannerDetails.bannerObject.offsetWidth
 var htmlToInsert = ""
 var insertsRequired = 1
 var actualWidthOfContent = 0
 do
 {
  htmlToInsert = htmlToInsert + bannerDetails.bannerHTML
  bannerDetails.bannerObject.innerHTML = "<nobr>"+htmlToInsert+"</nobr>"
  actualWidthOfContent = getTotalOffsetWidthOfChildren(bannerDetails.bannerObject)
  if ( actualWidthOfContent > actualBannerInitialOffsetWidth ) break
  insertsRequired++
 }
 while ( true )
 insertsRequired++
 htmlToInsert = htmlToInsert + bannerDetails.bannerHTML
 bannerDetails.bannerObject.innerHTML = "<nobr>"+htmlToInsert+"</nobr>"
 actualWidthOfContent = getTotalOffsetWidthOfChildren(bannerDetails.bannerObject)
  
 bannerDetails.contentWidth = parseInt(String(actualWidthOfContent / insertsRequired))
 if ( actualWidthOfContent % insertsRequired != 0 ) bannerDetails.contentWidth++
 
 bannerDetails.interval = parseInt(banner.getAttribute("interval"))
 if ( bannerDetails.interval == null || isNaN(bannerDetails.interval) ) bannerDetails.interval = bannerInterval
 bannerDetails.intervalCount = 0 
}

function getTotalOffsetWidthOfChildren(anObject)
{
 var totalOffsetWidth = 0
 for ( var i = 0; i < anObject.childNodes.length; i++ )
  if ( anObject.childNodes[i].offsetWidth > 0 )
   totalOffsetWidth += anObject.childNodes[i].offsetWidth
 return totalOffsetWidth
}

function moveBanners()
{
 for ( var index = 0; index < banners.length; index++ )
 {
  var bannerDetails = banners[index]
  bannerDetails.intervalCount += bannerInterval;
  if ( bannerDetails.intervalCount < bannerDetails.interval ) continue
  bannerDetails.intervalCount = 0
  var banner = bannerDetails.bannerObject
  var currentLeftStyle = 0
  if ( banner.style.marginLeft != null && banner.style.marginLeft.length > 2 )
   currentLeftStyle = parseInt(banner.style.marginLeft.substring(0,banner.style.marginLeft.length-2))
  var currentWidth = parseInt(banner.style.width.substring(0,banner.style.width.length-2))
  var newLeftStyle = currentLeftStyle - bannerSpacing
  var newWidth = currentWidth + bannerSpacing
  if ( -newLeftStyle >= bannerDetails.contentWidth )
  {
   newWidth = newWidth + newLeftStyle
   newLeftStyle = 0
  }
  banner.style.marginLeft = String(newLeftStyle)+"px"
  banner.style.width = String(newWidth)+"px"
 }
}

function resizeImagesStage1()
{
 for ( var index = 0; index < document.images.length; index++ )
 {
  var anImage = document.images[index]
  if ( anImage.className == "fitWidth" )
  {
   var imageContainer = anImage
   while ( imageContainer.tagName != "TD" )
   {
    imageContainer = imageContainer.parentNode
    if ( imageContainer == null ) break
   }
   if ( imageContainer == null ) continue
  }
  else if ( anImage.className != "fitScreenImage" ) continue
  var imageToResize = new Object
  imageToResize.imageObject = anImage
  imageToResize.actualWidth = anImage.width
  imageToResize.actualHeight = anImage.height
  if ( imagesToResize == null ) imagesToResize = new Array
  imagesToResize[imagesToResize.length] = imageToResize
  setMaxHeightForImageRow(anImage)
  anImage.width = 1
  anImage.height = 1
 }
}

function setMaxHeightForImageRow(anImage)
{
 var imageRow = getImageRow(anImage)
 if ( imageRow == null ) return
 imageRowData = getImageRowData(imageRow)
 if ( imageRowData == null )
 {
  if ( rowsWithImages == null ) rowsWithImages = new Array
  imageRowData = new Object
  imageRowData.row = imageRow
  rowsWithImages[rowsWithImages.length] = imageRowData
 }
 var anImageHeight = anImage.height
 if ( anImageHeight == null || anImageHeight < 1 ) anImageHeight = 99999
 if ( imageRowData.maxHeight == null || imageRowData.maxHeight < anImageHeight)
  imageRowData.maxHeight = anImageHeight
}

function getImageRow(anImage)
{
 var imageContainer = anImage
 while ( (imageContainer = imageContainer.parentNode) != null )
 {
  
  if ( imageContainer.tagName == "TR" ) return imageContainer
 }
 return null
}

function getImageRowData(row)
{
 if ( rowsWithImages == null ) return null
 for ( var index = 0; index < rowsWithImages .length; index++ )
 {
  var imageRowData = rowsWithImages[index]
  if ( imageRowData.row == row ) return imageRowData
 }
 return null
}

function getImageRowMaxHeight(row)
{
 var imageRowData = getImageRowData(row)
 if ( imageRowData != null ) return imageRowData.maxHeight
 return null
}

function resizeImagesStage2()
{
 if ( imagesToResize == null ) return
 document.body.scrollTop = 0
 document.body.scrollLeft = 0
// resizeRowsWithImagesToFitScreen()
 resizeToFitScreen()
// resizeToFitCell()
 resizeToFitWidth()
// document.body.scrollTop = 0
// document.body.scrollLeft = 0
}

function resizeRowsWithImagesToFitScreen()
{
// alert(document.body.offsetHeight)
 var tableRows = new Array
 tableRows[0] = document.body
 resizeRowsWithImagesToFitScreenRecurse(tableRows)
// alert(document.body.offsetHeight)
}

function resizeRowsWithImagesToFitScreenRecurse(tableRows)
{
 var newTableRows = getNextLevelOfTableRows(tableRows)
 if ( newTableRows.length == 0 ) return
 var minHeight = getStartingRowHeight(newTableRows)
 for ( var index = 0; index < newTableRows.length; index++ )
 {
  var aRow = newTableRows[index]
  aRow.style.height = String(minHeight)+"px"
 }
 var carryOn = true
 do
 {
  for ( var index = 0; index < newTableRows.length; index++ )
  {
   var aRow = newTableRows[index]
   //var containingRow = getContainingRow(aRow)
   // stop if current row greater than or equal to maximum size of biggest image in row
   if ( getCurrentHeight(aRow) != null && getImageRowMaxHeight(aRow) != null && getCurrentHeight(aRow) >= getImageRowMaxHeight(aRow) )
   {
    carryOn = false
    break
   }
    var documentHeight = document.getElementById('mainContent').offsetHeight
    if ( documentHeight < browserDisplaySize.height ) documentHeight = browserDisplaySize.height
    incrementObjectHeight(aRow)
    if ( document.getElementById('mainContent').scrollHeight > document.getElementById('mainContent').clientHeight)
    {
     decrementObjectHeight(aRow)
 //    document.getElementById('mainContent').style.overflow="hidden"
     carryOn = false 
     break
    }
  }
 }
 while ( carryOn )
 resizeRowsWithImagesToFitScreenRecurse(newTableRows)
}

function getStartingRowHeight(tableRows)
{
 var minimum = 999999
 for ( var i = 0; i < tableRows.length; i++ )
 {
  var tableRow = tableRows[i]
  var offsetHeight = tableRow.offsetHeight
  var oldStyleHeight = tableRow.style.height
  tableRow.style.height = String(offsetHeight)+"px"
  var newOffsetHeight = tableRow.offsetHeight
  var requiredHeight = offsetHeight-(newOffsetHeight-offsetHeight)
  tableRow.style.height = oldStyleHeight
  if ( requiredHeight < minimum ) minimum = requiredHeight
 }
 return minimum
}

function getContainingRow(anObject)
{
 var containingRow = anObject
 do
 {
   containingRow = containingRow.parentNode
   if ( containingRow == null ) return null
 }
 while ( containingRow.tagName != "TR" )
 return containingRow
}

function incrementObjectHeight(anObject)
{
 var currentHeight = getCurrentHeight(anObject)
 currentHeight = currentHeight+1
 anObject.style.height = String(currentHeight)+"px"
 if ( anObject.tagName != "TR" ) return
 var tableCell = getTableCellObject(anObject)
 tableCell.style.height = String(currentHeight)+"px"
}

function getTableCellObject(anObject)
{
 var childNodes = anObject.childNodes
 if ( childNodes == null ) return
 for ( var i = 0; i < childNodes.length; i++ )
 {
  var currentNode = childNodes[i]
  if ( currentNode.tagName == "TD" ) return currentNode;
 }
 for ( var i = 0; i < childNodes.length; i++ )
 {
  var currentNode = childNodes[i]
  if ( currentNode.tagName == "TR" ) return currentNode;
  if ( currentNode.tagName == "TABLE" ) return currentNode;
  var tableCellObject = getTableCellObject(currentNode)
  if ( tableCellObject != null ) return tableCellObject
 }
 return null; 
}

function decrementObjectHeight(anObject)
{
 var currentHeight = getCurrentHeight(anObject)
 currentHeight = currentHeight-1
 anObject.style.height = String(currentHeight)+"px"
}

function getCurrentHeight(anObject)
{
 var objectStyle = anObject.style
 if ( objectStyle == null ) return 0
 var currentHeightString = objectStyle.height
 if ( currentHeightString == null ) return 0
 if ( currentHeightString.length < 3 ) return 0
 return parseFloat(currentHeightString.substring(0,currentHeightString.length-2))
}

function getNextLevelOfTableRows(tableRows)
{
 var nextLevelOfTableRows = new Array
 getNextLevelOfTableRowsRecurse(tableRows,nextLevelOfTableRows)
 return nextLevelOfTableRows
}

function getNextLevelOfTableRowsRecurse(tableRows,nextLevelOfTableRows)
{
 var nodesToGoDeeperInto = new Array
 for ( var i = 0; i < tableRows.length; i++ )
 {
  var currentNode = tableRows[i]
  var childNodes = currentNode.childNodes
  for ( var j = 0; j < childNodes.length; j++ )
  {
   var currentChildNode = childNodes[j]
   if ( currentChildNode.className == "fitScreenImageContainer" )
    nextLevelOfTableRows[nextLevelOfTableRows.length] = currentChildNode
   else nodesToGoDeeperInto[nodesToGoDeeperInto.length] = currentChildNode
  }
 }
 if ( nodesToGoDeeperInto.length > 0 )
 getNextLevelOfTableRowsRecurse(nodesToGoDeeperInto,nextLevelOfTableRows)
}

function nodeDirectlyContainsImageToFitScreen(aNode)
{
 var childNodes = aNode.childNodes
 for ( var i = 0; i < childNodes.length; i++ )
 {
  var childNode = childNodes[i]
  if ( childNode.tagName == "IMG" && childNode.className == "fitScreenImage" ) return true
  if ( childNode.tagName == "TR" ) continue
  if ( nodeDirectlyContainsImageToFitScreen(childNode) ) return true
 }
 return false
}

function nodeContainsImageToFitScreen(aNode)
{
 if ( aNode.tagName == "IMG" && aNode.className == "fitScreen" ) return true
 var childNodes = aNode.childNodes
 for ( var i = 0; i < childNodes.length; i++ )
 {
  if ( nodeContainsImageToFitScreen(childNodes[i]) ) return true
 }
 return false
}

function nodeContainsTR(aNode)
{
 var childNodes = aNode.childNodes
 if ( childNodes == null ) return false
 for ( var i = 0; i < childNodes.length; i++ )
 {
  if ( childNodes[i].tagName == "TR" ) return true;
  if ( nodeContainsTR(childNodes[i]) ) return true
 }
 return false
}

function getBrowserDisplaySize()
{
 var returnValue = new Object
 returnValue.width = window.innerWidth
 returnValue.height = window.innerHeight
 if ( returnValue.width == null ) returnValue.width = document.documentElement.clientWidth
 if ( returnValue.height == null ) returnValue.height = document.documentElement.clientHeight
 if ( returnValue.width == null ) returnValue.width = document.getElementsByTagName('body')[0].clientWidth
 if ( returnValue.height == null ) returnValue.height = document.getElementsByTagName('body')[0].clientHeight
 return returnValue
}

function resizeevent()
{
 window.onresize = null
 var newBrowserDisplaySize = getBrowserDisplaySize()
 if ( newBrowserDisplaySize.height != browserDisplaySize.height || newBrowserDisplaySize.width != browserDisplaySize.width )
 {
  var originalLocation = document.location
  document.location = "about:blank"
  document.location = originalLocation
  return
 }
 window.onresize = resizeevent
}

function resizeToFitWidth()
{
 for ( var index = 0; index < imagesToResize.length; index++ )
 {
  var imageToResize = imagesToResize[index]
  if ( imageToResize.imageObject.className != "fitWidth" ) continue
  resizeImageToFitWidth(imageToResize)
 }
}

function resizeImageToFitWidth(imageToResize)
{
 var imageContainer = imageToResize.imageObject.parentNode
 while ( imageContainer.tagName != "TD" )
 {
  imageContainer = imageContainer.parentNode
  if ( imageContainer == null ) return
 }
 var componentWidth = imageContainer.offsetWidth - (imageToResize.imageObject.offsetWidth - imageToResize.imageObject.width)
 if ( componentWidth <= 0 ) return
 if ( imageToResize.actualWidth <= componentWidth )
 {
  imageToResize.imageObject.width = imageToResize.actualWidth
  imageToResize.imageObject.height = imageToResize.actualHeight
 }
 else
 // Image bigger than space so scale down
 {
  imageToResize.imageObject.width = componentWidth
  imageToResize.imageObject.height = Math.round(imageToResize.actualHeight * componentWidth / imageToResize.actualWidth)
 }
}

function resizeToFitScreen()
{
 var initialScrollHeight = document.getElementById('mainContent').scrollHeight
 var initialScrollWidth = document.getElementById('mainContent').scrollWidth
 do
 {
  var anychanges = false;
  for ( var index = 0; index < imagesToResize.length; index++ )
  {
   var imageToResize = imagesToResize[index]
   if ( imageToResize.imageObject.className != "fitScreenImage" ) continue
   if ( resizeImageToFitScreen(imageToResize) ) anychanges = true
  }
  if ( ! anychanges ) break
//  if ( document.body.scrollWidth > browserDisplaySize.width || document.body.scrollHeight > browserDisplaySize.height )
//  alert ( document.getElementById('mainContent').scrollWidth +" "+ document.getElementById('mainContent').clientWidth+" "+
//	document.getElementById('mainContent').scrollHeight +" "+ document.getElementById('mainContent').clientHeight )
  if ( document.getElementById('mainContent').scrollWidth > initialScrollWidth ||
	document.getElementById('mainContent').scrollHeight > initialScrollHeight )
  {
   for ( var index = 0; index < imagesToResize.length; index++ )
   {
    var imageToResize = imagesToResize[index]
    if ( imageToResize.imageObject.className != "fitScreenImage" ) continue
    revertImageToFitScreen(imageToResize)
   }
   break	
  }
 }
 while ( true )
}

function resizeImageToFitScreen(imageToResize)
{
 var image = imageToResize.imageObject
 imageToResize.previousWidth = image.width
 imageToResize.previousHeight = image.height
 if ( image.width == imageToResize.actualWidth && image.height == imageToResize.actualHeight ) return false
 if ( image.width >= imageToResize.actualWidth || image.height >= imageToResize.actualHeight )
 {
  image.width = imageToResize.actualWidth
  image.height = imageToResize.actualHeight
 }
 else if ( imageToResize.actualWidth > imageToResize.actualHeight )
 {
  image.width = image.width + 1
  image.height = Math.round(imageToResize.actualHeight * image.width / imageToResize.actualWidth)
 }
 else
 {
  image.height = image.height + 1
  image.width = Math.round(imageToResize.actualWidth * image.height / imageToResize.actualHeight)
 }
 return true
}

function revertImageToFitScreen(imageToResize)
{
 imageToResize.imageObject.width = imageToResize.previousWidth
 imageToResize.imageObject.height = imageToResize.previousHeight
}

function resizeToFitCell()
{
 for ( var index = 0; index < imagesToResize.length; index++ )
 {
  resizeImageToFitCell(imagesToResize[index])
 }
}

function resizeImageToFitCell(imageToResize)
{
 var image = imageToResize.imageObject
 var imageContainer = image
 while ( imageContainer.tagName != "TD" )
 {
  imageContainer = imageContainer.parentNode
  if ( imageContainer == null ) return
 }
 var componentWidth = imageContainer.offsetWidth - (image.offsetWidth - image.width)
 var componentHeight = imageContainer.offsetHeight - (image.offsetHeight - image.height)
 if ( componentWidth <= 0 ) return
 if ( imageToResize.actualWidth <= componentWidth && imageToResize.actualHeight <= componentHeight)
 {
  image.width = imageToResize.actualWidth
  image.height = imageToResize.actualHeight
 }
 else if ( imageToResize.actualWidth > componentWidth && imageToResize.actualHeight <= componentHeight)
 {
  image.width = componentWidth
  image.height = Math.round(imageToResize.actualHeight * componentWidth / imageToResize.actualWidth)
 }
 else if ( imageToResize.actualWidth <= componentWidth && imageToResize.actualHeight > componentHeight)
 {
  image.width = Math.round(imageToResize.actualWidth * componentHeight / imageToResize.actualHeight)
  image.height = componentHeight
 }
 else
 // Image bigger than space so scale down
 {
  scaledHeight = Math.round(imageToResize.actualHeight * componentWidth / imageToResize.actualWidth)
  scaledWidth = Math.round(imageToResize.actualWidth * componentHeight / imageToResize.actualHeight)
  if ( scaledWidth <= componentWidth )
  {
   image.width = scaledWidth
   image.height = componentHeight
  }
  else
  {
   image.width = componentWidth
   image.height = scaledHeight
  }
 }
}


//-->
