  $( function() {

	
		function remove_json_characters(stringfy){
					
					stringfy.replace(/\\n/g, "\\n")
							.replace(/\\'/g, "\\'")
							.replace(/\\"/g, '\\"')
							.replace(/\\&/g, "\\&")
							.replace(/\\r/g, "\\r")
							.replace(/\\t/g, "\\t")
							.replace(/\\b/g, "\\b")
							.replace(/\\f/g, "\\f");
						
					return stringfy;
					
		}
		
		data='';

		var request;
		var current_search_selected=-1;
		
		$("body").click(function(){
			//$("#search_response_list").html('');
			
			
		});
	
		$("#search_box").keyup(function(e) {
			$('#search_status').attr('class','fa fa-spinner fa-spin');
			if(e.keyCode != 32 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 &&  e.keyCode != 40 && e.keyCode != 27 && e.keyCode != 13  ){
				if (request) {request.abort();}
				 current_search_selected=-1;
				
				$("#search_response_list").html('');
				
				search_str=$(this).val();
				//alert(search_str);
				//search_str=search_str.replace(/[^a-z0-9\s]/gi, '');
				//alert(search_str);
				if(search_str !='-' && search_str !='' ){
					
					
					data={
						'search':search_str,
						'gov':$("#search_gov").val(),
						'city':$("#search_city").val()
					};
					
					request=$.ajax({
					  type: "POST",
					  url: 'search_txt.php',
					  data: remove_json_characters(JSON.stringify(data)),
					  success: function(response){
						  
						  if(response.response_num=='0'){
							//alert('لا يوجد نتائج');  
						  }else if(response.status=='done'){

							//xx=JSON.stringify(response.selected);
							
							for(i=0;i<response.response_num;i++){
								item=response.selected[i];
								$("#search_response_list").append( "<a class='search_link' href='"+item[5]+"'><img class='search_img' src='"+item[4]+"'><span class='search_text'>"+item[0]+"<br>"+item[1]+"<span></a>" ) ;
							}
							
							//console.log(response.selected[$i][0]);
						  
						  }
						  
						  $('#search_status').attr('class','fa fa-search');
						  
					  },
					  faild:function(response){
						  
						  $('#search_status').attr('class','fa fa-search');
  
					  },
					  
					  contentType: 'application/json; charset=utf-8',
					  async: true,
					  cache: false
					});
				
				}else{
					$('#search_status').attr('class','fa fa-search');
				}
			}
			
			if(e.keyCode == 27){
				$("#search_response_list").html('');
				$('#search_status').attr('class','fa fa-search');
			}

			if(e.keyCode == 13){
				//press enter
				e.preventDefault();
				search_link=$('.search_link.active').attr("href");
				window.location.href = search_link;
				$('#search_status').attr('class','fa fa-search');
			}
		
		

			if(e.keyCode == 40){
				//down
				
				
				//alert($('#search_response_list').find('li:first').html());
				//$('#search_response_list').focus();
				$('.search_link').removeClass('active');
				active=$('.search_link').eq(parseInt(current_search_selected)+1);
				active.addClass('active');
				$( "#search_response_list" ).scrollTop( active.position().top);
				//alert(active.position().top);
				current_search_selected++;
				
			}
			
			if(e.keyCode == 38){
				//up
				//alert($('#search_response_list').find('li:first').html());
				//$('#search_response_list').focus();
				if(current_search_selected>-1){
					$('.search_link').removeClass('active');
					active=$('.search_link').eq(parseInt(current_search_selected)-1);
					active.addClass('active');
					$("#search_response_list").scrollTop( active.position().top);
					current_search_selected--;
				}
			}
		});
	
	
		$('#search_response_list li').keyup(function(e) {
			
			if(e.keyCode == 40){
				
				//alert("S");
			}
			
		
		});
 
    
	
	$(document).on('change', '#search_gov', function() {
		
		$("#search_city option").hide();
		$("#search_city option[gov_id='"+$("#search_gov option:selected").attr("gov_id")+"']").show();
		$("#init_city").show();
		$("#search_city").val('جميع المدن');
		
	});
  
  
  });
  
