

function replaceSelects() {
	var inputs = document.getElementsByTagName('input');
	$A(inputs).each(function(input){
		sel = new FormElement(input);
	})
	var selects = document.getElementsByTagName('select');
	$A(selects).each(function(select){
		sel = new FormElement(select);
	})
}


var FormElement = new Class.create();

FormElement.prototype = {
	
	initialize: function(ele) {
		this.ele = $(ele);
		this.type = this.ele.nodeName;
		this.value = $F(this.ele);
		this.options = [];
		switch (this.type) {
			case 'SELECT':
				this.selectedIndex = this.ele.selectedIndex;
				this.selectCreate();
				this.ele.hide();
				window['testSelect'] = 0;
			break;
			case 'INPUT':
				switch (this.ele.type) {
					case 'text':
					
					break;
					case 'checkbox':
						this.checkCreate();
						this.ele.hide();
					break;
				}
			break;
		}
		this.selectHidden = true;
	},
	
	checkCreate: function() {
		var cls = this;
		this.onComplete = this.ele.onclick;
		
		this.input = Builder.node('div',{className:'formCheck'});
		this.input = $(this.input);
		
		this.checked = this.ele.hasAttribute('checked') ? true : false;
		if (this.checked) {
			this.input.addClassName('checked');
		}
		this.input.onclick = function() {
			//cls.ele.click();
			cls.checkClick();
		}
		this.ele.onchange = function() {
			cls.ele.click();
		}
		this.ele.onclick = function() {
			cls.checkClick();
		}
		this.input.onmouseover = function() {
			cls.input.addClassName('hover');
		}
		this.input.onmouseout = function() {
			cls.input.removeClassName('hover');
		}
		
		this.ele.parentNode.insertBefore(this.input,this.ele);
		this.ele.functions = {};
		this.ele.functions = this;
	},
	
	checkClick: function() {
		var checked = arguments[0] || this.ele.checked;
		switch(checked) {
			case true:
				this.checked = false;
				this.ele.checked = false;
				this.input.removeClassName('checked');
			break;
			case false:
				this.checked = true;
				this.ele.checked = true;
				this.input.addClassName('checked');
			break;
		}
		if (this.onComplete)
			this.onComplete();
	},
	
	selectCreate: function() {
		var cls = this;
		
		// this is just custom functions, this should be done another way, but it works with the classname
		switch (true) {
			case this.ele.hasClassName('onclick-updatevare'):
				this.onComplete = function() {
					updateVareInfo(cls.options[cls.selectedIndex].value);
				};
			break;
			case this.ele.hasClassName('onclick-updatelevering'):
				this.onComplete = function() {
					if ($('levering')) {
						if (cls.selectedIndex > 0) {
							//$('levering').functions.checkClick(false);
							$('levering').checked = false;
                            $('levering_more').style.display = 'none';
//							$('levering').functions.checkClick(true);
                            $('afhentning_check').checked = true;
						} else {
							//$('levering').functions.checkClick(true);
							$('levering').checked = true;
                            $('levering_more').style.display = '';
//							$('levering').functions.checkClick(false);
                            $('afhentning_check').checked = false;
						}
					}
				};
			break;
			case this.ele.hasClassName('onclick-updatebetalingstype'):
				this.onComplete = function() {
					if ($('dankortField')) {
						if (cls.selectedIndex == 1 && this.ele.value == 'dankort') {
							$('dankortField').show();
						} else {
							$('dankortField').hide();
						}
					}
				}
			break;
			default:
				this.onComplete = function(){};
			break;
		}
		
		this.ele.onchange = function() {
			cls.selectSelect(this.selectedIndex);
		}
		
		this.input = Builder.node('div',{className:'formSelect'});
		/*
		this.input.style.top = this.ele.offsetTop+'px';
		this.input.style.left = this.ele.offsetLeft+'px';
		*/
		this.ele.onfocus = function() {
			cls.selectDropdown();
		}
		
		
		var button = Builder.node('a',{href:'#',className:'formSelectButton'});
		button.onclick = function() {
			cls.selectDropdown();
			return false;
		}
		this.input.appendChild(button);
	
		this.label = Builder.node('a',{href:'#',className:'formSelectLabel'},this.ele.options[this.selectedIndex].text);
		this.label.onclick = function() {
			cls.selectDropdown();
			return false;
		}
		
		this.selected = Builder.node('div',this.label);
		this.input.appendChild(this.selected);
		
		// now for the options
		this.optionsHolder = Builder.node('div',{className:'formSelectOptions'});
		if (this.ele.options.length > 0) {
			var index = 0;
			for (var i = 0, l = this.ele.options.length; i < l; i++) {
				var tmpOption = this.ele.options[i];
				this.options[i] = {};
				this.options[i].node = Builder.node('a',{href:'#','index':index},tmpOption.text);
				this.options[i].text = tmpOption.text;
				this.options[i].value = tmpOption.value;
				this.options[i].node.onclick = function() {
					cls.selectSelect(this.getAttribute('index'));
					return false;
				}
				this.optionsHolder.appendChild(this.options[i].node);
				index++;
			}
		}
		
		this.input.appendChild(this.optionsHolder);
		$(this.optionsHolder).hide();
		this.selectStatus = 'hidden';
		this.ele.parentNode.insertBefore(this.input,this.ele);
		this.ele.functions = {};
		this.ele.functions = this;
	},
	
	selectTime: function(event) {
		var cls = this;
		var elt = $(Event.element(event)).up('.formSelect');
		if (elt != undefined && elt == this.input) {
			this.selectTimeoutSet = false;
			window.clearTimeout(this.selectTimeout);
			if (this.selectStatus == 'shown') {
				this.optionsHolder.show();
			}
		} else {
			if (this.selectStatus == 'hidden') {
				this.selectTimeoutSet = false;
				return;
			} else {
				if (this.selectTimeoutSet == false) {
					this.selectTimeout = window.setTimeout(function(){
						clearTimeout(cls.selectTimeout);
						cls.selectDropdown();
						this.selectTimeoutSet = false;
					},1000);
					this.selectTimeoutSet = true;
				}
			}
		}
	},
	
	selectDropdown: function() {
		var cls = this;
		if (this.selectStatus == 'shown') {
			window['testSelect']--;
			this.optionsHolder.hide();
			this.selectStatus = 'hidden';
			if(window['testSelect'] == 0) {
				Event.unloadCache();
			}
		} else {
			window['testSelect']++;
			Event.observe(document.body,'mouseover',function(event){
				cls.selectTime(event)
			});
			this.optionsHolder.show();
			this.selectStatus = 'shown';
		}
	},
	
	selectSelect: function(index) {
		this.selectedIndex = index;
		this.ele.selectedIndex = this.selectedIndex;
		this.optionsHolder.hide();
		this.selectStatus = 'hidden';
		// this works in IE
		var lbl = $(this.label);
		lbl.removeChild(lbl.firstChild);
		lbl.appendChild(document.createTextNode(this.options[index].text));
		
		/*
		try { // this doesnt work in IE
			var lbl = $(this.label);
			lbl.update(this.options[index].text);
		} catch(e) {
			alert(e); // this doesnt alert in IE
		}
		*/
		//lbl.innerHTML = this.options[index].text;
		this.onComplete();
	}
	
}