jQuery(document).ready(function()
{
	var jSet = false;

	jQuery('#textbox').focus(function()
	{
		if(jQuery.browser.msie)
		{
			jSet = true;
		}
	});
	jQuery('#results').focus(function()
	{
		if(jQuery.browser.msie)
		{
			jSet = true;
		}
	});
	jQuery('body').click(function()
	{

		if(jSet && (document.activeElement.id != "tsearch"))
		{

			jSet = false;
			setTimeout(this.hide.bind(document.getElementById("results")), 250);
       			document.getElementById("results").hasFocus = false;
        		document.getElementById("results").active = false;      		
		}
		else
		{

		}
	});
});

function jSetFunc(text)
{
	jSet = true;
}


var ComboBox = Class.create();

ComboBox.Autocompleter = Autocompleter.Local;

ComboBox.Autocompleter.prototype.onBlur = function(event) {
	if (Element.getStyle(this.update, 'display') == 'none') { return; }
	/*setTimeout(this.hide.bind(this), 250);
	this.hasFocus = false;
	this.active = false;*/
	

	if (Prototype.Browser.IE)
	{
		/*if ( element != null )*/
		/*if(document.activeElement.id != "results") {	
		setTimeout(this.hide.bind(this), 250);
		this.hasFocus = false;
		this.active = false;
		}*/
	}
	else
	{
		
		setTimeout(this.hide.bind(this), 250);
       		 this.hasFocus = false;
       		 this.active = false;
	}
}


ComboBox.prototype = {
	initialize: function(textElement, resultsElement, array, arrow) {
		this.textElement = $(textElement);
        
        // the first text box inside the container
        this.textBox = $A( this.textElement.getElementsByTagName('INPUT') ).findAll( function(input) {
            return (input.getAttribute('type') == 'text');
        })[0];
	
        this.results = $(resultsElement);
//        this.resultsO= this.results.innerHTML;
        this.textBox.paddingRight = '20px';
        this.textElement.style.width = '347px';
        this.textElement.style.position = 'relative';

		// we dynamically insert a SPAN that will serve as the drop-down 'arrow'
		this.arrow = arrow;
		this.textElement.appendChild(this.arrow);
	    this.array = array;

		this.results.style.display 	= 'none';
		
		this.events = {
			showChoices: 	    this.showChoices.bindAsEventListener(this),
			hideChoices: 	    this.hideChoices.bindAsEventListener(this),
			click:				this.click.bindAsEventListener(this),
			keyDown:			this.keyDown.bindAsEventListener(this)
		}
		
		this.autocompleter = new ComboBox.Autocompleter(this.textBox, this.results, this.array, this.arrow);
				
		Event.observe(this.arrow, 'click', this.events.click);
		Event.observe(this.textBox, 'keydown', this.events.keyDown);
	},
	
	getAllChoices: function(e) {
		var choices = this.array.collect( function(choice) { return '<li>' + choice + '</li>'; } );
		var html = '<ul>' + choices.join('') + '</ul>';
//		var html = this.resultsO;
		this.autocompleter.updateChoices(html);
	},
	
	keyDown: function(e) {
		if (e.keyCode == Event.KEY_DOWN && this.choicesVisible() ) {
			this.showChoices();
		}
	},
	
	// returns boolean indicating whether the choices are displayed
	choicesVisible: function() { return (Element.getStyle(this.autocompleter.update, 'display') == 'none'); },
	
	click: function() {
		if (this.choicesVisible() ) {
			this.showChoices();
		} else {
			this.hideChoices();
		}

	},
		
	showChoices: function() {
		this.textBox.focus();
	    this.autocompleter.changed = false;
	    this.autocompleter.hasFocus = true;
	    this.getAllChoices();
	},
	
	hideChoices: function() {
		this.autocompleter.onBlur();
	}
}
