function BgSearch_char_encode(c)
{
	return '%' + c.charCodeAt(0).toString(16);
}

function BgSearch_url_encode(url)
{
	return encodeURIComponent(url).replace( /\%20/g, '+' ).replace( /[!'()*~]/g, BgSearch_char_encode );
}

function MobliBgSearch( params )
{
	this.removeBusy = function()
	{
		this.is_busy = false;
		var c_val = $(this.search_el).val();
		if( this.last_query && this.last_query != c_val )
		{
			this.proceedRequest(c_val);
		}
		else
		{
			this.last_query = '';
		}
	};	
	
	this.setBusy = function()
	{
		if(this.busy_timer_h)
		{
			clearTimeout(this.busy_timer_h);
		}
		this.is_busy = true;
		this.busy_timer_h = setTimeout(this.container_id + '.removeBusy()', 1000);
	};

	this.setLoadingIndicator = function()
	{
		$(this.res_el).show();
		
		if ($(this.res_el).attr('rel') != 'loaded') {
		
			$(this.res_el).html("<br />Loading...<br />");
			$(this.res_el).attr('rel','loaded');
	
		}
		//$('#srch_loading').html("<br />Loading...<br />");
	};

	this.proceedResponse = function(data)
	{
		if(data.length>2)
		{
			$(this.res_el).html(data);
			var cid = this.container_id;
			var use_val = this.use_val;
			$('#' + cid + ' .searchres_item a').click(function(){

					$('#' + cid + ' .srch_field').val( $(this).attr('rel') );
					$('#' + cid + ' .srch_field').addClass('returned');
					if(use_val)
					{
						$('#' + cid + ' .val_field').val( $(this).attr('value') );
	
					}
					$('#' + cid + ' .srch_res_atoms').hide('normal', function(){$('#' + cid + ' .srch_res_atoms').html('');});
				});
		}
		else
		{
			$(this.res_el).hide();
			$(this.res_el).html('');
		}
	};
	
	this.proceedRequest = function(query)
	{
		this.setBusy();
		this.last_query = query;

		var cid = this.container_id;
		$.ajax({
			type:'POST',
			dataType:'html',
			data:"q=" + BgSearch_url_encode(query) + this.p_query,
			success:function(data, textStatus){ eval(cid + '.proceedResponse(data);'); },
			beforeSend:function(XMLHttpRequest){ eval(cid + '.setLoadingIndicator();'); },
			url: this.request_url
		});		
	};
	
	// Init params
	this.entity          = typeof(params.entity) !='undefined' ? params.entity : 'all';
	this.min_len         = typeof(params.min_len) !='undefined' ? params.min_len : 2;
	this.use_link        = typeof(params.use_link) !='undefined' ? params.use_link : 1;
	this.use_val         = typeof(params.use_val) !='undefined' ? params.use_val : 0;
	this.db_col          = typeof(params.db_col) != 'undefined' ? params.db_col : '';
	this.container_id    = typeof(params.container_id) !='undefined' ? params.container_id : 'bg_search';
	this.container_class = typeof(params.container_class) !='undefined' ? params.container_class : 'bg_search';
	this.request_url     = typeof(params.request_url) !='undefined' ? params.request_url : '/module_search/search';
	this.is_busy         = false;
	this.busy_timer_h	 = null;
	this.last_query      = '';
	
	// Get elements
	this.container_el = $('#' + this.container_id);
	this.value_el = $('#' + this.container_id + ' .val_field');
	this.search_el = $('#' + this.container_id + ' .srch_field');
	this.res_el = $('#' + this.container_id + ' .srch_res_atoms');
	$(this.res_el).hide()
	this.p_query = '&entity=' + this.entity + '&use_link=' + this.use_link + '&use_val=' + this.use_val + '&col=' + this.db_col;
	
	// Bind functionality
	var minlen = this.min_len;
	var cid = this.container_id;
	var url = this.request_url;
	$(this.search_el).keyup(function(){ 
		
			var val = $(this).val();
			eval('var is_busy = ' + cid + '.is_busy;');
			if( val.length >= minlen && !is_busy)
			{
				eval(cid + '.proceedRequest("' + val.replace('"', '\"') + '");');
			}
			else
			{
				if( val.length < minlen)
				{
					$('#' + cid + ' .srch_res_atoms').hide();
					$('#' + cid + ' .srch_res_atoms').html('');
					eval(cid + '.last_query="";');
					eval(cid + '.is_busy=false;');
					eval('clearTimeout(' + cid + '.busy_timer_h);');
					
				}
				else
				{
					eval(cid + '.setBusy();');
				}
			}
		});
}

///////////////////////////////////

