var FloorplanImage = function(image) {
	this.image = image;
	var position = image.position();

	this.get_coo = function() {
		return [position.left, position.top];
	}
};
var Shape = function( id, coo_str, action, data, colors, name_prefix ) {
	this.id = id;
	this.coo_str = coo_str;
	this.coo = $.evalJSON(coo_str);
	$.each(this.coo, function() {
		var xy = relative2absolute(this);
		this[0] = xy[0];
		this[1] = xy[1];
	});
	this.start_coo = clone(this.coo); // save start coordinates
	this.start_move_coo = null;
	
	var _data = data;
	var _name = name_prefix + this.id;
	var _colors = colors;
	var _action = action;
	this._is_highlight = false;
	this.alwaysVisible = false;
	this.divs = null;

	this._drawCommon = function() {
		this.divs = $("#" + _data.floorplan_image.image.parent().attr("id") + " > div > :not(div[name])");
		this.divs.attr("name", _name);
		this.divs.click(_action);
	};

	this.drawAsBox = function(shape_type) {
		if (this.coo.length < 2) return;
		
		this.box_shape_type = shape_type;

		var topLeftCoo = [ 
			Math.min(this.coo[0][0], this.coo[1][0]), 
			Math.min(this.coo[0][1], this.coo[1][1])
		];
		var w = Math.abs(this.coo[0][0] - this.coo[1][0]);
		var h = Math.abs(this.coo[0][1] - this.coo[1][1]);
		var old_color = _data.jg.color;
		_data.jg.setColor(_colors.def);
		if (1 == shape_type) {
			_data.jg.fillRect(topLeftCoo[0], topLeftCoo[1], w, h);
		}
		else if (2 == shape_type) {
			var r = Math.sqrt(w*w + h*h);
			_data.jg.fillEllipse(this.coo[0][0] - r, this.coo[0][1] - r, r*2, r*2)
		}
		_data.jg.paint();
		_data.jg.setColor(old_color);
		this._drawCommon();
		this.divs.hover(
			function(box) {
				return function() { //box.divs.css("background-color", _colors.hover);  
				box.show(); }
			}(this),
			function(box) {
				return function() {
					if (box._is_highlight) {
						box.divs.css("background-color", _colors.highlight);
					} else {
						box.divs.css("background-color", _colors.def);
					};
					box.hide();
				}
			}(this)
		);
		this.show();
		// this.divs.css("border", "1px solid " + _colors.border);
	};

	this.drawAsArea = function() {
		if (this.coo.length < 3) return;

		var xx = new Array();
		var yy = new Array();
		for (i in this.coo) {
			xx.push(this.coo[i][0]);
			yy.push(this.coo[i][1]);
		}
		var old_color = _data.jg.color;
		_data.jg.setColor(_colors.def);
		_data.jg.fillPolygon(xx, yy);
		//_data.jg.setColor(_colors.border);
		//_data.jg.drawPolygon(xx, yy);
		_data.jg.paint();
		_data.jg.setColor(old_color);
		this._drawCommon();
		this.divs.hover(
			function(area) {
				return function() { area.show(); }
			}(this),
			function(area) {
				return function() { area.hide(); }
			}(this)
		);
		this.show();
	};
	
	this.setHighlight = function(is_highlight) {	
		this._is_highlight = is_highlight;
		if (this._is_highlight) {
			this.divs.css("background-color", _colors.highlight);
		} else {
			this.divs.css("background-color", _colors.def);
		};
	};
	
	this.show = function() {	
		this.divs.css('opacity', '.6');
		if($.browser.msie){
			this.divs.css('filter', 'progid:DXImageTransform.Microsoft.Alpha(opacity=60)');
		};
	};
	
	this.hide = function() {
		if (!this.alwaysVisible) {
			this.divs.css('opacity', '0');
			if($.browser.msie){
				this.divs.css('filter', 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)');
			};
		}; 
	};
	
	this.width_koef = 1;
	this.height_koef = 1;
	this.left = 0;
	this.top = 0;
	
	this.repaint_box = function() {
		this.coo[0][0] = this.start_coo[0][0] * this.width_koef + this.left;
		this.coo[1][0] = this.start_coo[1][0] * this.width_koef + this.left;
		this.coo[0][1] = this.start_coo[0][1] * this.height_koef + this.top;
		this.coo[1][1] = this.start_coo[1][1] * this.height_koef + this.top;
		this.drawAsBox(this.box_shape_type);
		this.setHighlight(this._is_highlight);
	};
	
	this.repaint_area = function() {
		for (i in this.coo) {
			this.coo[i][0] = this.start_coo[i][0] * this.width_koef + this.left;
			this.coo[i][1] = this.start_coo[i][1] * this.height_koef + this.top;
		}
		this.drawAsArea();
		this.setHighlight(this._is_highlight);
	};
	
	this.zoom_as_box = function(width_koef, height_koef, img_left, img_top) {
		if (this.width_koef < 1) {
			this.width_koef = 1;	
		};
		if (this.height_koef < 1) {
			this.height_koef = 1;
		};
		this.width_koef = width_koef;
		this.height_koef = height_koef;
		if (!img_left) {
			img_left = 0;
		};
		if (!img_top) {
			img_top = 0;
		};
		this.left = img_left;
		this.top = img_top;
		this.repaint_box();
	};
	
	this.zoom_as_area = function(width_koef, height_koef, img_left, img_top) {
		if (this.width_koef < 1) {
			this.width_koef = 1;	
		};
		if (this.height_koef < 1) {
			this.height_koef = 1;
		};
		this.width_koef = width_koef;
		this.height_koef = height_koef;
		if (!img_left) {
			img_left = 0;
		};
		if (!img_top) {
			img_top = 0;
		};
		this.left = img_left;
		this.top = img_top;
		this.repaint_area();
	};
	
	this.start_move = function() {
		if (this.start_move_coo == null) {
			this.start_move_coo = clone(this.coo);
		};
	};
	
	this.finish_move_as_box = function(left, top) {
		this.left = left;
		this.top = top;
		this.repaint_box();
	};
	
	this.finish_move_as_area = function(left, top) {
		this.left = left;
		this.top = top;
		this.repaint_area();
	};
};

function absolute2relative( xy ) {
	return [
		xy[0] - data__g.floorplan_image.get_coo()[0],
		xy[1] - data__g.floorplan_image.get_coo()[1]
	];
}
function relative2absolute( xy ) {
	return [
		xy[0] + data__g.floorplan_image.get_coo()[0],
		xy[1] + data__g.floorplan_image.get_coo()[1]
	];
}
