// adds some extra properties to an event object
function formatEvent(e)
{
	var target = e.target;
	if (!target) return null;
	while (target && !target.offsetLeft)
	{
		target = target.parentNode;
	}
	var pc = getPageCoords(target);
	var ec = new Point(window.pageXOffset + e.clientX, window.pageYOffset + e.clientY);
	e.offsetX = ec.x - pc.x;
	e.offsetY = ec.y - pc.y;
	return e;
}


function getPageCoords (element) {
  var coords = new Point(0, 0);
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}


function ToolTip(parentId)
{
	if (!document.getElementById) return;
	if (parentId && parentId.length>0)
		this.parent = document.getElementById(parentId);
	else
		this.parent = document.body;
	if (!this.parent) return;
	this.isBuilt			= false;
	this.cssClass			= "tooltip-container";
	this.headerCssClass		= "tooltip-header";
	this.contentCssClass	= "tooltip-content";
	this.padding			= {x: 0, y: 0};
	this.position			= new Point(0, 0);
	this.positioning		= "relative";
	this.interval			= false;
	this.speed				= 25;
	this.closeImg			= "";
	this.enableFade			= true;

	this.show = function(text)
	{
		this.build();
		this.content.innerHTML = text;
		if (this.isVisible()) this.setVisibility(false);
		this.setDisplay();
	}
	
	this.hide = function()
	{
		this.setVisibility(false);
	}
	
	this.setDisplay = function()
	{
		this.setVisibility(!this.isVisible());
	}
	
	this.setVisibility = function(visible)
	{
		if (visible) this.setOpacity(0);
		this.container.style.display = (visible) ? 'block' : 'none';
		if (visible) 
		{
			if (typeof(global) != 'undefined' && global.interval) // clear out any old fade interval
			{
				clearInterval(global.interval);
				global.interval = false;
			}
			if (this.enableFade)
			{
				this.fadeTo(99);
			}
			else
			{
				this.setOpacity(99);
			}
		}
	}

	this.getOpacity = function()
	{
		if (typeof(this.container.style.MozOpacity) != 'undefined')
		{
			return this.container.style.MozOpacity * 100;
		} 
		else if (this.container.filters && this.container.filters.alpha) 
		{
			return this.container.filters.alpha.opacity;
		} 
		else 
		{
			return 0;
		}
	}
	
	this.setOpacity = function(value)
	{
		if (typeof(this.container.style.MozOpacity) != 'undefined')
		{
			this.container.style.MozOpacity = value / 100;
		} 
		else if (this.container.filters && this.container.filters.alpha) 
		{
			this.container.filters.alpha.opacity = value;
		}
	}
	
	// fades the container to a specified size
	this.fadeTo = function(destination)
	{
		var current = this.getOpacity();
		if (current.toString()==destination.toString())
		{
			clearInterval(this.interval);
			this.interval = false;
			status = "";
			return;
		}
		var increment = 3;
		var direction = (current>destination) ? -1 : 1;
		this.setOpacity(current + (increment * direction));
		if (!this.interval)
		{
			global = this;
			this.interval = setInterval("global.fadeTo(" + destination + ")", this.speed);
		}
	}
	
	// determines if this quote is currently visible
	this.isVisible = function()
	{
		return (this.container.style.display != 'none');
	}
	
	this.getPosition = function()
	{
		var x = this.container.style.left.replace(/[^\d+]/g, "");
		var y = this.container.style.top.replace(/[^\d+]/g, "");
		return new Point(x, y);
	}
	
	// positions the quote near the specified point
	this.setPosition = function(point)
	{
		var proposed = new Point(point.x + this.padding.x, point.y - this.padding.y);
		this.container.style.left = proposed.x + "px";
		this.container.style.top = proposed.y + "px";
	}
	
	this.toString = function()
	{
		return this.text;
	}
	
	// creates the tool tip
	this.build = function()
	{
		if (this.isBuilt) return;
		
		// create object in DOM
		this.container = document.createElement("div");
		
		// add an image tag above the container
		if (this.closeImg && this.closeImg.length>0)
		{
			this.header = document.createElement("div");
			this.header.className = this.headerCssClass;

			this.image = document.createElement("img");
			this.image.setAttribute("src", this.closeImg);
			this.image.setAttribute("title", "Close Window");
			this.image.tooltip = this;
			this.image.onclick = function() {this.tooltip.hide();}
			
			this.header.appendChild(this.image);
			
			this.container.appendChild(this.header);
		}
		
		// add content to container
		this.content = document.createElement("div");
		this.container.appendChild(this.content);
		
		// apply formatting
		this.content.className			= this.contentCssClass;
		this.container.className 		= this.cssClass;
		this.container.style.display 	= "none";
		this.container.style.position 	= this.positioning;
	
		this.parent.appendChild(this.container);
		
		this.isBuilt = true;
	}
}

function Point(x, y)
{
	this.x = x;
	this.y = y;
	this.toString = function()
	{
		return "(" + x + ", " + y + ")";
	}
}

function Region(l, t, r, b)
{
	this.left	= l;
	this.top 	= t;
	this.right 	= r;
	this.bottom	= b;
	
	this.topleft 		= new Point(this.left, this.top);
	this.bottomright 	= new Point(this.right, this.bottom);
	this.topright		= new Point(this.right, this.top);
	this.bottomleft		= new Point(this.left, this.bottom);

	this.getWidth = function() {return this.right - this.left;}
	
	this.getHeight = function() {return this.bottom - this.top;}
	
	// Tests if a point is within this region
	this.containsPoint = function(point)
	{
		return (point.x >= this.left && point.x <= this.right && point.y >= this.top && point.y <= this.bottom);
	}
	
	// Tests if a region is within this region
	this.containsRegion = function(region)
	{
		return this.containsPoint(region.topleft) && this.containsPoint(region.bottomright);
	}
	
	this.toString = function() 
	{
		return this.topleft.toString() + " " + this.bottomright.toString();
	}
	
	// adds a box around the region
	this.addBox = function(cssClass, parentElement)
	{
		var parent 					= parentElement || document.body;
		this.box 					= document.createElement("div");
		var page					= getPageCoords(parent); 
		this.box.className 			= cssClass;
		this.box.style.position		= "absolute";
  		this.box.style.width		= this.getWidth() + "px";
		this.box.style.height		= this.getHeight() + "px";
		this.box.style.left			= (this.left + page.x) + "px";
		this.box.style.top			= (this.top + page.y) + "px";

		if (parentElement.hasChildNodes())
		{
			parentElement.insertBefore(this.box, parentElement.firstChild);
		} else {
			parentElement.appendChild(this.box);
		}
	}
	
	// removes a box from around the region
	this.removeBox = function()
	{
		if (this.box)
		{
			this.box.parentNode.removeChild(this.box);
			this.box = null;
		}
	}
}
