﻿var expandedSelect = null;
var expandingSelect = false;

function ScrollerResize () {
	var scroller = document.getElementById('scroller_wrapper');	
	if (scroller) 
	{	var wrapper = scroller.parentNode;
		var top_wrapper = document.getElementById('top_wrapper');
		var bottom_wrapper = document.getElementById('bottom_wrapper');
		var input_wrapper = document.getElementById('scroll_lister').parentNode;
		if (scroller && wrapper && input_wrapper) {
			var wrapper_height = 447;
			if (top_wrapper) wrapper_height-=top_wrapper.offsetHeight;
			if (bottom_wrapper) wrapper_height-=bottom_wrapper.offsetHeight;
			wrapper.style.height=wrapper_height+"px";
			input_wrapper.style.height=wrapper_height+"px";
			if (scroller.offsetHeight<wrapper_height) input_wrapper.style.display="none";
			//alert (top_wrapper.offsetHeight+"/"+bottom_wrapper.offsetHeight+"/"+wrapper.offsetHeight);
		}
	}
}
function SelectScrollUp(select_options) {
	//var select_options = this.parentNode.getElementsByTagName("A");
	var open_items = 0;
	//var remain_items =select_options.length;
	var past_items = 0;
	for(var q=0; q < select_options.length; q++) {
		//remain_items--;
		if (select_options[q].className=="" && open_items==0 && past_items>0) {
			select_options[q-1].className="";
			open_items++;
		}
		if (open_items==5) {
			select_options[q].className="hidden_option";
			open_items++;
		}
		if (select_options[q].className!="") {
			past_items ++;
		}
		if (select_options[q].className=="") {
			open_items++;
		}
	}
}
function SelectScrollDown(select_options) {
	//var select_options = this.parentNode.getElementsByTagName("A");
	var open_items = 0;
	var remain_items =select_options.length;
	for(var q=0; q < select_options.length; q++) {
		remain_items--;
		if (select_options[q].className=="" && open_items==0 && remain_items>4) {
			select_options[q].className="hidden_option";
			open_items++;
		}
		if (open_items==5) {
			select_options[q].className="";
			open_items++;
		}
		if (select_options[q].className=="") {
			open_items++;
		}
	}
}
function ReplaceSelects(container) {
	var selects = container.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++) {
		if (!selects[i].replaced) {
			var div = document.createElement('div');
			div.className = selects[i].className + ' collapsed';
			div.select = selects[i];
			div.id = selects[i].id + 'replaced';
			div.link = document.createElement('a');
			div.link.href = '#';
			div.link.className = 'button';
			
			if (selects[i].options.length > 0){
				if (selects[i].selectedIndex == -1) selects[i].selectedIndex = 0;
				div.link.innerHTML = selects[i].options[selects[i].selectedIndex].text;
			}
			div.link.onclick =
				function () {
					var container = this.parentNode;
					if ((expandedSelect) && (container != expandedSelect)) {
						expandedSelect.className = expandedSelect.className.replace('expanded', 'collapsed');
						expandedSelect.parentNode.parentNode.className="select_left";
					}
					if (container.className.indexOf('collapsed') > -1) {
						if (!container.select.disabled) {
							container.className = container.className.replace('collapsed', 'expanded');
							expandedSelect = container;
							expandedSelect.parentNode.parentNode.className="select_left open";
							expandingSelect = true;
						}
					} else {
						container.className = container.className.replace('expanded', 'collapsed');
						container.parentNode.parentNode.className="select_left";
						expandedSelect = null;
					} // end if
					return false;
				} // end event
			div.appendChild(div.link);
			div.popup = document.createElement('p');
			var rightBg = document.createElement('div');
			rightBg.className="right_bg";
			var buttonUp = document.createElement('div');
			buttonUp.className="up";
			buttonUp.onmousedown =
					function () {
						var select_options = this.parentNode.getElementsByTagName("A");
						scroll_interval = setInterval(function () { SelectScrollUp(select_options); }, 150);
						return false;
					}
			var buttonDown = document.createElement('div');
			buttonDown.className="down";
			buttonDown.onmousedown =
					function () {
						var select_options = this.parentNode.getElementsByTagName("A");
						scroll_interval = setInterval(function () { SelectScrollDown(select_options); }, 150);
						return false;
					}
			var optionsBg = document.createElement('div');
			optionsBg.className="optionsBg";
			div.popup.appendChild(rightBg);
			div.popup.appendChild(buttonDown);			
			div.popup.appendChild(buttonUp);
			
			for (var j = 0; j < selects[i].options.length; j++) {
				var item = document.createElement('a');
				item.innerHTML = selects[i].options[j].text;
				item.href = '#';
				item.option = selects[i].options[j];
				if (j>4) item.className="hidden_option";
				item.container = div;
				item.onclick =
					function () {
						this.option.selected = true;
						this.container.link.innerHTML = this.innerHTML;
						if (this.container.select.onchange) this.container.select.onchange();
						this.container.className = this.container.className.replace('expanded', 'collapsed');
						if (expandedSelect) {
							expandedSelect.className = expandedSelect.className.replace('expanded', 'collapsed');
							expandedSelect.parentNode.parentNode.className="select_left";
						}
						expandedSelect = null;
						return false;
					} // end event
				div.popup.appendChild(item);
			} // end for
			div.appendChild(div.popup);
			selects[i].style.display = 'none';
			selects[i].parentNode.insertBefore(div, selects[i]);
			selects[i].replaced = true;
		} // end if
	} // end for
} // end function
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
var ReplaceAllSelects =
	function () {
		ReplaceSelects(document);
		ScrollerResize();
		var bb = document.getElementById('main_wrapper');
		if (bb) bb.className="main_wrapper js";
		//init();
	} // end event
addLoadEvent(ReplaceAllSelects);

document.onclick =
	function () {
		if (!expandingSelect && (expandedSelect)) 
			expandedSelect.className = expandedSelect.className.replace('expanded', 'collapsed');
	} // end event
