function Point (_x, _y) {
	this.x = _x;
	this.y = _y;
//--------------------------------------------------------------
	this.compare = function (_point) {
		if (this.x==_point.x && this.y==_point.y) {
			return true;
		} else {
			return false;
		}
	}
}

function Line (_point1, _point2) {
	this.points = new Array();
	this.data = new Array();
	this.points.push(_point1);
	this.points.push(_point2);
	if (Math.abs(_point2.x-_point1.x) > Math.abs(_point2.y-_point1.y)) {
		var accu = _point1.y;
		if (_point2.x > _point1.x) {
			for (var x = _point1.x; x < _point2.x+1; x++) {
				y = Math.floor(accu);
				accu += (_point2.y - _point1.y) / (_point2.x - _point1.x);
				this.data.push(new Point(x,y));
			}
		} else if (_point1.x > _point2.x) {
			for (var x = _point1.x; x > _point2.x-1; x--) {
				y = Math.floor(accu);
				accu -= (_point2.y - _point1.y) / (_point2.x - _point1.x);
				this.data.push(new Point(x,y));
			}
		} else {
			if (_point2.y > _point1.y) {
				for (var y=_point1.y; y<_point2.y+1; y++) {
					this.data.push(new Point(_point1.x,y))
				}
			} else {
				for (var y=_point1.y; y>_point2.y-1; y--) {
					this.data.push(new Point(_point1.x,y))
				}
			}
		}
	} else {
		var accu = _point1.x;
		if (_point2.y > _point1.y) {
			for (var y = _point1.y; y < _point2.y+1; y++) {
				x = Math.floor(accu);
				accu += (_point2.x - _point1.x) / (_point2.y - _point1.y);
				this.data.push(new Point(x,y));
			}
		} else if (_point1.y > _point2.y) {
			for (var y = _point1.y; y > _point2.y-1; y--) {
				x = Math.floor(accu);
				accu -= (_point2.x - _point1.x) / (_point2.y - _point1.y);
				this.data.push(new Point(x,y));
			}
		} else {
			if (_point2.x > _point1.x) {
				for (var x=_point1.x; x<_point2.x+1; x++) {
					this.data.push(new Point(x,_point1.y))
				}
			} else {
				for (var x=_point1.x; x>_point2.x-1; x--) {
					this.data.push(new Point(x,_point1.y))
				}
			}
		}
	}
//--------------------------------------------------------------
	this.intersection = function (_line) {
		var ret = false;
		for (var i=0; i=this.points.length; i++) {
			for (var j=0; j=_line.points.length; j++) {
				if (this.points[i].compare(_line.points[j])) {
					ret = true;
				}
			}
		}
		return ret;
	}
//--------------------------------------------------------------
	this.testPointV = function (_point) {
		var ret = false;
		for (var i=0; i<this.data.length; i++) {
			if (this.data[i].x == _point.x) {
				ret = this.data[i];
			}
		}
		return ret;
	}
//--------------------------------------------------------------
	this.testPointH = function (_point) {
		var ret = false;
		for (var i=0; i<this.data.length; i++) {
			if (this.data[i].y == _point.y) {
				ret = this.data[i];
			}
		}
		return ret;
	}
	this.draw = function (_color) {
		for (var i=0; i<this.data.length; i++) {
			drawPixel(this.data[i], _color);
		}
	}
}

function Polygon () {
	this.points = new Array();
	this.lines = new Array();
	this.done = false;
//--------------------------------------------------------------
	this.addPoint = function (_point) {
		this.points.push(_point);
		if (this.points.length >2) {
			this._create();
		}
	}
//--------------------------------------------------------------
	this._create = function () {
		this.lines = new Array();
		for (i=0; i<this.points.length; i++) {
			if (i+1 == this.points.length) {
				this.lines.push(new Line(this.points[i], this.points[0]));
			} else {
				this.lines.push(new Line(this.points[i], this.points[i+1]));
			}
		}
		this.done = true;
	}
//--------------------------------------------------------------
	this.testPoint = function (_point) {
		var pointv = new Array();
		var pointh = new Array();
		for (var i=0; i<this.lines.length; i++) {
			var tmp = this.lines[i].testPointV(_point);
			if (tmp != false) {
				var test = true;
				for (var j=0; j<pointv.length; j++) {
					if (pointv[j].compare(tmp)) {
						test = false;
					}
				}
				if (test) pointv.push(tmp);
			}
			tmp = this.lines[i].testPointH(_point);
			if (tmp != false) {
				test = true;
				for (var j=0; j<pointh.length; j++) {
					if (pointh[j].compare(tmp)) {
						test = false;
					}
				}
				if (test) pointh.push(tmp);
			}
		}
		if (pointv.length < 2) {
			return false;
		}
		if (pointh.length < 2) {
			return false;
		}
		var retv = false;
		var reth = false;
		var tmp1 = pointv[0].y;
		if (pointv.length > 2) {
			var tmp2 = pointv[2].y;
		} else {
			var tmp2 = pointv[1].y;
		}
		if (tmp1 > tmp2) {
			if (_point.y < tmp1 && _point.y > tmp2) {
				retv = true;
			}
		} else {
			if (_point.y > tmp1 && _point.y < tmp2) {
				retv = true;
			}
		}
		tmp1 = pointh[0].x;
		if (pointh.length > 2) {
			tmp2 = pointh[2].x;
		} else {
			tmp2 = pointh[1].x;
		}
		if (tmp1 > tmp2) {
			if (_point.x < tmp1 && _point.x > tmp2) {
				reth = true;
			}
		} else {
			if (_point.x > tmp1 && _point.x < tmp2) {
				reth = true;
			}
		}
		return (retv && reth);
	}
//--------------------------------------------------------------
	this.move = function (_point) {
		this.done = false;
		for (var i=0; i<this.points.length; i++) {
			this.points[i].x += _point.x;
			this.points[i].y += _point.y;
		}
		this._create();
	}
	this.draw = function (_color) {
		for (var i=0; i<this.lines.length; i++) {
			this.lines[i].draw(_color);
		}
	}
}

function drawPixel(_point, _color) {
	var obj = document.createElement("div");
	obj.style.position = "absolute";
	obj.style.clip = "rect(0,1,1,0)";
	obj.style.background = _color;
	obj.innerHTML = "<span style=\"line-height:1px;font-size:1px\">&nbsp;</span>";
	obj.width = 1;
	obj.height = 1;
	obj.style.left = _point.x+"px";
	obj.style.top = _point.y+"px";
	obj.style.zIndex = 50;
	obj.style.cursor = "default";
	obj.style.margin = "0";
	obj.style.padding = "0";
	document.body.appendChild(obj);
}

function realPosition(_obj) {
	var currPos = new Point(_obj.offsetLeft,_obj.offsetTop);
	var workPos = new Point(0,0);
	if (_obj.offsetParent.tagName != "BODY") {
		workPos = realPosition(_obj.offsetParent);
		currPos.x += workPos.x;
		currPos.y += workPos.y;
	}
	return currPos;
}

