// JavaScript Document
// Written by Frederic Goddeeris
//


var nbAnimations = 0;
var animations = new Array;

function PictureAnimation(imgID, maxSizeX, maxSizeY, captionID)
{
  this.m_imgID = imgID;
  this.m_captionID = captionID;
  this.m_animationPending = false;
  this.m_maxSizeX = maxSizeX; // abs space the image can occupy
  this.m_maxSizeY = maxSizeY; // abs space the image can occupy
  this.m_currSize = this.m_maxSizeX;
  this.m_maxSize; // Size at which the animation will stop
  this.m_minSize = 10; // Size at which the animation will stop
  this.m_currStep;
  this.m_normalStep = 30;
  this.m_newImgObj;
  this.m_newCaption;
  this.m_imgObj;
  
  // Store in array of objects
  this.m_myAnimationId = nbAnimations;
  animations[nbAnimations] = this;
  nbAnimations++;
}

function isUndefined(a) 
{
     return typeof a == 'undefined';
} 

PictureAnimation.prototype.changePicture = function(newImgSrc, newCaption)
{
  if (this.m_animationPending)
  {
    clearTimeout(this.m_timeout);
  }
  
  // Find target object for image and caption
  if (isUndefined(this.m_imgObj))
  {
    this.m_imgObj = document.getElementById(this.m_imgID);
    //alert(this.m_imgID + "=>" + this.m_imgObj);
  }  
  if (isUndefined(this.m_captionObj))
  {
    this.m_captionObj = document.getElementById(this.m_captionID);
    //alert(this.m_captionID + "=>" + this.m_captionObj);
  }
  
  // Get new image and calculate max size
	this.m_newImgObj = new Image;
	this.m_newImgObj.src = newImgSrc;
	
	// FIXME: I suppose we should wait here for the image to be loaded
	//while (!this.m_newImgObj.complete);
//	if (this.m_newImgObj.width == 0)
//	 alert("Error: size of image is 0")

  // calc maximum size of this new image
	if (this.m_newImgObj.width >= this.m_newImgObj.height)
		this.m_maxSize = this.m_maxSizeX;
	else
		this.m_maxSize = this.m_newImgObj.width * (this.m_maxSizeY / this.m_newImgObj.height);
	
	// Clear current caption
	this.m_newCaption = newCaption;

	try {this.m_captionObj.innerHTML = "";} catch(execption) {};

	if (! this.m_animationPending)
	{
		// Start new animation
		this.m_currStep = -2 * this.m_normalStep;
		this.m_animationPending = true;
	}
	else
	{
    // There is an animtion running
	  if (this.m_currStep < 0)
	  {
	     // Image is scrinking, ok
    }
    else
    {
      // Image is growing
      if (this.m_currSize + this.m_currStep > this.m_maxSize)
  	  {
  		  //alert("alread too large");
  		  this.m_currSize =  this.m_minSize;
  		  try{ this.m_imgObj.width = this.m_currSize; } catch(execption) {};
  		}  
      // Modify image immediately
		  this.m_imgObj.src = this.m_newImgObj.src;
		}
	}	  
	
	// start or continue animation
	this._animate();
}

PictureAnimation.prototype._animate = function()
{
//	if (this.m_currSize > this.m_maxSize && this.m_currStep>0)
//		alert(this.m_currSize + " > " + this.m_maxSize);
  
  this.m_currSize += this.m_currStep;
  if (this.m_currSize > this.m_maxSize)
    this.m_currSize = this.m_maxSize;
	
	//alert(this.m_currStep);
	try{ this.m_imgObj.width = this.m_currSize; } catch(execption) {};

	if (this.m_currSize <= this.m_minSize)
	{
	  // Change image and start increasing again
		this.m_imgObj.src = this.m_newImgObj.src;
		this.m_currStep = this.m_normalStep;	
	}

  if (this.m_currStep < 0 || this.m_currSize < this.m_maxSize)
  {
    // Squedule new animation
    //alert("new animate " +"animations["+this.m_myAnimationId+"]._animate()" )
		this.m_timeout = setTimeout("animations["+this.m_myAnimationId+"]._animate()" ,30);
  }
	else
	{
		this.m_animationPending = false;	
		this.m_captionObj.innerHTML = this.m_newCaption;
	}
}



var nbPhotoTables = 0;
var photoTables = new Array;

function PhotoTableEntry(thumbPath, fullPath, description)
{
	this.m_thumbPath = thumbPath;
	this.m_fullPath = fullPath;
	this.m_description = description;
}


function PhotoTable(previmg_id, previmg_maxX, previmg_maxY, nb_thumbs_per_row, caption_id)
{
	this.m_previmg_id = previmg_id;
	this.m_previmg_maxX = previmg_maxX;
	this.m_previmg_maxY = previmg_maxY;
	this.m_caption_id = caption_id;
	this.m_nb_thumbs_per_row = nb_thumbs_per_row;
	this.timeout;

	this.m_photos = new Array();
	this.m_nbPhotos = 0;
	
	this.m_animation = new PictureAnimation(previmg_id, previmg_maxX, previmg_maxY, caption_id);
	
	this.m_ptID = nbPhotoTables;
	
	photoTables[nbPhotoTables] = this;
	nbPhotoTables++;	
}

PhotoTable.prototype.addPhoto = function(thumbPath, fullPath, description)
{
  if (description.length > 0)
    description = "<img src=arrow22.gif> "+description;
	var entry = new PhotoTableEntry(thumbPath, fullPath, description);
	this.m_photos[this.m_nbPhotos] = entry;

	this.m_nbPhotos++;
}



PhotoTable.prototype.displayThumbs = function()
{
  var nbCols;
  var colCount;
  
  nbCols = this.m_nb_thumbs_per_row;
  //alert("nbcols= " +nbCols);  
	colCount = 0;
	document.write("<table><tr>");
  for (i in this.m_photos)
	{
		with (this.m_photos[i])
		{
		  if (i==0) continue;
			document.write("<td class=pt_thumb>");
			document.write("<img");
      document.write(" class=pt_thumb src=\"",m_thumbPath,"\" ");
      document.write(" onmouseover=photoTables[",this.m_ptID,"]._changePrev(",i,") ");
      document.write(" onmouseout=photoTables[",this.m_ptID,"]._leftPrev(",i,") ");
      if (m_fullPath.length > 0)
      {
	     document.write(" onclick=photoTables[",this.m_ptID,"]._displayFull(",i,") ");
	     document.write(" alt='click for full image'");
	    }
      document.write(">");
    	document.write("</td>");

			colCount++;
			if ( colCount >= nbCols ) 
			{
				document.write("</tr><tr>\n");
				colCount = 0;
			}
		}
	}
	document.write("</tr></table>\n");
	
	//this.m_animation.changePicture(this.m_defaultImgSrc, "");  
}

PhotoTable.prototype._changePrev = function(photoID)
{
  //alert(this.m_animation);
  
  // Cancel Timout if pending
 // if (!isUndefined(this.m_timout))
  {
 //   alert("clear timer" + this.m_timeout)
    window.clearTimeout(this.m_timeout);
    this.m_timout = undefined;
  }
  
  this.m_animation.changePicture(this.m_photos[photoID].m_thumbPath, this.m_photos[photoID].m_description);  
}

PhotoTable.prototype._leftPrev = function()
{
  //alert("start timer")
  // Start a timer, when no new image is selected in a few seconds, to to image 0
  this.m_timeout = window.setTimeout("photoTables["+this.m_ptID+"]._changePrev(0)", 5000);
}

/*
var fullImg = new Image;

PhotoTable.prototype._displayFull = function(photoID)
{
	// Preload image
	var img = new Image;
	fullImg = img;
	img.src = this.m_photos[photoID].m_fullPath;
	img.onload = onFullImgLoaded();
}

function onFullImgLoaded()
{
//	alert("Full Image loaded..." + fullImg.src + " " + fullImg.width + " " + fullImg.height );	

	var newWindow = window.open("pt_fullsize.htm?"+fullImg.src+","+fullImg.width+","+fullImg.height,"", "toolbar=no, directories=no, location=no,status=no, menubar=no, resizable=no, scrollbars=no, width="+screen.width+", height="+screen.height+", left=0, top=0");
}
*/

PhotoTable.prototype._displayFull = function(photoID)
{
    //alert("_displayFull");
    if (this.m_photos[photoID].m_fullPath.indexOf("LINK:") == 0)
    {
      //alert("Link");
      window.open(this.m_photos[photoID].m_fullPath.substr(5));
    }
    else
    {
		  window.open("pt_fullsize.htm?"+this.m_photos[photoID].m_fullPath, "", "toolbar=no, directories=no, location=no,status=no, menubar=no, resizable=no, scrollbars=no, width="+screen.width+", height="+screen.height+", left=0, top=0");
		}
}


function startPhotoTables()
{
  _startPhotoTables();
}

function _startPhotoTables()
{
  for (i in photoTables)
    photoTables[i]._changePrev(0)
}



function do_counters()
{
    if (window.location.protocol == "file:")
    {
      //alert("local, not updating counters");
      //alert(document.URL);
      return;
    } 

		var d=document;
		pag="";col="";scr=0;b=navigator.appName;
		scr=screen.width+"*"+screen.height;
		ref=parent==self ? escape(window.document.referrer) : escape(top.document.referrer);
		pag=escape(d.URL);
		if (b != "Netscape") {col=screen.colorDepth}
		else {col=screen.pixelDepth}
		if(col=="undefined"){col="";}
		d.write("<a href=http://www.belstat.be/viewstat.asp?UserID=fredericg\&lang=nl target=_blank><img border=0 src=\"http://www.belstat.be/regstat.aspx?");
		d.write("UserID=fredericg&BColor=orange&refer=" + ref + "&pag=" + pag + "&b=" + b + "&col=" + col + "&scr=" + scr);
		d.write("\" align=center width=16 height=16 alt=\"Monitored by BelStat - Your Site Counts\"><\/a>");		
}

function do_counters2()
{
    if (window.location.protocol == "file:")
    {
      //alert("local, not updating counters");
      //alert(document.URL);
      return;
    } 
		d=document;
		pag="";col="";scr=0;b=navigator.appName;
		scr=screen.width+"*"+screen.height;
		ref=parent==self ? escape(window.document.referrer) : escape(top.document.referrer);
		pag=escape(d.URL);
		if (b != "Netscape") {col=screen.colorDepth}
		else {col=screen.pixelDepth}
		if(col=="undefined"){col="";}
		d.write("<a href=http://www.belstat.be/viewstat.asp?UserID=fredericg\&lang=nl target=_blank><img border=0 src=\"http://www.belstat.be/regstat.aspx?");
		d.write("UserID=fredericg&Num=1&BColor=teller1&refer=" + ref + "&pag=" + pag + "&b=" + b + "&col=" + col + "&scr=" + scr);
		d.write("\" align=center width=45 height=45 alt=\"Monitored&nbsp;by&nbsp;BelStat&nbsp;-&nbsp;Your&nbsp;Site&nbsp;Counts\"><\/a>");
}
