var CoolSelect = Class.eventClass();
Object.extend (CoolSelect.prototype	, {
	name: "",
	value: "",
	initialize: function(f_cJ, f_ar, f_q, f_bn) {
		this.id = f_cJ;
		this.name =(f_bn.name)?f_bn.name:this.id;
		this.f_bt = [];
		this.f_I = TrimPath.parseTemplate(f_ar);
		this.f_ae = document.createElement("select");
		this.f_ae.style.display = "none";
		this.f_ae.name = this.name;
		this.f_q = $(f_q);
		this.f_q.appendChild(this.f_ae);
		this.f_aB = $(document.createElement("div"));
		
		this.f_q.appendChild(this.f_aB);
		Event.observe(document,"click",this.f_fU.bind(this));
		this.f_eo();
		if (f_bn.options) this.add(f_bn.options);
	},
	add: function(f_bt) {
		$A(f_bt).each(function (f_gO) {
				this.f_ae.options.add(new Option(f_gO[0],f_gO[1],f_gO[2]));
				this.f_bt.push(f_gO);
			}.bind(this));
		this.value = this.f_ae.value;
		this.f_eo();
	},
	f_eo: function() {
		var f_A = this.f_I.process({options:this.f_ae.options, itemArray: $A($R(0,this.f_ae.options.length-1)), originalData: this.f_bt});
		this.f_aB.innerHTML = f_A;
		this.f_aB.getElementsBySelector("li").each(function (f_aX, index) {
				if (!this.f_bt[index][3]) {				
					Event.observe(f_aX, "mouseover" , this.f_az.bindAsEventListener(this,false,f_aX));
				  Event.observe(f_aX, "click" , this.f_fv.bindAsEventListener(this,f_aX.readAttribute("key")));
				  if (f_aX.hasClassName("onhover")) this.f_r = f_aX;
				}
		}.bind(this));
		this.f_aB.getElementsBySelector("[use='coolSelectTitle']").each(function (f_e) {
			Event.observe(f_e, "click" , this.f_go.bindAsEventListener(this,true));
		}.bind(this));
		this.f_aB.getElementsBySelector("[use='coolSelectList']").each(function (f_e) {
			Element.hide(f_e);
			Event.observe(f_e, "click" , this.f_go.bindAsEventListener(this,true));
		}.bind(this));
	},
	f_go:function(event) {
		Event.stop(event);
		this.f_aB.getElementsBySelector("[use='coolSelectList']").each(function (f_e) {
			Element.toggle(f_e);
		});
	},
	f_fv: function(event, f_i, f_de) {
		if (f_i==this.value) return;
		$A(this.f_ae.options).each(function (f_ha, f_aw) {
			if (f_ha.value == f_i) {
				this.value = f_i;
				this.f_ae.options.selectedIndex = f_aw;
			}
		}.bind(this));
		this.f_eo();
		if (!f_de) this.fireEvent("change");
	},
	setValue: function(f_i, f_de) {
		this.f_fv(null, f_i ,f_de);
	},
	f_az: function(event, f_P, f_aR) {
		var f_e;
		if (event && !f_P) { 
			Event.stop(event); 
			f_e = f_aR; 
		}	else if (f_P == "next") f_e = this.f_r.next();
		else if (f_P == "prev") f_e = this.f_r.previous(); 
		else if (typeof(f_P) == "object") f_e = f_P;
		if (typeof(f_e) == "undefined" || f_e==null) {
				var f_cI = this.f_r.up().immediateDescendants();
				f_e = f_cI[(f_P=="prev")?(f_cI.length - 1):0];
		} 	
		this.f_aY();
		this.f_r = f_e;
		if (f_e.readAttribute("type") == "1" || f_e.readAttribute("type") == "2") {
			this.f_az(event, (f_P=="prev")?"prev":"next");
			return;
		}	else if (f_e.readAttribute("type") == "0") {
			this.f_r = false;
		} else {
			f_e.addClassName("onhover");
		}
	},
	f_cg: function(event, f_P) {
		var f_e;
		if (f_P) f_e = f_P;
		else var f_e = Event.element(event);
		f_e.removeClassName("onhover");
	},
	f_aY: function() {
		if(this.f_r) this.f_cg(false, this.f_r);
	},
	f_fU: function() {
		this.f_aB.getElementsBySelector("[use='coolSelectList']").each(function (f_e) {
			Element.hide(f_e);
		});
	}
});