var controlsDisplay = "yes"
var useTransitions = "yes"
var imagePath = ""+"images"
var prevPath = imagePath + "/prev.gif" 
var nextPath = imagePath + "/next.gif" 
var playPath = imagePath + "/play.gif" 
var pausePath = imagePath + "/pause.gif" 
var stopPath = imagePath + "/stop.gif" 
var blankPath = imagePath + "/blank.gif" 
var brandingPath = imagePath + "/branding.gif" 
var runSlidesFlag = 0
var cImage = vImage.length; 
var iImage = cImage - 1  //JCAMEN for autorun see Line 231
var Mode = "Welcome"
var controlsEnabled = 1
var buttonsEnabled = 1
var fullscreen = (window.parent != window)
var timeoutid
var isIE = navigator.appName != "Netscape" || navigator.appName.indexOf("Microsoft") > -1;
var useTransitions = isIE ? 'yes' : 'no';
var MaxPicHeight=0, MaxPicWidth=0;
var vImageCache = new Array(iImage);

var loaded = 0;
var cPercent = 0;

var barLayer = null;
var percentLayer = null;
var statLayer = null;

function getLayer(layerID) {
if (document.getElementById)
	return document.getElementById(layerID);
else if (document.all)
	return document.all[layerID];
else 
	return null;
}

function updateBar() {
statLayer.innerHTML = "<font face=\"Arial\" color=\"#FFFFFF\"><B>" +loaded+ "/" +cImage+ "</B></font>";
var percent = Math.round(loaded/cImage * 100);
if (cPercent != percent)
	{
	cPercent = percent;
	barLayer.style.width = (cPercent) +"%";
	percentLayer.innerHTML = "<font color=\"#BBBB00\"><B>" +cPercent+ "%</B></font>";
	}
if (loaded == cImage)
	{
	done();
	}
}

function startLoading() {
if (document.getElementById || document.all)
	{
	barLayer = getLayer("bar");
	percentLayer = getLayer("percent");
	statLayer = getLayer("stat");
	doneMsgLayer = getLayer("doneMsg");
	
	for (i=0; i<cImage; i++)
		{
		MaxPicWidth=Math.max(MaxPicWidth,cxImage[i]);
    	MaxPicHeight=Math.max(MaxPicHeight,cyImage[i]);
    	vImageCache[i]=new Image(cxImage[i],cyImage[i]);
    	vImageCache[i].onload = imgLoaded;
		vImageCache[i].onerror = imgFailed;
		vImageCache[i].src = vImage[i];
		}
	}
else
	{
done();
	}
}

function done() {
//alert("done");
Slideshow('launch')
}

function imgFailed() {
alert("The following image failed to load, probably a broken link:\n" +this.src+ "\nPlease contact the webmaster of the site you are visiting about this.  The program will skip this file now.");
loaded++;
updateBar();
}

function imgLoaded() {
loaded++;
if (loaded>cImage)
return
updateBar();
}

function loadedDoc()
{
     self.focus();
      if (controlsDisplay)
       {
           var contactText = "<center><table style=\"overflow-y: auto\">"
           for (i = 0 ; i < cImage / 5; i++)
           {
              contactText = contactText + "<tr>"
              for (j = 0; j < 5 ; j++)
              {
                 var x = (i * 5 + j)
                 if (x < cImage)
                 {
                    contactText = contactText + "<td height=120 width=120><img onclick=filmJump(" + x + ") style=\"cursor:pointer; padding: 20px\" src=\"" + vThumb[x] + "\"></td>"
                 }
              }
              contactText = contactText + "</tr>"
           }
           contactText = contactText + "</table></center>"
           contact.innerHTML = contactText

           var filmThumbsText = "<table><tr height=120>"
           for (i = 0 ; i < vThumb.length ; i++)
           {
              filmThumbsText = filmThumbsText + "<td align=center width=120 id=filmThumb" + i + "><div id=filmThumbCell" + i + " style = \"padding: 2px; border: solid 0 #00b; border-width:2px\"><img onclick=filmSelect(" + i + ") class=THUMB style=\"cursor:pointer; padding: 2px\" src=\"" + vThumb[i] + "\"></div></td>"
           }
           filmThumbsText = filmThumbsText + "</tr></table>"
           filmthumbs.innerHTML = filmThumbsText
       }

       forwButton.src=nextPath
       runButton.src=playPath
       prevButton.src=prevPath
       stopButton.src=stopPath
startLoading();
         
}

function Ticker()
{
    if (runSlidesFlag == 1 && Mode == "Slideshow") 
    {
        nextImage()
        timeoutid = setTimeout("Ticker()", 5000)
    }
}

function ShowControls(x)
{
    if (controlsDisplay)
    {
        if (x=="Slideshow")
        {
           advcontrols1.style.color = "#ffffff"
           advcontrols2.style.color = "#03a0fc"
           advcontrols3.style.color = "#03a0fc"
        }
        else if (x=="Film")
        {
           advcontrols1.style.color = "#03a0fc"
           advcontrols2.style.color = "#ffffff"
           advcontrols3.style.color = "#03a0fc"
        }
        else if (x=="Contact")
        {
           advcontrols1.style.color = "#03a0fc"
           advcontrols2.style.color = "#03a0fc"
           advcontrols3.style.color = "#ffffff"
        }
    }
    else
    {
        advcontrols.style.visibility = "hidden"
    }
}

function ShowStopButton()
{
    if (0)//(fullscreen)
    {
       stopButton.width = 32;
       stopButton.style.cursor = "pointer"
       stopButton.style.visibility = ""
    }
    else
    {
       stopButton.width = 0;
       stopButton.style.cursor = ""
       stopButton.style.visibility = "hidden"
    }

}

function ShowButtons(f)
{
    ShowStopButton();
    //JCAMEN isIE stuff
    if (isIE)
    {
    forwButton.filters[0].Apply();
    runButton.filters[0].Apply();
    prevButton.filters[0].Apply();
    stopButton.filters[0].Apply();
    }
    if (f)
    {
        buttonsEnabled = 1
    		if (isIE)
    		{	
    	  forwButton.filters[0].transition = 3;
        runButton.filters[0].transition = 3;
        prevButton.filters[0].transition = 3;
    		 }
     forwButton.src=nextPath
        runButton.src=playPath
        prevButton.src=prevPath
        forwButton.style.cursor="pointer"
        runButton.style.cursor="pointer"
        prevButton.style.cursor="pointer"
    }
    else
    {
        buttonsEnabled = 0
        if (isIE)
    	  {	
        forwButton.filters[0].transition = 2;
        runButton.filters[0].transition = 2;
        prevButton.filters[0].transition = 2;
        }
        prevButton.src=blankPath
        forwButton.src=blankPath
        runButton.src=blankPath
        forwButton.style.cursor=""
        runButton.style.cursor=""
        prevButton.style.cursor=""
    }
    if (isIE)
    { //JCAMEND
    forwButton.filters[0].Play();
    runButton.filters[0].Play();
    prevButton.filters[0].Play();
    stopButton.filters[0].Play();
	}

}

function Film()
{
    if (controlsEnabled)
    {
        ShowControls('Film')
        Mode = "Film"
        runSlidesFlag = 0
        welcome.style.display = "none"
        controls.style.display = ""
	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()
    }
}

function Slideshow(x)
{
    if (controlsEnabled)
    {
        ShowControls('Slideshow')
        Mode = "Slideshow"
        welcome.style.display = "none"
        controls.style.display = ""
        slideshow.style.display = ""
        if (x == "launch")
        {
            ShowStopButton()
        		runSlidesButton();  //JCAMEN
        }
        else
        {
            ShowButtons(1)
        }
        contact.style.display = "none"
        film.style.display = "none"
        filmthumbs.style.display = "none"
        slidemain.src = vImage[iImage]
        doResize()
    }
}

function Contact()
{
    if (controlsEnabled)
    {
        ShowControls('Contact')
        Mode = "Contact"
        runSlidesFlag = 0
        welcome.style.display = "none"
        controls.style.display = ""
        contact.style.display = ""
        slideshow.style.display = "none"
        ShowButtons(0)
        film.style.display = "none"
        filmthumbs.style.display = "none"
    }
}

function changeImage(x,y)
{
    iImage = (iImage + vImage.length + x) % vImage.length
    if (y == "notrans" || useTransitions != "yes")
    {
        fadeoutin(slidemain,vImageCache[iImage].src,1)
        //slidemain.src = vImage[iImage]
    }
    else
    {
        slidemain.filters[0].Apply();
        slidemain.filters[0].transition = 23
        slidemain.src = vImageCache[iImage].src
        slidemain.filters[0].Play();
    }
    if(vText){showtext.innerHTML=vText[iImage];}
    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()
{
    window.parent.close()
}

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 = vImage[i];
    for (var j = 0 ; j < cImage ; j++)
    {
        document.all["filmThumbCell"+j].style.border=""
    }
    document.all["filmThumbCell"+i].style.border="#00b 2px solid"
    j=document.getElementById('filmthumbholder');
    //var clientWidth = (document.body.clientWidth - 40)
    j.scrollLeft=(i>8) ? i*83 : 0;
    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 = vImage[i]
    doResize()
}

document.onkeydown = keyDown

function keyDown()	
{
   if (27 == event.keyCode)
   {         
     if (fullscreen == 1)
     {
	exiteButton()
     }
   }
   else if (Mode != "Welcome")
   {
      if (37 == event.keyCode)
      {
         if (Mode == "Slideshow")
         {
            prevImageButton()
         }
         else if (Mode == "Film")
         {
             prevFilmImage()
         }
      }
      else if (39 == event.keyCode)
      {
         if (Mode == "Slideshow")
         {
             nextImageButton()
         }
         else if (Mode == "Film")
         {
             nextFilmImage()
         }
      }
      else if (32 == event.keyCode)
      {
         if (Mode == "Slideshow")
         {
            runSlidesButton()
         }
         else
         {
            Slideshow()
         }
      }
   }
}

function doResize()
{
    var clientWidth = (document.body.clientWidth - 40)
    var clientHeight= (document.body.clientHeight - document.all.controls.offsetHeight - 40)
		
	 //if (!isIE){clientHeight= window.innerHeight-90}	
		//alert(clientWidth+"=w winW="+winW+"\n "+clientHeight+"=h winH="+winH)
    if (Mode == "Film")
    {
        clientHeight -= document.all.filmthumbs.offsetHeight
    }

    var cx = cxImage[iImage]
    var cy = cyImage[iImage]
/*
    if (clientWidth < cx)
    {
        cy = cy * clientWidth / cx;
        cx = clientWidth
    }
    if (clientHeight < cy)
    {
        cx = cx * clientHeight / cy;
        cy = clientHeight
    }
*/
    if (Mode == "Film")
    {
		//alert(film.height+"=film.height MaxPicHeight="+MaxPicHeight);
		//film.height=MaxPicHeight+"px";      
		cx=(film.offsetWidth-cx)/2
		cy=(MaxPicHeight-cy)/2  
		filmmain.style.marginLeft=cx;  
      filmmain.style.marginTop=cy;
      filmmain.style.marginBottom=cy;
        //filmmain.width = cx;
        //filmmain.height = cy;
    	//filmmain.height=MaxPicHeight;
    }
    else
    {
        slidemain.width = cx;
        slidemain.height = cy;
    }
}

function fadeoutin(imgname,newsrc,newrun) {
  var a,i,t,o
  if (typeof imgname == 'object') {
  		img=imgname;
  		imgname=imgname.id;
  }else{
  		img=document.getElementById(imgname);
  		}
  
  if(newrun && img.doingfade){return 0}
  
  if ((typeof img.opacity == 'undefined') || img.opacity == 99){ img.opacity = 100;}
  t=25;i=-5;
  if (img.src==newsrc){ i=5; }
  img.opacity+=i;
  o=(img.opacity>99)? 99 : img.opacity; //Mozilla >.99 flicker bug fix
  img.style.MozOpacity=o/100;
  if (img.opacity < 5 ) {img.src=newsrc; newsrc=img.src; t=250}
  if (img.opacity < 100) {
  	img.doingfade=1;
  	img.fader=setTimeout("fadeoutin('"+imgname+"','"+newsrc+"',0)", t);
  	}else{img.doingfade=0;img.fader=0;}
}