function SelectBox(params) {
	this.setup(params);
}

SelectBox.prototype = {
	
	id: null,
	handle: null,
	container: null,
	updateContainer: null,
	div: null,
	values: [],

	params: null,
	
	setup: function(params) {
		this.id              = params.source;
		this.handle          = params.handle;
		this.container       = params.container;
		this.updateContainer = params.update;
		this.params          = params;

		this.parseSelectBox();
		this.assignEvents();
		this.build();
	},

	toggleSelectBox: function() {
		if (this.div.style.display.toLowerCase() == 'none') {
			this.openSelectBox();
		} else {
			this.closeSelectBox();
		}
	},
	
	openSelectBox: function() {
		this.updateSelectBox();
		var coords = this.findPlace(document.getElementById(this.container));
		this.div.style.left = coords.x + 'px';
		this.div.style.top  = coords.y + 'px';
		this.div.style.display = 'block';
	},

	closeSelectBox: function() {
		this.div.style.display = 'none';
	},
	
	assignEvents: function() {
		var self = this;
		document.getElementById(this.handle).onclick = function() {
			self.toggleSelectBox();
		};
	},
	
	update: function(text) {
		var elem = document.getElementById(this.updateContainer);
		if (typeof elem.innerHTML != 'undefined') {
			elem.innerHTML = text;
		} else {
			elem.value = text;
		}
	},
	
	parseSelectBox: function() {
		var element = document.getElementById(this.id);
		this.values.length = 0;
		for (var i=0; i<element.options.length; i++) {
			this.values[i] = {
				value:    element.options[i].value,
				text:     element.options[i].text,
				selected: element.selectedIndex == i,

				toString: function() {
					return 'value: ' + this.value + ', text: ' + this.text + ', selected: ' + this.selected;
				}
			}
		}
	},

	updateSelectBox: function() {
		var self = this;
		this.parseSelectBox();
		this.div.innerHTML = '';
		for (var i=0; i<this.values.length; i++) {
			var content = document.createElement('div');
			
			content.innerHTML = self.values[i].text;
			content.value     = this.values[i].value;
			content.className = 'selectBoxOption';
			
			content.onclick = function() {
				document.getElementById(self.id).value = this.value;
				self.update(this.innerHTML);
				self.closeSelectBox();
				if (self.params.onchange) {
					self.params.onchange(this.value, this.innerHTML);
				}
			}
			this.div.appendChild(content);
		}
	},
	
	build: function() {
		var self = this;
		var close = function() {
			self.closeSelectBox();
		}
		
		var div = document.createElement('div');
		div.style.display = 'none';
		document.getElementById(this.container).appendChild(div);
		this.div = div;
		div.className = 'selectBoxContainer';
		this.updateSelectBox();
		this.update(this.values[0].text);
		
		if (navigator.appName == 'Microsoft Internet Explorer') {
			document.attachEvent('onclick', close);
			document.getElementById(this.handle).attachEvent('onclick', self.stopEvent);
		} else {
			document.addEventListener('click', close, false);
			document.getElementById(this.handle).addEventListener('click', self.stopEvent, false);
		}
	},
	
	findPlace: function(element) {
		var x = 0;
		var y = 0;

		if (navigator.appVersion.indexOf('MSIE 6.0') == 17) {
			document.getElementById(this.container).style.overflow = 'hidden';
		}

		if (this.params.leftCorrection) {
			x += this.params.leftCorrection;
		}

		return {
			x: x,
			y: y
		}
	},
	
	stopEvent: function(ev) {
		ev || (ev = window.event);
		if (navigator.appName == 'Microsoft Internet Explorer') {
			ev.cancelBubble = true;
			ev.returnValue = false;
		} else {
			ev.preventDefault();
			ev.stopPropagation();
		}
	}
}

