/**
 * WYSIWYG form scripts
 *
 * @copyright (c) 2004-2008, SWsoft
 */
var SbWysiwygForm = Class.create();
SbWysiwygForm.prototype = {

	/**
	 * SB modal form object
	 *
	 * @var obj
	 * @access private
	 */
	_formObject: null,

	/**
	 * Before open events
	 *
	 * @var array
	 * @access private
	 */
	_onBeforeOpen: null,

	/**
	 * After open events
	 *
	 * @var array
	 * @access private
	 */
	_onAfterOpen: null,

	/**
	 * After close events
	 *
	 * @var array
	 * @access private
	 */
	_onAfterClose: null,

	/**
	 * Object constructor
	 *
	 * @access public
	 * @param obj SB modal form object or it's id
	 * @return void
	 */
	initialize: function(modalForm) {
		this._formObject = $(modalForm);
		this.resetEventsListeners();
	},

	/**
	 * Resets events listeners
	 *
	 * @access public
	 * @return void
	 */
	resetEventsListeners: function() {
		this._onBeforeOpen = new Array();
		this._onAfterOpen = new Array();
		this._onAfterClose = new Array();
	},

	/**
	 * Observe on before open method
	 *
	 * @access public
	 * @param method
	 * @return void
	 */
	observeOnBeforeOpen: function(method) {
		this._onBeforeOpen.push(method);
	},

	/**
	 * Observe on after close method
	 *
	 * @access public
	 * @param method
	 * @return void
	 */
	observeOnAfterOpen: function(method) {
		this._onAfterOpen.push(method);
	},

	/**
	 * Observe on after close method
	 *
	 * @access public
	 * @param method
	 * @return void
	 */
	observeOnAfterClose: function(method) {
		this._onAfterClose.push(method);
	},

	/**
	 * Opens form dialog
	 *
	 * @access public
	 * @return void
	 */
	open: function() {
		this._fireEvent(this._onBeforeOpen);
		this._formObject.show();
		this._fireEvent(this._onAfterOpen);
	},

	/**
	 * Closes form dialog without saving
	 *
	 * @access public
	 * @return void
	 */
	close: function() {
		this._formObject.hide();
		this._fireEvent(this._onAfterClose);
	},

	/**
	 * Changes modal form size
	 *
	 * @access public
	 * @param int
	 * @param int
	 * @return void
	 */
	changeSize: function(width, height) {
		this._formObject.changeSize(width, height);
	},

	/**
	 * Returns form dimensions
	 *
	 * @access public
	 * @return object
	 */
	getDimensions: function() {
		return Element.getDimensions(this._formObject);
	},

	/**
	 * Returns max form dimensions
	 *
	 * @access public
	 * @return object
	 */
	getMaxDimensions: function() {
		return this._formObject.getMaxDimensions();
	},

	/**
	 * Fires event listeners
	 *
	 * @access private
	 * @param array methods
	 * @return void
	 */
	_fireEvent: function(listenersArray) {
		for (var i = 0; i < listenersArray.length; i++) {
			var method = listenersArray[i];
			try {
				method();
			} catch (exception) {
				continue;
			}
		}
	}
}