/*
	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 = 22;

JSUI_TSelectManager.TSelects = [];
JSUI_TSelectManager.TSelectsSources = [];

/* 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
	{
		// oli
		JSUI_TSelectManager.fixOptionsWidth(s);
	}
}

/* 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) {
	var value = (s.$HTMLOptionsElement.offsetWidth - 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 || bv.gecko) return;

	if (sCollection != "undefined") {
		for(var i=0;i < sCollection.length;i++) {
			var cel = sCollection[i];
			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;