/* ScaleBox Begin */
var scalingobj = null;
//var borderobj = null;
var markerobj = null;

var ScalePosition = {
	LEFTTOP: 1,
	MIDDLETOP: 2,
	RIGHTTOP: 3,
	RIGHTMIDDLE: 4,
	RIGHTBOTTOM: 5,
	MIDDLEBOTTOM: 6,
	LEFTBOTTOM: 7,
	LEFTMIDDLE: 8
}

function CreateMarkerBox(parent) {
	if (markerobj != null) {
		RemoveMarkerBox();
	}
	markerobj = document.createElement("div");
	var bordersize = 3;
	markerobj.id = "markerobj";
	with (markerobj.style) {
		position = "absolute";
		border = "1px dashed #FFF";
		left = (Pos.regionLeft(parent.control) - bordersize) + noPx;
		top = (Pos.regionTop(parent.control) - bordersize) + noPx;
		width = (parseInt(parent.control.style.width) + (bordersize * 2) - 2) + noPx;
		height = (parseInt(parent.control.style.height) + (bordersize * 2) - 2) + noPx;
		zIndex = 0;
		filter = "alpha(opacity=30)";
		mozOpacity = ".30";
		opacity = ".30";
	}
	document.body.appendChild(markerobj);
}

function CreateScaleBox(parent, minwidth, minheight, containerPos, customZIndex, options, disabledBoxes) {
	var dBoxes = Object.extend({
		leftTop: false,
		middleTop: false,
		rightTop: false,
		rightMiddle: false,
		rightBottom: false,
		middleBottom: false,
		leftBottom: false,
		leftMiddle: false
	}, disabledBoxes);

	options = Object.extend({
		keepAspectRatio: false
	}, options);

	var borderobj = document.createElement("div");
	borderobj.id = "borderobj";
	var bordersize = 3;
	var sub = Prototype.Browser.IE && options && options.subXPos > 0 ? options.subXPos : 0;
	with (borderobj.style) {
		position = "absolute";
		border = "1px dashed #FFF";
		left = (Pos.regionLeft(parent.control) - bordersize - sub) + noPx;
		top = (Pos.regionTop(parent.control) - bordersize) + noPx;
		width = (parseInt(parent.control.style.width) + (bordersize * 2) - 2) + noPx;
		height = (parseInt(parent.control.style.height) + (bordersize * 2) - 2) + noPx;
		zIndex = (customZIndex != null && typeof customZIndex != 'undefined') ? customZIndex : 8;
	}
	document.body.appendChild(borderobj);

	if (!dBoxes.leftTop) {
		new ScaleBox(borderobj, parent, ScalePosition.LEFTTOP, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.middleTop) {
		new ScaleBox(borderobj, parent, ScalePosition.MIDDLETOP, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.rightTop) {
		new ScaleBox(borderobj, parent, ScalePosition.RIGHTTOP, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.rightMiddle) {
		new ScaleBox(borderobj, parent, ScalePosition.RIGHTMIDDLE, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.rightBottom) {
		new ScaleBox(borderobj, parent, ScalePosition.RIGHTBOTTOM, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.middleBottom) {
		new ScaleBox(borderobj, parent, ScalePosition.MIDDLEBOTTOM, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.leftBottom) {
		new ScaleBox(borderobj, parent, ScalePosition.LEFTBOTTOM, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
	if (!dBoxes.leftMiddle) {
		new ScaleBox(borderobj, parent, ScalePosition.LEFTMIDDLE, minwidth, minheight, options.keepAspectRatio, containerPos, options);
	}
}

function ScaleBox(border, parent, position, minwidth, minheight, keepAspectRatio, containerPos, options) {
	this.options = Object.extend({
		snap: 1
	}, options);

	this.corner = document.createElement("div");
	this.corner.className = 'scalebox';
	this.position = position;
	this.parent = parent;
	this.isscaling = false;
	this.border = border;
	this.minheight = minheight;
	this.minwidth = minwidth;
	this.id = "ScaleBox_" + this.parent.id + "_" + position;
	this.corner.id = this.id;
	this.orgLeft = Pos.regionLeft(parent.control);
	this.orgTop = Pos.regionTop(parent.control);
	this.orgRight = Pos.regionRight(parent.control);
	this.orgBottom = Pos.regionBottom(parent.control);
	this.orgWidth = this.orgRight - this.orgLeft;
	this.orgHeight = this.orgBottom - this.orgTop;
	this.keepAspectRatio = keepAspectRatio;
	this.containerPos = Object.extend({
		left: 0,
		top: 0
	}, containerPos); //Default is body
	
	controls[controls.length] = this;
	with (this.corner.style) {
		position = 'absolute';
		background = '#FFF';
		border = '1px solid #555';
		width = '4px';
		height = '4px';
		zIndex = 9;
	}

	// IE 6 Hack
	if (window.external && (typeof window.XMLHttpRequest == "undefined")) {
		this.corner.innerHTML = '<img src="http://artogfx.cloud2.artodata.com/sitegfx/grafik/spacer.gif" width="3" height="3" />';
	}

	var bordersize = 2;
	if (position == ScalePosition.LEFTTOP) {
		this.corner.style.left = Pos.regionLeft(this.border) - bordersize + noPx;
		this.corner.style.top = Pos.regionTop(this.border) - bordersize + noPx;
		this.cursor = "nw-resize";
	}
	if (position == ScalePosition.MIDDLETOP) {
		this.corner.style.left = (Pos.regionMiddleX(this.border) - bordersize) + noPx;
		this.corner.style.top = Pos.regionTop(this.border) - bordersize + noPx;
		this.cursor = "n-resize";
	}
	if (position == ScalePosition.RIGHTTOP) {
		this.corner.style.left = (Pos.regionRight(this.border) - bordersize) + noPx;
		this.corner.style.top = Pos.regionTop(this.border) - bordersize + noPx;
		this.cursor = "ne-resize";
	}
	if (position == ScalePosition.RIGHTMIDDLE) {
		this.corner.style.left = (Pos.regionRight(this.border) - bordersize) + noPx;
		this.corner.style.top = (Pos.regionMiddleY(this.border) - bordersize) + noPx;
		this.cursor = "e-resize";
	}
	if (position == ScalePosition.RIGHTBOTTOM) {
		this.corner.style.left = (Pos.regionRight(this.border) - bordersize) + noPx;
		this.corner.style.top = (Pos.regionBottom(this.border) - bordersize) + noPx;
		this.cursor = "se-resize";
	}
	if (position == ScalePosition.MIDDLEBOTTOM) {
		this.corner.style.left = (Pos.regionMiddleX(this.border) - bordersize) + noPx;
		this.corner.style.top = (Pos.regionBottom(this.border) - bordersize) + noPx;
		this.cursor = "s-resize";
	}
	if (position == ScalePosition.LEFTBOTTOM) {
		this.corner.style.left = Pos.regionLeft(this.border) - bordersize + noPx;
		this.corner.style.top = (Pos.regionBottom(this.border) - bordersize) + noPx;
		this.cursor = "sw-resize";
	}
	if (position == ScalePosition.LEFTMIDDLE) {
		this.corner.style.left = Pos.regionLeft(this.border) - bordersize + noPx;
		this.corner.style.top = (Pos.regionMiddleY(this.border) - bordersize) + noPx;
		this.cursor = "w-resize";
	}
	this.corner.style.cursor = this.cursor;
	document.body.appendChild(this.corner);
}

ScaleBox.prototype = {
	scaleRatioTopLeft: function (mouseX, mouseY) {
		this.scaleLeft(mouseX, mouseY);
		var scaleratio = this.parent.control.getWidth() / this.orgWidth;
		this.parent.control.style.height = (this.orgHeight * scaleratio) + noPx;
		this.parent.control.style.top = (this.orgBottom - this.parent.control.getHeight()) + noPx;
	},

	scaleRatioTopRight: function (mouseX, mouseY) {
		this.scaleRight(mouseX, mouseY);
		var scaleratio = this.parent.control.getWidth() / this.orgWidth;
		this.parent.control.style.height = (this.orgHeight * scaleratio) + noPx;
		this.parent.control.style.top = (this.orgBottom - this.parent.control.getHeight()) + noPx;
	},

	scaleRatioBottomRight: function (mouseX, mouseY) {
		this.scaleRight(mouseX, mouseY);
		var scaleratio = this.parent.control.getWidth() / this.orgWidth;
		this.parent.control.style.height = (this.orgHeight * scaleratio) + noPx;
	},

	scaleRatioBottomLeft: function (mouseX, mouseY) {
		this.scaleLeft(mouseX, mouseY);
		var scaleratio = this.parent.control.getWidth() / this.orgWidth;
		this.parent.control.style.height = (this.orgHeight * scaleratio) + noPx;
	},

	scaleRatioLeftTop: function (mouseX, mouseY) {
		this.scaleTop(mouseX, mouseY);
		var scaleratio = this.parent.control.getHeight() / this.orgHeight
		this.parent.control.style.width = (this.orgWidth * scaleratio) + noPx;
	},

	scaleRatioRightBottom: function (mouseX, mouseY) {
		this.scaleBottom(mouseX, mouseY);
		var scaleratio = this.parent.control.getHeight() / this.orgHeight;
		this.parent.control.style.width = (this.orgWidth * scaleratio) + noPx;
	},

	scaleLeft: function (mouseX, mouseY) {
		with (this.parent) {
			mouseX = Math.round(mouseX / this.options.snap) * this.options.snap;
			var OWidth = Pos.regionRight(control) - mouseX;
			if (OWidth > 0) {
				control.style.width = OWidth + noPx;
				if (WidthCheck(true) && mouseX < Pos.regionRight(control)) {
					control.style.left = mouseX - this.containerPos.left + noPx;
				}
			}
		}
	},

	scaleTop: function (mouseX, mouseY) {
		with (this.parent) {
			mouseY = Math.round(mouseY / this.options.snap) * this.options.snap;
			var OHeight = (Pos.regionBottom(control) - mouseY);
			if (OHeight > 0) {
				control.style.height = OHeight + noPx;
				if (HeightCheck(true) && mouseY < Pos.regionBottom(control)) {
					control.style.top = mouseY - this.containerPos.top + noPx;
				}
			}
		}
	},

	scaleRight: function (mouseX, mouseY) {
		with (this.parent) {
			mouseX = Math.round(mouseX / this.options.snap) * this.options.snap;
			var OWidth = (mouseX - Pos.regionLeft(control))
			if (OWidth > 0) {
				control.style.width = OWidth + noPx;
				WidthCheck(false);
			}
		}
	},

	scaleBottom: function (mouseX, mouseY) {
		with (this.parent) {
			mouseY = Math.round(mouseY / this.options.snap) * this.options.snap;
			var OHeight = (mouseY - Pos.regionTop(control));
			if (OHeight > 0) {
				control.style.height = OHeight + noPx;
				HeightCheck(false);
			}
		}
	}
}

function ScaleBoxMonitorMouseMove(e) {
	if (scalingobj && scalingobj.isscaling) {
		var mouseX = Event.pointerX(e);
		var mouseY = Event.pointerY(e);
		var keepScaling = true;
		if (typeof scalingobj.parent.scalingValid == 'function') {
			keepScaling = scalingobj.parent.scalingValid(mouseX, mouseY, scalingobj.position);
		}
		if (keepScaling) {
			with (scalingobj.parent) {
				if (scalingobj.position == ScalePosition.LEFTTOP) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioTopLeft(mouseX, mouseY);
					}
					else {
						scalingobj.scaleLeft(mouseX, mouseY);
						scalingobj.scaleTop(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.MIDDLETOP) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioLeftTop(mouseX, mouseY);
					}
					else {
						scalingobj.scaleTop(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.RIGHTTOP) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioTopRight(mouseX, mouseY);
					}
					else {
						scalingobj.scaleRight(mouseX, mouseY);
						scalingobj.scaleTop(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.RIGHTMIDDLE) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioBottomRight(mouseX, mouseY);
					}
					else {
						scalingobj.scaleRight(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.RIGHTBOTTOM) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioBottomRight(mouseX, mouseY);
					}
					else {
						scalingobj.scaleRight(mouseX, mouseY);
						scalingobj.scaleBottom(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.MIDDLEBOTTOM) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioRightBottom(mouseX, mouseY);
					}
					else {
						scalingobj.scaleBottom(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.LEFTBOTTOM) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioBottomLeft(mouseX, mouseY);
					}
					else {
						scalingobj.scaleLeft(mouseX, mouseY);
						scalingobj.scaleBottom(mouseX, mouseY);
					}
				}
				if (scalingobj.position == ScalePosition.LEFTMIDDLE) {
					if (scalingobj.keepAspectRatio == true) {
						scalingobj.scaleRatioBottomLeft(mouseX, mouseY);
					}
					else {
						scalingobj.scaleLeft(mouseX, mouseY);
					}
				}
			}
			scalingobj.parent.renderScaling();
		}
	}
	return true;
}

function WidthCheck(leftToRight) {
	if (scalingobj instanceof ScaleBox) {
		if (parseInt(scalingobj.parent.control.style.width) < scalingobj.minwidth) {
			scalingobj.parent.control.style.width = scalingobj.minwidth + noPx;
			if (leftToRight) {
				scalingobj.parent.control.style.left = (scalingobj.orgRight - scalingobj.minwidth - scalingobj.containerPos.left) + noPx;
			}
			else {
				scalingobj.parent.control.style.left = (scalingobj.orgLeft - scalingobj.containerPos.left) + noPx;
			}
			return false;
		}
	}
	return true;
}

function HeightCheck(topToBottom) {
	if (scalingobj instanceof ScaleBox) {
		if (parseInt(scalingobj.parent.control.style.height) < scalingobj.minheight) {
			scalingobj.parent.control.style.height = scalingobj.minheight + noPx;
			if (topToBottom) {
				scalingobj.parent.control.style.top = (scalingobj.orgBottom - scalingobj.minheight - scalingobj.containerPos.top) + noPx;
			}
			else {
				scalingobj.parent.control.style.top = (scalingobj.orgTop - scalingobj.containerPos.top) + noPx;
			}
			return false;
		}
	}
	return true;
}

function ScaleBoxMonitorMouseDown(e) {
	var srcElement = Event.element(e);
	if (srcElement && srcElement.id) {
		scalingobj = controls.getInstance(srcElement.id);
		if (scalingobj instanceof ScaleBox) {
			document.body.style.cursor = scalingobj.cursor;
			scalingobj.isscaling = true;
			scalingobj.orgLeft = Pos.regionLeft(scalingobj.parent.control);
			scalingobj.orgTop = Pos.regionTop(scalingobj.parent.control);
			scalingobj.orgRight = Pos.regionRight(scalingobj.parent.control);
			scalingobj.orgBottom = Pos.regionBottom(scalingobj.parent.control);
			scalingobj.parent.beginScaling();
		}
	}
	return true;
}

function ScaleBoxMonitorMouseUp(e) {
	if (scalingobj instanceof ScaleBox) {
		scalingobj.isscaling = false;
		scalingobj.parent.endScaling();
		scalingobj = null;
		document.body.style.cursor = "default";
	}
	return true;
}

Event.observe(document, 'mousedown', ScaleBoxMonitorMouseDown.bindAsEventListener(this));
Event.observe(document, 'mousemove', ScaleBoxMonitorMouseMove.bindAsEventListener(this));
Event.observe(document, 'mouseup', ScaleBoxMonitorMouseUp.bindAsEventListener(this));

function RemoveMarkerBox() {
	if (markerobj && markerobj != null) {
		document.body.removeChild(markerobj);
		markerobj = null;
	}
}

function RemoveScaleBoxes() {
	var borderRemoved = false;
//	if (borderobj && borderobj != null) {
//		try {
//			document.body.removeChild(borderobj);
//			borderobj = null;
//		} catch (exception) {
//		}
//	}
	for (i = controls.length - 1; i >= 0; i--) {
		var o = controls[i];
		if (o instanceof ScaleBox) {
			controls.splice(i, 1);
			document.body.removeChild(o.corner);
			
			if (!borderRemoved && o.border && o.border != null) {
				document.body.removeChild(o.border);
				o.border = null;
				borderRemoved = true;
			}
		}
	}
}

/* ScaleBox End */
try {
} catch (e) {}
