scrollerCheckElements = new Array("completePageContent");
scrollerSize = scrollerDefaultSize;
window.onresize = checkWindowSize;
flashDetected = false;
var currentImageToDisplay = 0;
var counter = 0;
var pos;

function checkGalleryLength() {
  if(slideCount > dispayedThumbs) {
    setArrowActiv('backward');
    setArrowActiv('forward');
  }
}

function setArrowInactiv(direction) {
  if(direction == "backward") {
    contentLeft = '<img src="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_back_afafaf.gif" ) + '" width="13" height="12">';
    writeIntoLayer('imagebarLeftNavi',contentLeft);
  }
  if(direction == "forward") {
    contentRight = '<img src="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_forward_afafaf.gif" ) + '" width="13" height="12">';
    writeIntoLayer('imagebarRightNavi',contentRight);
  }
}

function setArrowActiv(direction) {
  if(direction == "backward") {
    contentLeft = '<a href="javascript:displaySlide(-2);" onmouseover="switchImage(\'arrowBackward\',1);" onmouseout="switchImage(\'arrowBackward\',0);"><img src="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_back.gif" ) + '" width="13" height="12" preload="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_back-h.gif" ) + '" id="arrowBackward"></a>';
    writeIntoLayer('imagebarLeftNavi',contentLeft);
  }

  if(direction == "forward") {
    contentRight = '<a href="javascript:displaySlide(-1);" onmouseover="switchImage(\'arrowForward\',1);" onmouseout="switchImage(\'arrowForward\',0);"><img src="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_forward.gif" ) + '" width="13" height="12" preload="' + buildValidServerRelativeUrl("/_common/narrowband/img/standard_elements/img_forward-h.gif" ) + '" id="arrowForward"></a>';
    writeIntoLayer('imagebarRightNavi',contentRight);
  }
}

function setArrowStatus(count) {
  var tempIndex = slideCount-1;
  if(count == 0 && count < tempIndex) {
    setArrowInactiv('backward');
    setArrowActiv('forward');
  } 
  if(count > 0 && count < tempIndex) {
    setArrowActiv('backward');
    setArrowActiv('forward');
  }
  if(count == tempIndex) {
    setArrowActiv('backward');
    setArrowInactiv('forward');
  }
}

function displaySlide(slideIndex, pos){
  var slide;
  
  if(slideIndex == -1) {
    slideIndex = currentImageToDisplay + 1;
    if (savedPos == (dispayedThumbs - 1) && slideIndex == currentImageToDisplay+1) {
      moveImages('forward');
    }
    setImgBorderPermanent(slideIndex, savedPos+1);
  }
  if(slideIndex == -2) {
    slideIndex = currentImageToDisplay - 1;
    if (savedPos == 0 && slideIndex == currentImageToDisplay-1) {
      moveImages('backward');
    }
    setImgBorderPermanent(slideIndex, savedPos-1);
  }

  if(!highbandUser) {
    prepareBigImage(slideIndex);
  }
  
  document.getElementById("legend").style.visibility = "hidden";
  
  for (var x = 0; x < slideCount; x++){
    if(oneTextOnly) {
      slide = 0;
    }
    else {
      slide = x;
    }
    
    if(document.getElementById("completeText" + x)!= null && !oneTextOnly){
      document.getElementById("completeText" + x).style.visibility = "hidden";
    }
    
    if(document.getElementById("legend" + x)!= null && !oneTextOnly){
      document.getElementById("legend" + x).style.visibility = "hidden";
    }

    if(x == slideIndex){
      if(highbandUser && flashDetected) {
        if(typeof(document.getElementById("imageSlideShowSwf").scrollTo) == "function") {
          document.getElementById("imageSlideShowSwf").scrollTo(slideIndex);
        }
      }
      else {
        document.images["displayedImage"].src = imageDatabase.images[slideIndex];
      }
      document.getElementById("completeText" + slide).style.visibility = "visible";
      if(document.getElementById("legend" + slide).innerHTML != "") {
        document.getElementById("legend").style.visibility = "visible";
        document.getElementById("legend" + slide).style.visibility = "visible";
      }
    }
  }
  
  if(pos | (pos == 0)) {
    setImgBorderPermanent(slideIndex, pos);
  }
  
  currentImageToDisplay = slideIndex;
  setArrowStatus(slideIndex);
  
  if(confTrackingEnabled) {
    var trackingUrl = window.location.pathname.replace(/\./g, "_slide_" + slideIndex + ".");
    trackAbsolute(trackingUrl,'');
  }
}

function onUnloadFunctions(){
  resetBottomNavigation();
}


function setImgBorder(divNr, pos){
  if (pos > (dispayedThumbs - 1)) {pos = (dispayedThumbs - 1);}
  posX = 316 + ((pos * 87) + pos)
  moveObject('imgBorder',posX,363);

  content='<a href="javascript:displaySlide(' + divNr + ',' + pos + ');" onmouseout="deleteImgBorder();"><img src="' + buildValidServerRelativeUrl("/_common/html/img/standard_elements/highlight_box_90x54.gif" ) + '" border="0" width="90" height="54"></a><br>';

  writeIntoLayer('imgBorder' ,content);
  setVisibility("imgBorder",1,"block");
}

function setImgBorderPermanent(divNr, pos){
  if (pos > (dispayedThumbs - 1)) {pos = (dispayedThumbs - 1);}
  if (pos < 0) {pos = 0;}
  
  posX = 316 + ((pos * 87) + pos)
  moveObject('imgBorderPermanent',posX,363);
  setVisibility("imgBorderPermanent",1,"block");
  setVisibility("imgBorder",0,"none");

  savedPos = pos;
  return savedPos;
}

function deleteImgBorder(){
  setVisibility("imgBorder",0,"none");
}

function prepareBigImage(id) {
  setClassName("equipmentBody","loading");
  setVisibility("bigImageLoader",1);
  preLoadBigImage(id);
}

function preLoadBigImage(id) {
  myId = id;
  bigImage = new Image();
  bigImage.onabort = loadUpdate;
  bigImage.onerror = loadUpdate;
  bigImage.onload = loadUpdate;
  bigImage.src = imageDatabase.images[id];
}

function loadUpdate() {
  if (checkObject()) {
    setVisibility("bigImageLoader",0);
    setClassName("equipmentBody","");
    document.images["displayedImage"].src = bigImage.src;
  }
}

function checkObject() {
  actualImageSrc = imageDatabase.images[myId];
  if (bigImage.src == getFullPath(self.location.href, actualImageSrc)) {
    return true;
  }
  else {
    return false;
  }
}

function imageSlideShowGetFlashContent()  {
  return imageDatabase;
}

function moveImages(direction) {
  var imageGallery = "";
  var maximum;

  if(direction == "start") { counter = 0; }

  if(direction == "backward") {
    counter--;
    if(counter < 0) {
      counter = 0;
    }
  }

  if(direction == "forward") {
    counter++;
    if(counter >= (slideCount - dispayedThumbs)) {
      counter = slideCount - dispayedThumbs;
    }
  }
	
  if(slideCount < dispayedThumbs) {
    maximum = slideCount;
  } else {
    maximum = counter + dispayedThumbs;
  }

  for(i=counter,pos=0; i < maximum; i++) {
    imageGallery += '<a href="javascript://" onmouseover="setImgBorder(' + i + ',' + pos + ');" class="thumbLink"><img src="'+imageDatabase.thumbs[i]+'" width="86" height="48" style="margin-right:1px;"></a>';
    pos++;
  }
  writeIntoLayer('imagebar',imageGallery);
}

function startSlideshow(imageParam) {
  if(imageParam != null && imageParam != -1 && !isNaN(imageParam) && (imageParam <= slideCount) && imageParam != 0) {
    imageParam--;
    
    if(imageParam < dispayedThumbs) {
      displaySlide(imageParam, imageParam);
    }
    
    if(imageParam >= dispayedThumbs) {
      displaySlide((dispayedThumbs - 1), (dispayedThumbs - 1));
      for(i=0; i < (imageParam + 2); i++) {
        displaySlide(-1);
      }
    }
  } else {
    displaySlide(0, 0);
    setImgBorderPermanent(0, 0);
  }
}

