/*
	ArrayUtils
*/

/* boolean */ Array.prototype.contains = function (/* Mixed */ key) {
	for (var i=0; i < this.length; i++) {
		if (this[i] == key) return true;
	} return false;
}/*
	DOMTools
*/

DOMTools = new Object();

/* Array */ DOMTools.getSelects = function() {
	return document.getElementsByTagName("select");
}
/*
	JSUI_EventsManager
*/
JSUI_EventsManager = new Object();

/* void */ JSUI_EventsManager.stopEventPropagation = function (/* event */ e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}/*
	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;
}

}/*
	JSUI_TSelectManager
*/

JSUI_TSelectManager = new Object();

JSUI_TSelectManager.SELECT_HOLDER = "DIV";
JSUI_TSelectManager.SELECT_BODY = "TABLE";
JSUI_TSelectManager.SELECT_BODY_ROW = "TR";
JSUI_TSelectManager.SELECT_TEXT_CELL = "TD";
JSUI_TSelectManager.SELECT_TEXT = "DIV";
JSUI_TSelectManager.SELECT_BUTTON_CELL = "TD";
JSUI_TSelectManager.SELECT_OPTIONS_SIZE = 8;

JSUI_TSelectManager.OPTIONS_HOLDER = "DIV";
JSUI_TSelectManager.OPTION = "DIV";

JSUI_TSelectManager.SELECT_BUTTON_CONTAINER_WIDTH_MODIFIER = 21;

/* JSUI_TSelectManager.WIDTH_MODIFICATOR = TSelectOption horizontal paddings + TSelectOptionsHolder borders width */
JSUI_TSelectManager.OPTION_WIDTH_MODIFIER = 23;

JSUI_TSelectManager.TSelects = new Array();
JSUI_TSelectManager.TSelectsSources = new Array();

/* void */ JSUI_TSelectManager.expandTSelect = function (/* TSelect */ s) {
	if (s.hasOptions()) {
		if (s.$expanded) {
			s.$HTMLOptionsElement.style.visibility = "hidden";
		} else {
			JSUI_TSelectManager.collapseAllTSelects();
			s.$HTMLOptionsElement.style.visibility = "visible";
		}
			s.$expanded = !s.$expanded;
	}
}

/* void */ JSUI_TSelectManager.collapseAllTSelects = function () {
	for (var i=0; i < JSUI_TSelectManager.TSelects.length; i++) {
		var s = JSUI_TSelectManager.TSelects[i];
		if(s.$expanded) {
			s.$expanded = !s.$expanded;
			s.$HTMLOptionsElement.style.visibility = "hidden";
			s.$HTMLOptionsElement.scrollTop = 0;
		}
	}
}

/* void */ JSUI_TSelectManager.setHover = function (/* TSelectOption*/ o) {
	o.addCssClass("hover");
}

/* void */ JSUI_TSelectManager.removeHover = function (/* TSelectOption*/ o) {
	o.removeCssClass("hover");
}

/* Array */ JSUI_TSelectManager.getOptionsData = function (/* Array */ oCollection) {
	if (oCollection.length > 0) {
		var oDataSet = [];
		for(var i=0;i < oCollection.length;i++) {
			var oel = oCollection[i];
			var oData = {"value":oel.value,"text":oel.text,"selected":oel.selected}
			oDataSet.push(oData);
		} return oDataSet;
	} else return;
}

/* TSelect */ JSUI_TSelectManager.createTSelectFromSelect = function (/* SELECT */ select) {
	var tmpSelect = new JSUI_TSelect(select.offsetWidth);
	tmpSelect.addOptions(JSUI_TSelectManager.getOptionsData(select.options));
	return tmpSelect;
}

/* integer */ JSUI_TSelectManager.getMaxOptionWidth = function (/* TSelect */ s) {
	var max = 0;
	for (var i=0; i < s.$options.length; i++) {
		if (s.$options[i].$HTMLElement.offsetWidth > max) {
			max = s.$options[i].$HTMLElement.offsetWidth;
		}
	}
	return max;
}

/* void */ JSUI_TSelectManager.setMinOptionsHolderWidth = function (/* TSelect */ s, /* integer */ width) {
	if (s.$HTMLElement.offsetWidth > width) {
		s.$HTMLOptionsElement.style.width = s.$HTMLElement.offsetWidth + 10 + 'px';
	} else
	{
		// wooki
		var scrollerWidthModifier = (s.$options.length > JSUI_TSelectManager.SELECT_OPTIONS_SIZE)? 16 : 0;
		s.$HTMLOptionsElement.style.width = (width + scrollerWidthModifier) + "px";
		JSUI_TSelectManager.fixOptionsWidth(s, width);
	}
}

/* void */ JSUI_TSelectManager.fixOptionsHolderHeight = function (/* TSelect */ s) {
	if (s.$options.length > JSUI_TSelectManager.SELECT_OPTIONS_SIZE) {
		s.$HTMLOptionsElement.style.height = s.$options[0].$HTMLElement.offsetHeight * JSUI_TSelectManager.SELECT_OPTIONS_SIZE + "px";
	}
}

/* void */ JSUI_TSelectManager.fixOptionsWidth = function (/* TSelect */ s, /* integer */ width ) {
	var value = (width - JSUI_TSelectManager.OPTION_WIDTH_MODIFIER) + "px";
	for (var i=0; i < s.$options.length; i++) {
		s.$options[i].$HTMLElement.style.width = value;
	}
}

/* integer */ JSUI_TSelectManager.getTSelectIndex = function (/* TSelect */ s) {
	for (var i=0; i < JSUI_TSelectManager.TSelects.length; i++) {
		if (JSUI_TSelectManager.TSelects[i] == s) return i;
	}
}

/*void */ JSUI_TSelectManager.updateTSelect = function (/* TSelectOption */ o) {
	var TSelect = o.$owner;
	var TSelectSource = JSUI_TSelectManager.TSelectsSources[JSUI_TSelectManager.getTSelectIndex(o.$owner)];

	if (TSelect.$selectedIndex != o.$index) {
		TSelect.$options[TSelect.$selectedIndex].deselect();
		o.select();
		//JSUI_TSelectManager.setHover(o);
		TSelect.$value  = o.$value;
		TSelect.$text = TSelect.$HTMLTextElement[bv.ie? "innerText" : "innerHTML"] = o.$text;
		TSelect.$selectedIndex = o.$index;
		/* update source Element */
		TSelectSource.selectedIndex = TSelect.$selectedIndex;
		if (TSelectSource.onchange) TSelectSource.onchange();
	}
}

/* void */ JSUI_TSelectManager.renderSelects = function (/* Array */ sCollection) {
	if (bv.opera) return;

	if (sCollection != "undefined") {
		for(var i=0;i < sCollection.length;i++) {
			var cel = sCollection[i];
			if (cel.type != 'select-multiple') {
				JSUI_TSelectManager.TSelectsSources.push(cel);
				var slc = JSUI_TSelectManager.createTSelectFromSelect(cel);
				JSUI_TSelectManager.TSelects.push(slc);
				cel.style.display = 'none';
				cel.parentNode.insertBefore(slc.render(),cel);
				JSUI_TSelectManager.setMinOptionsHolderWidth(slc, JSUI_TSelectManager.getMaxOptionWidth(slc));
// oli
//				JSUI_TSelectManager.fixOptionsWidth(slc);
				JSUI_TSelectManager.fixOptionsHolderHeight(slc);
			}
		}
	} else return;
}

document.onclick = JSUI_TSelectManager.collapseAllTSelects;/*
	JSUI_TSelectOption
*/
function JSUI_TSelectOption (value, text) {
	this.$owner = null;
	this.$index = -1;
	this.$selected = false;
	this.$value = value;
	this.$text = text;
	this.$HTMLElement = null;

/* void */ this.select = function () {
	this.$selected = true;
}

/* void */ this.deselect = function () {
	this.$selected = false;
}


/* void */ this.addCssClass = function (/* string */ className) {
	var classes = this.$HTMLElement.className.split(" ");
	if (!classes.contains(className)) classes.push(className);
	this.$HTMLElement.className = classes.join(" ");
}

/* void */ this.removeCssClass = function (/* string */ className) {
	var classes = this.$HTMLElement.className.split(" ");
	if (classes.contains(className)) classes.pop(className);
	this.$HTMLElement.className = classes.join(" ");
}

/* HTMLElement */ this.render = function () {
	this.$HTMLElement = document.createElement(JSUI_TSelectManager.OPTION);
	this.$HTMLElement.className = "TSelectOption";
	this.$HTMLElement.innerHTML = this.$text;
	
	var _this = this;
	this.$HTMLElement.onmouseover = function () {
		JSUI_TSelectManager.setHover(_this);
	}
	this.$HTMLElement.onmouseout = function () {
		JSUI_TSelectManager.removeHover(_this);
	}
	this.$HTMLElement.onclick = function () {
		JSUI_TSelectManager.updateTSelect(_this);
	}

	return this.$HTMLElement;
}

}
