var wpfe;                   // Pointer to the WPF/E plug-in
var currentSlide = 1;       // Current slide being displayed
var currentDownload = 1;    // Current slide downloading
var downloader              // WPF/E downloader
var maxNumSlides = 8;       // Number of slides in the presentation

function downloadAssets (sender, args) {
    wpfe = document.getElementById("wpfeobj");

    downloader = wpfe.createObject("Downloader");
  	downloader.downloadProgressChanged = "javascript:downloadProgressChanged";
  	downloader.completed = "javascript:downloadCompleted";
  	
  	var progressText = sender.findName("progressText");
    progressText.text = "Downloading: i" + currentDownload + ".jpg";
  	downloader.open("GET", "assets/i" + currentDownload + ".jpg", true);
  	downloader.send();
}

function downloadProgressChanged(sender, args) {
    var progressRect = sender.findName("progressRect");
    progressRect.width = (((currentDownload - 1) + sender.downloadProgress) / maxNumSlides) * 450;
}

function downloadCompleted(sender, args) {
    // Process finished image
    if (sender.downloadProgress == 1) {
        var image = sender.findName("slide" + currentDownload);
        image.setSource(sender);
    }

    currentDownload++;
    if (currentDownload <= maxNumSlides) {
        var progressText = sender.findName("progressText");
        progressText.text = "Downloading: i" + currentDownload + ".jpg";
        
        downloader = wpfe.createObject("Downloader");
  	    downloader.downloadProgressChanged = "javascript:downloadProgressChanged";
  	    downloader.completed = "javascript:downloadCompleted";
        
        downloader.open("GET", "assets/i" + currentDownload + ".jpg", true);
  	    downloader.send();
    }
    else {
        // Hide progress UI
        var loadedRoot = sender.findName("loadedRoot");
        loadedRoot.opacity = 1;
        var downloadUIFadeOut = sender.findName("downloadUIFadeOut");
        downloadUIFadeOut.begin();
    }
}

function enterForward (s,e)
{
  if (currentSlide < 8)
  {
    wpfe.findName("forwardButton").fill = "#FFAAAAAA";
    wpfe.findName("forwardButton").stroke = "#FFAAAAAA";
  }
}

function leaveForward (s,e)
{
  if (currentSlide < 8)
  {
    wpfe.findName("forwardButton").fill = "#FF000000";
    wpfe.findName("forwardButton").stroke = "#FFAAAAAA";
  }
}
function downForward (s,e)
{
  if (currentSlide < 8)
  {
    wpfe.findName("forwardButton").fill = "#FFFFFFFF";
    wpfe.findName("forwardButton").stroke = "#FFFFFFFF";
  }
}

function upForward (s,e)
{   
  if (currentSlide < 8)
  {
    currentSlide++;
    wpfe.findName("story_Enter"+ currentSlide).Begin();
    if (currentSlide < 8)
    {
      wpfe.findName("forwardButton").fill = "#FF000000";
      wpfe.findName("forwardButton").stroke = "#FFAAAAAA";
    }
    else
    {
      wpfe.findName("forwardButton").fill = "#FF000000";
      wpfe.findName("forwardButton").stroke = "#FF222222";
    }
  }
  wpfe.findName("backButton").fill = "#FF000000";
  wpfe.findName("backButton").stroke = "#FFAAAAAA";

}

function enterBack (s,e)
{
  if (currentSlide > 1)
  {
    wpfe.findName("backButton").fill = "#FFAAAAAA";
    wpfe.findName("backButton").stroke = "#FFAAAAAA";
  }
}
function leaveBack (s,e)
{
  if (currentSlide > 1)
  {
    wpfe.findName("backButton").fill = "#FF000000";
    wpfe.findName("backButton").stroke = "#FFAAAAAA";
  }
}
function downBack (s,e)
{
  if (currentSlide > 1)
  {
    wpfe.findName("backButton").fill = "#FFFFFFFF";
    wpfe.findName("backButton").stroke = "#FFFFFFFF";
  }
}

function upBack (s,e)
{
  if (currentSlide > 1)
  {
    currentSlide--;
    wpfe.findName("story_Enter"+ currentSlide).begin();
    if (currentSlide > 1)
    {
      wpfe.findName("backButton").fill = "#FF000000";
      wpfe.findName("backButton").stroke = "#FFAAAAAA";
    }
    else
    {
      wpfe.findName("backButton").fill = "#FF000000";
      wpfe.findName("backButton").stroke = "#FF222222";
    }
  }
  wpfe.findName("forwardButton").fill = "#FF000000";
  wpfe.findName("forwardButton").stroke = "#FFAAAAAA";
}
