

/*
 *
 * JS needed for search widget that does live search
 *
 * requires prototype
 *
 * Works for system:grouped search UI
 * although it can work for any UI with a 
 * q[mode]=comp
 *
 *
*/



function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


var search_timer_id;
var search_timer_on = false;
var search_wait_time = 350;
var search_min_chars = 3;

function search_set_timer()
{
	if (search_timer_on) {
		clearTimeout(search_timer_id);
	}

	search_timer_on = true;
	search_timer_id = setTimeout("search_complete()", search_wait_time);

}

function search_complete()
{
	var search_text = $('live_search_text').value.replace(/^\s+/,""); //ltrim

	var div_name = 'live_search_results';
	var comp_div = $(div_name);

	if (search_text.length >= search_min_chars) {

		$('live_search_text').classNames().add('waiting');
		var search_url = $('live_search_form').action;
		var url =  search_url + '?q[js]=1&q[mode]=comp&q[text]='+URLEncode(search_text)

		var myAjax = new Ajax.Updater(div_name, url, 
					{
						method: 'get',
						onSuccess: function(transport) {
								// if rtrim(text).length > 1
								
								if ($('live_search_text').value.replace(/^\s+/,"").length > 1 ) {
								
									if ((typeof live_search_on_open) == 'function') 
										live_search_on_open();

									comp_div.style.display = 'block';

									$('live_search_text').classNames().remove('waiting');
								}
							},
							'parameters':'asAjax=Y'
					});
	}
	else if (search_text.length == 0 ){
		comp_div.style.display = 'none';
	}

}

function ls_cancel_empty_submit(evt)
{
	var keyCode = evt.which ? evt.which : evt.keyCode;

	box = $('live_search_text');

	if  (box.value=='' && evt.keyCode == 13)
		evt.stop();
}

function search_initialize()
{
	try {
		var search_url = $('live_search_form').action;
		$('live_search_js').value = '1';
	 
		box = $('live_search_text');
		Event.observe(box, 'keyup', search_set_timer);
	
		Event.observe(box, 'keypress', ls_cancel_empty_submit);

		form = $('live_search_form');
		form.method = 'GET';
	}
	catch (err) {}
}


function close_live_search()
{
	
	search_comp = $('live_search_results');
	search_comp.style.display='none';
	search_comp.innerHTML='';

	if ((typeof live_search_on_close) == 'function') 
		live_search_on_close();
}

// instead call search_initialize(); right after search textbox.
//Event.observe(window, 'load', search_initialize, false);
