var SelectObject = Class.create();

SelectObject.prototype = {
	
	initialize: function (container,heightMax) {
		this.container = container;
		this.heightMax = heightMax;
		this.datas = [];
		this.main = this.container.getElementsByTagName("div")[0];
		this.mask = this.container.getElementsByTagName("div")[2];
		this.content = this.mask.getElementsByTagName("div")[0];
		this.hidden = this.container.getElementsByTagName("input")[0];
		this.scrollItems = this.container.getElementsByTagName("div")[4];
		this.onChangeFunction = this.currentKey = this.keyItemSelected = this.itemMouseOver = "";
		this.isOpen = this.closeOnClick = this.keyActive = this.changeFunctionActive = false;
		this.keyIndex = {};
		this.openIndex = this.keyCntCheck = 0;
	},
   
	add: function (o) {
		this.addKeyIndex(o.label.charAt(0).toLowerCase(),this.datas.length);
		this.datas[this.datas.length] = o;
		this.index = 0;
	},
	
	addKeyIndex: function (key,index) {
		if (!this.keyIndex[key]) this.keyIndex[key] = {index:[]};
		this.keyIndex[key].index[this.keyIndex[key].index.length] = index;
	},
	
	addOnChangeEvent: function (f) {
		this.onChangeFunction = f;
	},
	
	init: function (scroller) {
		
		this.scroller = scroller;
		this.hidden.value = this.getSelectedData();
		this.main.getElementsByTagName("div")[0].innerHTML = this.getSelectedLabel();
		
		var str = "";
		for (var i=0;i<this.datas.length;i++) str += '<div id="'+i+''+this.container.id+'"><a href="#0">'+this.datas[i].label+'</a></div>';
		this.content.innerHTML = str;
		
		var tmp = this.scroller.objs;
		if (tmp.container.offsetHeight<this.heightMax) tmp.mask.style.height =  tmp.mask.style.clip.bottom = tmp.container.offsetHeight + "px";
		tmp.scDn.style.top = tmp.mask.offsetHeight + tmp.mask.offsetTop - 7 + "px";
		
		this.setEvents();
		this.setSelectedIndex(this.index);
		this.scroller.init();
		
	},
	
	setEvents: function () {
		
		var tmp = this.content.getElementsByTagName("a");
		
		for (var i=0;i<tmp.length;i++) {
			tmp[i].className = "s"+i;
			Event.observe(tmp[i], "click", this.onItemClickEvents.bindAsEventListener(this));
			Event.observe(tmp[i], "mouseover", this.onItemOverEvents.bindAsEventListener(this));
			Event.observe(tmp[i], "mouseout", this.onItemOutEvents.bindAsEventListener(this));
		}
		
		Event.observe(this.main, "click", this.onMainClick.bindAsEventListener(this));
		Event.observe(this.main, "mouseover", this.onMainOver.bindAsEventListener(this));
		Event.observe(this.main, "mouseout", this.onMainOut.bindAsEventListener(this));
		Event.observe(this.main, this.scroller.mouseWheelEvent, this.onMainMouseWheel.bindAsEventListener(this));
		
		Event.observe(this.container, "mouseover", this.onContainerMouseOver.bindAsEventListener(this));
		Event.observe(this.container, "mouseout", this.onContainerMouseOut.bindAsEventListener(this));
		
		Event.observe(document, "mouseup", this.checkToClose.bindAsEventListener(this));
		Event.observe(document, "keypress", this.setSelectedKeyboardIndex.bindAsEventListener(this));
		
	},
	
	onItemClickEvents: function (e) {
		this.content.style.visibility = this.scrollItems.style.visibility = this.mask.style.visibility = "hidden";
		this.isOpen = this.closeOnClick = this.keyActive = false;
		this.setSelectedIndex(parseInt(Event.element(e).parentNode.id)); 
		this.scroller.reset();
	},
	onItemOverEvents: function (e) {
		if (this.itemMouseOver!="") Element.removeClassName($(this.keyItemSelected), "on");
		this.itemMouseOver = Event.element(e);
	},
	onItemOutEvents: function () {
		this.itemMouseOver = "";
	},
	
	onMainClick: function () {
		this.showHideDatas();
		if (this.scroller.scrollingVertical) {
			this.scroller.reset();
			this.scroller.moveToAnchor(this.index+this.container.id);
		}
	},
	onMainOver: function () {
		this.keyActive = true;
	},
	onMainOut: function () {
		if (!this.isOpen) this.keyActive = false;
	},
	onMainMouseWheel: function (e) {
		(Event.wheel(e)==-1) ?this.setSelectedIndex(this.index+1) :this.setSelectedIndex(this.index-1);
	},
	
	onContainerMouseOver: function () {
		if (this.isOpen) { this.keyActive = true; this.closeOnClick = false; }
	},
	onContainerMouseOut: function () {
		if (this.isOpen) { this.keyActive = false; this.closeOnClick = true; }
	},
	
	showHideDatas: function () {
		this.content.style.visibility = this.mask.style.visibility = (!this.isOpen) ?"visible" :"hidden";
		if (this.scroller.scrollingVertical) this.scrollItems.style.visibility = (!this.isOpen) ?"visible" :"hidden";
		this.isOpen = this.closeOnClick = keyActive = (this.mask.style.visibility=="hidden") ?false :true;
		if (this.isOpen) this.openIndex = this.index;
		this.itemMouseOver = "";
		if (this.onChangeFunction!=""&&this.index!=this.openIndex) eval(this.onChangeFunction)();
	},
	
	getSelectedData: function () {
		return this.datas[this.index].data;
	},
	
	getSelectedLabel: function () {
		return this.datas[this.index].label;
	},
	
	setSelectedIndex: function (index) {
		if (index>=0&&index<this.datas.length) {
			Element.removeClassName(this.content.getElementsByTagName("div")[this.index], "on");
			this.index = index;
			this.hidden.value = this.getSelectedData();
			this.main.getElementsByTagName("div")[0].innerHTML = this.getSelectedLabel();
			Element.addClassName(this.content.getElementsByTagName("div")[index], "on");
			if (this.onChangeFunction!=""&&!this.isOpen&&this.changeFunctionActive) eval(this.onChangeFunction)();
			if (!this.changeFunctionActive) this.changeFunctionActive = true;
		}
	},
	
	setSelectedData : function (data) {
		this.changeFunctionActive = false;
		for (var i=0;i<this.datas.length;i++) if (this.datas[i].data==data) this.setSelectedIndex(i);
	},
	
	checkToClose: function () {
		if (this.closeOnClick&&this.isOpen) this.showHideDatas();
	},
	
	setSelectedKeyboardIndex: function (e) {

		var tmp = (window.event) ?String.fromCharCode(e.keyCode).toLowerCase() :String.fromCharCode(e.which).toLowerCase();
		
		if (e.keyCode==13&&this.isOpen) {
			if (this.itemMouseOver!="") this.setSelectedIndex(parseInt(this.itemMouseOver.parentNode.id));
			this.showHideDatas();
		}
		
		if (this.keyActive) {
			
			if (this.keyIndex[tmp]) {
				
				this.keyCntCheck =  (this.currentKey==tmp) ?(this.keyCntCheck<this.keyIndex[tmp].index.length-1) ?this.keyCntCheck + 1 :0 :0;
				
				if (this.keyItemSelected!="") Element.removeClassName($(this.keyItemSelected), "on");
				Element.removeClassName(this.content.getElementsByTagName("div")[this.index], "on");
				
				this.keyItemSelected = this.keyIndex[tmp].index[this.keyCntCheck]+this.container.id;
				Element.addClassName($(this.keyItemSelected), "on");
				
				this.scroller.moveToAnchor(this.keyItemSelected);
				this.currentKey = tmp;
				this.itemMouseOver = $(this.keyItemSelected).childNodes(0);
				this.setSelectedIndex(this.keyIndex[tmp].index[this.keyCntCheck]);
			
			}
		
			if (e.keyCode==38&&this.index>0) this.setSelectedIndex(this.index-1);
			else if (e.keyCode==40&&this.index<this.datas.length-1) this.setSelectedIndex(this.index+1);
			
		}
		
	}
	
}