function ImageExpander(oThumb, sImgSrc)
{
	// store thumbnail image and overwrite its onclick handler.
	oThumb.style.position='relative';
	this.oThumb = oThumb;
	this.oThumb.expander = this;
	this.oThumb.onclick = function() { this.expander.expand(); }

	var pos = this.findPos(oThumb);
	// record original size
	this.smallLeft = pos[0];//oThumb.offsetLeft;
	this.smallTop  = pos[1];//oThumb.offsetTop;	 

	this.smallWidth = oThumb.offsetWidth;
	this.smallHeight = oThumb.offsetHeight;	

	this.bExpand = true;
	this.bTicks = false;
	
	// self organized list
	if ( !ImageExpander.aImageExpanders )
	{
		ImageExpander.aImageExpanders = new Array();
	}
	ImageExpander.aImageExpanders.push(this);

	// create the full sized image.
    this.create(sImgSrc);
    this.oThumb.style.cursor='progress';
}
ImageExpander.aImageExpanders = null;
ImageExpander.prototype.findPos =function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop  += obj.offsetTop;
		} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
};
ImageExpander.prototype.create = function(sImgSrc) {
	this.oImg = new Image();
	this.oImg.expander = this;
	this.oImg.onload = function(){this.expander.onload();}
    document.body.style.cursor='progress';
	this.oImg.src = sImgSrc; // fire download
}
ImageExpander.prototype.destroy = function(sImgSrc) {
    if(!this.oDiv) return;
	this.oImg.style.visibility = "hidden";
	this.oDiv.style.visibility = "hidden";
}

ImageExpander.prototype.onload = function()
{
	this.oDiv = document.createElement("div");
	document.body.appendChild(this.oDiv);
	this.oDiv.appendChild(this.oImg);
	this.oDiv.style.position = "absolute";
    document.body.style.cursor='auto';
    this.oThumb.style.cursor='pointer';
    this.oDiv.style.zIndex='1000';
	this.oDiv.expander = this;
	this.oDiv.onclick = function() {this.expander.toggle();};
	
	this.oImg.title = "Click to reduce.";
	this.bigWidth = this.oImg.width;
	this.bigHeight = this.oImg.height;
	
	if ( this.bExpand )
	{
		this.expand();
	}
	else
	{
		this.destroy();
	}
}
ImageExpander.prototype.toggle = function()
{
	this.bExpand = !this.bExpand;
	if ( this.bExpand )
	{
		for ( var i in ImageExpander.aImageExpanders )
			if ( ImageExpander.aImageExpanders[i] !== this )
				ImageExpander.aImageExpanders[i].reduce();
	}
}
ImageExpander.prototype.expand = function()
{
	// set direction of expansion.
	this.bExpand = true;

	// set all other images to reduce
	for ( var i in ImageExpander.aImageExpanders )
		if ( ImageExpander.aImageExpanders[i] !== this )
			ImageExpander.aImageExpanders[i].reduce();

	// if not loaded, don't continue just yet
	if ( !this.oDiv ) return;
	
	
	// calculate initial dimensions
	var pos = this.findPos(this.oThumb);
	this.x = pos[0];//this.oThumb.offsetLeft;
	this.y = pos[1];//this.oThumb.offsetTop;
	this.w = this.oThumb.clientWidth;
	this.h = this.oThumb.clientHeight;
	// hide the thumbnail
	this.oThumb.style.visibility = "hidden";
	
	this.oDiv.style.left = this.x + "px";
	this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";
	this.oDiv.style.visibility = "visible";
	this.oImg.style.visibility = "visible";
	// start the animation engine.
	if ( !this.bTicks )
	{
		this.bTicks = true;
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);	
	}
}
ImageExpander.prototype.reduce = function()
{
	if ( !this.oDiv ) return;
	// set direction of expansion.
	this.bExpand = false;
}
ImageExpander.prototype.tick = function()
{
	if ( !this.oDiv ) return;
	var cw = (window.innerWidth ||document.documentElement.clientWidth ||document.body.clientWidth);
	var ch = (window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight);
	var cx = (window.pageXOffset||document.documentElement.scrollLeft  ||document.body.scrollLeft) + cw / 2;
	var cy = (window.pageYOffset||document.documentElement.scrollTop   ||document.body.scrollTop)  + ch / 2;

	// calculate target
	var tw,th,tx,ty;
	if ( this.bExpand )
	{
		tw = this.bigWidth;
		th = this.bigHeight;
		if ( tw > cw )
		{
			th *= cw / tw;
			tw = cw;
		}	
		if ( th > ch )
		{
			tw *= ch / th;
			th = ch;
		}
		tx = cx - tw / 2;
		ty = cy - th / 2; 
	}
	else
	{
		tw = this.smallWidth;
		th = this.smallHeight;
		tx = this.smallLeft;
		ty = this.smallTop;
	}	
	// move 5% closer to target
	var nHit = 0;
	var fMove = function(n,tn) 
	{
		var dn = tn - n;
		if ( Math.abs(dn) < 3 )
		{
			nHit++;
			return tn;
		}
		else
		{
			return n + dn / 10;
		}
	}
	this.x = fMove(this.x, tx);
	this.y = fMove(this.y, ty);
	this.w = fMove(this.w, tw);
	this.h = fMove(this.h, th);
	
	this.oDiv.style.left = this.x + "px";
	this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";

	// if reducing and size/position is a match, stop the tick	
	if ( !this.bExpand && (nHit == 4) )
	{
        this.destroy();
		this.oThumb.style.visibility = "visible";

		this.bTicks = false;
	}
	
	if ( this.bTicks )
	{
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);
	}
}

