// becomes method .src of objects in arrPictures()
// Returns a path where to find a pciture
function getSrc(whichPath)
{
  return arrPaths[whichPath] + this.key + ".jpg";
}

// Global variables
var arrPaths          = new Array("../Picture/Web/","../Picture/Dwnl/","../Picture/Thumb/");
var supportFilter     = false;

function getObject(elemID){
  var elem = null;
  if (document.all){
    elem = document.all(elemID);
  }else if (document.getElementById){
    elem = document.getElementById(elemID);
  }
  return elem;
}

function putPicture(doFilter,Id)
{
  if (false == supportFilter){
    doFilter = false;
  }
  doFilter = false;
  // Download-button
  var _pictDownload = getObject("_PictDownload"+Id);
  if (_pictDownload){
    if ("0 KB" == arrContext[Id].arrPictures[arrContext[Id].curPic].size){
      _pictDownload.style.visibility = "hidden";
    }else{
      _pictDownload.style.visibility = "visible";
      _pictDownload.href             = arrContext[Id].arrPictures[arrContext[Id].curPic].src(1);
      _pictDownload.title            = arrContext[Id].arrPictures[arrContext[Id].curPic].size;
      _pictDownload.target           = "_blanc";
    }
  }
  // Prev- and Next-buttons
  if (true == arrContext[Id].doNavigation){
    var _pictPrev = getObject("_PictPrev"+Id);
    var _pictNext = getObject("_PictNext"+Id);
    if (_pictNext && _pictPrev){
      // Arrithmetics start at index=0
      var prevPic
      var nextPic
      if (0 == arrContext[Id].curPic){
        prevPic = arrContext[Id].arrPictures.length-1;
      }else{
        prevPic = arrContext[Id].curPic - 1;
      }
      if (arrContext[Id].curPic == arrContext[Id].arrPictures.length-1){
        nextPic = 0;
      }else{
        nextPic = arrContext[Id].curPic + 1;
      }
      // Mmi starts at index=1
      ++nextPic;
      ++prevPic;
      _pictPrev.title = "Bild " + prevPic + " von " + arrContext[Id].arrPictures.length;
      _pictNext.title = "Bild " + nextPic + " von " + arrContext[Id].arrPictures.length;
    }
  }
  // Direct Indexes
  if (arrContext[Id].arrPictures.length > 5){
    var _pictIndex = getObject("_PictIndex"+Id);
    if (_pictIndex){
      _pictIndex.value = (arrContext[Id].curPic + 1) + " / " + arrContext[Id].arrPictures.length; // +1 because of 0-based index
    }
  }
  // positioning the picture
  var _pictImage = getObject("_PictImage"+Id);
  if (_pictImage){
    _pictImage.style.top    = (arrContext[Id].heigth - arrContext[Id].arrPictures[arrContext[Id].curPic].y) / 2;
    _pictImage.style.left   = (arrContext[Id].width - arrContext[Id].arrPictures[arrContext[Id].curPic].x) / 2;
    if (true == doFilter){
      //document.getElementById("_PictImage").filters.revealTrans.Apply();
      _pictImage.filters.blendTrans.Apply();
    }
    _pictImage.src          = arrContext[Id].arrCachedPictures[arrContext[Id].curPic].src;
    if (true == doFilter){
      //document.getElementById("_PictImage").filters.revealTrans.Play();
      _pictImage.filters.blendTrans.Play();
    }
    _pictImage.alt          = arrContext[Id].arrCachedPictures[arrContext[Id].curPic].alt;
  }
}

function downloadAllPictures(){
  var i = 0;
  var Succeeded = true;
  try{
    while (Succeeded){
      Succeeded = downloadPictures(i++);
    }
    /*
    var _div = getObject("pictCont");
    _div.style.visibility = "hidden";
    */
  }
  catch(e){
  }
} 


// cache-activation: make new array
function downloadPictures(Id){
  try{
    if (arrContext[Id].arrPictures.length > 0){
      if (arrContext[Id].arrPictures.length > 1){
        arrContext[Id].doNavigation = true;
      }
      // making array of img-objects will load, but not show picture
      for (i = 0;  i < arrContext[Id].arrPictures.length; i++){
        arrContext[Id].arrCachedPictures[i] = new Image(arrContext[Id].arrPictures[i].x,arrContext[Id].arrPictures[i].y);
        arrContext[Id].arrCachedPictures[i].src = arrContext[Id].arrPictures[i].src(0);
        arrContext[Id].arrCachedPictures[i].alt = arrContext[Id].arrPictures[i].title;
        if (arrContext[Id].arrPictures[i].x > arrContext[Id].width){
          arrContext[Id].width = arrContext[Id].arrPictures[i].x;
        }
        if (arrContext[Id].arrPictures[i].y > arrContext[Id].heigth){
          arrContext[Id].heigth = arrContext[Id].arrPictures[i].y;
        }
      }
      // define the transition-filter see(http://de.selfhtml.org/dhtml/modelle/dynamische_filter.htm#blend_trans)
      // Transition=21: Jalousie-Effekt waagerecht
      // Transition=12: Zerbröselungseffekt in alle Richtungen
      var _pictImage = getObject("_PictImage"+Id);
      if (arrContext[Id].arrPictures.length <= 5){
        // don't make fancy when to many pictures
        try{
          _pictImage.style.filter = "blendTrans(Duration=1)";
          _pictImage.filters.blendTrans;
          supportFilter = true;
        }
        catch (Exception){
        }
      }
      // Hide what we don't need
      if (false == arrContext[Id].doNavigation){
        var _pictPrev = getObject("_PictPrev"+Id);
        var _pictNext = getObject("_PictNext"+Id);
        _pictPrev.style.visibility = "hidden";
        _pictNext.style.visibility = "hidden";
      }
      putPicture(false,Id);
    }
    return true;
  }
  catch(e){
    return false;
  }
}

function processPrevious(Id)
{
  if (arrContext[Id].curPic == 0){
    arrContext[Id].curPic = arrContext[Id].arrPictures.length-1;
  }else{
    arrContext[Id].curPic--;
  }
  putPicture(true,Id);
}


function processNext(Id)
{
  if (arrContext[Id].curPic == arrContext[Id].arrPictures.length-1){
    arrContext[Id].curPic = 0;
  }else{
    arrContext[Id].curPic++;
  }
  putPicture(true,Id);
}

function processThis(Id)
{
  var _pictIndex = getObject("_PictIndex"+Id);
  try{
    if (_pictIndex){
      var i = parseInt(_pictIndex.value);
      i = i - 1;  // for 0-base
      if ((0 <= i) && (i < arrContext[Id].arrPictures.length)){
        arrContext[Id].curPic = i;
        putPicture(true,Id);
      }else{
        throw 1;
      }
    }
  }
  catch(e){
    if (_pictIndex){
      _pictIndex.value = "[1 .. " + arrContext[Id].arrPictures.length + "]";
    }
  }
}

