var onSelectOption = false;
var selObj;

function selectBoxReplacement(obj, nr) 
	{
		this.obj = obj;
		this.nr = nr;
		this.functionArray;
		
		if(obj)
			this.setSelectBox();
	}
		selectBoxReplacement.prototype.setSelectBox= function()	{
			this.obj.id = "select_" + this.nr;
			this.changeSelectBox(this.obj);
		
			
			var container = document.createElement('div');
			var ul = document.createElement('ul');
			var div = document.createElement('div');
		
			container.className = "selectContainerAll selectContainer_"+this.nr;
			
			ul.id = 'ulId_' +  this.nr;
			ul.className = 'selectCombo selectId_' +  this.nr;
			ul.onmouseover = function(){ onSelectOption=true;	}
			ul.onmouseout = function(){ onSelectOption=false; }
			
			div.id = 'textBoxDivId_' +  this.nr;
			div.className = 'selectCombo selectId_' + this.nr;
			div.onclick = function() { ul.style.display = "block"; selObj = ul; }
			
			this.selectBoxOptions(this.obj, ul, div);
			
			var newContainer = this.obj.parentNode.insertBefore(container,this.obj);
			newContainer.appendChild(div);
			newContainer.appendChild(ul);
		}
		
		selectBoxReplacement.prototype.changeSelectBox = function(obj) {
			if(obj.onchange)
			{	
				var functionString = this.obj.getAttribute("onchange") + "";
				if(functionString.indexOf("{") != -1)
				{	
					functionString = functionString.substring(functionString.indexOf("{")+2);
					functionString = functionString.substring(0, functionString.indexOf("}")-1);
	
				}
				
				this.functionArray = functionString.split(";"); 
				for(var i=0; i<this.functionArray.length; i++)
				{
					if(this.functionArray[i])
					{
						var stripFunction = this.functionArray[i] + "";
						stripFunction = stripFunction.substring(stripFunction.indexOf("(")+2);
						stripFunction = stripFunction.substring(0, stripFunction.indexOf(")")-1);
						this.functionArray[i] = stripFunction;
					}
				}
			}	
		}
		
		selectBoxReplacement.prototype.selectBoxOptions = function(obj, ul, div) {
			var liFunctionArray = this.functionArray;
			var selectedOpt = this.obj.selectedIndex;
			var opts = this.obj.options;

			
			if(opts.length < 10)
				//ul.style.height = (opts.length*15) + "px";
			
			for (var i=0; i<opts.length; i++)
			{
				
				var li = document.createElement('li');

				/* VK */
				li.id = opts[i].text.toLowerCase();
				
				var x = document.language.elements['language-options'].selectedIndex;
				var optDisabled = document.language.elements['language-options'].options[i].disabled;
				
				var txt = document.createTextNode(opts[i].text);
				var liClickObj = this.obj;
				
				li.appendChild(txt);

				if ( optDisabled == false ) {

				li.onmouseover = function(){ if(this.className == "selected") this.className+=" optionOver"; else this.className="optionOver"; }
				li.onmouseout = function(){ if(this.className.indexOf("selected") == -1) this.className="optionOut"; else this.className="selected"; }
				li.onclick = function(){ sBr=new selectBoxReplacement; sBr.selectMe(this, div, liClickObj.id, liFunctionArray); }

				}
				
				else {
					li.className = "disabled";
					}

				if (i == selectedOpt) 
				{
					div.innerHTML="<div class=\"selectArrow\"></div><span class=" + opts[i].text.toLowerCase() + ">" + opts[i].text + "</span>";
					li.className = 'selected';
					//alert(div.innerHTML);
				}

				ul.appendChild(li);
			}
		}
	
		selectBoxReplacement.prototype.selectMe = function(obj, div, selectObjId, functionArray) {
			if(div)
				div.innerHTML="<div class=\"selectArrow\"></div><span class=" + obj.innerHTML.toLowerCase() + ">" + obj.innerHTML + "</span>"
			
			var lis = obj.parentNode.getElementsByTagName('li');
			obj.parentNode.style.display = "none";
			
			for (var i=0; i<lis.length; i++)
				if (lis[i] != obj)
				{
					lis[i].className='optionOut';
					selObj = "";
				}
				else if(selectObjId)
				{
					document.getElementById(selectObjId).options[i].selected = true;
					obj.className='selected';

					if(functionArray)
						if(functionArray[0].indexOf("submit") != -1)
							document[functionArray[0].substring((functionArray[0].indexOf("[") + 1), functionArray[0].indexOf("]"))].submit();
						else
							location.href = functionArray[0]+"/"+document.getElementById(selectObjId).options[i].value;
				}
		}


	//Mouse down event function
	document.onmousedown = function() {
		if(!onSelectOption && selObj)
			selObj.style.display = "none";
	}
	
	
	//Init function
	function initFormElements() {
		var s = document.getElementById('top-languageselect').getElementsByTagName('select');
//		document.getElementById('select-language').style.display = "none";
//		document.getElementById('language-submit').style.display = "none";
	
		for (var j=0; j<s.length; j++) {
			new selectBoxReplacement(s[j], j);
			}
	}
	
//	setTimeout("initFormElements()", 5000);
	initFormElements();