/**
* TUI Fisheye Component
* Copyright Toolbox Software Corporation 2007, All Rights Reserved
* @version 1.0
* @author Ethan Farmer
*/
var tbox;
if (!tbox){tbox = {};}
if (!tbox.UI){tbox.UI = {};}

tbox.UI.Fisheye = function(element,options)
{
	this.icons = [];
	var self = this;
	var iconOpt = {};
	if (options){
		this.direction = (!options["dir"])?"horizontal":options["dir"];
		this.xThresh = (!options["xThreshold"])?10:options["xThreshold"];
		this.yThresh = (!options["yThreshold"])?10:options["yThreshold"];
		if (options["labelClass"]){iconOpt.labelClass = options["labelClass"];}
		if (options["showLabel"]){iconOpt.showLabel = options["showLabel"];}
	}
	this.element = element;
	anchors = this.element.getElementsByTagName("a");
	for (x=0;x<anchors.length;x++){
		this.icons[x] = new tbox.UI.Fisheye.Icon(anchors[x].getElementsByTagName("img")[0],iconOpt);
	}
	tbox.UI.addEventListener(document.body,"mousemove",function (e){return self.mouseMove(e);});
}

tbox.UI.Fisheye.prototype.mouseMove = function(event)
{
	c = tbox.UI.getRect(this.element);
	//@todo need ie scroll position
	cx = (event.pageX)?event.pageX:event.clientX;
	cy = (event.pageY)?event.pageY:event.clientY;
	if (cx > (c[2] + this.xThresh) ||
		cx < (c[0] - this.xThresh) ||
		cy > (c[3]+this.yThresh) ||
		cy < (c[1]-this.yThresh)){return this.mouseOut();}	
	for (var x=0;x<this.icons.length;x++){
		img = this.icons[x].image;	
		//in case both width and height are set and for ie
		img.removeAttribute("width");
		oh = this.icons[x].origHeight;
		imgC = tbox.UI.getRect(img);
	
		iconCenter = (imgC[5] /2)+imgC[0];
		iconMiddle = (imgC[4]/2)+imgC[1];
		
		xLoc = cx - iconCenter;
		yLoc = cy - iconMiddle;
		if (this.direction == "horizontal"){
			newHeight = 3*oh - Math.abs(xLoc/1.5);
			img.height = (newHeight > oh)?newHeight:oh;
		} else {
			newHeight = 3*oh - Math.abs(yLoc/1.5);
			img.height = (newHeight > oh)?newHeight:oh;
		}
	}
}
tbox.UI.Fisheye.prototype.mouseOut = function(event)
{
	for (var x=0;x<this.icons.length;x++){
		this.icons[x].image.height = this.icons[x].origHeight;
	}	
}

tbox.UI.Fisheye.Icon = function(element,options)
{	
	this.init(element,options);
}

tbox.UI.Fisheye.Icon.prototype.init = function(element,attribute)
{
	var self = this;
	this.image = element;
	this.origHeight = this.image.height;
	this.newHeight = this.origHeight;
	
	if (this.image.style.width) { this.image.style.width = "";}
	if (this.image.style.height) { this.image.style.height = ""; this.image.height = this.origHeight; }
	this.showLabel =  (attribute["showLabel"])?attribute["showLabel"]:false;
	this.label = document.createElement("div");
	this.label.className = (!attribute || !attribute["labelClass"])?"tui_fe_label":attribute["labelClass"];
	this.image.parentNode.style.textAlign = "center";
	this.image.parentNode.appendChild(this.label);
	this.mouseOut();
	this.label.appendChild(document.createTextNode(this.image.alt));
	
	ael = tbox.UI.addEventListener;
	ael(this.image,'mouseover',function(e) { return self.mouseOver(); });
	ael(this.image,'mouseout',function(e) { return self.mouseOut(); });	
}

tbox.UI.Fisheye.Icon.prototype.mouseOver = function()
{
	if (this.showLabel){this.label.style.display = "block";}
	
}

tbox.UI.Fisheye.Icon.prototype.mouseOut = function()
{
	this.label.style.display = "none";
}