/**
 * Modal form scripts
 *
 * @copyright (c) 2004-2006, SWsoft
 */

var SbModalFormDragControlObject;

SbModalFormDragControlObject = new SbModalFormDragControl();

function SbModalFormDragControl() {
	this.entity = null;
	this.isDragging = false;
}

SbModalFormDragControl.prototype.startDragging = function(object) {
	this.entity = object;
	this.isDragging = true;
	addEvent(window, 'blur', SbModalFormDragControl_mouseUp);
	addEvent(document, 'mouseup', SbModalFormDragControl_mouseUp);
	addEvent(document, 'mousemove', SbModalFormDragControl_mouseMove);
}

SbModalFormDragControl.prototype.onMouseUp = function(event) {
	if (this.isDragging && (null != this.entity)) {
		if (document.all) {
			event = window.event;
		}
		removeEvent(window, 'blur', SbModalFormDragControl_mouseUp);
		removeEvent(document, 'mouseup', SbModalFormDragControl_mouseUp);
		removeEvent(document, 'mousemove', SbModalFormDragControl_mouseMove);
		this.entity.onMouseUp(event);
		this.isDragging = false;
		this.entity = null;
	}
}

SbModalFormDragControl.prototype.onMouseMove = function(event) {
	if (this.isDragging && (null != this.entity)) {
		if (document.all) {
			event = window.event;
		}
		this.entity.onMouseMove(event);
	}
}

SbModalFormDragControl_mouseUp = function(event) {
	return SbModalFormDragControlObject.onMouseUp(event);
}

SbModalFormDragControl_mouseMove = function(event) {
	return SbModalFormDragControlObject.onMouseMove(event);
}













function SbInitModalForm(formId) {
	var object;

	if (null == window.sbModalFormsContainer) {
		window.sbModalFormsContainer = new Array();
	}
	object = document.getElementById(formId);
	window.sbModalFormsContainer[window.sbModalFormsContainer.length] = object;
	SbModalForm(object, formId, 200 - 3 * window.sbModalFormsContainer.length);
}





function SbModalForm(formObject, name, defaultZIndex) {
	formObject.identifier = name;
	formObject.initialZIndex = formObject.style.zIndex;
	formObject.horizontalDelta = 0;
	formObject.verticalDelta = 0;
	formObject.positionLeft = 0;
	formObject.positionTop = 0;
	formObject.isMoved = false;
	formObject.disabledItems = new Array();
	formObject.disabledItemsVisibility = new Array();
	formObject.headerObject = document.getElementById(name + 'Header');
	formObject.backgroundObject = document.getElementById(name + 'Background');
	formObject.titleObject = document.getElementById(name + 'Title');

	formObject._prepareSelectboxesBeforeShow = SbModalForm._prepareSelectboxesBeforeShow;
	formObject._prepareSelectboxesBeforeHide = SbModalForm._prepareSelectboxesBeforeHide;
	formObject.setStyles = SbModalForm.setStyles;
	formObject.resetPosition = SbModalForm.resetPosition;
	formObject.hide = SbModalForm.hide;
	formObject.show = SbModalForm.show;
	formObject.onMouseUp = SbModalForm.onMouseUp;
	formObject.onMouseMove = SbModalForm.onMouseMove;
	formObject.setPosition = SbModalForm.setPosition;
	formObject.setTitle = SbModalForm.setTitle;

	formObject.setStyles(defaultZIndex);
	formObject.resetPosition();

	formObject.headerMouseDown = SbModalForm.headerMouseDown;
	formObject.headerObject.onmousedown = function(event) { formObject.headerMouseDown(event); };

	addEvent(window, 'resize', function(event) { formObject.resetPosition(); });
	addEvent(window, 'scroll', function(event) { formObject.resetPosition(); });
}

SbModalForm.setStyles = function(defaultZIndex) {
	this.style.position = 'absolute';
	this.style.overflow = 'hidden';
	if (!this.initialZIndex) {
		this.style.zIndex = defaultZIndex;
		this.backgroundObject.style.zIndex = defaultZIndex - 1;
	} else {
		this.backgroundObject.style.zIndex = this.style.zIndex - 1;
	}
	var modalForm = this;
	if (this.style.display.toLowerCase() == 'none') {
		addEvent(self, 'load', function (event) {modalForm.hide();});
	} else {
		addEvent(self, 'load', function (event) {modalForm.show();});
	}
}

SbModalForm.hide = function() {
	this._prepareSelectboxesBeforeHide();
	this.style.display = 'none';
	this.backgroundObject.style.display = 'none';
}

SbModalForm.show = function() {
	this._prepareSelectboxesBeforeShow(self);
	this.style.display = 'block';
	this.backgroundObject.style.display = 'block';
	this.resetPosition();
}

SbModalForm._prepareSelectboxesBeforeShow = function(winObject) {
	if (document.all) {
		var wholeList = winObject.document.getElementsByTagName('SELECT');
		var ownList = this.getElementsByTagName('SELECT');
		for (var i = 0; i < wholeList.length; i++) {
			var item = wholeList[i];
			var isOwn = false;
			for (var j = 0; j < ownList.length; j++) {
				if (item == ownList[j]) {
					isOwn = true;
					break;
				}
			}
			if (isOwn) {
				continue;
			}
			this.disabledItems.push(item);
			this.disabledItemsVisibility.push(item.style.visibility);
			item.style.visibility = 'hidden';
		}
		for (var i = 0; i < winObject.frames.length; i++) {
			this._prepareSelectboxesBeforeShow(winObject.frames[i]);
		}
	}
}

SbModalForm._prepareSelectboxesBeforeHide = function() {
	if (document.all) {
		for (var i = 0; i < this.disabledItems.length; i++) {
			this.disabledItems[i].style.visibility = this.disabledItemsVisibility[i];
		}
		this.disabledItems = new Array();
		this.disabledItemsVisibility = new Array();
	}
}

SbModalForm.resetPosition = function() {
	setObjectSize(this.backgroundObject, getScreenWidth(), getScreenHeight());
	var left = getScrollLeft();
	var top = getScrollTop();
	setObjectPosition(this.backgroundObject, left, top);
	if (this.isMoved) {
		left = this.positionLeft;
		top = this.positionTop;
	} else {
		left += Math.ceil(getScreenWidth()/2 - getObjectWidth(this)/2);
		top += Math.ceil(getScreenHeight()/2 - getObjectHeight(this)/2);
	}
	this.setPosition(left, top);
}

SbModalForm.headerMouseDown = function(event) {
	if (document.all) {
		event = window.event;
	}
	this.horizontalDelta = getScreenX(event) - this.positionLeft;
	this.verticalDelta = getScreenY(event) - this.positionTop;
	if (this.horizontalDelta < 0) {
		this.horizontalDelta = 0;
	}
	if (this.verticalDelta < 0) {
		this.verticalDelta = 0;
	}
	SbModalFormDragControlObject.startDragging(this);
	stopEvent(event);
}

SbModalForm.onMouseUp = function(event) {
	this.setPosition(getScreenX(event), getScreenY(event));
	this.horizontalDelta = 0;
	this.verticalDelta = 0;
}

SbModalForm.onMouseMove = function(event) {
	this.setPosition(getScreenX(event), getScreenY(event));
	this.isMoved = true;
}

SbModalForm.setPosition = function(left, top) {
	var endOffset = 20; //const

	left -= this.horizontalDelta;
	top -= this.verticalDelta;
	if ((left + getObjectWidth(this)) > (getScreenWidth() + getScrollLeft() - endOffset)) {
		left = getScreenWidth() + getScrollLeft() - endOffset - getObjectWidth(this);
	}
	if (left < endOffset + getScrollLeft()) {
		left = endOffset + getScrollLeft();
	}
	if ((top + getObjectHeight(this)) > (getScreenHeight() + getScrollTop() - endOffset)) {
		top = getScreenHeight() + getScrollTop() - endOffset - getObjectHeight(this);
	}
	if (top < endOffset + getScrollTop()) {
		top = endOffset + getScrollTop();
	}
	if ((left + getObjectWidth(this)) > (getScreenWidth() + getScrollLeft() - endOffset)) {
		left = Math.ceil(endOffset / 2);
		this.positionLeft = left;
	} else {
		this.positionLeft = left;
	}
	if ((top + getObjectHeight(this)) > (getScreenHeight() + getScrollTop() - endOffset)) {
		top = Math.ceil(endOffset / 2);
		this.positionTop = top;
	} else {
		this.positionTop = top;
	}
	setObjectPosition(this, left, top);
}

SbModalForm.setTitle = function(newTitle) {
	this.titleObject.innerHTML = newTitle; 
}
