/**************************************
	
	Author: Michael Turnwall
	Created: 07.16.2007
	Description: Object to achieve a "grow"
					effect for image thumbnails
	Copyright 2007 Michael Turnwall - All Rights Reserved

**************************************/

function GrowThumbs(thumbCont)
{
	this.obj = "growThumbs";
	this.thumbConts = document.getElementsByClassName(thumbCont);	// get all the thumb containers
	this.thumbLinks = [];
	// go through the thumb containers and find the links for the thumbs
	for(var i = 0; i < this.thumbConts.length; i++)
	{
		var childNodes = this.thumbConts[i].childNodes;
		for(var j = 0; j < childNodes.length; j++)
		{
			// the child is a link then add it to the link array
			if(childNodes[j].nodeName.toLowerCase() == "a")
			{
				this.thumbLinks.push(childNodes[j]);
			}
		}
	}
	//alert(this.thumbLinks.length)
	// time to create the onclick events
	var self = this;
	for(value in this.thumbLinks)
	{
		this.thumbLinks[value].onclick = function()
		{
			self.createLrgCont(this);
			return false;
		}
	}
	this.effectTimer;
}
/*----------------------*/

GrowThumbs.prototype.createLrgCont = function(linkage)
{
	this.thumb = linkage.firstChild;
	// get rid of the old full grown image
	if(document.getElementById("largerImageCont"))
		removeElement("largerImageCont");
	
	// Create the needed elements
	this.fullImg = document.createElement("img");
	var a = document.createElement("a");
	this.nextLink = document.createElement("a");
	this.prevLink = document.createElement("a");
	this.div = document.createElement("div");
	this.p = document.createElement("p");
	this.imgDescrip = document.createTextNode(this.thumb.getAttribute("alt"));
	
	// Give our elements their attributes
	this.div.setAttribute("id","largerImageCont");
	a.setAttribute("id","largerImgLink");
	a.onclick = this.closeImage;
	this.fullImg.setAttribute("id","largerImage");
	this.fullImg.setAttribute("src", this.thumb.src);
	this.fullImg.style.width = this.thumb.width + "px";
	this.fullImg.style.height = this.thumb.height + "px";
	
	this.div.style.top = linkage.parentNode.offsetTop + "px";
	// stop the image from going beyond the window
	if((linkage.parentNode.offsetLeft+this.fullImg.width) > document.body.clientWidth)
	{
		this.div.style.left = document.body.clientWidth - this.fullImg.width + "px";
	}
	else
	{
		this.div.style.left = linkage.parentNode.offsetLeft + "px";
	}
	this.targetWidth = this.fullImg.width;
	this.targetHeight = this.fullImg.height;
	
	this.growWidth = Math.round((this.targetWidth - this.thumb.width)/15);
	this.growHeight = Math.round((this.targetHeight - this.thumb.height)/15);

	a.appendChild(this.fullImg);
	this.div.appendChild(a);
	setOpacity(this.div,0);
	linkage.parentNode.appendChild(this.div);
	this.growImage();
	fadeIn(this.div.id,0,100,10);
}
/*----------------------*/

GrowThumbs.prototype.growImage = function()
{
	clearTimeout(this.effectTimer);
	var img = this.fullImg;
	var curWidth = img.offsetWidth;
	var curHeight = img.offsetHeight;
	//alert(curWidth + " " + curHeight)

	if(curWidth < this.targetWidth-25 && curHeight < this.targetHeight)
	{
		img.style.width = curWidth + this.growWidth + "px";
		img.style.height = curHeight + this.growHeight + "px";
		this.effectTimer = window.setTimeout(this.obj+".growImage()",10);
	}
	else if(curWidth < this.targetWidth)
	{
		img.style.height = this.targetHeight + "px";
		img.style.width = curWidth + this.growWidth + "px";
		this.effectTimer = window.setTimeout(this.obj+".growImage()",10);
	}
	else if(curHeight < this.targetHeight)
	{
		img.style.width = this.targetWidth + "px";
		img.style.height = curHeight + this.growHeight + "px";
		this.effectTimer = window.setTimeout(this.obj+".growImage()",10);
	}
	else
	{
		img.style.width = this.targetWidth + "px";
		img.style.height = this.targetHeight + "px";
		//this.p.appendChild(this.imgDescrip);
		//this.div.appendChild(this.p);
	}
}
/*----------------------*/

GrowThumbs.prototype.nextImage = function()
{
	
}
/*----------------------*/

GrowThumbs.prototype.closeImage = function()
{
	//el = document.getElementById("largerImageCont");
	fadeOut("largerImageCont",100,0,15,"delete");
}
/*----------------------*/