/*
 * Copyright (c) 2009 The Olympos Development Team.
 *
 * http://sourceforge.net/projects/olympos/
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code,
 * this entire header must remain intact.
 */
Ext.namespace("chi.Util");

/**
 * Extracts the Class Name out of an OID.
 * 
 * @param {String}
 *            oid The OID to extract UWM Class Name from.
 * @return The class name of the OID.
 * @type String
 */
chi.Util.getClassNameFromOid = function(oid) {
	var result = oid.match(/^[^:]+/)[0];

	if (oid.charAt(0) == "{") {
		result = oid.match(/:([^}]+)/)[1];
	}

	return result;
};

/**
 * Extracts the numeric part out of an OID.
 * 
 * @param {String}
 *            oid The OID to extract numeric part from.
 * @return {int} The numeric part of the OID
 */
chi.Util.getNumericFromOid = function(oid) {
	return oid.match(/:([0-9]+)/)[1];
};

/**
 * Displays a message to the user.
 * 
 * @param {String}
 *            title Title of the message.
 * @param {String}
 *            message The message body. May contain HTML tags.
 * @param {chi.Util.messageType}
 *            messageType The type of the message.
 */
chi.Util.showMessage = function(title, message, messageType) {
	var messageContainer = Ext.get("messageContainer");
	if (!messageContainer) {
		messageContainer = Ext.DomHelper.insertFirst(document.body, {
		    id : "messageContainer",
		    style : "position: absolute"
		}, true);
	}
	messageContainer.alignTo(document, 't-t');
	var messageBox = Ext.DomHelper.append(messageContainer, {
		html : '<div>' + '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>' + '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>' + title
		        + '</h3>' + message + '</div></div></div>' + '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>' + '</div>'
	}, true);
	messageBox.slideIn('t').pause(3).ghost("t", {
		remove : true
	});
};

/**
 * List of message types.
 */
chi.Util.messageType = {
    INFO : 1,
    WARNING : 2,
    ERROR : 3
};

/**
 * Disables text selection on a DOM element.
 * 
 * @param {DOMElement}
 *            elem The DOM element to disable selection on.
 */
chi.Util.setElementUnselectable = function(elem) {
	if (elem) {
		elem.style.MozUserSelect = "none";
		elem.style.KhtmlUserSelect = "none";
		elem.unselectable = "on";
	}
};

/**
 * Returns a new array with all entries of completeArray except the ones in
 * arrayToRemove.
 * 
 * @param {array}
 *            completeArray The source array which should be filtered.
 * @param {array}
 *            arrayToRemove The array with the elements to remove.
 * 
 * @return {array} A new array with all entries of completeArray except the ones
 *         in arrayToRemove.
 */
chi.Util.except = function(completeArray, arrayToRemove) {
	var result = [];

	for ( var i = 0; i < completeArray.length; i++) {
		var currCompleteEntry = completeArray[i];
		var completeOid = chi.Util.getOidOrValue(currCompleteEntry);

		var found = false;

		for ( var j = 0; j < arrayToRemove.length; j++) {
			var currRemoveEntry = arrayToRemove[j];
			var removeOid = chi.Util.getOidOrValue(currRemoveEntry);

			if (completeOid == removeOid) {
				found = true;
				break;
			}
		}

		if (!found) {
			result.push(currCompleteEntry);
		}
	}

	return result;
};

/**
 * Returns a new array containing the intersection between two arrays.
 * 
 * @param {array}
 *            array1 The first array. Matching values are taken from this array.
 * @param {array}
 *            array2 The second array.
 * 
 * @return {array} A new array containing the intersection between two arrays.
 */
chi.Util.intersect = function(array1, array2) {
	var result = [];

	for ( var i = 0; i < array1.length; i++) {
		var currValue1 = array1[i];
		var currOid1 = chi.Util.getOidOrValue(currValue1);

		for ( var j = 0; j < array2.length; j++) {
			var currValue2 = array2[j];
			var currOid2 = chi.Util.getOidOrValue(currValue2);

			if (currOid1 == currOid2) {
				result.push(currValue1);
				break;
			}
		}
	}

	return result;
};

/**
 * Returns the oid if value is a model record, value itself otherwise.
 * 
 * @param {mixed}
 *            value The value to evaluate.
 * 
 * @return {string} The oid if value is a model record, value itself otherwise.
 */
chi.Util.getOidOrValue = function(value) {
	return value.isModelRecord ? value.getOid() : value;
};
