var activeView = 0;  // 0: Webcambild, 99: wetterbild,
var activeImage = 0; // 0 ist das aktuelle Webcam Image
var bigImagePath = "http://www.flugschule-hochries.de/webcam/hochries";
var interval = 1000*300; // Angabe in ms: 300sec
var fps = 2;
var animationImageIndex = 0;
var animation = false;
var ImageArray = new Array();
var animationInterval = setInterval("LoadNewImage()",interval);
var animationImages = 7;  // 288 entspricht 24h
var thumbOpacity = 60;


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function InitWebcam()
{
      InitImages();
      setInterval("LoadNewImage()",interval);
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ShowImage()
{
    //debugger;
    document.images["WebcamImage"].src = ImageArray[activeImage].src;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function LoadNewImage()
{
      //debugger;
      reload = new Date();
      reload = "?"+reload.getTime();
      var img = new Image();
      img.src = bigImagePath + ".jpg" + reload;

      // Problem bei langsamen Internetverbindungen !!
      //if(ImageArray[0].fileSize != img.fileSize)
      //{
            for (i=6; i>0; i--)
            {
                  ImageArray[i] = ImageArray[i-1];
                  ImageArray[i-1] = null;
            }
            ImageArray[0] = img;
            if(activeView != 99) ShowImage();
            LoadThumbImages();
      //}
      img = null;
      reload = null;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function InitImages()
{
      //debugger;
      reload = new Date();
      reload = "?"+reload.getTime();

      for (i=0; i<7; i++)
      {
            ImageArray[i] = new Image();

            if(i == 0) ImageArray[i].src = bigImagePath + ".jpg" + reload;
            else  ImageArray[i].src = bigImagePath + i + ".jpg" + reload;
      }
      ShowImage();
      LoadThumbImages();
      reload = null;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function LoadThumbImages() {

    try {
    
        document.images["currentImage"].src = ImageArray[0].src;
        document.images["previous5minImage"].src = ImageArray[1].src;
        document.images["previous10minImage"].src = ImageArray[2].src;
        document.images["previous15minImage"].src = ImageArray[3].src;
        document.images["previous20minImage"].src = ImageArray[4].src;
        document.images["previous25minImage"].src = ImageArray[5].src;
        document.images["previous30minImage"].src = ImageArray[6].src;
        ChanceThumbOpacity(activeImage);
    }
    catch(ex){}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ImageLoadError(objectId)
{
      //debugger;
      var sourceobject = document.getElementById(objectId);
      sourceobject.src = "images/error.png";
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ChanceThumbOpacity(value)
{

      if(value != 0)
      {
            current.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_1.style.fontWeight = "normal";
      }
      if(value != 1)
      {
            previous5min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_2.style.fontWeight = "normal";
      }
      if(value != 2)
      {
            previous10min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_3.style.fontWeight = "normal";
      }
      if(value != 3)
      {
            previous15min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_4.style.fontWeight = "normal";
      }
      if(value != 4)
      {
            previous20min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_5.style.fontWeight = "normal";
      }
      if(value != 5)
      {
            previous25min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_6.style.fontWeight = "normal";
      }
      if(value != 6)
      {
            previous30min.style.filter = "alpha(opacity="+thumbOpacity+")";
            ThumbLabel_7.style.fontWeight = "normal";
      }


      if(animation != true)
      {
          switch (value)
          {
                case 0: current.style.filter = "alpha(opacity=100)";
                        ThumbLabel_1.style.fontWeight = "bold";
                        break;
                case 1: previous5min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_2.style.fontWeight = "bold";
                        break;
                case 2: previous10min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_3.style.fontWeight = "bold";
                        break;
                case 3: previous15min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_4.style.fontWeight = "bold";
                        break;
                case 4: previous20min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_5.style.fontWeight = "bold";
                        break;
                case 5: previous25min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_6.style.fontWeight = "bold";
                        break;
                case 6: previous30min.style.filter = "alpha(opacity=100)";
                        ThumbLabel_7.style.fontWeight = "bold";
                        break;
          }
      }
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function Thumbnail_Button(mouseEvent, newImage, objectId)
{
      switch (mouseEvent)
      {
            case "over":  var sourceobject = document.getElementById(objectId);
                          sourceobject.style.filter = "alpha(opacity=100)";
                          break;

            case "out":   if(animation != true)
                          {
                                if(newImage != activeImage)
                                {
                                      var sourceobject = document.getElementById(objectId);
                                      sourceobject.style.filter = "alpha(opacity="+thumbOpacity+")";
                                }
                          }
                          else
                          {
                                var sourceobject = document.getElementById(objectId);
                                sourceobject.style.filter = "alpha(opacity="+thumbOpacity+")";
                          }
                          break;
                          
            case "down":  AnimationStop();
                          activeImage = newImage;
                          ShowImage();
                          ChanceThumbOpacity(newImage);

                          break;
      }
}

////////////////////////////////////////////////////////////////////////////////
//////////////////Bildanimations-Funktionen/////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function AnimationImageChance()
{
      document.images["WebcamImage"].src = ImageArray[animationImageIndex].src;
      animationImageIndex--;
      if(animationImageIndex < 0) animationImageIndex = animationImages-1;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function AnimationButton(mouseEvent)
{
      switch (mouseEvent)
      {
            case "over":  Animation_Button.src = "/images/webcam/animation_button_active.png";
                          break;

            case "out":   if(animation == true) Animation_Button.src = "/images/webcam/animation_button_show.png";
                          else Animation_Button.src = "/images/webcam/animation_button.png";
                          break;

            case "down":  if(animation == false)
                          {
                              Animation_Button.alt = "Animation stoppen";
                              Animation_Button.src = "/images/webcam/animation_button_show.png";
                              AnimationStart();
                              ChanceThumbOpacity(-1);
                          }
                          else
                          {
                              Animation_Button.alt = "Animation starten";
                              Animation_Button.src = "/images/webcam/animation_button_active.png";
                              AnimationStop();
                          }
                          break;
        }











 /*
      if(mouseEvent == "over") Animation_Button.src = "/images/webcam/animation_button_active.png";

      else if(mouseEvent == "out")
      {
          if(animation == true) Animation_Button.src = "/images/webcam/animation_button_show.png";
          else Animation_Button.src = "/images/webcam/animation_button.png";
      }
      
      else if (mouseEvent == "down")
      {
          if(animation == false)
          {
              Animation_Button.alt = "Animation stoppen";
              Animation_Button.src = "/images/webcam/animation_button_show.png";
              AnimationStart();
              ChanceThumbOpacity(-1);
          }
          else
          {
              Animation_Button.alt = "Animation starten";
              Animation_Button.src = "/images/webcam/animation_button_active.png";
              AnimationStop();
          }
      }
      */
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function AnimationStart()
{
      if(animation == false)
      {
            animation = true;
            animationInterval = setInterval("AnimationImageChance()", 1000/fps);
      }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function AnimationPause()
{
      if (animation == true)
      {
            clearInterval(animationInterval);
            animation = false;
      }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function AnimationStop()
{
      if (animation == true)
      {
            clearInterval(animationInterval);
            animation = false;
            animationImageIndex = animationImages-1;
            ChanceThumbOpacity(activeImage);
            ShowImage();
      }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ChanceFPS(value)
{
      if (animation == true)
      {
            AnimationPause();
            fps = value;
            AnimationStart();
      }
}