////////////////////////
// Created 9-12-2002
// by Colm
////////////////////////

DoorObject.prototype   = new ObjectGroup();
DoorObject.constructor = DoorObject;
DoorObject.superclass  = ObjectGroup.prototype;


function DoorObject(a, b, lwidth, rad, sw, la, ar, draw ){
	if(arguments.length > 0)
		this.init(a, b, lwidth, rad, sw, la, ar, draw );
}

DoorObject.prototype.init = function( a, b, lwidth, rad, sw, la, ar, draw ) {
  DoorObject.superclass.init.call( this );
	this.wallWidth = lwidth;
	
	this.cntrpt = new Point2D(a.x, a.y);
 	this.pt1 = new Point2D(a.x , a.y - (this.wallWidth / 2));
	this.pt2 = new Point2D(b.x, b.y);
	
	this.radius = rad;
	this.sweep = sw;
	this.large_arc = la;
	this.axis_rot = ar;
	
	this.wallAngle = 0;
	this.orient_flag = 90;
	
	this.lateral_flag  = 1;
	
	if ( draw ) {
    this.make();
	}
}

DoorObject.prototype.make = function() {
	this.doorRect = new Rectangle(this.pt1.x, this.pt1.y, this.pt2.x, this.pt1.y, 
																this.pt2.x, this.pt2.y, this.pt2.y, this.pt1.x, true);	
	this.doorRect.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");

	this.doorLine = new Line(this.cntrpt.x,this.cntrpt.y, this.cntrpt.x, this.cntrpt.y, "off", true);
	this.doorLine.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");

	this.doorArc = new simpleArc(this.cntrpt.x,  this.cntrpt.y, this.cntrpt.x, this.cntrpt.y,
															 this.radius, this.sweep, this.large_arc, this.axis_rot, true);
	this.doorArc.svgNode.setAttribute("style", "fill:none ; stroke-width:1; stroke: green");
	
	this.addObject(this.doorRect);
	this.addObject(this.doorLine);
	this.addObject(this.doorArc);
}

DoorObject.prototype.constructArc = function(){
	this.radius = Math.sqrt((this.cntrpt.x - this.pt2.x) * (this.cntrpt.x - this.pt2.x));

	var doorArc = "M"	+ this.doorRect.handles[2].point.x + ", "
								  + this.doorRect.handles[2].point.y + "A"+ this.radius + ", " + this.radius 
									+ " " + this.axis_rot + " " + this.large_arc + ", " + this.sweep + " "  
									+ this.doorLine.handles[1].point.x + " " + this.doorLine.handles[1].point.y; 
  return doorArc;
}

DoorObject.prototype.update = function(x, y, maskAngle) {
	if (arguments.length > 0){
		var dist = 0;
		this.wallAngle = maskAngle;
		if (maskAngle == 0){
			this.pt2.x = x;
			this.pt2.y = y;
		}
		else {
			dist = Math.sqrt(((this.pt1.x - x) * (this.pt1.x - x)) + ((this.pt1.y - y) * (this.pt1.y - y))); 		
			if (x > this.pt1.x)
				this.pt2.x = this.pt1.x + dist;
			else if (x < this.pt1.x)
				this.pt2.x = this.pt1.x - dist;
			else {
				if(maskAngle < 0){
					if (y >= this.pt1.y)
						this.pt2.x = this.pt1.x - dist;	
					else if (y < this.pt1.y)
						this.pt2.x = this.pt1.x + dist;				
				}
				else if(maskAngle >= 0){
					if (y >= this.pt1.y)
						this.pt2.x = this.pt1.x + dist;
					else if (y < this.pt1.y)
						this.pt2.x = this.pt1.x - dist;
				}
			}			
			this.pt2.y = this.pt1.y;			
		}
	}
	var sw = 0;
	this.doorRect.update(this.pt1.x, this.pt1.y, this.pt2.x, (this.pt1.y + this.wallWidth) );
	this.doorRect.rotatePlease(this.cntrpt, this.wallAngle);

	if (this.lateral_flag == 1){
		this.doorLine.update(this.cntrpt.x, this.cntrpt.y, this.pt2.x, this.cntrpt.y);       
		this.doorLine.rotatePlease(this.cntrpt, this.wallAngle +  this.orient_flag);
		this.radius = Math.sqrt((this.cntrpt.x - this.pt2.x) * (this.cntrpt.x - this.pt2.x));
		this.doorArc.update(this.doorRect.handles[2].point.x, this.doorRect.handles[2].point.y, 
											this.doorLine.handles[1].point.x, this.doorLine.handles[1].point.y, 
											this.radius, this.sweep);
	}
	else if (this.lateral_flag == 0){
		if (this.sweep == 1) { sw = 0; }
		else { sw = 1; } 
		this.doorLine.update(this.doorRect.handles[0].point.x, this.doorRect.handles[0].point.y, 
												 this.doorRect.handles[1].point.x, this.doorRect.handles[1].point.y);       
		this.doorLine.rotatePlease(this.doorRect.handles[1].point, -this.orient_flag);
		this.radius = Math.sqrt((this.cntrpt.x - this.pt2.x) * (this.cntrpt.x - this.pt2.x));
		this.doorArc.update(this.doorRect.handles[0].point.x, this.doorRect.handles[0].point.y, 
											this.doorLine.handles[0].point.x, this.doorLine.handles[0].point.y, 
											this.radius, sw);	
	}
}

DoorObject.prototype.setOrient = function(sweep, o_flag) {
	this.sweep = sweep;
	this.orient_flag = o_flag; 
	this.update();
}

DoorObject.prototype.setLateralOrient = function (l_flag) {
	this.lateral_flag = l_flag;
	this.update();
}

DoorObject.prototype.placeDoor = function() {
	this.doorRect.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.doorLine.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.doorArc.svgNode.setAttribute("style", "fill:none ; stroke-width:1; stroke: black");
}

DoorObject.prototype.destructor = function() {
	DoorObject.superclass.destructor.call(this);
}
