// MEDIA DISPLAY FUNCTIONS /////////////////////////////////////////////

// WINDOW GLOBAL VARIABLES /////////////////////////////////////////////////

// slideshow variables for the "Photos and videos" page
window.slideshowTimerID = null;
window.slideshowTimeBetweenChange = 3000; // milliseconds

var photoList = new Array();
var videoList = new Array();
var tourList = new Array();

var mediaLocation = "http://media.thomson.co.uk";

// search media of a prticular type for unique tag names
function findUniqueMediaTags( mediaType ){
  var mediaList = getMediaList( mediaType );

  var uniqueTags = $A();

  // run through all the media items
  mediaList.each( function( mediaItem ){
    var tags = $A( mediaItem.tags );

    // run through each tag for the item and remember the tag name if we haven't seen it before
    tags.each( function( tag ){
      if( uniqueTags.indexOf( tag ) < 0 ){
        uniqueTags.push( tag );
      }
    } );
  } );

  return uniqueTags;
}




// get a list of all the media of a particular type
function getMediaList( mediaType ){
  var mediaList = $A();

  switch( mediaType ){
    case "photo" : mediaList = $A( photoList ); break;
    case "video" : mediaList = $A( videoList ); break;
    case "virtualtour" : mediaList = $A( tourList ); break;
  }

  return mediaList;
}




// get a list of all the media items of a given type that match a given tag
function findMediaItemsMatchingTag( mediaType, tagToMatch ){

//reset left value of scroller each time invoked, but not for ovwerlays page
if(document.getElementById('mediaThumbnailsCarouselWrapper') != null){
document.getElementById('mediaThumbnailsCarouselWrapper').scrollLeft = 0;
scrollCount = 20;
currentItem = 1;
}
  var mediaList = getMediaList( mediaType );

  // if empty tag specified then match all the items
  if( tagToMatch == "" ){ return mediaList; }

  var matchingItems = $A();

  // run through all the items
  mediaList.each( function( item ){
    var tags = $A( item.tags );
    if( tags.indexOf( tagToMatch ) >= 0 ){
      matchingItems.push( item );
    }
  } );

  return matchingItems;

}



// dynamically create the media links for the different photo and video tags
function constructMediaLinks(){
  var mediaLinksDiv = $( "MediaLinks" );

  // don't do anything if we don't have anywhere to put the links
  if( mediaLinksDiv == null ){ return; }

  if(photoList.length>0){

    // construct the photo tag links

    // create the photo links header and an empty list
    new Insertion.Bottom( mediaLinksDiv, '<h3>Photos (' + photoList.length + ')</h3>' );
    new Insertion.Bottom( mediaLinksDiv, '<ul id="MediaPhotoLinks" class="mediaLinksList"></ul>' );

    var photoLinksList = $( "MediaPhotoLinks" );

    // put the "All photos" link at the top of the list
    var item = constructMediaLink( "", "All photos", "photo", photoList.length, function(){ selectMediaTag( "", "photo" ); } );
    photoLinksList.appendChild( item );

    // run through all the photo tags and add a corresponding link to the list
    var tags = findUniqueMediaTags( "photo" );
    tags.each( function( tag, index ){
      var matchingPhotos = findMediaItemsMatchingTag( "photo", tag );

      // add the link to the list
      var tagId = "MediaPhotoTag" + (index + 1);
      var item = constructMediaLink( tag, tag, "photo", matchingPhotos.length, function(){ selectMediaTag( tag, "photo" ); } );
      photoLinksList.appendChild( item );
    } );

  }
  if(videoList.length>0){

    // construct the video links
    // create the video links header and an empty list
    new Insertion.Bottom( mediaLinksDiv, '<h3>Videos (' + videoList.length + ')</h3>' );
    new Insertion.Bottom( mediaLinksDiv, '<ul id="MediaVideoLinks" class="mediaLinksList"></ul>' );

    var videoLinksList = $( "MediaVideoLinks" );

    // put the "All videos" link at the top of the list
    var item = constructMediaLink( "", "All videos", "video", videoList.length, function(){ selectMediaTag( "", "video" ); } );
    videoLinksList.appendChild( item );

    // run through all the video tags and add a corresponding link to the list
    var tags = findUniqueMediaTags( "video" );
    tags.each( function( tag, index ){
      var matchingVideos = findMediaItemsMatchingTag( "video", tag );

      // add the link to the list
      var tagId = "MediaVideoTag" + (index + 1);
      var item = constructMediaLink( tag, tag, "video", matchingVideos.length, function(){ selectMediaTag( tag, "video" ); } );
      videoLinksList.appendChild( item );
    } );

  }

  if(tourList.length>0){
    // construct the virtual tour tag links

    // create the virtual tour links header and an empty list
    new Insertion.Bottom( mediaLinksDiv, '<h3>Virtual Tours (' + window.virtualtours.length + ')</h3>' );
    new Insertion.Bottom( mediaLinksDiv, '<ul id="MediaVirtualtourLinks" class="mediaLinksList"></ul>' );

    var virtualtourLinksList = $( "MediaVirtualtourLinks" );

    // put the "All Virtual Tours" link at the top of the list
    var item = constructMediaLink( "", "All virtual tours", "virtualtour", window.virtualtours.length, function(){ selectMediaTag( "", "virtualtour" ); } );
    virtualtourLinksList.appendChild( item );



    // run through all the virtual tour tags and add a corresponding link to the list
    var tags = findUniqueMediaTags( "virtualtour" );
    tags.each( function( tag, index ){
      var matchingVirtualtours = findMediaItemsMatchingTag( "virtualtour", tag );

      // add the link to the list
      var tagId = "MediaVirtualtourTag" + (index + 1);
      var item = constructMediaLink( tag, tag, "virtualtour", matchingVirtualtours.length, function(){ selectMediaTag( tag, "virtualtour" ); } );
      virtualtourLinksList.appendChild( item );
    } );

  }

}


// shortcut method for creating a media link LI node
function constructMediaLink( mediaTag, title, mediaType, itemCount, clickHandlerFn ){
  var item = document.createElement( "li" );
  new Insertion.Bottom( item, '<span><strong>' + title + '</strong> (' + itemCount + ')</span>' );
  item.onclick = clickHandlerFn;
  item.mediaTag = mediaTag;
  item.mediaType = mediaType;
  item.title = title;

  return item;
}



function constructMediaShortLinks(){
  var shortLinksDiv = $( "MediaShortLinks" );
  // don't do anything if we don't have a place in the page to put the short links
  if( shortLinksDiv == null ){ return; }

  if(photoList.length>0){
    // the "All photos" link
    var photoCount = findMediaItemsMatchingTag( "photo", "" ).length;
    var item = document.createElement( "a" );
    item.innerHTML = "All photos (" + photoCount + ")";
    item.onclick = function(){ selectMediaTag( "", "photo" );}
    item.mediaTag = "";
    item.mediaType = "photo";
    item.title = "All photos";
    shortLinksDiv.appendChild( item );
  }

  if(videoList.length>0){
    // the "All videos" link
    var videoCount = findMediaItemsMatchingTag( "video", "" ).length;
    var item = document.createElement( "a" );
    item.innerHTML = "All videos (" + videoCount + ")";
    item.onclick = function(){ selectMediaTag( "", "video" ); }
    item.mediaTag = "";
    item.mediaType = "video";
    item.title = "All videos";
    shortLinksDiv.appendChild( item );
  }


  if(tourList.length>0){
    // the "All virtual tours" link
    var virtualtourCount = findMediaItemsMatchingTag( "virtualtour", "" ).length;
    var item = document.createElement( "a" );
    item.innerHTML = "All virtual tours (" + virtualtourCount + ")";
    item.onclick = function(){ selectMediaTag( "", "virtualtour" ); }
    item.mediaTag = "";
    item.mediaType = "virtualtour";
    item.title = "All virtual tours";
    shortLinksDiv.appendChild( item );
  }
}




function getThumbnails(){
  // get hold of the list where we're going to update the thumbnails
  var thumbnailList = $( "MediaThumbnailsList" );

  // clear all the existing thumbnails
  return $A( thumbnailList.childNodes );
}



// remove all the thumbnails from the list, and return the list
function clearThumbnailList(){
  // clear all the existing thumbnails
  var thumbnails = getThumbnails();
  thumbnails.each( function( thumbnail ){
    Element.remove( thumbnail );
  } );
  return $( "MediaThumbnailsList" );
}



// update the tag links to highlight the selected one
function updateMediaTagDisplay( selectedMediaTag, selectedMediaType ){

  // get a list of all the media links
  var allTagLinks = [];

  // check if there are the standard links that appear on the right hand side of the media display on eg. the accommodation page
  if( $( "MediaPhotoLinks" ) != null ){
    allTagLinks = allTagLinks.concat( $A( $( "MediaPhotoLinks" ).getElementsByTagName( "LI" ) ) );
  }
  if( $( "MediaVideoLinks" ) != null ){
    allTagLinks = allTagLinks.concat( $A( $( "MediaVideoLinks" ).getElementsByTagName( "LI" ) ) );
  }
  if( $( "MediaVirtualtourLinks" ) != null ){
    allTagLinks = allTagLinks.concat( $A( $( "MediaVirtualtourLinks" ).getElementsByTagName( "LI" ) ) );
  }

  // check if there are "short" links that appear above the thumbnails on eg. the destination info page
  if( $( "MediaShortLinks" ) != null ){
    allTagLinks = allTagLinks.concat( $A( $( "MediaShortLinks" ).getElementsByTagName( "A" ) ) );
  }

  // run through the links and update their "selected" status
  for( var i = 0; i < allTagLinks.length; i++ ){
    var tagLink = allTagLinks[i];
    if( (tagLink.mediaTag == selectedMediaTag) && (tagLink.mediaType == selectedMediaType) ){
      Element.addClassName( tagLink, "selected" );
    } else {
      Element.removeClassName( tagLink, "selected" );
    }
  }
}


function updateMediaThumbnailsHeader( mediaTag, mediaType, mediaItemsCount ){
  var header = $( "MediaThumbnailsHeaderText" );

  // don't try updating if there isn't a header!
  if( header == null ){ return; }

  // change the header on the thumbnail list
  var headerText = (mediaTag == "" ? "All " + mediaType + "s" : mediaTag );
  headerText += '<span class="unbold"> (' + mediaItemsCount + ')</span>';
  header.update( headerText );
}


// force a given tag to be selected, and update the thumbnail list accordingly
function selectMediaTag( mediaTag, mediaType ){

  // stop any slideshow running if there is one
  slideshowCancel();
  // if a video is playing destroy it
  killVideo();
  killVT();

  // update the media tag links to show which one is selected
  updateMediaTagDisplay( mediaTag, mediaType );

  // get a list of all the media items that match
  var mediaItems = findMediaItemsMatchingTag( mediaType, mediaTag );

  // change the header on the thumbnail list
  updateMediaThumbnailsHeader( mediaTag, mediaType, mediaItems.length );

  // change the media display panel to the correct media type
  showMediaDisplayPanel( mediaType );

  // make the thumbnails
  populateThumbnails( mediaItems, mediaType );

  var thumbnails = getThumbnails();

  if( (mediaType == "photo") && (thumbnails.length > 1) ){
    Element.show( "MediaSlideshowLink" );
  } else {
    Element.hide( "MediaSlideshowLink" );
  }

  if( mediaType == "video" ){
    Element.hide( "MediaPhotoContainer" );
    Element.show( "MediaVideoContainer" );
    Element.show( "MediaVideoProblems" );
    Element.hide( "MediaVirtualtourContainer" );
  } else {
    Element.hide( "MediaVideoProblems" );
  }

  if( mediaType == "virtualtour" ){
    Element.hide( "MediaPhotoContainer" );
    Element.hide( "MediaVideoContainer" );
    Element.hide( "MediaVideoProblems" );
    Element.show( "MediaVirtualtourContainer" );

  } else {
    Element.hide( "MediaVideoProblems" );
  }

  // display the first media item from the tag we've selected
  if( mediaType == "photo" ){
    displayFirstMediaItemInThumbnails();
  } else if( (mediaType == "video") || (mediaType == "virtualtour") ){
    // for videos, we have to leave a slight delay before we
    // select the first one because the Flash movie takes a
    // little while to become ready
    setTimeout( "displayFirstMediaItemInThumbnails();", 150 );
  }
}



// display the first item in the thumbnail list
function displayFirstMediaItemInThumbnails(){
  var thumbnails = getThumbnails();
  if( thumbnails.length > 0 ){
    displayMediaItem( thumbnails[0].mediaItem, 0, thumbnails.length, false );
  }
}



// given a list of media items, populate the thumbnail list with the item thumbnails
function populateThumbnails( mediaItems, mediaType ){
  // empty the thumbnail list in preparation for the new thumbnails
  var thumbnailList = clearThumbnailList();

  // calculate how long the thumbnail list will be
  // NOTE: assumes 128px wide thumbnails with 5 pixel padding either side
  thumbnailList.style.width = String( (5 + 128 + 5) * mediaItems.length ) + "px";

  // run through all the items and add each thumbnail
  mediaItems.each( function( mediaItem, index ){

    // then the thumbnail itself
    var thumbnail = document.createElement( "li" );
    thumbnail.id = "MediaThumbnail" + index;
    thumbnail.mediaItem = mediaItem;
    mediaItem.mediaType = mediaType;
    var mediathumb = mediaItem.thumbnail;
    if(mediathumb==null)
    {
      mediathumb = "/images/mmshowcase/"+mediaType+"/defaultthumb.gif";
    }
    new Insertion.Bottom( thumbnail, '<img src="' + mediathumb + '" alt="' + mediaItem.caption + '" title="' + mediaItem.caption + '"/>' );
    thumbnail.onclick = function(){ displayMediaItem( mediaItem, index, mediaItems.length, false ); };
    thumbnailList.appendChild( thumbnail );
  } );
}



function showMediaDisplayPanel( mediaType ){


  if( mediaType == "photo" ){
    // photos
    if(videoList.length>0){
      Element.hide( "MediaVideoContainer" );
      Element.removeClassName( "MediaDisplay", "videoDisplay" );
    }
    if(tourList.length>0){
      Element.hide( "MediaVirtualtourContainer" );
      Element.removeClassName( "MediaDisplay", "virtualtourDisplay" );
    }
    if(photoList.length>0){
      Element.show( "MediaPhotoContainer" );
      Element.addClassName( "MediaDisplay", "photoDisplay" );
    }


  } else if( mediaType == "video" ){
    // videos
    if(photoList.length>0){
      Element.hide( "MediaPhotoContainer" );
      Element.removeClassName( "MediaDisplay", "photoDisplay" );
    }
    if(tourList.length>0){
      Element.hide( "MediaVirtualtourContainer" );
      Element.removeClassName( "MediaDisplay", "virtualtourDisplay" );
    }
    if(videoList.length>0){
      Element.show( "MediaVideoContainer" );
      Element.addClassName( "MediaDisplay", "videoDisplay" );
    }

  } else if( mediaType == "virtualtour" ){
    // videos
    if(photoList.length>0){
      Element.hide( "MediaPhotoContainer" );
      Element.removeClassName( "MediaDisplay", "photoDisplay" );
    }
    if(tourList.length>0){
      Element.show( "MediaVirtualtourContainer" );
      Element.addClassName( "MediaDisplay", "virtualtourDisplay" );
    }
    if(videoList.length>0){
      Element.hide( "MediaVideoContainer" );
      Element.removeClassName( "MediaDisplay", "videoDisplay" );
    }

  } else {
    // unknown media type - display nothing
    if(photoList.length>0){
      Element.hide( "MediaPhotoContainer" );
      Element.removeClassName( "MediaDisplay", "photoDisplay" );
    }
    if(videoList.length>0){
      Element.hide( "MediaVideoContainer" );
      Element.removeClassName( "MediaDisplay", "videoDisplay" );
    }
    if(tourList.length>0){
      Element.show( "MediaVirtualtourContainer" );
      Element.removeClassName( "MediaDisplay", "virtualtourDisplay" );
    }
  }
}



// event handler called when a thumbnail is clicked
function displayMediaItem( mediaItem, thumbnailIndex, thumbnailCount, isSlideshow ){
  // if the photo change was caused by the user, not by a slideshow,
  // then cancel any existing slideshow
  if( !isSlideshow ){
    slideshowCancel();
  }

  // show the correct media display panel for the item
  showMediaDisplayPanel( mediaItem.mediaType );

  // show the media item
  if( mediaItem.mediaType == "photo" ){
    displayPhoto( mediaItem, thumbnailIndex, thumbnailCount );
  } else if( mediaItem.mediaType == "video" ){
    displayVideo( mediaItem, thumbnailIndex, thumbnailCount );
  } else if( mediaItem.mediaType == "virtualtour" ){
    displayVirtualtour( mediaItem, thumbnailIndex, thumbnailCount );
  }

  // highlight the selected thumbnail
  highlightThumbnail( thumbnailIndex, thumbnailCount );
}



// use the UFO functions to dynamically create the flash video player
function createFlashVideoPlayer(videoPath){

}

// use the swfobject to build the virtual tour
function createFlashVirtualtourPlayer(virtualtourPath){
  if(document.getElementById("virtualtour"))
  {
    document.getElementById('MediaVirtualtourContainer').innerHTML='<div id="virtualTourAlternative"></div><div class="clearer"></div>';
  }
  //virtualtourPath = "/images/virtual-tours/test.ivp";
  var flashvars = {};
  flashvars.panorama = virtualtourPath;
  var params = {};
  params.allowfullscreen = "true";
  params.allowscriptaccess = "sameDomain";
  var attributes = {};
  attributes.id = "virtualtour";
  swfobject.embedSWF("/images/mmshowcase/virtualtour/PurePlayer.swf", "virtualTourAlternative", "400", "300", "9.0.0", false, flashvars, params, attributes);
}




function displayPhoto( photo, thumbnailIndex, thumbnailCount ){
  // update the photo and captions in the main display area
  $( "MediaIndexCaption" ).update( "" + (thumbnailIndex + 1) + " of " + thumbnailCount );
  $( "MediaCaption" ).update( photo.caption );
  $( "MediaPhotoDisplay" ).src = photo.filename;
  $( "MediaPhotoDisplay" ).alt = photo.caption + " (photo)";
  $( "MediaPhotoDisplay" ).title = photo.caption + " (photo)";
}



function displayVideo( video, thumbnailIndex, thumbnailCount ){
  var flashVideoPlayer = $( "MediaVideoFlash" );

  // because Safari and Firefox 1.5 have trouble initialising the Flash movie if its contianer
  // div is hidden, we wait until the first time that the video container is displayed before
  // we create the Flash movie
  /*if( flashVideoPlayer == null ){
    createFlashVideoPlayer();
    flashVideoPlayer = $( "MediaVideoFlash" );
  }*/

  $( "MediaIndexCaption" ).update( "" + (thumbnailIndex + 1) + " of " + thumbnailCount );
  $( "MediaCaption" ).update( video.caption );
/*
  // note: need to move relative path up one directory to get out of Flash folder
  var videoPath = "../" + video.filename;

  // if we're persitently trying to load a previous video, then stop
  if( typeof( window.mediaFlashVideoKeepTrying ) != "undefined" ){
    clearTimeout( window.mediaFlashVideoKeepTrying );
  }

  createFlashVideoPlayer(videoPath);
  */
  var videoPath = video.filename;
  //videoPath = mediaLocation+videoPath;
  //var videoPath = "http://media.thomson.co.uk"+video.filename;
  createVideoPlayer(videoPath);
}


function displayVirtualtour( virtualtour, thumbnailIndex, thumbnailCount ){
  var flashVirtualtourPlayer = $( "MediaVirtualtourFlash" );

  var virtualTourPath = virtualtour.filename;

  // because Safari and Firefox 1.5 have trouble initialising the Flash movie if its contianer
  // div is hidden, we wait until the first time that the video container is displayed before
  // we create the Flash movie
  if( flashVirtualtourPlayer == null ){
    flashVirtualtourPlayer = $( "MediaVirtualtourFlash" );
  }

  $( "MediaIndexCaption" ).update( "" + (thumbnailIndex + 1) + " of " + thumbnailCount );
  $( "MediaCaption" ).update( virtualtour.caption );
  createFlashVirtualtourPlayer( virtualTourPath);

}


// highlight a thumbnail in the list
function highlightThumbnail( thumbnailIndex, thumbnailCount ){
  // run through all the thumbnails and make sure only the selected one is highlighted
  for( var i = 0; i < thumbnailCount; ++i ){
    var thumbnail = $( "MediaThumbnail" + i );

    if( i == thumbnailIndex ){
      Element.addClassName( thumbnail, "selected" );
    } else {
      Element.removeClassName( thumbnail, "selected" );
    }
  }
}



// Javascript-enabled browsers get an interactive media panel
function makeMediaDisplayInteractive(){
  getMedia();
}



// MEDIA PHOTO SLIDESHOW FUNCTIONS //////////////////////////////////////////////

// start a slideshow to run through the currently displayed list of thumbnails
function slideshowStart(){
  var thumbnailList = $( "MediaThumbnailsList" );
  var thumbnailItems = $A( thumbnailList.getElementsByTagName( "LI" ) );

  // cancel any existing slideshow
  slideshowCancel();

  // it's only worth starting a slideshow if there are more than one thumbnail!
  if( thumbnailItems.length > 1 ){
    window.slideshowCounter = -1;

    // immediately jump to the second image in the slideshow to give immediate visual feedback that the slideshow has started
    slideshowNext();
    slideshowNext();
    window.slideshowTimerID = setInterval( slideshowNext, window.slideshowTimeBetweenChange );
  }
}



// display the next photo in the slideshow
function slideshowNext(){
  var thumbnailList = $( "MediaThumbnailsList" );
  var thumbnailItems = $A( thumbnailList.getElementsByTagName( "LI" ) );

  // make sure that there's more than one image to display
  if( thumbnailItems.length > 1 ){
    // increment the slidehow counter
    window.slideshowCounter = (window.slideshowCounter + 1) % thumbnailItems.length;

    // display the corresponding photo
    var mediaItem = thumbnailItems[window.slideshowCounter].mediaItem;
    if( mediaItem.mediaType == "photo" ){
      displayMediaItem( mediaItem, window.slideshowCounter, thumbnailItems.length, true );
    }
  }
}



// clear any slideshow that is currently running
function slideshowCancel(){

  // if there was a slideshow running...
  if( window.slideshowTimerID != null ){
    // then stop it and clear the timer
    clearInterval( window.slideshowTimerID );
    window.slideshowTimerID = null;
  }

}


// show a flash video in an overlay
function videoOverlayShow( videoPath, videoTitle ){
  // make sure the video overlay structure has been created
  videoOverlayEnsure();
  var overlay = $( "VideoOverlay" );

  modalWindowOverlayShow();

  // set the overlay title and display it
  Element.update( "VideoOverlayTitle", videoTitle );
  overlay.style.display = "block";

  positionElementInViewportCentre( overlay );

  // if we're persistently trying to load a previous video, then stop
  if( typeof( window.videoOverlayFlashKeepTrying ) != "undefined" ){
    clearTimeout( window.videoOverlayFlashKeepTrying );
  }

  // make sure the flash video player has been created
  videoOverlayEnsureFlash();

  // play the video
  videoPath = "../" + videoPath;
  videoOverlayLoad( videoPath, true );
}



function videoOverlayHide(){
  // delete the flash video player now that we've finished with it
  Element.remove( "VideoOverlayFlash" );

  // hide the overlays
  var overlay = $( "VideoOverlay" );
  overlay.style.display = "none";
  modalWindowOverlayHide();
}



// load a video into the Flash video player, and optionally keep trying if the Flash object isn't initialized properly yet.
function videoOverlayLoad( videoPath, keepTrying ){
  var flashVideoPlayer = $( "VideoOverlayFlash" );
  var flashVideoPlayerReady = (flashVideoPlayer != null);

  // if the Flash video player isn't ready to be told which video to load...
  if( !flashVideoPlayerReady && keepTrying ){
    // ... then try again a little bit later
    window.videoOverlayFlashKeepTrying = setTimeout( "videoOverlayLoad('" + videoPath + "', true);", 500 );
  } else {

    // otherwise try to load the video
    // we try two different approaches, because there isn't a singe cross-browser method
    // First we try calling the loadVideo() method, but if that doesn't work then try setting a Flash variable
    try{
      flashVideoPlayer.loadVideo( videoPath );
    } catch( e ){
      try{
        flashVideoPlayer.SetVariable( "videoURL", videoPath );
      } catch( e ){
        // do nothing
      }
    }
  }

}



// make sure the Flash video player has been created
function videoOverlayEnsureFlash(){
  var flashVideoPlayer = $( "VideoOverlayFlash" );

  // NOTE: we can't set wmode to anything other than "window" (the default)
  // because it needs to be rendered on top of other lays to prevent problems
  // with the Mac
  if( flashVideoPlayer == null ){
    var UFOparams = {
      movie: "flash/FlowPlayer.swf",
      id: "VideoOverlayFlash",
      name: "VideoOverlayFlash",
      width: "453",
      height: "247",
      majorversion: "8",
      build: "0",
      allowscriptaccess: "samedomain",
      xi: "false"
    };
    UFO.create( UFOparams, "VideoOverlayUFOPlaceholder" );
  }
}



// make sure the HTML structure for the video overlay exists in the page
function videoOverlayEnsure(){
  var overlay = $( "VideoOverlay" );
  if( overlay != null ){ return; }

  var overlayHTML = '';

  overlayHTML += '<div id="VideoOverlay" class="genericVideoOverlay overlay">';
  overlayHTML += '  <div class="pointerTop"></div>';
  overlayHTML += '  <div class="pointerLeft"></div>';
  overlayHTML += '  <div class="pointerBottom"></div>';
  overlayHTML += '  <div class="pointerRight"></div>';
  overlayHTML += '  <div class="genericVideoSidesShadow">';
  overlayHTML += '    <div class="overlayWrapper">';
  overlayHTML += '      <div class="headerBlock">';
  overlayHTML += '        <h4><span id="VideoOverlayTitle" class="padder">&nbsp;</span></h4>';
  overlayHTML += '        <a href="#" onclick="videoOverlayHide(); return false;"><img src="/images/buttons/close.gif" width="18" height="16" class="closePanel" alt="Close panel" /></a>';
  overlayHTML += '      </div>';
  overlayHTML += '      <div class="overlayPadder">';
  overlayHTML += '        <div class="contentBlock">';
  overlayHTML += '          <div id="VideoOverlayUFOPlaceholder"></div>';
  overlayHTML += '        </div>';
  overlayHTML += '      </div>';
  overlayHTML += '    </div>';
  overlayHTML += '  </div>';
  overlayHTML += '  <div class="genericVideoBottomShadow">';
  overlayHTML += '    <div class="bottomDropBlock4"></div>';
  overlayHTML += '  </div>';
  overlayHTML += '</div>';

  new Insertion.Bottom( document.body, overlayHTML );
}


// functions for controlling scrolling carousel on photos, videos pages

var scrollTimer;
var scrollCount = 20;
var currentItem = 1;

function mediaThumbnailsScrollLeft(){
  if(scrollCount>0){
    scrollCount -= 20;
    document.getElementById('mediaThumbnailsCarouselWrapper').scrollLeft = scrollCount;
    scrollTimer = setTimeout('mediaThumbnailsScrollLeft()',50);
  }
}

function mediaThumbnailsScrollRight(){
  var maxWidth = document.getElementById('MediaThumbnailsList').clientWidth - document.getElementById('mediaThumbnailsCarouselWrapper').clientWidth;
  if(scrollCount<maxWidth){
    scrollCount += 20;
    document.getElementById('mediaThumbnailsCarouselWrapper').scrollLeft = scrollCount;
    scrollTimer = setTimeout('mediaThumbnailsScrollRight()',50);
  }
}


function killScroll(){
  clearTimeout(scrollTimer);
}

function getMedia()
{
  //check that the media list exists if not then return false
  if(document.getElementById("mediaList") != null)
  {
    var mediaListContainer = document.getElementById("mediaList");
    getMediaLists('mediaPhotoList','photoList');
    getMediaLists('mediaVideoList','videoList');
    getMediaLists('mediaTourList','tourList');
    constructMediaLinks();
    constructMediaShortLinks();

    // start by selecting the first tag link - "All photos"
    selectMediaTag( "", "photo" );

    // display the first photo
    var thumbnails = getThumbnails();
    if( thumbnails.length > 0 ){
      displayMediaItem( thumbnails[0].mediaItem, 0, thumbnails.length, false );
    }
      return true;
    }
    else
    {
      return false;
    }
}

function getMediaLists(id,listName)
{
  if(document.getElementById(id))
  {
    var listContainer = document.getElementById(id);
    var list = listContainer.getElementsByTagName('li');
    buildMedia(list,listName);
  }
  else
  {
    return false;
  }
}

function buildMedia(list,listName)
{
  for(var i=0; i<list.length; i++)
  {
    var anchors = list[i].getElementsByTagName('a');
    var filename = null;
    var thumbnail = null;
    var caption = "";
    var tags = new Array();
    if(list[i].className){tags=list[i].className;}
    for(var j=0; j<anchors.length; j++)
    {
      if(anchors[j].className=="filename")
      {
        filename = anchors[j].href;
        caption = anchors[j].title;
      }
      else if(anchors[j].className=="thumbnail")
      {
        thumbnail = anchors[j].href;
      }
    }
    filename = makeRelative(filename);
    var mediaObj = new mediaObject(filename,thumbnail,caption,tags);
    eval(listName).push(mediaObj);
  }
}

function makeRelative(url)
{
  //taken out the make relative manipulation, and left this to pass the url out, as a
  //last minute fix to get this to work.  Function will need cleaning up/removing at
  //some point in the future.
  //var url_match = /http:\/\/[^\/]*\//;
  //url = url.replace(url_match,"/");
  return url;
}

function mediaObject(filename,thumbnail,caption,tags)
{
  this.filename = filename;
  this.thumbnail = thumbnail;
  this.caption = caption;
  this.tags = tags;
}

function createVideoPlayer(filename) {
  if(document.getElementById('MediaVideoContainer')) {
    var outputVideo = '<object id="Player" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="480" height="270"><param name="URL" value="' + filename + '">' + '<param name="autoStart" value="true">' + '<param name="uimode" value="mini">' + '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" width="480" height="270" src="' + filename + '" showcontrols="1" autostart="1">' + '</object></div><div class="clearer"></div>';
    document.getElementById('MediaVideoContainer').innerHTML = outputVideo;
    //window.scrollTo(0,(getYPos(document.getElementById('vid_player'))-10));
  }
  return false;
}

function killVideo()
{
  if(document.getElementById('MediaVideoContainer'))
  {
    document.getElementById('MediaVideoContainer').innerHTML = '';
  }
}
function killVT()
{
  if(document.getElementById('virtualtour'))
  {
    document.getElementById('MediaVirtualtourContainer').innerHTML='<div id="virtualTourAlternative"></div><div class="clearer"></div>';
  }
}