
document.observe("dom:loaded", function() {
	//if (navigator.appName.indexOf("Microsoft") != -1) return;
	$$('select[class~="jsui"]').each(function(select, selectID) {
		var divSelect = new Element('span', { 'class': 'jsuiSelect'});
		var divView = new Element('span', { 'class': 'view'});
		var divRolloutBtn = new Element('span', { 'class': 'rolloutBtn'});
		var divRolloutContainer = new Element('span', { 'class': 'rolloutContainer'});
		var divRollout = new Element('span', { 'class': 'rollout'});
		
		divSelect.style.zIndex = 10000 - selectID*100;
		divView.style.zIndex = divSelect.style.zIndex + 1;
		divRolloutBtn.style.zIndex = divSelect.style.zIndex + 5;
		divRolloutContainer.style.zIndex = divSelect.style.zIndex + 1;
		divRollout.style.zIndex = divSelect.style.zIndex + 2;
		
		
		divSelect.appendChild(divRolloutBtn);
		divSelect.appendChild(divView);
		divRolloutContainer.appendChild(divRollout);
		divSelect.appendChild(divRolloutContainer);
		
		select.childElements().each(function(el, index) {
			if (el.nodeName.toLowerCase() == 'option') {
				var span = new Element('span');
				span.update(el.innerHTML);
				var link = new Element('a', {'href': 'javascript:;'});
				link.update(span);
				
				if ( el.disabled == false ) {
					link.onclick = function() {
						divView.update(el.innerHTML);
						divRolloutBtn.onclick();
						select.selectedIndex = index;
						select.onchange();
					};
				} else {
					link.className = "jsuiSelectDisabled";
				}
				
				if (el.selected) divView.update(el.innerHTML);
				
				if (!divSelect.cntOptions) divSelect.cntOptions = 0;
				
				if ( el.disabled == false ) {
					divRollout.appendChild(link);
					divSelect.cntOptions++;
				}
			}
		});
		
		divRolloutContainer.hide();
		divSelect.rolledOut = false;
		
		var visibleChilds = Math.min(divSelect.cntOptions, 5);
		divRolloutContainer.style.height = ((divSelect.cntOptions * 20) + 6)+'px';
		divRollout.style.height = ((divSelect.cntOptions * 20))+'px';
		
		divRolloutBtn.onmouseover = function() {
			divSelect.addClassName('jsuiSelectOn');
		}
		divRolloutBtn.onmouseout = function() {
			divSelect.removeClassName('jsuiSelectOn');
		}
		divRolloutBtn.onclick = function() {
			if (divSelect.rolledOut == false) {
				divSelect.rolledOut = true;
				divRolloutContainer.show();
			} else {
				divSelect.rolledOut = false;
				divRolloutContainer.hide();
			}
		}
		select.hide();
		select.parentNode.insertBefore(divSelect, select);
	});
});