// JavaScript Document

// start fadomatic
Fadomatic.INTERVAL_MILLIS = 25;

function Fadomatic (element, rate, initialOpacity, minOpacity, maxOpacity) {
  this._element = element;
  this._intervalId = null;
  this._rate = rate;
  this._isFadeOut = true;

  this._minOpacity = 0;
  this._maxOpacity = 99;
  this._opacity = 99;

  if (typeof minOpacity != 'undefined') {
    if (minOpacity < 0) {
      this._minOpacity = 0;
    } else if (minOpacity > 99) {
      this._minOpacity = 99;
    } else {
      this._minOpacity = minOpacity;
    }
  }

  if (typeof maxOpacity != 'undefined') {
    if (maxOpacity < 0) {
      this._maxOpacity = 0;
    } else if (maxOpacity > 99) {
      this._maxOpacity = 99;
    } else {
      this._maxOpacity = maxOpacity;
    }

    if (this._maxOpacity < this._minOpacity) {
      this._maxOpacity = this._minOpacity;
    }
  }
  
  if (typeof initialOpacity != 'undefined') {
    if (initialOpacity > this._maxOpacity) {
      this._opacity = this._maxOpacity;
    } else if (initialOpacity < this._minOpacity) {
      this._opacity = this._minOpacity;
    } else {
      this._opacity = initialOpacity;
    }
  }

  if(typeof element.style.opacity != 'undefined') {

    this._updateOpacity = this._updateOpacityW3c;

  } else if(typeof element.style.filter != 'undefined') {

    if (element.style.filter.indexOf("alpha") == -1) {

      var existingFilters="";
      if (element.style.filter) {
        existingFilters = element.style.filter+" ";
      }
      element.style.filter = existingFilters+"alpha(opacity="+this._opacity+")";
    }

    this._updateOpacity = this._updateOpacityMSIE;
    
  } else {

    this._updateOpacity = this._updateVisibility;
  }

  this._updateOpacity();
}

Fadomatic.prototype.fadeOut = function () {
  this._isFadeOut = true;
  this._beginFade();
}

Fadomatic.prototype.fadeIn = function () {
  this._isFadeOut = false;
  this._beginFade();
}

Fadomatic.prototype.show = function () {
  this.haltFade();
  this._opacity = this._maxOpacity;
  this._updateOpacity();
}

Fadomatic.prototype.hide = function () {
  this.haltFade();
  this._opacity = 0;
  this._updateOpacity();
}

Fadomatic.prototype.haltFade = function () {

  clearInterval(this._intervalId);
}

Fadomatic.prototype.resumeFade = function () {

  this._beginFade();
}

Fadomatic.prototype._beginFade = function () {

  this.haltFade();
  var objref = this;
  this._intervalId = setInterval(function() { objref._tickFade(); },Fadomatic.INTERVAL_MILLIS);
}

Fadomatic.prototype._tickFade = function () {

  if (this._isFadeOut) {
    this._opacity -= this._rate;
    if (this._opacity < this._minOpacity) {
      this._opacity = this._minOpacity;
      this.haltFade();
    }
  } else {
    this._opacity += this._rate;
    if (this._opacity > this._maxOpacity ) {
      this._opacity = this._maxOpacity;
      this.haltFade();
    }
  }

  this._updateOpacity();
}

Fadomatic.prototype._updateVisibility = function () {
  
  if (this._opacity > 0) {
    this._element.style.visibility = 'visible';
  } else {
    this._element.style.visibility = 'hidden';
  }
}

Fadomatic.prototype._updateOpacityW3c = function () {
  
  this._element.style.opacity = this._opacity/100;
  this._updateVisibility();
}

Fadomatic.prototype._updateOpacityMSIE = function () {
  
  this._element.filters.alpha.opacity = this._opacity;
  this._updateVisibility();
}

Fadomatic.prototype._updateOpacity = null;


//start slideshow

var total = 0;
var loaded = 0;
var fadeSpeed = 10;
var showSpeed = 4000;
var current = 1;
var timer1 = null;
var faderNext = null;
var faderCurrent = null;

function init(total_images) {
	for(i=1;i<=999;i++) {
		var slide = document.getElementById('slide'+i);
		if (slide == null) { break; }
		var intro = document.getElementById('intro'+i);
		var readmore = document.getElementById('readmore'+i);
		slide.style.visibility = (i == 1 ? 'visible':'hidden');
		intro.style.visibility = (i == 1 ? 'visible':'hidden');
		readmore.style.visibility = (i == 1 ? 'visible':'hidden');
		total = i;
	}
	if (total_images && total_images > 0) {
		total = total_images;
		timer1 = setTimeout("play()", showSpeed);
	}
}

function imageLoaded() {
	loaded++;
	if (loaded >= total) {
		timer1 = setTimeout("play()", showSpeed);
	}
}

function fadeTo(next) {
	stop();
	
	if (next > total) { next = 1; }
	if (next <= 0) { next = total; }
	
	var nextSlide = document.getElementById('slide'+next);
	var currentSlide = document.getElementById('slide'+current);
	var nextIntro = document.getElementById('intro'+next);
	var currentIntro = document.getElementById('intro'+current);
	var nextReadmore = document.getElementById('readmore'+next);
	var currentReadmore = document.getElementById('readmore'+current);

	if (nextSlide != null) {
		faderNext = new Fadomatic(nextSlide, fadeSpeed, 0, 0, 100);
		faderNext.fadeIn();
	}
	
	if (current != next && currentSlide != null) {
		faderCurrent = new Fadomatic(currentSlide, fadeSpeed, 100, 0, 100);
		faderCurrent.fadeOut();
	}
	
	if (current != next && currentIntro != null) {
		currentIntro.style.visibility = 'hidden';
	}

	if (nextIntro != null) {
		nextIntro.style.visibility = 'visible';
	}
	
	if (current != next && currentReadmore != null) {
		currentReadmore.style.visibility = 'hidden';
	}
	
	if (nextReadmore != null) {
		nextReadmore.style.visibility = 'visible';
	}
	
	current = next;
}

function showNext() {
	fadeTo(current + 1);
}

function showPrevious() {
	fadeTo(current - 1);
}

function play() {
	if (current + 1 > total) {
		stop();
		fadeTo(1);
	} else {
		fadeTo(current + 1);
	}
	timer1 = setTimeout("play()", showSpeed);
	
	if (timer1 != null) {
		playbutton(true);
	}
}

function stop() {
	if (faderNext != null) {
		faderNext.show();
	}
	if (faderCurrent != null) {
		faderCurrent.hide();
	}

	if (timer1 != null) {
		clearTimeout(timer1);
		timer1 = null;
	}
	playbutton(false);
}

function playbutton(play) {
	var playbutton = document.getElementById("playbutton");
	var stopbutton = document.getElementById("stopbutton");
	if (playbutton != null && stopbutton != null) {
		playbutton.style.display = (play ? "none":"inline");
		stopbutton.style.display = (play ? "inline":"none");
	}	
}
