/*
Element coordinates object implementation
*/
function ThCoords()
{
	this._x = 0;
	this._y = 0;
}
function ThCoords( element )
{
	this._element = element;
	this._x = 0;
	this._y = 0;
}

ThCoords.prototype.create = function( x, y )
{
	if (isNaN(x) || isNaN(y)) 
	{
		x = 0;
		y = 0;
	}
	this._x = x;
	this._y = y;
	return new _ThCoordsEngine( this, x, y ); 
}

ThCoords.prototype.pageXOffset = window.pageXOffset;

ThCoords.prototype.pageYOffset = window.pageYOffset;

ThCoords.prototype.moveTo = function ( x, y )
{
	var newpos =  new _ThCoordsEngine( this, x, y);
	newpos.reposition ( this._element );
	this._x = x;
	this._y = y;
}
ThCoords.prototype.resetPos = function( event )
{
	event = TH.events().get(event);
	this._x = 0;
	this._y = 0;
	window.status = this._x+","+this._y;
}

ThCoords.prototype.toString = function ()
{
	return this._x+","+this._y;
}

ThCoords.prototype.mousePos = function(event)
{
	event = TH.events().get(event);
	return TH.coordinates().create(event.clientX, event.clientY);
}

ThCoords.prototype.displayMousePos = function( event )
{
	event = TH.events().get(event);
	window.status = "Mouse pos : " + TH.coordinates().create(event.clientX, event.clientY).toString();	
}
ThCoords.prototype.displayElementPos = function(element)
{
	window.status = window.status + " element : " + TH.coordinates(element)._offset(element).toString();	
}
ThCoords.prototype.elementPos = function( element )
{
//	return TH.coordinates(element)._offset(element);
	return TH.coordinates(element).topLeftOffset(element);
}

ThCoords.prototype.mouseOffset = function ( event )
{
	event = TH.events().get(event);
	if ( event.pageX >= 0 || event.pageX < 0)
	{
		return this.create(event.pageX,event.pageY);
	}
	else if ( event.clientX >= 0 || event.clientX < 0)
	{
		return this.mousePos(event).plus(this.scrollOffset());	
	}
}

ThCoords.prototype.empty = function()
{
	return this.create(0,0);
}

ThCoords.prototype.scrollOffset = function()
{
	if (window.pageXOffset) 
	{
		return this.create(window.pageXOffset, window.pageYOffset);
	} 
	else if (document.documentElement) 
	{
		return this.create(
				document.body.scrollLeft + document.documentElement.scrollLeft, 
				document.body.scrollTop + document.documentElement.scrollTop);
	} 
	else if (document.body.scrollLeft >= 0) 
	{
		return this.create(document.body.scrollLeft, document.body.scrollTop);
	} 
	else 
	{
		return this.create(0, 0)
	} 
}
ThCoords.prototype.topLeftOffset = function()
{
	var off = this._offset( this._element );
	var parent = this._element.offsetParent;
	while( parent )
	{
		off = off.plus(this._offset( parent ));
		parent = parent.offsetParent;
	}
	return off;
}
ThCoords.prototype.position = function()
{
	var left = parseInt(TH.styles(this._element).read('left'));
	var top = parseInt( TH.styles(this._element).read('top'));
	left = isNaN(left) ? 0 : left;
	top = isNaN(top) ? 0 : top;
	return this.create(left,top);
}
ThCoords.prototype.size = function()
{
	return this._size(this._element);
}

ThCoords.prototype._offset = function ( element )
{
	return this.create(element.offsetLeft, element.offsetTop);
}

ThCoords.prototype._size = function( element )
{
	return this.create( element.offsetWidth, element.offsetHeight);
}
/*
Helper engine class
*/

function _ThCoordsEngine( obj, x, y )
{
	this.point = obj;
	this.x = isNaN(x) ? 0 : x;
	this.y = isNaN(y) ? 0 : y;
}
_ThCoordsEngine.prototype.plus = function( point )
{
	return this.point.create(this.x + point.x, this.y + point.y );
}
_ThCoordsEngine.prototype.minus = function ( point )
{
	return this.point.create( this.x - point.x,this.y - point.y);
}
_ThCoordsEngine.prototype.min = function( point )
{
	return this.point.create(Math.min(this.x,point,x), Math.min( this.y, point.y ));
}
_ThCoordsEngine.prototype.max = function ( point )
{
	return this.point.create( Math.min( this.x, point.x),Math.min( this.y,point,y));
}
_ThCoordsEngine.prototype.toString = function()
{
	return this.x + ","+ this.y;
}
_ThCoordsEngine.prototype.distance = function ( point )
{
	return Math.sqrt(Math.pow(this.x - point.x,2) + Math.pow(this.y - point.y,2));
}	
_ThCoordsEngine.prototype.reposition = function ( element )
{
	element.style['top'] = this.x + "px";
	element.style['left'] = this.y + "px";
}
_ThCoordsEngine.prototype.x = this.x;
_ThCoordsEngine.prototype.y = this.y;
_ThCoordsEngine.prototype.display = function()
{
	window.status = window.status + "offset : "+this.x +":"+this.y;
}