/*
	JSUI_TSelect
*/
function JSUI_TSelect( /* int */ width) {

	this.$text = "";
	this.$value = null;
	this.$selectedIndex = -1;
	this.$width = width;
	this.$options = [];
	this.$expanded = false;
	this.$HTMLElement = null;
	this.$HTMLSelectBody = null;
	this.$HTMLTextElement = null;
	this.$HTMLOptionsElement = null;


/* void */ this.addOptions = function (/* Array */ optionsDataSet) {
	if (optionsDataSet.length > 0) {
		for (var i=0; i < optionsDataSet.length; i++) {
			var od = optionsDataSet[i];
			var option = new JSUI_TSelectOption(od.value, od.text);
			option.$index = i;
			option.$owner = this;
			if (od.selected) option.select();
			this.$options.push(option);
		}
	} else return;
}

/* Boolean */ this.hasOptions = function () {
	return (this.$options.length > 0)? true : false;
}

/* integer */ this.getSelectedOptionIndex = function () {
	var selectedIndex = -1;
	for (var i=0; i < this.$options.length; i++) {
		if (this.$options[i].$selected) return this.$options[i].$index;
	}
	return selectedIndex;
}

/* void */ this.init = function () {
	var index = this.getSelectedOptionIndex();
	if (index > -1) {
		this.$text = this.$HTMLTextElement.innerHTML = this.$options[index].$text;
		this.$value = this.$options[index].$value;
		this.$selectedIndex = index;
	} else {
		this.$text = this.$HTMLTextElement.innerHTML = this.$options[0].$text;
		this.$value = this.$options[0].$value;
		this.$selectedIndex = 0;
	}
}

/* HTMLElement */ this.render = function () {
	this.$HTMLElement = document.createElement(JSUI_TSelectManager.SELECT_HOLDER);
	this.$HTMLElement.className = "TSelectHolder";
	this.$HTMLElement.style.width = this.$width + 'px';
	
	var table = document.createElement(JSUI_TSelectManager.SELECT_BODY);
	table.className = "TSelectBody";
	table.cellSpacing = 0;
	table.cellPadding = 0;
	var _this = this;
	table.onclick = function (e) { 
		JSUI_EventsManager.stopEventPropagation(e);
		JSUI_TSelectManager.expandTSelect(_this); 
	}

	var tbody = document.createElement("TBODY");
	var row = document.createElement(JSUI_TSelectManager.SELECT_BODY_ROW);

	var textElement_cell = document.createElement(JSUI_TSelectManager.SELECT_TEXT_CELL);
	textElement_cell.className = "TSelectTextCell";

	this.$HTMLTextElement = document.createElement(JSUI_TSelectManager.SELECT_TEXT);
	this.$HTMLTextElement.className = "TSelectText";
	this.$HTMLTextElement.style.width = this.$width - JSUI_TSelectManager.SELECT_BUTTON_CONTAINER_WIDTH_MODIFIER + 'px';
	this.$HTMLTextElement.innerHTML = this.$text;
	var button_cell = document.createElement(JSUI_TSelectManager.SELECT_BUTTON_CELL);
	button_cell.className = "TSelectButtonCell";
	button_cell.innerHTML = "&nbsp;";

	textElement_cell.appendChild(this.$HTMLTextElement);

	row.appendChild(textElement_cell);
	row.appendChild(button_cell);

	tbody.appendChild(row);
	table.appendChild(tbody);

	this.$HTMLElement.appendChild(table);

	if (this.hasOptions()) {
		this.$HTMLOptionsElement = document.createElement(JSUI_TSelectManager.OPTIONS_HOLDER);
		this.$HTMLOptionsElement.className = "TSelectOptionsHolder";
		this.$HTMLOptionsElement.style.visibility = "hidden";
		for (i=0;i < this.$options.length;i++) {
			this.$HTMLOptionsElement.appendChild(this.$options[i].render());
		}
		this.$HTMLElement.appendChild(this.$HTMLOptionsElement);
		this.init();
	}

	return this.$HTMLElement;
}

}