/*
 * Used to create a Rect object, Rect being short for rectangle,
 * This function require the canvas it should be draw on and 
 * an optional rect object with the fields x, y, width, height and rgb
 * if such and object is given its fields values will be copied to the 
 * created object fields values. 
 * the c
 */
function Rect(rect){
	return new RectClass(rect);	
}
function RectClass(rect){
	DomNodeInterface(this);
	if (rect) {
		this.x = rect.x;
		this.y = rect.y;
		this.width = rect.width;
		this.height = rect.height;
		this.color=rect.color;			
		this.setStyle(rect.style);	
	}else {
		this.x = 0;
		this.y = 0;
		this.width = 0;
		this.height = 0;
		this.color=ColorObject(0,0,0,0.5);
		this.setStyle("fill");
	}	
}

RectClass.prototype.fill = function(){
	var dims=this.getRect();
	this.canvas.fillStyle(this.color.getRGBAString());				
	this.canvas.fillRect(dims.x,dims.y,dims.w,dims.h);			
};

RectClass.prototype.stroke = function(){
	var dims=this.getRect();
	this.canvas.strokeStyle(this.color.getRGBAString());
	this.canvas.strokeRect(dims.x,dims.y,dims.w,dims.h);			
};

RectClass.prototype.clear = function(){
	var dims=this.getRect();
	this.canvas.clearRect(dims.x,dims.y,dims.w,dims.h);			
};

RectClass.prototype.clip = function(){
	var dims=this.getRect();		
	
	this.canvas.clipRect(dims.x,dims.y,dims.w,dims.h);			
};
/*!
 * Sets the style and the draw function.
 */
RectClass.prototype.setStyle = function(style){
	switch (style) {
		case 'stroke':
			this.draw=this.stroke;
		break;
		case 'clear':
			this.draw=this.clear;			
		break;
		case 'clip':
			this.draw=this.clip;										
		break;
		default:
			this.draw=this.fill;		
			style="fill";
		break;
	}
	this._style=style;
};

if (typeof(JCanvasDSLClass)=="function") {
	//FILL_OR_STROKE=stroke
	//              |fill
	//<rectangle x="INT" y="INT" width="INT" height="INT" color="COLOR" style="FILL_OR_STROKE"/>
	JCanvasDSLClass.prototype.rootNode.rectangle = function(self, node, parent){
		var ele = Rect();
		parent.appendChild(ele);
		self.mapAttributes(node, ele, 
				[
				 ['style','setStyle',self.makeParseLegalStrings(['fill','stroke'],'fill')]
				 ,['x', 'x', parseInt], ['y', 'y', parseInt]
				 ,['color', 'color', self.colorSolverRGBA] 
				 ,['width','width',function(w){return ele.setWidthByString(w);}]
				 ,['height','height',function(h){return ele.setHeightByString(h);}]
				   ]);
	};
}




