function Rectangle(x, y, width, height) {
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}

Rectangle.prototype.contains = Rectangle_contains;

function Rectangle_contains(x, y) {
	if (x >= this.x && x <= this.x+this.width-1 &&
		y >= this.y && y <= this.y+this.height-1)
		return true;
	else
		return false;
}


BORDER_PLAIN = 1;
BORDER_FRAME = 2;
BORDER_UP    = 4;
BORDER_DOWN  = 8;
BORDER_TAB   = 16;

ZebuDOM.turnIntoZebuBorderElement = ZebuDOMEx_turnIntoZebuBorderElement;


function ZebuDOMEx_turnIntoZebuBorderElement(zebuElement, borderWidth, borderType){
    if (!borderWidth)
        borderWidth = 1;
    if (!borderType)
        borderType = BORDER_PLAIN;
	var size = zebuElement.zbGetSize();
	
	if (zebuElement.leftBorder == null)
		zebuElement.leftBorder   = document.zbAppendLayer(zebuElement.id+"left", borderWidth, size.height, zebuElement);
	zebuElement.leftBorder.zbSetLocation(0, 0);
	if (zebuElement.topBorder == null)
		zebuElement.topBorder    = document.zbAppendLayer(zebuElement.id+"top", size.width, borderWidth, zebuElement);
	zebuElement.topBorder.zbSetLocation(0, 0);
	if (zebuElement.rightBorder == null)
		zebuElement.rightBorder  = document.zbAppendLayer(zebuElement.id+"right", borderWidth, size.height, zebuElement);
	zebuElement.rightBorder.zbSetLocation(size.width-borderWidth, 0);
	if (zebuElement.bottomBorder == null)
		zebuElement.bottomBorder = document.zbAppendLayer(zebuElement.id+"bottom", size.width, borderWidth, zebuElement);
	zebuElement.bottomBorder.zbSetLocation(0, size.height-borderWidth);

	var width = null;
	var height = null;
	if (size.width > 0)	
		width = size.width-2*borderWidth;
	if (size.height > 0)	
		height = size.height-2*borderWidth;
	if (zebuElement.client == null)
		zebuElement.client = document.zbAppendLayer(zebuElement.id+"clientarea", width, height, zebuElement);
	zebuElement.client.zbSetLocation(borderWidth, borderWidth);
    zebuElement.borderWidth  = borderWidth;
   
   	if (zebuElement.zbOldSetSize == null)
		zebuElement.zbOldSetSize = zebuElement.zbSetSize;
	zebuElement.zbSetSize    = ZebuBorderElement_setSize;
	zebuElement.zbSetContent = ZebuBorderElement_setContent;
	zebuElement.zbSetBorder  = ZebuBorderElement_setBorder;
	
    zebuElement.zbSetBorder(borderType);
}	


function ZebuBorderElement_setSize(width, height){
	this.zbOldSetSize(width, height);

	this.leftBorder.zbSetSize(this.borderWidth, height);
	this.leftBorder.zbSetLocation(0, 0);
	
	this.topBorder.zbSetSize(width, this.borderWidth);
	this.topBorder.zbSetLocation(0, 0);
	
	this.rightBorder.zbSetSize(this.borderWidth, height);
	this.rightBorder.zbSetLocation(width-this.borderWidth, 0);
	
	this.bottomBorder.zbSetSize(width, this.borderWidth);
	this.bottomBorder.zbSetLocation(0, height-this.borderWidth);

	this.client.zbSetSize(width-this.borderWidth*2, height-this.borderWidth*2);
	this.client.zbSetLocation(this.borderWidth, this.borderWidth);
}

function ZebuBorderElement_setContent(content){
	this.client.zbSetContent(content);
}

function ZebuBorderElement_setBorder(borderType){
	this.borderType = borderType;
	
	if (this.borderType == BORDER_PLAIN){
		var bgColor = this.zbGetBackgroundColor();
		this.leftBorder.zbSetBackgroundColor(bgColor);
		this.topBorder.zbSetBackgroundColor(bgColor);
		this.rightBorder.zbSetBackgroundColor(bgColor);
		this.bottomBorder.zbSetBackgroundColor(bgColor);
	}
	else if (this.borderType == BORDER_FRAME){
		this.leftBorder.zbSetBackgroundColor("#A9A9A9");
		this.topBorder.zbSetBackgroundColor("#A9A9A9");
		this.rightBorder.zbSetBackgroundColor("#A9A9A9");
		this.bottomBorder.zbSetBackgroundColor("#A9A9A9");
	}
	else if (this.borderType == BORDER_UP){
		this.leftBorder.zbSetBackgroundColor("#D3D3D3");
		this.topBorder.zbSetBackgroundColor("#D3D3D3");
		this.rightBorder.zbSetBackgroundColor("#696969");
		this.bottomBorder.zbSetBackgroundColor("#696969");
	}
	else if (this.borderType == BORDER_DOWN){
		this.leftBorder.zbSetBackgroundColor("#696969");
		this.topBorder.zbSetBackgroundColor("#696969");
		this.rightBorder.zbSetBackgroundColor("#D3D3D3");
		this.bottomBorder.zbSetBackgroundColor("#D3D3D3");
	}
	else if (this.borderType == BORDER_TAB){
		var bgColor = this.zbGetBackgroundColor();
		this.leftBorder.zbSetBackgroundColor("#D3D3D3");
		this.topBorder.zbSetBackgroundColor("#D3D3D3");
		this.rightBorder.zbSetBackgroundColor("#696969");
		this.bottomBorder.zbSetBackgroundColor(bgColor);
	}
}

