var count_li_next = 2;
var count_li_prev = 0;
var count_li_selected = 0;
var count_new_li = 0;

function ReplaceAll(Source,stringToFind,stringToReplace){

  var temp = Source;

    var index = temp.indexOf(stringToFind);

        while(index != -1){

            temp = temp.replace(stringToFind,stringToReplace);

            index = temp.indexOf(stringToFind);

        }

        return temp;

}

function update_auto_complete(element, evt, prefix) {
	if((evt.keyCode>=48 && evt.keyCode<=90) || (evt.keyCode>=96 && evt.keyCode<=111) || evt.keyCode==38 || evt.keyCode==40 || evt.keyCode==8 || evt.keyCode==46) {
		
		count_li_next = 2;
		count_li_prev = 0;
		count_li_selected = 0;
		count_new_li = 0;
		
		//if(jQuery(element).val!='') {
		if(jQuery(element).val().length>=1) {
			if((evt.keyCode == 40 || evt.keyCode == 38)) {
				if(jQuery('.'+prefix+'_li_search_list').length>0) {
					jQuery('.'+prefix+'_li_search_list').each(function() {
						if(jQuery(this).is('.highlight') && count_li_selected==0) {
							count_li_selected=1;
							jQuery(this).removeClass('highlight');
							if(evt.keyCode == 40) {
								jQuery('#'+prefix+'_search_li_'+count_li_next).addClass('highlight');
								count_new_li = count_li_next;
							} else {
								if(count_li_prev > 0) {
									jQuery('#'+prefix+'_search_li_'+count_li_prev).addClass('highlight');
									count_new_li = count_li_prev;
								} else {
									jQuery('.'+prefix+'_search_inner_container ul li:last').addClass('highlight');
									count_new_li = jQuery('.'+prefix+'_li_search_list').length;
								}
							}
						}
						count_li_next++;
						count_li_prev++;
					});
					if(!jQuery('#'+prefix+'_search_li_'+count_new_li+' .name').length) {
						count_li_selected=0;
						if(jQuery('.'+prefix+'_search_inner_container ul li:first .name').length) {
							var str = ReplaceAll(jQuery('.'+prefix+'_search_inner_container ul li:first .name').html(), '<b>', '');
							str = ReplaceAll(str, '</b>', '');
							str = ReplaceAll(str, '<B>', '');
							str = ReplaceAll(str, '</B>', '');
							jQuery('#'+prefix+'_search_text').val( str );
						}
					} else {
						var str = ReplaceAll(jQuery('#'+prefix+'_search_li_'+count_new_li+' .name').html(), '<b>', '');
							str = ReplaceAll(str, '</b>', '');
							str = ReplaceAll(str, '<B>', '');
							str = ReplaceAll(str, '</B>', '');
						jQuery('#'+prefix+'_search_text').val( str );
					}
					if(count_li_selected == 0) {
						jQuery('.'+prefix+'_search_inner_container ul li:first').addClass('highlight');
						count_li_selected = 1;
						count_new_li = count_li_selected;
					}
				}
			} else {
				if(window.SearchStartTimerIsOn==1) {
					clearTimeout(window.SearchStartTimer);
				}
		  		window.AllowAutoCompleteFlag = 0;
		  		window.SearchStartTimer = setTimeout('AllowAutoComplete()', 499);
		  		window.SearchStartTimerIsOn = 1;
		  		setTimeout(function() { CheckAutoComplete(jQuery(element).val(), prefix); }, 500);
			}
		} else {
			jQuery('#'+prefix+'_search_list_container').css('display','none');
		}
		
	}
	
}

function AllowAutoComplete() {
	window.AllowAutoCompleteFlag = 1;
}

function CheckAutoComplete(search_text, prefix) {
	if(window.AllowAutoCompleteFlag==1) {
		ExecuteAutoComplete(search_text, prefix);
	}
}

function ExecuteAutoComplete(search_text, prefix) {
	jQuery.get("/includes/search/search_ajax.php",{ action:'get_auto_complete', search_text: search_text, form_prefix:prefix } ,function(data) {
				if(data!='') {
		  			jQuery('#'+prefix+'_search_list_container .inner').html(data);
		  			jQuery('#'+prefix+'_search_list_container').css('display','block');
				} else {
					jQuery('#'+prefix+'_search_list_container').css('display','none');
					jQuery('.'+prefix+'_li_search_list').html('');
				}
	   });
}

function add_search_events(prefix) {
	
	jQuery('#'+prefix+'_ul_search_list li').each(function() {
		//alert('asdasd');
		// Add onClick, blur, onmouseover, onmouseout  to each li
		jQuery(this).click(function() {
			var str = ReplaceAll(jQuery(this).find('.name').html(), '<b>', '');
				str = ReplaceAll(str, '</b>', '');
				str = ReplaceAll(str, '<B>', '');
				str = ReplaceAll(str, '</B>', '');
			jQuery('#'+prefix+'_search_text').val( str );
			jQuery('form[name=\''+prefix+'_search\']').submit();
		}).blur(function() {
			jQuery(this).removeClass('highlight');
		}).mouseover(function() {
			var str = ReplaceAll(jQuery(this).find('.name').html(), '<b>', '');
				str = ReplaceAll(str, '</b>', '');
				str = ReplaceAll(str, '<B>', '');
				str = ReplaceAll(str, '</B>', '');
			jQuery('#'+prefix+'_search_text').val( str );
			//jQuery('#'+prefix+'_search').attr('action',jQuery('#'+jQuery(this).attr('id')+' .link').html());
			jQuery(this).addClass('highlight');
		}).mouseout(function() {
			jQuery(this).removeClass('highlight');
		});
		
	});
	
}
