/*
  Author...: Andrea Galligani
  Copyright: Copyright (c) 2007-2008 Andrea Galligani
  Created..: 09-11-2007

  Modified.: 28-01-2008
  Note.....: Added image preloading
	
	Modified.: 19-03-2008
	Note.....: updateIndicator modified to use graphics arrows
  
  Modified.: 17-04-2009
  Note.....: Allow controls to be disabled
*/
var Images 		  = new Array();
var nImages 	  = 0;
var iImage 		  = 0;
// message strings
var sPhoto			= "sPhoto";
var sLoadErr		= "sLoadErr";
var sLoadWarn	  = "sLoadWarn";
var sLoadProg	  = "sLoadProg";
var sLoadStart  = "sLoadStart";
var ShowControls= true;
   
/*
  Nella pagina che ospita le immagini deve essere aggiunta una riga
  come la seguente per ciascuna delle immagini che si vuole visualizzare
  
  Images[nImages++] = "<percorso dell'immagine>";
*/
   
function previous()
{
  iImage--;
  if ( iImage < 0 ) iImage = nImages - 1;
    
  document.getElementById( "mainImage" ).src = Images[iImage];
  if ( ShowControls )
    updateIndicator( sPhoto );
}
   
function next()
{
  iImage++;
  if ( iImage >= nImages ) iImage = 0;
    
  document.getElementById( "mainImage" ).src = Images[iImage];
  if ( ShowControls )
    updateIndicator( sPhoto );
}

/*
function updateIndicator()
{
  document.getElementById( "indicator" ).innerHTML =
    "<a href=\"javascript:previous();\" style=\"font-weight: 800;\">&laquo;</a> " +
    sPhoto + " " + ( iImage + 1 ) + " / " + nImages + 
    " <a href=\"javascript:next();\" style=\"font-weight: 800;\">&raquo;</a>";
}
*/
function updateIndicator()
{
  if ( !ShowControls )
    return;

  document.getElementById( "indicator" ).innerHTML =
    "<a href=\"javascript:previous();\"><img src=\"../img/bw2.png\" border=\"0\" align=\"top\"></a> " +
    sPhoto + " " + ( iImage + 1 ) + " / " + nImages + 
    " <a href=\"javascript:next();\"><img src=\"../img/fw2.png\" border=\"0\" align=\"top\"></a>";
}

function getposOffset( what, offsettype )
{
	return ( what.offsetParent ) ?
		what[offsettype] + this.getposOffset( what.offsetParent, offsettype ) :
		what[offsettype];
}

// image loader definition
function ImageLoader( images, completed, progress )
{
  // store the callbacks
  this.fCompleted = completed;
  this.fProgress  = progress;

  // initialize internal state.
  this.nLoaded = 0;
  this.nCompleted = 0;
  this.aImages = new Array;
 
  // record the number of images.
  this.nImages = images.length;
 
  // for each image, call Loader()
  for ( var i = 0; i < images.length; i++ )
    this.Loader( images[i] );
}

ImageLoader.prototype.Loader = function(image)
{
  // create new Image object and add to array
  var oImage = new Image;
  this.aImages.push( oImage );
  
  // set up event handlers for the Image object
  oImage.onload  = ImageLoader.prototype.onLoad;
  oImage.onerror = ImageLoader.prototype.onError;
  oImage.onabort = ImageLoader.prototype.onAbort;
  
  // assign pointer back to this.
  oImage.oImageLoader = this;
  oImage.bLoaded = false;
  
  // assign the .src property of the Image object
  oImage.src = image;
}

ImageLoader.prototype.onComplete = function()
{
  this.nCompleted++;
  if ( this.nCompleted == this.nImages )
  {
    this.fCompleted( this.aImages, this.nLoaded );
  }
  else {
    this.fProgress( this.aImages, this.nLoaded );
  }
}

ImageLoader.prototype.onLoad = function()
{
  this.bLoaded = true;
  this.oImageLoader.nLoaded++;
  this.oImageLoader.onComplete();
}

ImageLoader.prototype.onError = function()
{
  this.bError = true;
  this.oImageLoader.onComplete();
}

ImageLoader.prototype.onAbort = function()
{
  this.bAbort = true;
  this.oImageLoader.onComplete();
}

function onCompleted( aImages, nImages )
{
	var warn = document.getElementById( "warn" );
	
  if ( nImages != aImages.length )
  {
		warn.innerHTML = sLoadErr;
  }
  else if ( ShowControls )
  {
	  warn.style.visibility = "hidden";
    updateIndicator();
  }
}
   
function onProgress( aImages, nImages )
{
  if ( ShowControls )
  {
    document.getElementById( "warn" ).innerHTML =
      sLoadProg + nImages + "/" + aImages.length;
  }
}
	 
function onBodyLoad()
{
  if ( ShowControls )
  {
    var warn = document.getElementById( "warn" );
    var ind	 = document.getElementById( "indicator" );
  
    warn.style.top 				= getposOffset( ind, "offsetTop" ) - 40;
    warn.style.left 			= getposOffset( ind, "offsetLeft" );
    warn.style.width 			= ind.offsetWidth; // 245
    if ( navigator.userAgent.indexOf( "MSIE" ) == -1 )
    {
      var s = warn.style.width;
      var	n = 0;
      for ( i = 0; i < s.length && s[i] >= '0' && s[i] <= '9'; i++ )
        n = ( n * 10 ) + Number( s[i] );
      warn.style.width = n - 16;
    }
    warn.style.visibility = "visible";
    warn.innerHTML = sLoadStart;
  }

  il = new ImageLoader( Images, onCompleted, onProgress );
}

