// language specific vars
var ipGalMonthNames;
var ipGalLoading; 

// document nodes
var ipGalCurrentImage;
var ipGalPreviewSlider;
var ipGalPicIndex;
var ipGalSelImgTitle;
var ipGalImgDate;
var ipGalImgDescription;

var ipGalMainPanel;
var ipGalThumbsPanel;

// vars initialized in InitData()
// dont't modify names - begin
var ipGalImgCount = 0;
var ipGalImges = new Array();
var ipGalRootFolder = '';
var ipGalCurrentWidth = 0;
var ipGalPreImgWidth = 100;
var ipGalSpacerImage = '';
// dont't modify names - end

// global vars
var ipGalMainPanelVisible = true;
var ipGalUseImgOnload = false;

var ipGalImgSubFolder = 'images/';
var ipGalCurIndex = 0;
var ipGalPrevIndex = 0;
var ipGalMiddleIndex = 0;
var ipGalHorPos = 0;

var ipGalSliderWidth = (5 * ipGalPreImgWidth) + "px";

// slide speed (1-10 - multiple of 5)
var ipGalSlideSpeed=10;	  
var ipGalTimerVal;
var ipGalButtonDown = false;
var ipGalSliding = false;
var ipGalMod = 0;

var ipGalJumpTime;
var ipGalSlideCount = 1;

// changes if ipGalPreImgWidth changes
var ipGalFastSlideSpeed = (2 * 10);

function Image_getFilename() {
  return this.filename;
}

function Image_getTitle() {
  return this.title;
}

function Image_getDescription() {
  return this.description;
}

function Image_getMonth() {
  return this.month;
}

function Image_getYear() {
  return this.year;
}

function Image_getWidth() {
  return this.width;
}

function Image_getHeight() {
  return this.height;
}

function sanitize_quotes(str) {
  return str.replace(/"/g, '\\\"');
}

function GalImage(fn, tit, desc, m, y, w, h) {
  this.filename = fn;
  this.title = tit;
  this.description = desc;
  this.month = m;
  this.year = y;
  this.width = w;
  this.height = h;
}

// Forces prototype object to be created:
new GalImage("", "", "", 0, 0, 0, 0);

GalImage.prototype.getFilename = Image_getFilename;
GalImage.prototype.getTitle = Image_getTitle;
GalImage.prototype.getDescription = Image_getDescription;

GalImage.prototype.getMonth = Image_getMonth;
GalImage.prototype.getYear = Image_getYear;

GalImage.prototype.getWidth = Image_getWidth;
GalImage.prototype.getHeight = Image_getHeight;

function InitGallery() {

  ipGalCurrentImage = document.getElementById ? 
    document.getElementById("currentImage") : 
    document.all.currentImage;

  ipGalPreviewSlider = document.getElementById ? 
    document.getElementById("previewSlider") : 
    document.all.previewSlider;

  ipGalPicIndex = document.getElementById ? 
    document.getElementById("picIndex") : 
    document.all.picIndex;

  ipGalSelImgTitle = document.getElementById ? 
    document.getElementById("selImageTitle") : 
    document.all.selImageTitle;

  ipGalImgDate = document.getElementById ? 
    document.getElementById("selImageDate") : 
    document.all.date;

  ipGalImgDescription = document.getElementById ? 
    document.getElementById("selImageDescription") : 
    document.all.selImageDescription;
            
  ipGalMainPanel = document.getElementById ? 
    document.getElementById("mainPanel") : 
    document.all.mainPanel;

  ipGalThumbsPanel = document.getElementById ? 
    document.getElementById("thumbsPanel") : 
    document.all.thumbsPanel;

  ipGalMainPanelVisible = false;
  ipGalMainPanel.style.display = "none";

  RemoveNoScriptLinks();
  
  InitData();

  if (ipGalImgCount)
    LoadImage(ipGalImges[0].getFilename(), 0);
}

function ipGalPreloadImage(src) {
  var d=document;
  
  if(d.images){
	if(!d.ip_p) d.ip_p=new Array();
	var i = new Image;
	i.src=src;
	d.ip_p[d.ip_p.length] = i; 
  }
}

function RemoveNoScriptLinks() {
	var elems = document.getElementsByTagName('a');
	var elem;
	
	for (var i = 0; ( elem = elems[i] ); i++ ){
		if ( elem.className == 'galimagelink' ){
			elem.href='javascript:void(0)';
			elem.target='';
		}
	}
}

function GetMonthName(num) {
  if (num > 0 & num < 13) {
	return ipGalMonthNames[num-1];
  } else {
	return "";
  }  
}

function DisplayImageLoadingText() {
  ipGalSelImgTitle.innerHTML = ipGalLoading;
  ipGalImgDate.innerHTML = "";
  ipGalImgDescription.innerHTML = "";
}

function LoadText(ind) {
  ipGalSelImgTitle.innerHTML = ipGalImges[ind].getTitle();
  ipGalImgDescription.innerHTML = ipGalImges[ind].getDescription();
  if (ipGalImges[ind].getMonth() > 0) {
    ipGalImgDate.innerHTML = '(' + GetMonthName(ipGalImges[ind].getMonth()) + ' ' + ipGalImges[ind].getYear() + ')';
  }
  ipGalPicIndex.innerHTML = (ind + 1);
}

function ToggleThumbnailPanel() {
  if (ipGalMainPanelVisible) {
    ipGalMainPanel.style.display = "none";
    ipGalThumbsPanel.style.display = "block";
    ipGalMainPanelVisible = false;
  }
  else {
    ipGalMainPanel.style.display = "block";
    ipGalThumbsPanel.style.display = "none";
    ipGalMainPanelVisible = true;
  }
}

function SkipToImage(filename, index) {
  ToggleThumbnailPanel();
  LoadImage(filename, index, true);
}

function getBrowserWidth() {
  if (window.innerWidth) {
    return window.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientWidth != 0) {
    return document.documentElement.clientWidth;
  }
  else if (document.body) {
    return document.body.clientWidth;
  }
	
  return 0;
}

function LoadImage(fileName, index, abruptJump /*optional - true for JumpToCenterImage()*/) {
  if (ipGalMiddleIndex != index) {
    if (abruptJump) {
      JumpToCenterImage(index, ipGalMiddleIndex);
    }
    else {
      SlideToCenterImage(index, ipGalMiddleIndex);
    }
    
    ipGalPrevIndex = ipGalCurIndex;
    ipGalCurIndex = index;
    ipGalMiddleIndex = index;
  }

  DisplayImageLoadingText();
		
  if(ipGalUseImgOnload) {
    // if Image.onload is supported we load the image immediately and we set
    // new image size in the onload event handler
	ipGalCurrentImage.src = ipGalRootFolder + ipGalImgSubFolder + fileName;
	ipGalCurrentImage.alt = ipGalImges[index].getTitle();
  } else {
    // if Image.onload is NOT supported we use the blank image to avoid unsightly
    // distorsion when the new image loads. We load the new image delayed
    // to have enough time for the blank image to be displayed
	ipGalCurrentImage.src=ipGalSpacerImage;
    setTimeout('LoadImageAfterDelay(' + index + ', "' + ipGalRootFolder + ipGalImgSubFolder + fileName + '", "' + sanitize_quotes(ipGalImges[index].title) +'", "' + (ipGalImges[index].getWidth()) + '", "' + (ipGalImges[index].getHeight()) + '")', 300);
  }
}

function LoadImageAfterDelay(index, mySrc, myAlt, myWidth, myHeight) {
  ipGalCurrentImage.src = mySrc;
  ipGalCurrentImage.alt = myAlt;
  ipGalCurrentImage.width = myWidth;
  ipGalCurrentImage.height = myHeight;
  
  LoadText(index);
}

function IsSpacerImageUrl(s1){
	var s2 = ipGalSpacerImage;
	
	if (s1 && s1.length >= s2.length){		
		return (s1.substr(s1.length-s2.length, s2.length) == s2);
	}
	
	return false;	
}

function OnLoadCurrentImage() {
  if (ipGalCurrentImage)  {
	if(!ipGalUseImgOnload && !IsSpacerImageUrl(ipGalCurrentImage.src)) {
		ipGalUseImgOnload = true;
	}
	  
	ipGalCurrentImage.width = ipGalImges[ipGalCurIndex].getWidth();
	ipGalCurrentImage.height = ipGalImges[ipGalCurIndex].getHeight();

	LoadText(ipGalCurIndex);
  }	
}

function CenterOnCurrentImage() {
  ipGalPreviewSlider.style.left=(-1 * ipGalCurIndex * ipGalPreImgWidth) + "px";
  ipGalMiddleIndex = ipGalCurIndex;
}

function SelectCenterImage() {
  ipGalPrevIndex = ipGalCurIndex;
  ipGalCurIndex = ipGalMiddleIndex;
  LoadImage(ipGalImges[ipGalCurIndex].getFilename(), ipGalCurIndex);
}

function slideleft() {
  if ((-1 * parseInt(ipGalPreviewSlider.style.left)) + parseInt(ipGalSliderWidth) < (ipGalCurrentWidth - ipGalHorPos)) {
    ipGalPreviewSlider.style.left=parseInt(ipGalPreviewSlider.style.left)-ipGalSlideSpeed+"px"

      ipGalMod = (parseInt(ipGalPreviewSlider.style.left) % ipGalPreImgWidth);

    if (ipGalMod == 0) {
      if (ipGalCurIndex < (ipGalImgCount - 1))
	ipGalPrevIndex = ipGalCurIndex;
	ipGalCurIndex += 1;
        ipGalMiddleIndex += 1;

      if (!ipGalButtonDown) {
	clearInterval(ipGalTimerVal);
	LoadImage(ipGalImges[ipGalCurIndex].getFilename(), ipGalCurIndex);
	ipGalSliding = false;
      }
    }
  }
  else {
    clearInterval(ipGalTimerVal);
    LoadImage(ipGalImges[ipGalCurIndex].getFilename(), ipGalCurIndex);
    ipGalSliding = false;
  }
}

function slideright() {
  if (parseInt(ipGalPreviewSlider.style.left) < (ipGalHorPos)) {
    ipGalPreviewSlider.style.left=parseInt(ipGalPreviewSlider.style.left)+ipGalSlideSpeed+"px"

      ipGalMod = (parseInt(ipGalPreviewSlider.style.left) % ipGalPreImgWidth);

    if (ipGalMod == 0) {
      if (ipGalCurIndex > 0)
	ipGalPrevIndex = ipGalCurIndex;
	ipGalCurIndex -= 1;
        ipGalMiddleIndex -= 1;

      if (!ipGalButtonDown) {
	clearInterval(ipGalTimerVal);
	LoadImage(ipGalImges[ipGalCurIndex].getFilename(), ipGalCurIndex);
	ipGalSliding = false;
      }
    }
  }
  else {
    clearInterval(ipGalTimerVal);
    LoadImage(ipGalImges[ipGalCurIndex].getFilename(), ipGalCurIndex);
    ipGalSliding = false;
  }
}

// jumps x pictures to the left
function jumpleft(x) {
  if ((-1 * parseInt(ipGalPreviewSlider.style.left)) + parseInt(ipGalSliderWidth) < (ipGalCurrentWidth - ipGalHorPos)) {
    ipGalPreviewSlider.style.left=parseInt(ipGalPreviewSlider.style.left)-ipGalFastSlideSpeed+"px"

      ipGalMod = (parseInt(ipGalPreviewSlider.style.left) % ipGalPreImgWidth);

    if (ipGalMod == 0) {
      if (ipGalSlideCount >= x) {
		clearInterval(ipGalJumpTime);
		ipGalSliding = false;
      }
      else
		ipGalSlideCount++;
    }
  }
  else {
    clearInterval(ipGalJumpTime);
    ipGalSliding = false;
  }
}

function shiftCenterIndex(x) {
  ipGalMiddleIndex += x;

  if (ipGalMiddleIndex < 0) 
    ipGalMiddleIndex = 0;

  if (ipGalMiddleIndex >= ipGalImgCount) 
    ipGalMiddleIndex = ipGalImgCount - 1;
}

// jumps x pictures to the right
function jumpright(x) {
  if (parseInt(ipGalPreviewSlider.style.left) < (ipGalHorPos)) {
    ipGalPreviewSlider.style.left=parseInt(ipGalPreviewSlider.style.left)+ipGalFastSlideSpeed+"px"

      ipGalMod = (parseInt(ipGalPreviewSlider.style.left) % ipGalPreImgWidth);

    if (ipGalMod == 0) {
      if (ipGalSlideCount >= x) {
	clearInterval(ipGalJumpTime);
	ipGalSliding = false;
      }
      else
	ipGalSlideCount++;
    }
  }
  else {
    clearInterval(ipGalJumpTime);
    ipGalSliding = false;
  }
}

function SlideToCenterImage(index, ipGalMiddleIndex) {
  var deltaPos = index - ipGalMiddleIndex;

  ipGalSlideCount = 1;
  clearInterval(ipGalJumpTime); 
  if (deltaPos > 0) {
    ipGalJumpTime=setInterval("jumpleft(" + deltaPos + ")", 30);
  }
  else if (deltaPos < 0) {
    ipGalJumpTime=setInterval("jumpright(" + (-1 * deltaPos) + ")", 30);
  }
}

function JumpToCenterImage(index, ipGalMiddleIndex) {
  var previewSliderLeft = parseInt(ipGalPreviewSlider.style.left);

  ipGalPreviewSlider.style.left = 
    (previewSliderLeft - ((index - ipGalMiddleIndex) * ipGalPreImgWidth)) + "px";
}

function PrevPicHandler() {
  if (!ipGalSliding) {
    CenterOnCurrentImage(); 
    ipGalSliding = true;
    clearInterval(ipGalTimerVal); 
    ipGalTimerVal=setInterval("slideright()", 25);
    ipGalButtonDown = true;
  }
}

function NextPicHandler () {
  if (!ipGalSliding) {
    CenterOnCurrentImage(); 
    ipGalSliding = true;
    clearInterval(ipGalTimerVal); 
    ipGalTimerVal = setInterval("slideleft()", 25);
    ipGalButtonDown = true;
  }
}

function JumpRightHandler() {
  if (!ipGalSliding) { 
    ipGalSlideCount = 1; 
    ipGalSliding = true;
    clearInterval(ipGalJumpTime);
    shiftCenterIndex(5);
    ipGalJumpTime = setInterval("jumpleft(5)",10);
  }
}

function JumpLeftHandler() {
  if (!ipGalSliding) {
    ipGalSlideCount = 1; 
    ipGalSliding = true;
    clearInterval(ipGalJumpTime); 
    shiftCenterIndex(-5);
    ipGalJumpTime = setInterval("jumpright(5)",10);
  }
}
