/********************************
  File: WindowObject.js
  Created by Colm
  Create Date: 18.9.2002

A window consists of 4 rectangles and
two lines. The lines represent the small
overrun you can see on windows in workPlus.

-----------------------------------
**********************************/



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

function WindowObject(a, b, lwidth, draw ){
	if(arguments.length > 0)
		this.init(a, b, lwidth, draw );
}

WindowObject.prototype.init = function( a, b, lwidth, draw ) {
  WindowObject.superclass.init.call( this );
	this.cntrpt = new Point2D(a.x, a.y);
	this.wallWidth = lwidth;
 	this.pt1 = new Point2D(a.x , a.y - (this.wallWidth / 2));
	this.pt2 = new Point2D(b.x, b.y);
	this.pt3 = new Point2D(0,0);	
	this.wallAngle = 0;
	this.orient_flag = 0;
	this.stub = 0;

	if ( draw ) {
    this.make();
	}
}

WindowObject.prototype.make = function() {

	this.winRect1 = 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.winRect1.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");
	
	this.winRect2 = 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.winRect2.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");
	
	this.winRect3 = 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.winRect3.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");

	this.winRect4 = 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.winRect4.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: green");

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

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


	this.addObject(this.winLine1);
	this.addObject(this.winLine2);

	this.addObject(this.winRect1);
	this.addObject(this.winRect2);
	this.addObject(this.winRect3);
	this.addObject(this.winRect4);


}


WindowObject.prototype.update = function(x, y, maskAngle) {
	if (arguments.length > 0){
		var dist = 0;
		this.wallAngle = maskAngle;
		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 / 2;
			this.pt3.x = this.pt1.x + dist;
			this.stub = dist / 20;
		}
		else if (x < this.pt1.x){
			this.pt2.x = this.pt1.x - dist / 2;
			this.pt3.x = this.pt1.x - dist;
			this.stub = - (dist / 20);
		}
		else {
			if(maskAngle <= 0){
				if (y >= this.pt1.y){
					this.pt2.x = this.pt1.x - dist / 2;	
					this.pt3.x = this.pt1.x - dist;
					this.stub = -(dist / 20);
				}
				else if (y < this.pt1.y){
					this.pt2.x = this.pt1.x + dist / 2;	
					this.pt3.x = this.pt1.x + dist;
					this.stub = (dist / 20);
				}			
			}
			else if(maskAngle > 0){
				if (y >= this.pt1.y){
					this.pt2.x = this.pt1.x + dist / 2;
					this.pt3.x = this.pt1.x + dist;
					this.stub = (dist / 20);

				}
				else if (y < this.pt1.y){
					this.pt2.x = this.pt1.x - dist / 2;
					this.pt3.x = this.pt1.x - dist;
					this.stub = -(dist / 20);
				}
			}
		}			
		this.pt2.y = this.pt1.y;
		this.pt3.y = this.pt2.y;			
	}
	if (this.orient_flag == 0){
		var offset1 = this.wallWidth;
		var offset2 = ((this.wallWidth/3)*2);
		var offset3 = this.wallWidth / 3;
	}
	else {
		var offset1 = this.wallWidth;
		var offset2 = this.wallWidth/3;
		var offset3 = ((this.wallWidth / 3)*2);
	}		
	this.winRect1.update(this.pt1.x, this.pt1.y, this.pt2.x, this.pt2.y + offset3 );
	this.winRect1.rotatePlease(this.cntrpt, this.wallAngle);
	this.winRect2.update(this.pt2.x, this.pt2.y + offset2, this.pt3.x, this.pt3.y + offset1 );
	this.winRect2.rotatePlease(this.cntrpt, this.wallAngle);
	this.winRect3.update(this.pt1.x, this.pt1.y + offset3 , this.pt2.x, this.pt2.y + offset1 );
	this.winRect3.rotatePlease(this.cntrpt, this.wallAngle);
	this.winRect4.update(this.pt2.x, this.pt2.y , this.pt3.x, this.pt3.y + offset2);
	this.winRect4.rotatePlease(this.cntrpt, this.wallAngle);

	if (this.stub > 7) this.stub = 7;
	else if (this.stub < -7) this.stub = -7;
	this.winLine1.update(this.pt2.x, this.pt2.y + offset3, this.pt2.x + this.stub, this.pt2.y + offset3);
	this.winLine1.rotatePlease(this.cntrpt, this.wallAngle); 
	this.winLine2.update(this.pt2.x, this.pt2.y + offset2, this.pt2.x - this.stub, this.pt2.y + offset2);
	this.winLine2.rotatePlease(this.cntrpt, this.wallAngle); 
}

WindowObject.prototype.setOrient = function(o_flag) {
	this.orient_flag = o_flag; 
	this.update();
}

WindowObject.prototype.placeWindow = function() {
	this.winRect1.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.winRect2.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.winRect3.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.winRect4.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.winLine1.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
	this.winLine2.svgNode.setAttribute("style", "fill:white ; stroke-width:1; stroke: black");
}


WindowObject.prototype.destructor = function() {
  // this.winRect.destructor();
  WindowObject.superclass.destructor.call(this);
}
