﻿var arrayindex;

var dia = new Array();
arrayindex = 0;
for(x in xsltdia) {
dia[arrayindex] = xsltdia[x];
arrayindex += 1;
}

var wdh = new Array();
arrayindex = 0;
for(x in xsltwdh) {
wdh[arrayindex] = xsltwdh[x];
arrayindex += 1;
}

var hgt = new Array();
arrayindex = 0;
for(x in xslthgt) {
hgt[arrayindex] = xslthgt[x];
arrayindex += 1;
}

var txt = new Array();
arrayindex = 0;
for(x in xslttxt) {
txt[arrayindex] = xslttxt[x];
arrayindex += 1;
}

var diasTotal = dia.length;
var lastIndex = diasTotal-1;
var thisIndex = 0;

var imageWdh, imageHgt, imageObj;

for(var n in dia) {
imageWdh = wdh[n];
imageHgt = hgt[n];
imageObj = new Image(imageWdh, imageHgt);
imageObj.src = imagePath + dia[n];
}

function init() {
  if(presentation!='irrelevant') {
    if(presentation=='thumbnails') {
    document.getElementById("tn0").style.border = '2px solid red';
    document.getElementById("thumbnail").style.visibility = 'visible';
    }
    if(txt.length>0) document.getElementById("imagetext").innerHTML = txt[0];
  }
}

function showText(index) {
document.getElementById("imagetext").innerHTML = txt[index];
}

//Slideshow presentation

function jump(direction) {
  if(direction=='forward') {
    if(thisIndex<lastIndex) {
    thisIndex += 1;
    }
    else {
    thisIndex = 0;
    }
  }
  else {
    if(thisIndex>0) {
    thisIndex -= 1;
    }
    else {
    thisIndex = lastIndex;
    }
  }
  document.getElementById("image").width = wdh[thisIndex];
  document.getElementById("image").height = hgt[thisIndex];
  document.getElementById("image").src = imagePath + dia[thisIndex];
  document.getElementById("imageindex").innerHTML = 'Billede ' + (thisIndex+1) + ' af ' + diasTotal;
  showText(thisIndex);
}

//Thumbnails presentation

function showThumbnail(index) {
  var thumbnailID;
  document.getElementById("image").width = wdh[index];
  document.getElementById("image").height = hgt[index];
  document.getElementById("image").src = imagePath + dia[index];
  showText(index);
  thumbnailID = 'tn' + index;
  document.getElementById(thumbnailID).style.border = '2px solid red';
  if(thisIndex!=index) {
  thumbnailID = 'tn' + thisIndex;
  document.getElementById(thumbnailID).style.border = '2px solid black';
  }
  thisIndex = index;
}

function Calculate(actualWidth, actualHeight) {
var factor = actualHeight/40;
var reducedWidth = actualWidth/factor;
var roundedWidth = Math.round(reducedWidth);
return roundedWidth;
}

if(presentation=='thumbnails') {
  if(diasTotal>1) {
    var thumbnails = '';
    var thumbnailWidth;
    for(i in dia) {
    thumbnailWidth = Calculate(wdh[i], hgt[i]);
    thumbnails += '<img id="tn' + i + '" src="' + imagePath + dia[i] + '" width="' + thumbnailWidth + '" height="40" alt="" onclick="showThumbnail(' + i + ')" />';
    }
  }
}
