/* Funcao utilizada por nlSelectCommand */
var nlSelectCommandFuncMousedown = null;

// Funcao executada quando o selectCommando Fecha (listener) 
var nlSelectCommandCloseListener = null;

function nlSelectCommandShow(_btnShow, evt){
	if (window.event) evt = window.event;
	
	var idBotao = _btnShow.id;
	var idx = idBotao.lastIndexOf('_btnShow_');
	var prefix = idBotao.substring(0, idx);
	var sufix = idBotao.substring(idx + 9);
	
	var _content = document.getElementById(prefix + '_content_' + sufix);
	var _btnHide = document.getElementById(prefix + '_btnHide_' + sufix);
	var _group = document.getElementById(prefix + '_group_' + sufix);
	
	nlSelectCommandFuncMousedown = function(){
		nlSelectCommandClose(_content, _btnHide);
	};
	addListener(document, 'mousedown', nlSelectCommandFuncMousedown);
	
	_content.style.visibility = 'visible';
	_btnShow.style.visibility = 'hidden';
	_btnHide.style.visibility = 'visible';
	_btnShow.style.display = 'none';
	_btnHide.style.display = 'block';
	
	if (_btnShow.className.search('nlSelectCommand_show_link') >= 0) {
		_content.style.left = evt.clientX + document.body.scrollLeft;
		if (_btnShow.getAttribute("alinhaY")) {
			_content.style.top = evt.clientY + document.body.scrollTop;
		}
	} else if (_btnShow.className.search('nlSelectCommand_show_button') >= 0) {
		if (_content.offsetWidth < _group.offsetWidth) {
			_content.style.width = _group.offsetWidth;
		}
	}
	
	var elementY = 0;
	var theElement = _content;
	
	while (theElement != null) {
		elementY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	
	var ie5 = document.all && !window.opera;
	
	var windowHeight = ie5 ? iecompattest().document.body.clientHeight : window.innerHeight;
	var scrollY = document.body.scrollTop;
	
	var totalContentHeight = windowHeight + scrollY;
	var totalWindowHeight = elementY + _content.clientHeight;
	
	if (totalWindowHeight > totalContentHeight) {
		var _posTop = _content.clientHeight + _content.parentNode.clientHeight;
		_content.style.marginTop = '-' + _posTop + 'px';
	}
}

function nlSelectCommandHide(_btnHide){
	var idBotao = _btnHide.id;
	var idx = idBotao.lastIndexOf('_btnHide_');
	var prefix = idBotao.substring(0, idx);
	var sufix = idBotao.substring(idx + 9);
	
	var _content = document.getElementById(prefix + '_content_' + sufix);
	var _btnShow = document.getElementById(prefix + '_btnShow_' + sufix);
	
	_content.style.visibility = 'hidden';
	_content.style.marginTop = '-1px';
	_btnShow.style.visibility = 'visible';
	_btnHide.style.visibility = 'hidden';
	_btnShow.style.display = 'block';
	_btnHide.style.display = 'none';
	if (nlSelectCommandCloseListener) {
		nlSelectCommandCloseListener();
	}
	nlSelectCommandCloseListener = null;
}

function nlSelectCommandClose(_content, _btnHide){
	nlSelectCommandHide(_btnHide);
	removeListener(document, 'mousedown', nlSelectCommandFuncMousedown);
}

function nlSelectCommandContentMouseDown(evt){
	if (window.event) evt = window.event;
	evt.cancelBubble = true;
}

function nlSelectCommandContentClick(_content){
	var _id = _content.id;
	var idx = _id.lastIndexOf('_content_');
	var prefix = _id.substring(0, idx);
	var sufix = _id.substring(idx + 9);
	
	var _btnHide = document.getElementById(prefix + '_btnHide_' + sufix);
	nlSelectCommandClose(_content, _btnHide);
}

/**
 * Função para abrir as opcoes de um selectCommand pelo ID
 *
 * @param {Object} _id - id do component
 * @param {Object} _evt - event atual
 */
function nlSelectCommandOpen(_id, _evt, _listenerClose){
	//Cancela a propagacao do evento
	_evt.cancelBubble = true;
	var _hide = document.getElementById("nlSelectCommand_btnHide_" + _id);
	var _show = document.getElementById("nlSelectCommand_btnShow_" + _id);
	if (_hide && _show) {
		_show.setAttribute("alinhaY", true);
		fireClick(_hide, _evt);
		fireClick(_show, _evt);
		if (_listenerClose) {
			nlSelectCommandCloseListener = _listenerClose;
		} else {
			nlSelectCommandCloseListener = null;
		}
	}
}
