/* Copyright 2011 iBiSS Software Solutions GmbH */
/* http://www.ibiss.biz */
/* All rights reserved */

var gImageBar, gImage;
var gImagePos = 0;
var gFocusPos = 0;
var gImageMax = 0;
var gImages = new Array();
var gOpacity = 100;

function InitScaledImageBar (aImageBar, aImage) {
  gImage = aImage;
  gImageBar = aImageBar;

  vImage = document.images[gImage];
  vImage.onload = ImageLoaded;

  ShowImage();

  window.onresize = ScaleImage;
  if (gImageMax <= 1) {
    document.getElementById('pfeil_zurueck').style.display = 'none';
    document.getElementById('pfeil_weiter').style.display = 'none';
    document.getElementById('container_thumbs').style.display = 'none';
  } else {
    var vContainerThumb = document.getElementById('container_thumbs');
    vContainerThumb.onmousemove = SetThumbPos;
  }

  SetThumbPosition(0);

}

function MaxPicHeight() {

  var vViewportWidth;
  var vViewportHeight;

  if (typeof window.innerWidth != 'undefined') {
    vViewportWidth = window.innerWidth,
    vViewportHeight = window.innerHeight
  }

  else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0) {
    vViewportWidth = document.documentElement.clientWidth,
    vViewportHeight = document.documentElement.clientHeight
  } else {
    vViewportWidth = document.getElementsByTagName('body')[0].clientWidth,
    vViewportHeight = document.getElementsByTagName('body')[0].clientHeight
  }

  var vTopDiv = document.getElementById('top');
  var vBottomDiv = document.getElementById('container_thumbs');
  vMaxHeight = vViewportHeight - vTopDiv.offsetHeight - vBottomDiv.offsetHeight - 64;
  return vMaxHeight;
}

function ScaleImage() {

  var vPicDiv = document.getElementById(gImageBar);
  vPicDiv.style.height = MaxPicHeight() + 'px';
  var vDivWidth = vPicDiv.offsetWidth;
  var vDivHeight = MaxPicHeight(); //vPicDiv.offsetHeight;
  var vDivQuotient = vDivWidth / vDivHeight;

  var vTop = 0;
  var vPicHeight = 0;

  var vPicture = document.images[gImage];
  var vImgWidth = vPicture.width;
  var vImgHeight = vPicture.height;
  var vImgQuotient = vImgWidth / vImgHeight;

  if (vDivQuotient >= vImgQuotient) {
    vPicHeight = (vDivHeight);
  } else {
    vPicHeight = (vDivWidth / vImgQuotient);
    vTop = (vDivHeight / 2) - (vPicHeight / 2);
  }

  vPicture.style.height = vPicHeight + "px";
  vPicture.style.marginTop = vTop + "px";

  var vBottomDiv = document.getElementById('container_thumbs');
  var vTop = vPicDiv.offsetTop + vPicDiv.offsetHeight;
  vBottomDiv.style.top = vTop + "px";

  var vArrow = document.getElementById('pfeil_zurueck');
  var vTop = vPicDiv.offsetTop + (vPicDiv.offsetHeight / 2) - (vArrow.offsetHeight / 2);
  var vLeft = (vPicDiv.offsetWidth / 2) - (vPicture.width / 2) - (vArrow.offsetWidth) - 4;
  vArrow.style.top = vTop + "px";
  vArrow.style.left = vLeft + "px";

  var vArrow = document.getElementById('pfeil_weiter');
  var vTop = vPicDiv.offsetTop + (vPicDiv.offsetHeight / 2) - (vArrow.offsetHeight / 2);
  var vLeft = (vPicDiv.offsetWidth / 2) + (vPicture.width / 2) + 4;
  vArrow.style.top = vTop + "px";
  vArrow.style.left = vLeft + "px";

  var vContact = document.getElementById('contact');
  if (vContact != null) {
    vTop = (vPicDiv.offsetHeight / 2) - (vContact.offsetHeight / 2);
    vContact.style.top = vTop + "px";
    vLeft = vPicture.offsetLeft;
    vContact.style.left = vLeft + "px";
  }

}

function NextImage() {

  if (gOpacity < 100) {
    return;
  }

  gImagePos = gImagePos + 1;
  if (gImagePos >= gImageMax) {
    gImagePos = 0;
  }

  FadeOut();

}

function PrevImage() {

  if (gOpacity < 100) {
    return;
  }

  gImagePos = gImagePos - 1;
  if (gImagePos < 0) {
    gImagePos = gImageMax - 1;
  }

  FadeOut();

}

function ShowImage() {
  var vImageName = gImages[gImagePos];
  var vPicture = document.images[gImage];
  vPicture.style.marginTop = "0px";
  vPicture.src = vImageName;
  ScaleImage();
  gFocusPos = gImagePos;
  UpdateFocus();
}

function ImageLoaded() {
  ScaleImage();
  FadeIn();
}

function AddImage(aImageName) {
  gImages[gImageMax] = aImageName;
  gImageMax = gImageMax + 1;
}

function FadeOut() {

  gOpacity = gOpacity - 5;
  UpdateOpacity();
  if (gOpacity >= 0) {
    setTimeout('FadeOut()', 25);
  } else {
    ShowImage();
  }

}

function FadeIn() {

  gOpacity = gOpacity + 5;

  UpdateOpacity();

  if (gOpacity < 100) {
    setTimeout('FadeIn()', 25);
  } else {
  }

}

function UpdateOpacity() {
  var vPicture = document.getElementById(gImageBar);

  vOpacity100 = gOpacity;
  if (vOpacity100 < 0) {
    vOpacity100 = 0;
  }

  if (vOpacity100 > 100) {
    vOpacity100 = 100;
  }

  vOpacity = vOpacity100 / 100;

  SetOpacity(vPicture, vOpacity);


}

function SetOpacity(aElement, aOpacity) {

  if (aElement) {

    if(document.getElementById&&document.all){
      vOpacity100 = aOpacity * 100;
      aElement.style.filter='alpha(opacity=' + vOpacity100 + ')';
    } else {
      aElement.style.MozOpacity = aOpacity;
      aElement.style.opacity = aOpacity;
    }

  }

}

function SetThumbPos(e) {
  if (e) {
    e = e;
  } else {
    e = window.event;
  }

  if (e.pageX) {
    var vXBase = e.pageX;
  } else {
    var vXBase = e.clientX;
  }

  SetThumbPosition(vXBase);

}

function SetThumbPosition(aMousePos) {

  var vBar = document.getElementById('contentbar');
  var vConThumb = document.getElementById('container_thumbs');
  var vThumb = document.getElementById('thumbs');

  var vXPos = aMousePos - vBar.offsetLeft;
  var vConMax = vConThumb.offsetWidth;
  var vThumbMax = vThumb.offsetWidth;
  //
  if (vThumbMax > vConMax) {
     var vOverlay = vThumbMax - vConMax;
     var vCurPos = vXPos * 100 / vConMax;
     var vNewLeft = (vOverlay * vCurPos / 100) * (-1);
  } else {
     var vNewLeft = (vConMax / 2 ) - (vThumbMax / 2);
  }

  vThumb.style.left = vNewLeft + "px";

  UpdateFocus();

}

function JumpPic(aPicNo) {

  if (gOpacity < 100) {
    return;
  }

  if (gImagePos != aPicNo) {

    gImagePos = parseInt(aPicNo);
    if (gImagePos >= gImageMax) {
      gImagePos = 0;
    }

    if (gImagePos < 0) {
      gImagePos = gImageMax - 1;
    }

    FadeOut();

  }

}

function FocusPic(aPicNo) {
  gFocusPos = parseInt(aPicNo);
  UpdateFocus();
}

function UpdateFocus() {

  for (var i = 0; i <= gImageMax; i++) {
    var vName = 'thumb_' + i;
    var vElement = document.getElementById(vName);

    if ( (i == gImagePos) || (i == gFocusPos) ) {
      SetOpacity(vElement, 1);
    } else {
      SetOpacity(vElement, 0.4);
    }

  }


}

function RemoveAllFocus() {
  gFocusPos = gImagePos;
  UpdateFocus();
}

