/*selectboxes.js*/
var MAG=Object.extend({_all_selects:[],pushSelect:function(a){this._all_selects.push(a)},repaintAllSelects:function(){for(i=0;i<this._all_selects.length;i++){this._all_selects[i].setArrowPosition()}}},MAG||{});

Autocompleter.SelectBox = Class.create();
Autocompleter.SelectBox.prototype = Object.extend(new Autocompleter.Base(), {

	_on_over: false,
	_custom_arrow: null,

	initialize: function(select, options) {

	if(select.onchange) this._onchange = select.onchange;
	var registry = Element.retrieve($(select), 'prototype_event_registry');
	if(registry)
	{
		var self = this;
		registry.each(function(arguments){
			self["sel_"+arguments[0]] = arguments[1];
		});
	}
	MAG["select_"+$(select).id] = this;
	MAG.pushSelect(this);

	var inputClasses = Element.classNames(select);
	this.element = new Element('input', {
		id: $(select).id + '_combo',
		type: 'text'
	});
	this.element.addClassName(inputClasses);

	new Insertion.Before(select, this.element);

	this.update = "<div id=\"" + $(select).id + "_options\" class=\"auto_complete\"></div>"
	new Insertion.Before(select, this.update);

    this.baseInitialize($(select).id + "_combo", $(select).id + "_options", options);
    this.tokenBounds = [-1, 0];
    this.select = select;

	$(this.element.id).setAttribute('readonly','readonly');
	this.element.readOnly = true;

	Element.hide(select);
	Element.addClassName(this.element.id, this.options.css);

	this.setSelectOptions();

	this._custom_arrow = new Element('ins');
	this._custom_arrow.addClassName('combo_arrow');
	new Insertion.After(select, this._custom_arrow);
	this.setArrowPosition();

	Event.observe(this.element, "click", this.activate.bindAsEventListener(this));
	Event.observe(this._custom_arrow, "click", this.activate.bindAsEventListener(this));

	if ($(select).selectedIndex >= 0) this.element.value = $(select).options[$(select).selectedIndex].innerHTML;

	var self = this;
	this.options.afterUpdateElement = function(text, li) {
		if(li.hasClassName('optgroup')) return;
		var optionList = $(select).getElementsByTagName('option');
		var nodes = $A(optionList);

		var opt = nodes.find( function(node){
			return (node.value == li.id);
		});
		$(select).selectedIndex=opt.index;

		if(self.sel_change) {
			self.sel_change.each(function(f){
				f.handler.call($(select));
			});
		}

		if(Object.isFunction(self._onchange)) {
			self._onchange.bind(self.select).call();
		}
	}

	Event.observe(this._custom_arrow, 'mouseover', function(){this._on_over=true;}.bind(this));
	Event.observe(this._custom_arrow, 'mousemove', function(){this._on_over=true;}.bind(this));
	Event.observe(this._custom_arrow, 'mouseout', function(){this._on_over=false;this.element.focus();}.bind(this));
	Event.observe(this.update, 'mouseover', function(){this._on_over=true;}.bind(this));
	Event.observe(this.update, 'mousemove', function(){this._on_over=true;}.bind(this));
    Event.observe(this.update, 'mouseout', function(){this._on_over=false;this.element.focus();}.bind(this));
  },

  setSelectOptions: function()
  {
		this.selectOptions = [];
		var optionList = $(this.select).getElementsByTagName('option');
		var nodes = $A(optionList);
		var tmp_parent = null;
		for(i=0; i<nodes.length;i++){
			var parent_element = nodes[i].parentNode;
			if(parent_element != tmp_parent) {
				tmp_parent = parent_element;
				if(parent_element.tagName === 'OPTGROUP') {
					this.selectOptions.push("<li class=\"optgroup\">" + parent_element.getAttribute('label') + '</li>');
					var html_class = 'optgroup_option';
				} else {
					var html_class = 'option';
				}
			}
			this.selectOptions.push("<li class=\""+html_class+"\" id=\"" + nodes[i].value + "\">" + nodes[i].innerHTML + '</li>');
			if (nodes[i].getAttribute("selected")) this.element.value = nodes[i].innerHTML;
		}
  },

  getUpdatedChoices: function() {
  		this.updateChoices(this.setValues());
  },

  setValues : function(){
		return ("<ul>" + this.selectOptions.join('') + "</ul>");
  },

  setOptions: function(options) {
    this.options = Object.extend({
		css			: 'combo'
	}, options || {});
  },

  reSet: function()
  {
  	this.setSelectOptions();
  	$(this.update).update(this.setValues());
  	if($(this.select).selectedIndex >= 0) this.element.value = $(this.select).options[$(this.select).selectedIndex].innerHTML;
  	else this.element.value = '';
  	MAG.repaintAllSelects();
  },

  onClick: function(event) {
  	var element = Event.findElement(event, 'LI');
  	if(element.hasClassName('optgroup')) return;
    this.index = element.autocompleteIndex;
    this.selectEntry();
    this.hide();
  },

  onBlur: function(event) {
  	if(this._on_over===true) return;

    setTimeout(this.hide.bind(this), 250);
    this.hasFocus = false;
    this.active = false;
  },

  setArrowPosition: function()
  {
  	Position.clone(this.element, this._custom_arrow, {
		setWidth: false,
		setHeight: false,
		offsetTop: 1,
		offsetLeft: this.element.offsetWidth - 20
	});
  }
});
Event.observe(window,'load',function(e){var c={onShow:function(a,b){if(!b.style.position||b.style.position=='absolute'){b.style.position='absolute';Position.clone(a,b,{setHeight:false,offsetTop:a.offsetHeight})}b.style.width=a.offsetWidth-2+'px';Effect.Appear(b,{duration:0.15})}};$$('select').each(function(a){new Autocompleter.SelectBox(a,c)})});
/*
CSS Browser Selector v0.3.4 (Sep 29, 2009)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',o='opera',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
