// JavaScript Document

var ROTATION_TIME = 5;
var Photos = [
  {
      'imgUrl': 'http://www.mediapeta.com/OlympicShame/Images/slideshow-1.jpg',
      'credit': 'Credit: Sea Shepherd Conservation Society',
      'text': 'Sealers crush baby seals\' skulls in before they skin them. Virtually all seals killed in Canada\'s annual slaughter are younger than 3 months old.',
	  'share': 'Images/slidefacebook.png'
  },
  {
      'imgUrl': 'http://www.mediapeta.com/OlympicShame/Images/slideshow-2.jpg',
      'credit': 'Credit: Sea Shepherd Conservation Society',
      'text': 'Veterinarians and Department of Fisheries officers have found that many seals\' skulls are not adequately crushed, which means that those seals may be skinned alive.'
  },
  {
      'imgUrl': 'http://www.mediapeta.com/OlympicShame/Images/slideshow-3.jpg',
      'credit': 'Credit: Sea Shepherd Conservation Society',
      'text': 'Many have not yet eaten their first solid meal or learned how to swim before they are beaten and skinned for a product that no one needs.'
  },
  {
      'imgUrl': 'http://www.mediapeta.com/OlympicShame/Images/slideshow-4.jpg',
      'credit': 'Credit: Sea Shepherd Conservation Society',
      'text': 'Sealers routinely hook seals in the eye, cheek, or mouth to avoid damaging the fur. Then they drag the seal across the ice&mdash;in many cases while the seal is still conscious.  '
  },
  {}
];
var preloadedImages = [];
var adIndex = -1;
function generatePhoto() {
    //  var adIndex = Math.floor(Math.random() * Photo.length - 1);
    var adHtml = '<img src="' + preloadedImages[adIndex].src + '" border="0" />' +
				'<p id="credit" style="font-size:6pt; text-align:center;">' + Photos[adIndex].credit + '</p>' +
				'<p id="slideshowFunctionButtons" style="text-align:center;">' + getFunctionButtonCode() + '</p>' +
				'<p class="slide" style="font-size:8pt;">' + Photos[adIndex].text + '</p>';
    document.getElementById('slide').innerHTML = adHtml;
}

function rotatePhoto() {
    adIndex++;
    if (adIndex > Photos.length - 2) adIndex = 0;
    generatePhoto();
    if (slideshowPlaying) slideShowTimer = setTimeout("rotatePhoto()", (ROTATION_TIME * 2000));
}
function initPhoto() {
    for (var i = 0; i < Photos.length; i++) {
        if (Photos[i].imgUrl) {
            var img = new Image();
            img.src = Photos[i].imgUrl;
            preloadedImages[i] = img;
        }
    }
    rotatePhoto();
}


//////////// ADDITIONAL FUNCTIONS //////////////

slideshowPlaying = true;
function getFunctionButtonCode() {
    var bHtml = '<a href="javascript:slideshow_prev();void(0);"><img src="../Media/Images/btn_previous.png" width="30" height="30" border="0" alt="Previous Image"></a>' +
				'<a href="javascript:slideshow_next();void(0);"><img src="../Media/Images/btn_forward.png" width="30" height="30" border="0" alt="Next Image"></a>' +
                '<a href="javascript:slideshow_togglePlay();void(0);"><img src="../Media/Images/btn_' + ((slideshowPlaying) ? 'pause' : 'play') + '.png" width="30" border="0" height="30" alt="' + ((slideshowPlaying) ? 'Pause' : 'Play') + ' Slideshow"></a>';
    return bHtml;
}

function slideshow_togglePlay() {
    slideshowPlaying = (slideshowPlaying) ? false : true;
    if (slideshowPlaying) {
        rotatePhoto();
    } else {
        clearTimeout(slideShowTimer);
        generatePhoto();
    }
}

function slideshow_next() {
    if (slideshowPlaying) clearTimeout(slideShowTimer);
    slideshowPlaying = false;
    rotatePhoto();
}
function slideshow_prev() {
    if (slideshowPlaying) clearTimeout(slideShowTimer);
    slideshowPlaying = false;
    adIndex--;
    if (adIndex < 0) adIndex = Photos.length - 2;
    generatePhoto();
}
