/**
 * Object selection script
 *
 * @copyright (c) 2004-2006, SWsoft
 */

function SbObjectSelection() {
	this._objectsList = new Array();
	this._selectedObject = null;
	this._onSelectMethodsList = new Array();
	this._onHighlightMethodsList = new Array();
	this._onResetMethodsList = new Array();
}

SbObjectSelection.prototype.addOnSelectEvent = function(method) {
	this._onSelectMethodsList.push(method);
}

SbObjectSelection.prototype.addOnHighlightEvent = function(method) {
	this._onHighlightMethodsList.push(method);
}

SbObjectSelection.prototype.addOnResetEvent = function(method) {
	this._onResetMethodsList.push(method);
}

SbObjectSelection.prototype.registerObject = function(itemObject) {
	itemObject.className = 'SB_control_not_selected';
	var currentSelectionObject = this;
	addEvent(itemObject, 'mouseover', function (event) { currentSelectionObject.highlight(itemObject); });
	addEvent(itemObject, 'mouseout', function (event) { currentSelectionObject.reset(itemObject); });
	addEvent(itemObject, 'click', function (event) { currentSelectionObject.select(itemObject); });
	this._reset(itemObject);
	this._objectsList.push(itemObject);
}

SbObjectSelection.prototype.select = function(itemObject) {
	var result = true;
	for (var i = 0; i < this._onSelectMethodsList.length; i++) {
		var method = this._onSelectMethodsList[i];
		var res = method(itemObject);
		if (false == res) {
			result = false;
			break;
		}
	}
	if (result) {
		if ((null != this._selectedObject) && (itemObject != this._selectedObject)) {
			this._reset(this._selectedObject);
		}
		this._selectedObject = itemObject;
		this._selectedObject.className = 'SB_control_selected';
	}
}

SbObjectSelection.prototype.highlight = function(itemObject) {
	if (itemObject != this._selectedObject) {
		var result = true;
		for (var i = 0; i < this._onHighlightMethodsList.length; i++) {
			var method = this._onHighlightMethodsList[i];
			var res = method(itemObject);
			if (false == res) {
				result = false;
				break;
			}
		}
		if (result) {
			itemObject.className = 'SB_control_highlighted';
		}
	}
}

SbObjectSelection.prototype.reset = function(itemObject) {
	if (itemObject != this._selectedObject) {
		this._reset(itemObject);
	}
}

SbObjectSelection.prototype._reset = function(itemObject) {
	var result = true;
	for (var i = 0; i < this._onResetMethodsList.length; i++) {
		var method = this._onResetMethodsList[i];
		var res = method(itemObject);
		if (false == res) {
			result = false;
			break;
		}
	}
	if (result) {
		itemObject.className = 'SB_control_not_selected';
	}
}