var controlsDisplay = "yes"
var useTransitions = "yes"

var prevPath = "media/prev.gif"
var nextPath = "media/next.gif"
var playPath = "media/play.gif"
var pausePath = "media/pause.gif"
var stopPath = "media/stop.gif"
var blankPath = "media/blank.gif"
var brandingPath = "media/branding.gif"

var runSlidesFlag = 0
var iImage = 0
var controlsEnabled = 1
var buttonsEnabled = 1
var fullscreen = (window.parent == window);
// alert("fullscreen " + fullscreen);
var timeoutid

function loadedDoc()
{
    if (!(navigator.appName == "Netscape" || navigator.appName.indexOf("Microsoft") == -1))
    {
       var preload = new Image
       preload.src = ImagePath + vImage[0]

       if (controlsDisplay)
       {
           var contactText = "<center><table style='overflow-y: auto'>"
           for (i = 0 ; i < vThumb.length / 5; i++)
           {
              contactText += "<tr>"
              for (j = 0; j < 5 ; j++)
              {
                 var x = (i * 5 + j)
                 if (x < vThumb.length)
                 {
                    contactText += "<td height=120 width=120><img onclick=filmJump(" + x + ") style='cursor:hand; padding: 20px' src='" + ImagePath + vThumb[x] + "'></td>"
                 }
              }
              contactText += "</tr>"
           }
           contactText += "</table></center>"
           contact.innerHTML = contactText

           var filmThumbsText = "<table style='width: " + (vThumb.length * 120) + "; overflow-x: scroll'><tr height=120>"
           for (i = 0 ; i < vThumb.length ; i++)
           {
              filmThumbsText += "<td align=center width=120 id=filmThumb" + i + "><div id=filmThumbCell" + i + " style = 'padding: 5px; border: solid 0 #00b; border-width:2px'><img onclick=filmSelect(" + i + ") class=THUMB style='cursor:hand; padding: 20px' src='" + ImagePath + vThumb[i] + "'></div></td>"
           }
           filmThumbsText += "</tr></table>"
           filmthumbs.innerHTML = filmThumbsText
       }
    else
    {
        advcontrols.style.visibility = "hidden"
    }

       filmmain.style.cursor = "hand"
       slidemain.style.cursor = "hand"

       forwButton.src=nextPath
       runButton.src=playPath
       prevButton.src=prevPath
       stopButton.src=stopPath

//        if (fullscreen)
//        {
//            Slideshow('launch')
//        }
       if (top.Mode == "Film") {
          Film();
       }
       else if (top.Mode == "Contact") {
          Contact();
       }
//       else Slideshow('launch')
       else Slideshow()
     }
}

function Ticker()
{
    if (runSlidesFlag == 1 && top.Mode == "Slideshow")
    {
        nextImage()
        timeoutid = setTimeout("Ticker()", 5000)
    }
}

function ShowStopButton()
{
    if (fullscreen)
    {
       stopButton.width = 32;
       stopButton.style.cursor = "hand"
       stopButton.style.visibility = ""
    }
    else
    {
       stopButton.width = 0;
       stopButton.style.cursor = ""
       stopButton.style.visibility = "hidden"
    }

}

function ShowButtons(f)
{
    ShowStopButton();
    forwButton.filters[0].Apply();
    runButton.filters[0].Apply();
    prevButton.filters[0].Apply();
    stopButton.filters[0].Apply();
    if (f)
    {
        buttonsEnabled = 1
        forwButton.filters[0].transition = 3;
        runButton.filters[0].transition = 3;
        prevButton.filters[0].transition = 3;
        stopButton.filters[0].transition = 3;
        forwButton.src=nextPath
        runButton.src=playPath
        prevButton.src=prevPath
        stopButton.src=stopPath
        forwButton.style.cursor="hand"
        runButton.style.cursor="hand"
        prevButton.style.cursor="hand"
        stopButton.style.cursor="hand"
    }
    else
    {
        buttonsEnabled = 0
        forwButton.filters[0].transition = 2;
        runButton.filters[0].transition = 2;
        prevButton.filters[0].transition = 2;
        stopButton.filters[0].transition = 2;
        prevButton.src=blankPath
        forwButton.src=blankPath
        runButton.src=blankPath
        stopButton.src=blankPath
        forwButton.style.cursor=""
        runButton.style.cursor=""
        prevButton.style.cursor=""
        stopButton.style.cursor=""
    }
    forwButton.filters[0].Play();
    runButton.filters[0].Play();
    prevButton.filters[0].Play();
    stopButton.filters[0].Play();
}

function Film()
{
    if (controlsEnabled)
    {
//        alert ("Film b4 " + top.Mode);
        top.Mode = "Film"
//        alert ("Film after " + top.Mode);
        runSlidesFlag = 0
        slideshowbuttons.style.visibility = ""
        advcontrols.style.visibility = ""
  if ((document.body.clientHeight - 220) > 0)
        {
            film.height = document.body.clientHeight - 220
        }
        film.style.display = ""
        filmthumbs.style.display = ""
        contact.style.display = "none"
        slideshow.style.display = "none"
        ShowButtons(0)
        filmSelect(iImage)
        doResize()
    }
    else
    {
        advcontrols.style.visibility = "hidden"
    }
}

function Slideshow(x)
{
    if (controlsEnabled)
    {
//        alert ("Slideshow b4 " + top.Mode);
        top.Mode = "Slideshow"
//        alert ("Slideshow after " + top.Mode);
        slideshowbuttons.style.visibility = ""
        advcontrols.style.visibility = ""
        if (x == "launch")
        {
            ShowStopButton()
        }
        else
        {
            ShowButtons(1)
        }
        contact.style.display = "none"
        film.style.display = "none"
        filmthumbs.style.display = "none"
        slidemain.src = ImagePath + vImage[iImage]
        slideshow.style.display = ""
        doResize()
    }
    else
    {
        advcontrols.style.visibility = "hidden"
    }
}

function Contact()
{
    if (controlsEnabled)
    {
//        alert ("Contact b4 " + top.Mode);
        top.Mode = "Contact"
//        alert ("Contact after " + top.Mode);
        runSlidesFlag = 0
        slideshowbuttons.style.visibility = ""
        advcontrols.style.visibility = ""
        contact.style.display = ""
        slideshow.style.display = "none"
        ShowButtons(0)
        film.style.display = "none"
        filmthumbs.style.display = "none"
    }
    else
    {
        advcontrols.style.visibility = "hidden"
    }
}

function changeImage(x,y)
{
    iImage = (iImage + vImage.length + x) % vImage.length
    if (y == "notrans" || useTransitions != "yes")
    {
        slidemain.src = ImagePath + vImage[iImage]
    }
    else
    {
        slidemain.filters[0].Apply();
        slidemain.filters[0].transition = 23
        slidemain.src = ImagePath + vImage[iImage]
        slidemain.filters[0].Play();
    }
    doResize()
}

function prevImage(x)
{
    changeImage(-1,x)
}
function nextImage(x)
{
    changeImage(+1,x)
}

function slideClick()
{
   if (runSlidesFlag == 1)
   {
      runSlides()
   }
   nextImage("notrans")
}

function nextImageButton()
{
    if (buttonsEnabled)
    {
        if (runSlidesFlag == 1)
        {
           runSlides()
        }
        nextImage("notrans")
    }
}

function runSlidesButton()
{
    if (buttonsEnabled)
    {
        if (runSlidesFlag == 0)
        {
     nextImage()
        }
        runSlides()
    }
}

function prevImageButton()
{
    if (buttonsEnabled)
    {
        if (runSlidesFlag == 1)
        {
           runSlides()
        }
        prevImage("notrans")
    }
}

function exitButton()
{
  clearTimeout(timeoutid)
  runButton.src = playPath
}

function runSlides()
{
    runSlidesFlag = (runSlidesFlag + 1) % 2
    if (runSlidesFlag == 1)
    {
        runButton.src = pausePath
        timeoutid = setTimeout("Ticker()", 5000)
    }
    else
    {
        clearTimeout(timeoutid)
        runButton.src = playPath
    }
}

function filmSelect(i)
{
    iImage = i;
    filmmain.src = ImagePath + vImage[i];
    for (var j = 0 ; j < vThumb.length ; j++)
    {
        document.all["filmThumbCell"+j].style.border=""
    }
    document.all["filmThumbCell"+i].style.border="#00b 2px solid"
    doResize()
}

function filmJump(i)
{
    iImage = i;
    Film();
}

function changeFilmImage(x)
{
    filmSelect((iImage + vImage.length + x) % vImage.length)
}

function prevFilmImage()
{
    changeFilmImage(-1)
}

function nextFilmImage()
{
    changeFilmImage(+1)
}

function contactSelect(i)
{
    iImage = i
    Film()
    filmmain.src = ImagePath + vImage[i]
    doResize()
}

document.onkeydown = keyDown

function keyDown()
{
//   alert ("keydown " + event.keyCode);
   if (27 == event.keyCode)
   {
     if (fullscreen == 1)
     {
  exitButton()
     }
   }
   else if (top.Mode != "Welcome")
   {
      if (37 == event.keyCode)
      {
         if (top.Mode == "Slideshow")
         {
            prevImageButton()
         }
         else if (top.Mode == "Film")
         {
             prevFilmImage()
         }
      }
      else if (39 == event.keyCode)
      {
         if (top.Mode == "Slideshow")
         {
             nextImageButton()
         }
         else if (top.Mode == "Film")
         {
             nextFilmImage()
         }
      }
      else if (32 == event.keyCode)
      {
         if (top.Mode == "Slideshow")
         {
            runSlidesButton()
         }
         else
         {
//            alert ("Slideshow keydown " + event.keyCode);
            Slideshow()
         }
      }
   }
}

function doResize()
{
    var clientWidth = (document.all.slideImage.width)
    var clientHeight = (document.all.galleryMenu.height)
//     alert("clientWidth=" + clientWidth + " clientHeight=" + clientHeight);

    if (top.Mode == "Film")
    {
        clientHeight -= document.all.filmthumbs.offsetHeight
//         alert("film clientHeight=" + clientHeight);
    }

    var cx = cxImage[iImage]
    var cy = cyImage[iImage]
//     alert("cx=" + cx + " cy=" + cy);

    if (clientWidth < cx)
    {
        cy = cy * clientWidth / cx;
        cx = clientWidth
    }
    if (clientHeight < cy)
    {
        cx = cx * clientHeight / cy;
        cy = clientHeight
    }
//     alert("clientWidth=" + clientWidth + " clientHeight=" + clientHeight + "\ncx=" + cx + " cy=" + cy);

    if (top.Mode == "Film")
    {
        filmmain.width = cx;
        filmmain.height = cy;
    }
    else
    {
        slidemain.width = cx;
        slidemain.height = cy;
    }
}
window.attachEvent("onload", loadedDoc);

