// Image gallery functions
function galleryClass(galleryId, anchorId, captionFader, pictures, captions, tooltips, anchors, preload)
{
  this.anchorId = anchorId;
  this.galleryId = galleryId;
  this.slideShowSpeed = 3000;
  this.crossFadeDuration = 3; // Set the duration of crossfade (in seconds)
  this.currentImage = 0;
  this.running = false;
  this.pictures = pictures; // Image source
  this.captions = captions; // Image caption
  this.tooltips = tooltips;
  this.anchors = anchors; // list of anchor actions
  this.timerObject = null;
  this.loopfunction = 'slideShowLoop(' + galleryId + ')';
  this.showCount = true;
  this.totalImages = this.pictures.length;
  this.captionFader = captionFader;
  this.loadFiles = new Array();
  load_images = true;
  if (arguments.length > 7){
    load_images = preload;
  }
  // Preload images
  for (var indx = 0; indx < this.totalImages; indx++){
    this.loadFiles[indx] = new Image();
    if (load_images){
      this.loadFiles[indx].src = this.pictures[indx];
    }
    therearecaptions = false;
    if (this.captions[indx] > ''){
    	therearecaptions = true;
    }
    if (therearecaptions == false){this.captionFader.fade = false}
   } 
}

function changeSpeed(gallery, speed){
  stopSlideShow(gallery);
  setSpeed(gallery, speed);
  playSlideShow(gallery);
}

function setSpeed(gallery, speed){
  gallery.slideShowSpeed = speed * 1000;
  gallery.crossFadeDuration = speed;
}
function init_gallery(gallery)
{
  setPosition(gallery);
}

function stopSlideShow(gallery){
  gallery.running = false;
  clearInterval(gallery.timerObject); 
}

function setTitle(gallery,titleText){
 // window.status = titleText;
  var id = gallery.galleryId + "_anchor";	
  if (document.all){
  	if ((typeof window[id]) != 'undefined'){
       window[id].title = titleText;
  	  }
	}
  else {
   elem = document.getElementById(id);
   elem.setAttribute('title',titleText);
  }
}

function playSlideShow(gallery){
  if (!gallery.running){
    runSlideShow(gallery);
  }
}

function runSlideShow(gallery){
  if (!gallery.running){
  	gallery.running = true;
  	slideShowLoop(gallery);
  }
}
function slideShowLoop(gallery){
  incrementImage(gallery);
  switchImage(gallery,gallery.currentImage);
  gallery.timerObject = setTimeout(gallery.loopfunction, gallery.slideShowSpeed);
}

function get_by_name(gallery, image_src){
  for (var indx = 0; indx < gallery.totalImages; indx++){
    if (gallery.pictures[indx] == image_src) {
      return indx;
    }
   } 
  return 0; // Image not found
}

function switchImage(gallery, newimageNumber, stoptimer){
if (stoptimer){
  stopSlideShow(gallery);
}
gallery.currentImage = newimageNumber;
if (gallery.loadFiles[newimageNumber].src  == '') {
	gallery.loadFiles[newimageNumber].src = gallery.pictures[newimageNumber];
}
if (gallery.captionFader.fade){
  fadein(gallery.captionFader,gallery.captions[newimageNumber]);}
else {
  updateLayer(gallery.captionFader.layer, gallery.captions[newimageNumber],true);
}
setTitle(gallery,gallery.tooltips[newimageNumber]);
setPosition(gallery);
if ((typeof document.images[gallery.galleryId+'_picture']) == 'undefined'){
  return;
}
if (document.all){
document.images[gallery.galleryId+'_picture'].style.filter="blendTrans(duration=CrossFadeDuration)";
document.images[gallery.galleryId+'_picture'].filters.blendTrans.Apply();
}
document.images[gallery.galleryId+'_picture'].src = gallery.loadFiles[newimageNumber].src;
if (document.all) {
	document.images[gallery.galleryId + "_picture"].filters.blendTrans.Play();
 }
} 

function setPosition(gallery){
	
 if (gallery.showCount){
  updateLayer(gallery.galleryId+"_counterDiv", get_image_number(gallery),true);
 }
}

function get_image_number(gallery){
  return (gallery.currentImage + 1);
}
function previousImage(gallery){
if (gallery.running){
  stopSlideShow(gallery);
  decrementImage(gallery);
}
decrementImage(gallery);
switchImage(gallery, gallery.currentImage);
}

function nextImage(gallery){
  if (gallery.running){
    stopSlideShow(gallery);
  }
   else {
   incrementImage(gallery);
  }
switchImage(gallery,gallery.currentImage);
}

function incrementImage(gallery){
  gallery.currentImage = gallery.currentImage + 1;
  if (gallery.currentImage >= (gallery.totalImages)) gallery.currentImage=0;
}

function decrementImage(gallery){
 gallery.currentImage = gallery.currentImage - 1;
 if (gallery.currentImage < 0) gallery.currentImage = gallery.totalImages - 1;
}

function popupThis(gallery){
  stopSlideShow(gallery);
  eval(gallery.anchors[gallery.currentImage]);
}

function faderClass(fader_object, text, bgcolor, fcolor, layer, steps, fadespeed)
{
  this.text = text;
  this.bgcolor = bgcolor;
  this.fcolor = fcolor;
  this.steps = steps;
  this.layer = layer;
  this.hide_period = 100;
  this.colors = new Array(steps);
  this.fadeSpeed = fadespeed;
  this.fade = divExists(layer);
  this.fader_object = fader_object;
  this.loopfunction = 'textFaderLoop(' + fader_object + ')';
  getFadeColors(this.bgcolor,this.fcolor,this.colors);
  this.color = 0;
  this.step = 1;
  this.timerObject = null;
}
function getFadeColors(ColorA, ColorB, Colors) {
  var len = Colors.length; 

  // strip '#' signs if present 
  if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
  if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);

  // substract rgb compents from hex string 
  var r = hexToInt(ColorA.substring(0,2));
  var g = hexToInt(ColorA.substring(2,4));
  var b = hexToInt(ColorA.substring(4,6));
  var r2 = hexToInt(ColorB.substring(0,2));
  var g2 = hexToInt(ColorB.substring(2,4));
  var b2 = hexToInt(ColorB.substring(4,6));

  // calculate size of step for each color component
  var rStep = Math.round((r2 - r) / len);
  var gStep = Math.round((g2 - g) / len);
  var bStep = Math.round((b2 - b) / len);

  // fill Colors array with fader colors
  for (var i = 0; i < len-1; i++) {
   Colors[i] = "#" + intToHex(r) + intToHex(g) + intToHex(b);
   r += rStep;
   g += gStep;
   b += bStep;
  }
  Colors[len-1] = ColorB; // make sure we finish exactly at ColorB
}

// intToHex: converts integers between 0-255 into a two digit hex string.
function intToHex(n) {
  var result = n.toString(16);
  if (result.length==1) result = "0"+result;
  return result;
}

// hexToInt: converts two digit hex strings into integer.
function hexToInt(hex) {
  return parseInt(hex, 16);
}
function fadein(fader, text){
	fader.fadinText = text;
	fader.fadeout = true;
	fader.color = fader.steps - 1;
	fader.step = -1;
	fader.done = 0;
	textFaderLoop(fader);
}
function newtext(fader){
	fader.text = fader.fadinText;
	fader.color = -1;
	fader.step = 1;
	fader.done = fader.steps - 1;
	textFaderLoop(fader);
}
function textFaderLoop(fader){
	fader.color += fader.step;
	if (typeof fader.text != 'undefined'){
	  var html = '<font color="'+fader.colors[fader.color]+'">'+fader.text+'</font>'
      updateLayer(fader.layer, html, true)
	}
	if (fader.color == fader.done){
      clearInterval(fader.timerObject);
	  if (fader.fadeout){
	  	fader.fadeout = false;
        setTimeout('newtext('+fader.fader_object+')',fader.hide_period);
	  }
	}
	else{
      fader.timerObject = setTimeout(fader.loopfunction, fader.fadeSpeed);
	}
}
