var TotalPics = BigPicSrcs.length-1;
var BigPicPrefix = 'a';
var AutoAdvanceId = TotalPics;
var AutoAdvanceCyclesMax = 3;
var AutoAdvanceCycle = 0;

var Engine = null;
var enOpera = 'Opera';
var enIE = 'IE';
var enGecko = 'Gecko';

function TestEngine()
{
  if (document.all) {
    if (window.opera) {
      Engine = enOpera;
    }
    else {
      Engine = enIE;
    }
  }
  else {
    Engine = enGecko;
  }
}//TestEngine

if (!Engine) TestEngine();

//=======================================================================================
function ChainController(prefix, currentId)
{
  this.CurrentChain = null;
  this.Prefix = prefix;
  this.AsyncObj = null;
  this.CurrentBlock = document.getElementById(this.Prefix + currentId);
  this.NewBlock = null;


  ChainController.prototype.ChainAllowed = function()
  {
    if (this.CurrentBlock == this.NewBlock) return false;
    return true;
  }//ChainAllowed


  ChainController.prototype.RequestChain = function(targetId)
  {
    this.NewBlock = document.getElementById(this.Prefix + targetId);
    
    if (this.ChainAllowed()) {
      if (this.CurrentChain != null) {
        this.CurrentChain.Terminate();
        if (this.CurrentChain.AsyncObj) clearTimeout(this.CurrentChain.AsyncObj);
        this.CurrentChain = null;
      }//if some chain existed earlier
      this.CurrentBlock.style.zIndex = 5;
      this.NewBlock.style.zIndex = 4;
      if (Engine == enIE) {
        this.NewBlock.style.filter='alpha(opacity=100)';    
      } else {
        this.NewBlock.style.opacity = 1;
      }//else      
      this.CurrentChain = new FadeChain(this, this.CurrentBlock);
      this.CurrentBlock = this.NewBlock;
    }//if ChainAllowed
  }//RequestChain


}//ChainController


//=======================================================================================
function FadeChain(controller, block)
{
  this.Controller = controller;
  this.Block = block;
  this.MinPos = 0;
  this.MaxPos = 100;
  this.Position = this.MaxPos;
  this.Interval = 40;
  this.Speed = 4;
  this.AsyncObj = setTimeout("Advance()", this.Interval);


  FadeChain.prototype.Advance = function ()
  {
    if (this.Position <= this.MinPos)
    {
      this.Controller.CurrentChain = null;
      this.Terminate();
      return;
    }//last position

    this.Position-=this.Speed;
    if (Engine == enIE) {
      this.Block.style.filter='alpha(opacity='+this.Position+')';
    } else {
      this.Block.style.opacity = this.Position/this.MaxPos;
    }//else

    this.AsyncObj = setTimeout("Advance()", this.Interval);
  }//Advance


  FadeChain.prototype.Terminate = function ()
  {
    if (Engine == enIE) {
      this.Block.style.filter='alpha(opacity=0)';
    } else {
      this.Block.style.opacity = 0;
    }//else
    this.Block.style.zIndex = 3;
  }//Terminate

}//FadeChain


//=======================================================================================
function Advance()
{
  Controller.CurrentChain.Advance();
}//Advance()


//        document.getElementById('left').innerHTML +=img.src;


function PrepareBigPics()
{
  var i;
  for (i = 1; i<TotalPics ; i++) {
    div = document.getElementById(BigPicPrefix + i);
    if (div) {
      img = div.childNodes.item(0).childNodes.item(0);
      if (img) {
          img.src = BigPicSrcs[i];
      }//if img found
    }//if div found
  }//for
}//PrepareBigPics


function CheckBigPics()
{
  var i;
  for (i = 1; i<=TotalPics ; i++) {
    div = document.getElementById(BigPicPrefix + i);
    if (div) {
      img = div.childNodes.item(0).childNodes.item(0);
      if (img) {
        if (!img.complete) {
          return false;
        }//if
      }//if img found
    }//if div found
  }//for
  return true;
}//CheckBigPics


function ShowBigPics()
{
  var i;
  for (i = 1; i<TotalPics ; i++) {
    div = document.getElementById(BigPicPrefix + i);
    if (div) {
      div.style.display = 'block';
    }//if div found
  }//for
}//ShowBigPics


function Observe()
{
  if (CheckBigPics()) {
    document.getElementById('navigation').style.display = 'block';
    //alert('Executed');
    ShowBigPics();
    setTimeout("AutoAdvance()", 5000);
  } else {
    setTimeout("Observe()", 500);
  }//else go on
}//Observe


function AutoAdvance()
{
  if (AutoAdvanceId > -1) {
    AutoAdvanceId++;
    if (AutoAdvanceId>TotalPics) {
      AutoAdvanceId = 1;
      AutoAdvanceCycle++;
      if (AutoAdvanceCycle>AutoAdvanceCyclesMax) return;
    }//if
    Controller.RequestChain(AutoAdvanceId);
    setTimeout("AutoAdvance()", 5000);
  }//if still in autoadvance mode
}//AutoAdvance
