// Fade interval in milliseconds
// Make this larger if you experience performance issues
Picfader.INTERVAL_MILLIS = 35;

// Creates a fader
// element - The element to fade
// rate - The speed to fade at, from 0.0 to 100.0
// initialOpacity (optional, default 100) - element's starting opacity, 0 to 100
// minOpacity (optional, default 0) - element's minimum opacity, 0 to 100
// maxOpacity (optional, default 0) - element's minimum opacity, 0 to 100
function Picfader (element, rate, initialOpacity, minOpacity, maxOpacity) {
  this._element = element;
  this._intervalId = null;
  this._rate = rate;
  this._isFadeOut = true;

  // Set initial opacity and bounds
  // NB use 99 instead of 100 to avoid flicker at start of fade
  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;
    }
  }

  // See if we're using W3C opacity, MSIE filter, or just
  // toggling visiblity
  if(typeof element.style.opacity != 'undefined') {

    this._updateOpacity = this._updateOpacityW3c;

  } else if(typeof element.style.filter != 'undefined') {

    // Attempt to preserve existing filters
    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();
}

// Initiates a fade out
Picfader.prototype.fadeOut = function () {
  this._isFadeOut = true;
  this._beginFade();
}

// Initiates a fade in
Picfader.prototype.fadeIn = function () {
  this._isFadeOut = false;
  this._beginFade();
}

// Makes the element completely opaque, stops any fade in progress
Picfader.prototype.show = function () {
  this.haltFade();
  this._opacity = this._maxOpacity;
  this._updateOpacity();
}

// Makes the element completely transparent, stops any fade in progress
Picfader.prototype.hide = function () {
  this.haltFade();
  this._opacity = 0;
  this._updateOpacity();
}

// Halts any fade in progress
Picfader.prototype.haltFade = function () {

  clearInterval(this._intervalId);
}

// Resumes a fade where it was halted
Picfader.prototype.resumeFade = function () {

  this._beginFade();
}

// Pseudo-private members

Picfader.prototype._beginFade = function () {

  this.haltFade();
  var objref = this;
  this._intervalId = setInterval(function() { objref._tickFade(); },Picfader.INTERVAL_MILLIS);
}

Picfader.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();
}

Picfader.prototype._updateVisibility = function () {
  
  if (this._opacity > 0) {
    this._element.style.visibility = 'visible';
  } else {
    this._element.style.visibility = 'hidden';
  }
}

Picfader.prototype._updateOpacityW3c = function () {
  
  this._element.style.opacity = this._opacity/100;
  this._updateVisibility();
}

Picfader.prototype._updateOpacityMSIE = function () {
  
  this._element.filters.alpha.opacity = this._opacity;
  this._updateVisibility();
}

Picfader.prototype._updateOpacity = null;



// ##################################################################



function startPicfader()
{
 	Pict.init();
}

var Pict = new Object();
Pict.init = function()
{
	var hideme = document.getElementById("picfadeIMG");
	Pict.fade = new Picfader(hideme, 10);
	Pict.isHalted = false;
}

function pf0()
{
	Rota(pfSort);
}

function pf1(id)
{
	document.getElementById([pfIcon]).className = 'rotaOff';

	clearTimeout(pfTimer);
	if (typeof pfTimer2 != 'undefined')
		clearTimeout(pfTimer2);
	pfTimer2 = setTimeout("pf2('"+id+"')", 510);
}

function pf2(id)
{
	clearTimeout(pfTimer);
	pf(id);
}

var pfText;
function pf(id)
{
	pfRota = id;
	var elm = "p"+id;
	var pic = document.getElementById([elm]);
	var big = pic.src.replace(/_m\./,".");
	
	document.getElementById('picfadeBGR').style.backgroundImage='url('+big+')';
	
	newText(pic.title);
	pfText = pic.title;
	
	Pict.fade.show();
	Pict.fade.fadeOut();
	
	setTimeout("pfNew('"+big+"')", 350);
}

function newText(txt) 
{
 	if(txt == 0)
 		pfTextTimer = setTimeout("newText('"+pfText+"')", 510);
 	else
 	{
 		clearTimeout(pfTextTimer);
 		document.getElementById('picfadeTXT').innerHTML = txt;
 	}
}

function pfNew(big) 
{
 	document.getElementById('picfadeIMG').style.backgroundImage='url('+big+')';
}

function newRota(id)
{
	pf(id);
	Rota(pfSort);
}

var pfRota;
function Rota(sort)
{
	pfSort = sort;

	// sort next
	if(pfSort == 1)
	{
		if(pfRota < pfLast)
			pfRota++;
		else
			pfRota = 1;
	}
	
	// sort random
	if(pfSort == 2)
	{
		ranNum = Math.round(Math.random() * pfLast+1);
		
		if(ranNum == pfRota) // not the same in a row
			pfRota++;
		else
			pfRota = ranNum;
		
		if(pfRota > pfLast)
			pfRota = 1;
	}
	
	
	// preload next image
	if(pfSort > 0)
	{
		document.getElementById([pfIcon]).className = 'rotaOn';
	
		var elm = "p"+pfRota;
		var pic = document.getElementById([elm]);
		var big = pic.src.replace(/_m\./,".");
		var pre = new Image();
		pre.src = big;
	}
	
	
	// do rotation
	pfTimer = setTimeout("newRota('"+pfRota+"')", pfTime);
}