var Coordinates =
{
	ORIGIN : new Coordinate(0, 0),

	northwestPosition : function(element) {
		var x = parseInt(element.style.left);
		var y = parseInt(element.style.top);

		return new Coordinate(isNaN(x) ? 0 : x, isNaN(y) ? 0 : y);
	},

	southeastPosition : function(element) {
		return Coordinates.northwestPosition(element).plus(
				new Coordinate(element.offsetWidth, element.offsetHeight));
	},

	northwestOffset : function(element, isRecursive) {
		var offset = new Coordinate(element.offsetLeft, element.offsetTop);

		if (!isRecursive) return offset;

		var parent = element.offsetParent;
		while (parent)
		{
			offset = offset.plus(new Coordinate(parent.offsetLeft, parent.offsetTop));
			parent = parent.offsetParent;
		}
		return offset;
	},

	southeastOffset : function(element, isRecursive) {
		return Coordinates.northwestOffset(element, isRecursive).plus(
				new Coordinate(element.offsetWidth, element.offsetHeight));
	},
	
	scrollPosition : function()
	{
		var x,y = 0;
		if (self.pageYOffset)
		{
			// all except Explorer
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
		{
			// Explorer 6 Strict
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			// all other Explorers
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		return new Coordinate(x,y);
	},
	
	clientWindow : function()
	{
		var x,y = 0;
		if (self.innerHeight)
		{
			// all except Explorer
			x = self.pageXOffset;
			y = self.innerWidth;
		}
		else if (document.documentElement && (document.documentElement.clientHeight || document.documentElement.clientWidth))
		{
			// Explorer 6 Strict
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			// all other Explorers
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return new Coordinate(x,y);
	},

	fixEvent : function(event)
	{
		event.windowCoordinate = new Coordinate(event.clientX, event.clientY);
	}
};

function Coordinate(x, y) {
	this.x = x;
	this.y = y;
}

Coordinate.prototype.toString = function() {
	return "(" + this.x + "," + this.y + ")";
}

Coordinate.prototype.plus = function(that) {
	return new Coordinate(this.x + that.x, this.y + that.y);
}

Coordinate.prototype.minus = function(that) {
	return new Coordinate(this.x - that.x, this.y - that.y);
}

Coordinate.prototype.distance = function(that) {
	var deltaX = this.x - that.x;
	var deltaY = this.y - that.y;

	return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
}

Coordinate.prototype.max = function(that) {
	var x = Math.max(this.x, that.x);
	var y = Math.max(this.y, that.y);
	return new Coordinate(x, y);
}

Coordinate.prototype.constrain = function(min, max) {
	if (min.x > max.x || min.y > max.y) return this;

	var x = this.x;
	var y = this.y;

	if (min.x != null) x = Math.max(x, min.x);
	if (max.x != null) x = Math.min(x, max.x);
	if (min.y != null) y = Math.max(y, min.y);
	if (max.y != null) y = Math.min(y, max.y);

	return new Coordinate(x, y);
}

Coordinate.prototype.reposition = function(element) {
	element.style["top"] = this.y + "px";
	element.style["left"] = this.x + "px";
}

Coordinate.prototype.equals = function(that) {
	if (this == that) return true;
	if (!that || that == null) return false;

	return this.x == that.x && this.y == that.y;
}

// returns true of this point is inside x range
Coordinate.prototype.inside = function(northwest, southeast)
{
	if ((this.x >= northwest.x) && (this.x <= southeast.x))
		return true;

	return false;
}

function rearrangeItems()
{
	if(document.cookie)
	{
		var cookies = document.cookie.split(";");
		
		for(var i=0; i<cookies.length; i++)
		{
			if(cookies[i].indexOf("magadoo_links") >= 0)
			{
				var dividgroup = cookies[i].substr(14).split(":");
				
				for(var j=0; j<dividgroup.length; j++)
				{
					var divids = dividgroup[j].split(",");
					
					for(var k=0; k<divids.length; k++)
					{
						if(!isNaN(divids[k]) && document.getElementById('lb' + divids[k]) && document.getElementById('tblcol' + j))
						{
							var container = document.getElementById('tblcol' + j);
							var divcontent = document.getElementById('lb' + divids[k]);
							container.appendChild(divcontent);
							//divcontent.firstChild.firstChild.innerHTML += " " + j + "/" + k;
						}
					}
				}

				return;
			}
		}
	}
}