var speed = 800
var isNav, isIE;
var Pic = new Array() // don't touch this
var t
var p=50000;
var j = 0;
var loopContinous = 1;
var Dopause=0
var stepdir=1
var isNav=false;
var isIE=false;
var loadedp=-1;
var wait = 0;
var DayInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31)
var yr;Today = new Date();yr = Today.getFullYear();
if (yr%4 == 0){
 DayInMonth=new Array(31,29,31,30,31,30,31,31,30,31,30,31);  
}

var DayArray;
var HourArray;
var hourintv=1;


if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {isNav = true;}
	else {isIE = true}
}

function ChangeCont(){
  if (loopContinous == 1){loopContinous = 0;}
  else loopContinous = 1;
}

function loadnext(){
  loadedp=loadedp+1
  if (loadedp<=p){
   hour = hour +hourintv
   if (hour >= 24){
     hour=hour-24
     day=day+1
     if (day>DayInMonth[month-1]){
       month=month+1
       day=1
       if (month>12){
         month=1
         year=year+1
       }
     }
   }
   if (month<10) date=year + "0" + month;
   else date=year + "" + month;
   if (day<10) date=date +"0"+day;
   else date=date + "" + day;
   if (hour<10) {HourStr = "0" + hour;}
   else {HourStr = "" + hour;}
   for (ivideo=1;ivideo<NrVideos+1;ivideo++){
      //alert("NrVideos = " + NrVideos); 
      eval("preLoad"+ivideo+"[loadedp] = new Image();");
      eval("preLoad"+ivideo+"[loadedp].src = ImgDir"+ivideo+"+date + HourStr + '.png'");
   }

   DayArray[loadedp] = "" + day + "/" + month +"/" + year +" "+hour + ":00";
   HourArray[loadedp] = "" + hour + ":00";
   if (loadedp>=0){
    for (ivideo=1;ivideo<NrVideos+1;ivideo++){
    	eval("document.status"+ivideo+".msg.value = 'Loaded : ' + Math.round(loadedp/p*100) + '%'");
    }
     if (loadedp == p) {
       rewind();
       runSlideShow();
     }
     else {
         checknext();
     }
   }
  // else {runSlideShow();}
  }
}

function showObject(obj) {
	if (isNav)obj.visibility = "show";
	else if (isIE)obj.visibility = "visible";
}
function hideObject(obj) {
	if (isNav) obj.visibility = "hide";
	else if (isIE) obj.visibility = "hidden";
}
function play(dir){
  Dopause=0;
  stepdir=dir
}
function runSlideShow(){
   if (Dopause  == 0) {
     j = j + stepdir
   }
   if (j<0 ){j=loadedp;}
   else if (j > loadedp){
          j=loadedp;
   	  if (loopContinous == 0){
   	       DoPause=1;
	  }else rewind();	
   }
   for (ivideo=1;ivideo<NrVideos+1;ivideo++){
     eval("document.images.SlideShow"+ivideo+".src = preLoad"+ivideo+"[j].src;");
     eval("document.status"+ivideo+".msg.value = 'Viser nu: ' + DayArray[j];");
   }
 
   t = setTimeout('runSlideShow()', speed);
}
function rewind(){
    j=0;
    if (loopContinous == 0)Dopause=1;
}
function stop(){
    j=0;
    Dopause=1;
}
function looping(way){
  if (way == -1) {
    if (isIE){hideObject(forfra.style);}
    else {hideObject(document.forfra);}
  }
  else {
    if (isIE){showObject(forfra.style);}
    else {showObject(document.forfra);}
    t = setTimeout('looping(-1)', 1000)
  }
}
function check(img){
  if (img.complete){
//    alert('complete ' + img.src);
    wait=0;
    return 1
  }
  else {
//    alert('not complete ' + img.src);
    wait=wait+1
    if (wait == 500) {
      missingimage(img.src)
      return -1
    }
  }
  return 0
}
function missingimage(fil){
  // document.write("<HR><H2>Sorry<br>The image " + fil + " failed to load in 30 seconds...<h3>The operation has been aboarded, there may be a problem with the server or your internet-connection is unsuited for this amount of traffic.<br><br>If you think the server is to blame, please try again later... <HR><h3>If you continously encounter these problems, please notify <a href='mailto:vandvagt@dhi.dk'>Vandvagten</a>")
  loadedp=loadedp -1;
  p=loadedp;
  alert("Sorry, the requested image in the animation can not be loaded...\nThe remainded of the animation will not be loaded!!\nPlease try again later...")
  rewind();
  runSlideShow();
}
function checkinitial(){
 rc=check(document.images.SlideShow1);
 if (rc==1) { 
//   alert('Initial img - ok');
   loadnext();}
 else if (rc==0){ t = setTimeout('checkinitial()', 100)}
}
function checknext(){
 for (ivideo=1;ivideo<NrVideos+1;ivideo++){
    eval("rc=check(preLoad"+ivideo+"[loadedp]);");
 }
 if (rc==1) { 
   //alert('Going to load next');
   for (ivideo=1;ivideo<NrVideos+1;ivideo++){
     eval("document.images.SlideShow"+ivideo+".src=preLoad"+ivideo+"[loadedp].src;");
   }
   loadnext();
 }
 else {
   if (rc==0){ t = setTimeout('checknext()', 100)}
 }
}
function step(dir){
   Dopause=1;
   j=j+dir
   if (j<0){j=loadedp;}
   if (j>p){j=0;}
}
function setspeed(dir){
   Dopause=0;
   if (dir == 1)speed=speed*2;
   else speed=speed/2;
}

function init(){
  DayArray = new Array(p);
  HourArray = new Array(p);
//  alert("About to init");
  checkinitial();
}
