var runningRequest = false;
var ajax;
var file_prodotto;
var highlight;
var url;


function AjaxRequest(id){
	//$('div#risultati_ricerca').fadeTo("fast",0.5);
	highlight=id;
	file_prodotto=url+"search.php";
	if (window.XMLHttpRequest)
		ajax=new XMLHttpRequest();
	else
		ajax=new ActiveXObject("Microsoft.XMLHTTP");
	if(id!="")
		{
			file_prodotto=file_prodotto+"?lang=it&key="+id;
			ajax.onreadystatechange=AjaxRequestState;
			ajax.open("GET",file_prodotto,true);
			ajax.send(null);
		}
}

function AjaxRequestState() 
{
	try
	{
    if (ajax.readyState == 4) 
	{
        if (ajax.status == 200)
			{
				runningRequest=false;
				var resultHtml="";
				var tmp_array=new Array();
				var array_file=new Array();
				var array_descr=new Array();
				var tmp_descr="";
				if(ajax.responseText!=0)
				{
				tmp_array=ajax.responseText.split("~");
				array_file=tmp_array[0].split("#");
				
				array_descr=tmp_array[1].split("#");
				
				if(array_file.length==array_descr.length)
				{
				for(i=0;i<array_file.length;i++)
					{
					 var tmp_str1="";
					 var tmp_img="";
					 var reg_expr=new RegExp("(specifiche|gallery|come_iniziare|soluzioni|servizi).html");
					 tmp_str1=decodeURIComponent(array_file[i].replace(/\+/g," "));					 
					 if(tmp_str1.search(reg_expr)!=-1)
					 {
						tmp_str1=tmp_str1.substr(0,tmp_str1.lastIndexOf("/"));
						tmp_str1=tmp_str1.substr(tmp_str1.lastIndexOf("/")+1,tmp_str1.length); 							
						
						if(tmp_str1.indexOf("_it")!=-1)
							tmp_str1=tmp_str1.replace("_it","");
							
						if(tmp_str1.indexOf("_en")!=-1)
							tmp_str1=tmp_str1.replace("_en","");
							
						if(tmp_str1.indexOf("_")!=-1)
							tmp_str1=tmp_str1.replace("_"," ");
							
						if(tmp_str1=="default")
							tmp_str1="Home";
					 }
					 else
					 {
						 if(tmp_str1.indexOf("/")!=-1)
						 	{
								//tmp_img="http://localhost/kiosk2011/"+tmp_str1.substr(0,tmp_str1.lastIndexOf("/")+1)+"img.jpg";
								tmp_str1=tmp_str1.substr(tmp_str1.lastIndexOf("/")+1,tmp_str1.length);
								tmp_str1=tmp_str1.substr(0,tmp_str1.lastIndexOf("."));
							}
						else 
						 	{
								//tmp_img="http://localhost/kiosk2011/img.jpg";
								tmp_str1=tmp_str1.substr(0,tmp_str1.lastIndexOf("."));
							}
						
						if(tmp_str1.indexOf("_it")!=-1)
							tmp_str1=tmp_str1.replace("_it","");
							
						if(tmp_str1.indexOf("_en")!=-1)
							tmp_str1=tmp_str1.replace("_en","");
							
						if(tmp_str1.indexOf("_")!=-1)
							tmp_str1=tmp_str1.replace("_"," ");
							
						if(tmp_str1=="default")
							tmp_str1="Home";
					 }
					 resultHtml+='<div class="riga_risultati" onclick="document.location=\''+url+''+decodeURIComponent(array_file[i].replace(/\+/g," "))+'\'"><div class="result">';
					 //<img src="'+tmp_img+'" alt="" border="" style="float:left; margin-right:2px;" width="50" height="50" />
					 //resultHtml+='<a href="http://localhost/kiosk2011/'+decodeURIComponent(array_file[i].replace(/\+/g," "))+'"><b>'+tmp_str1+'</b>';
					 resultHtml+='<span style="color:#5b5b5b; font-size:14px;">'+tmp_str1+'</span>';
					 tmp_descr=decodeURIComponent(array_descr[i].replace(/\+/g," "));					 
					 tmp_str1=tmp_descr.toUpperCase();
					 var tmp_str2=highlight.toUpperCase();
					 var tmp_pos=tmp_str1.indexOf(tmp_str2);
					 if(tmp_pos!=-1)
					 {
						 
						 if(tmp_descr.length>150)
						 {
							tmp_descr=tmp_descr.substr(0,149)+"...";
						 }
						 
						 var tmp_highlight=tmp_descr.substr(tmp_pos,highlight.length);
						 var tmp=tmp_descr.substr(0,tmp_pos);
						 tmp+='<span style="clear:both; float:none; background-color:#FF0; font-weight:bold;">'+tmp_highlight+'</span>'+tmp_descr.substr(tmp_pos+highlight.length,tmp_descr.length);
						 
						 resultHtml+='<br />'+tmp;
					 }
					 else{
						 if(tmp_descr.length>150){tmp_descr=tmp_descr.substr(0,149)+"...";}
						 resultHtml+='<br />'+tmp_descr;
						 }
					 
					 //resultHtml+='<a href="#" class="readMore">Read more..</a>';
					 resultHtml+='</div></div>';
					 $('div#risultati_ricerca').html('');
					 $('#risultati_ricerca').html(resultHtml);					
					}
				}
			//$('div#risultati_ricerca').fadeTo("fast",1);
			$('#close_ricerca').show();
			}
			else {$('div#risultati_ricerca').hide();alert("NESSUN RISULTATO");
					 $('#close_ricerca').show();	}
			}
			else{
				//$('div#risultati_ricerca').fadeTo("fast",1);
					 $('#close_ricerca').show();	}
    }
	}
	catch(e){
		//$('div#risultati_ricerca').hide();
			}
}
   //Identify the typing action
     $('input#campo_ricerca').keyup(function(e){
		var reg_expr=new RegExp("http://");
		if(String(document.location).search(reg_expr)!=-1)
		{
			var tmp=String(document.location);
			tmp=tmp.substring(7,tmp.length-1);
			if(tmp.indexOf("/")!=-1)
				tmp=tmp.substring(0,tmp.indexOf("/"));
			if(tmp!="www.net-eng.it")
			url="http://"+ tmp  +"/net-eng/";
			else
			url="http://"+ tmp  +"/";
		}
		
         e.preventDefault();
         var $q = $(this);
 
        if($q.val() == ''){
			 ajax.abort();
			$('div#risultati_ricerca').html("<img src='"+url+"img/loading.gif' alt='Loading' border='0' hspace='200' vspace='50' />");
			$('div#risultati_ricerca').hide();
					 $('#close_ricerca').hide();	
             return false;
         }
		 else{
			$('div#risultati_ricerca').show();
			}
 
        //Abort opened requests to speed it up
         if(runningRequest){
             ajax.abort();
         }
 
        runningRequest=true;
		AjaxRequest($q.val());
     });
	 
function pulisci(){
	if($("#campo_ricerca").val()=="Cerca")
		$("#campo_ricerca").val("");
	}
	
function inizializza(){
	if($("#campo_ricerca").val()=="")
		$("#campo_ricerca").val("Cerca");
	}



function chiudi_ricerca(){
	$('#close_ricerca').hide();
	$('#risultati_ricerca').hide();
	$('#campo_ricerca').val("Cerca");
	}
