// rect.js

var gRectList = new List();

//--------------------------------------------------
function Rect(top,left,height,width)
	{
	this.fixed = false;
	if (arguments.length > 0) this.set(top,left,height,width);
	return this;
	}
//--------------------------------------------------
Rect.prototype.set = function(top,left,height,width)
	{
	this.top = parseInt(top);
	this.left = parseInt(left);
	this.height = parseInt(height);
	this.width = parseInt(width);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.equalPos = function(rect)
	{
	return (this.left==rect.left && this.top==rect.top);
	}
//--------------------------------------------------
Rect.prototype.sect = function(rect)
	{
	var r = this;
	if	(r.left >= (rect.left+rect.width)
	||	(r.left+r.width) <= rect.left
	||	r.top >= (rect.top+rect.height)
	||	(r.top+r.height) <= rect.top) return false;
	return true;
	}

//--------------------------------------------------
Rect.prototype.getdim = function()
	{
	return {height:this.height, width:this.width};
	}
//--------------------------------------------------
Rect.prototype.setFromDim = function(dim)
	{
	this.set(0, 0, this.height, this.width);
	}

//--------------------------------------------------
Rect.prototype.clone = function()
	{
	return new Rect(this.top,this.left,this.height,this.width);
	}
//--------------------------------------------------
Rect.prototype.copyTo = function(dest)
	{
	dest.top = this.top;
	dest.left = this.left;
	dest.height = this.height;
	dest.width = this.width;
	return dest;
	}
//--------------------------------------------------
Rect.prototype.copyFrom = function(src)
	{
	this.top = src.top;
	this.left = src.left;
	this.height = src.height;
	this.width = src.width;
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.setLeft = function(left)
	{
	this.left = parseInt(left);
	this.right = this.left + this.width;
	return this;
	}
//--------------------------------------------------
Rect.prototype.setRight = function(right)
	{
	this.right = parseInt(right);
	this.left = this.right - this.width;
	return this;
	}
//--------------------------------------------------
Rect.prototype.setTop = function(top)
	{
	this.top = parseInt(top);
	this.bottom = this.top + this.height;
	return this;
	}
//--------------------------------------------------
Rect.prototype.setBottom = function(bottom)
	{
	this.bottom = parseInt(bottom);
	this.top = this.bottom - this.height;
	return this;
	}
//--------------------------------------------------
Rect.prototype.setTopLeft = function(top, left)
	{
	this.setTop(top);
	this.setLeft(left);
	return this;
	}

//--------------------------------------------------
Rect.prototype.fixRightBottom = function()
	{
	this.bottom = this.top + this.height;
	this.right = this.left + this.width;
	this.fixed = true;
	return this;
	}
//--------------------------------------------------
Rect.prototype.move = function(top, left)
	{
	this.top = top;
	this.left = left;
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.fitInRectWithProp = function(destRect)
	{
	if (this.height > destRect.height)
		{
		this.width = ((this.width*destRect.height)/this.height);
		this.height = destRect.height;
		}
	if (this.width > destRect.width)
		{
		this.height = ((this.height*destRect.width)/this.width);
		this.width = destRect.width;
		}
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.hfitInRectWithProp = function(destRect)
	{
	if (this.width > destRect.width)
		{
		this.height = ((this.height*destRect.width)/this.width);
		this.width = destRect.width;
		}
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.enlarge = function(vIncrem, hIncrem)
	{
	this.height += vIncrem;
	this.width += hIncrem;
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.enlargeByPct = function(pct)
	{
	this.enlarge(this.height*pct, this.width*pct);
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.inset = function(vInset, hInset)
	{
	if (arguments.length==1 || !hInset) hInset = vInset;
	this.top += vInset;
	this.height -= 2*vInset;
	this.left += hInset;
	this.width -= 2*hInset;
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.offset = function(vOffset, hOffset)
	{
	this.top += vOffset;
	this.left += hOffset;
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.centerInRect = function(destRect)
	{
	this.fitInRectWithProp(destRect);
	this.centerAboutRect(destRect);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.hcenterInRect = function(destRect)
	{
	this.hfitInRectWithProp(destRect);
	this.hcenterAboutRect(destRect);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.centerInRectAbs = function(destRect)
	{
	this.fitInRectWithProp(destRect);
	this.centerAboutRectAbs(destRect);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.hcenterInRectAbs = function(destRect)
	{
	this.hfitInRectWithProp(destRect);
	this.hcenterAboutRectAbs(destRect);
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.centerAboutRect = function(destRect)
	{
	var pos = new Object();
	var vBorder = (parseInt(destRect.height) - parseInt(this.height))/2;
	var hBorder = (parseInt(destRect.width) - parseInt(this.width))/2;
	
	this.top = parseInt(this.top) + parseInt(vBorder);
	this.left = parseInt(this.left) + parseInt(hBorder);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.hcenterAboutRect = function(destRect)
	{
	var pos = new Object();
	var hBorder = (parseInt(destRect.width) - parseInt(this.width))/2;
	this.left = parseInt(this.left) + parseInt(hBorder);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.centerAboutRectAbs = function(destRect)
	{
	var vBorder = (parseInt(destRect.height) - parseInt(this.height))/2;
	var hBorder = (parseInt(destRect.width) - parseInt(this.width))/2;
	
	this.top = parseInt(destRect.top) + parseInt(vBorder);
	this.left = parseInt(destRect.left) + parseInt(hBorder);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.hcenterAboutRectAbs = function(destRect)
	{
	var hBorder = (parseInt(destRect.width) - parseInt(this.width))/2;
	this.left = parseInt(destRect.left) + parseInt(hBorder);
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.keepInRect = function(destRect)
	{
	if (this.left < destRect.left) this.setLeft(this, destRect.left);
	else if (this.right > destRect.right) this.setRight(destRect.right);
	if (this.top < destRect.top) this.setTop(this, destRect.top);
	else if (this.bottom > destRect.bottom) this.setBottom(destRect.bottom);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.flushBot = function(rect)
	{
	this.top = rect.height-this.height;
	return this;
	}
//--------------------------------------------------
Rect.prototype.getCenter = function()
	{
	return {top:this.top+(this.height/2), left:this.left+(this.width/2)};
	}
//--------------------------------------------------
Rect.prototype.getCenterOffset = function()
	{
	return {top:this.height/2, left:this.width/2};
	}

//--------------------------------------------------
Rect.prototype.zoom = function(bounds, pct, anchorPt)
	{
	if (arguments.length < 3) anchorPt = bounds.getCenterOffset();

	var oldOffsetLeft = anchorPt.left - this.left;
	var oldOffsetTop = anchorPt.top - this.top;
	var newH = this.height + (this.height * pct);
	var newW = this.width + (this.width * pct);
	
	var newOffsetTop = (oldOffsetTop * newH) / this.height;
	var newOffsetLeft = (oldOffsetLeft * newW) / this.width;
	
	this.top = (anchorPt.top - newOffsetTop);
	this.left = (anchorPt.left - newOffsetLeft);
	this.height = newH;
	this.width = newW;
	this.fixRightBottom();
	return this;
	}

//--------------------------------------------------
Rect.prototype.mapPt = function(pt, destRect)
	{
	pt.left = parseInt(((pt.left-this.left)*destRect.width)/this.width);
	pt.top = parseInt(((pt.top-this.top)*destRect.height)/this.height);
	return this;
	}
//--------------------------------------------------
Rect.prototype.mapRect = function(srcRect, destRect)
	{
	var topLeft = this.getTopLeft();
	var botRight = this.getBotRight();
	
	srcRect.mapPt(topLeft, destRect);
	srcRect.mapPt(botRight, destRect);
	
	this.setFromPts(topLeft, botRight);
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.getTopLeft = function()
	{
	return {top:this.top, left:this.left};
	}
//--------------------------------------------------
Rect.prototype.getBotRight = function()
	{
	return {top:this.top+this.height, left:this.left+this.width};
	}
//--------------------------------------------------
Rect.prototype.setFromPts = function(topLeft, botRight)
	{
	this.top = topLeft.top;
	this.left = topLeft.left;
	this.height = botRight.top - topLeft.top;
	this.width = botRight.left - topLeft.left;
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.getElem = function(elem){
	if (elem.hasClass("body"))
		{
		var t = 0;
		var l = 0;
		var h = $(window).height();
		var w = $(window).width();
		}
	else
		{
		var t = parseInt(elem.css("top"));
		var l = parseInt(elem.css("left"));
		var h = parseInt(elem.css("height"));
		var w = parseInt(elem.css("width"));
		}
	if (!isnumber(t)) t = elem.position().top;
	if (!isnumber(l)) l = elem.position().left;
	if (!isnumber(h)) h = elem.height();
	if (!isnumber(w)) w = elem.width();

	if (!isnumber(t)) t = 0;
	if (!isnumber(l)) l = 0;
	if (!isnumber(h)) h = 480;
	if (!isnumber(w)) w = 640;
	this.set(t, l, h, w);
	return this;
	}
//--------------------------------------------------
Rect.prototype.setElem = function(elem)
	{
	elem.css({position:'absolute'});
	
	if (this.fixed)
		{
		elem.css({top:this.top, left:this.left, width:this.width, height:this.height});
		}
	else
		{
		if (this.top) elem.css("top",this.top);
		if (this.left) elem.css("left",this.left);
		if (this.height) elem.css("height",this.height);
		else if (this.bottom) elem.css("bottom",this.bottom);
		if (this.width) elem.css("width",this.width);
		else if (this.right) elem.css("right",this.right);
		}
	return this;
	}
//--------------------------------------------------
Rect.prototype.animElem = function(o)
	{
	if (o.speed==undefined) o.speed = 300;
	if (!o.func) o.func = donothing;
	o.elem.stop()
	.animate({top:this.top,left:this.left,height:this.height,width:this.width},
	o.speed, "linear", function(){o.func(o.arg)});
	}
//--------------------------------------------------
Rect.prototype.getElemPos = function(elem)
	{
	this.top = elem.css("top");
	this.left = elem.css("left");
	this.fixRightBottom();
	return this;
	}
//--------------------------------------------------
Rect.prototype.setElemPos = function(elem)
	{
	elem.css({left:this.left, top:this.top});
	return this;
	}

//--------------------------------------------------
Rect.prototype.zoomTo = function(rect, donefunc, arg)
	{
	var div = $("<div></div>").addClass("zoomPhantom")
	.css({opacity:.4, zIndex:32600})
	.attr("doneInfo", gRectList.add([donefunc,arg]))
	.css({zIndex:32760});
	this.setElem(div);
	div.appendTo($("body"));

	div.animate({
		top: rect.top,
		left: rect.left,
		width: rect.width,
		height: rect.height
		}, 300, "linear", done_rectZoomTo);
	}

//--------------------------------------------------
function done_rectZoomTo()
	{
	var info = gRectList.getremove($(this).attr("doneInfo"));
	info[0](info[1]);
	$(this).remove();
	}
