var search_keyword; //a kulcsszó, amit keresünk
var product_len = 0; //a tipuslista hossza
var search_pattern; //a kereséshez a regexp minta
var search_szamlalo = 2; //talalatok szamat merjuk vele
var search_found = false;


var search_max_showed_matches = 6;


$(document).ready(function(){
    
   $(".search-list .item").each(function(){
       product_len++;
   }); 
    
   $("#search_input").keyup(function() {
       search_keyword = $("#search_input").val();
       //alert(keyword);
       //filterSearchlista(search_keyword);
       
       
       
       ///////////////////////////////////////////////////////////////
       ////////kereses meginditasa AJAX
       var op = 'kereses';
       
        
        
       $.ajax({
              
              //url: '/ajax/'+op+'/'+search_keyword,
              url: '/talalatilista/'+search_keyword,
              method: 'post',
              success: function(data){
                
                    $(".search-list").html(data);                    
                    $(".search-list").css({'display' : 'block'});
              }
            
       });
       
       
       
   });    
   //alert('product_len: '+product_len);
   
    
   
    
    
    
});


function filterSearchlista(keyword) {
   
   $(".search-list").show();
   search_pattern=new RegExp(keyword, "i");
   
   search_szamlalo = 0;
   
   if(keyword == '') {
       
       $(".search-list .item").each(function(){
            $(this).css({'display' : 'none'});
       });
       
       $(".search-list").hide();
       return 0;
   }
   
   $(".search-list .item").each(function(){
       
       var value = $(this).attr('value');
       /*alert(value);*/
       search_found = search_pattern.test(value);    

        if( search_found && ( search_szamlalo < search_max_showed_matches) ) {
            $(this).css({'display' : 'block'});            
            //alert('found: '+i+'  --  '+areas[i]['name']);
            search_szamlalo++;

        } else {
            $(this).css({'display' : 'none'});            
        }
       
   });
  
   /*
  
       for(var i = 0; i < len; i++) {

            found = pattern01.test(tipuslista[i]['label']);    

            if( found && ( szamlalo < max_showed_matches) ) {
                $("#tipus_"+tipuslista[i]['value']).css({'display' : 'block'});            
                //alert('found: '+i+'  --  '+areas[i]['name']);
                szamlalo++;

            } else {
                $("#tipus_"+tipuslista[i]['value']).css({'display' : 'none'});            
            }

       }
     */  
   
   
   /*
   if(keyword == '') { //|| szamlalo == 0
         $(".mobil-lista").css({'display' : 'none'});
        
   } else {
       $(".mobil-lista").css({'display' : 'block'});
      
   }
   */
   search_found = false; //default érték visszaállítása
   return 0;
   
   
}



function addElement(container, item) {
   
   
   
   var el = '<div class="item" value="'+item.brandName+' '+item.type+'">'
                +'<a href="'+item.brand_url+'/'+item.type_url+'/'+item.key_product+'">'
                    +'<img src="/image_5.php?p='+item.key_product+'&s='+item.key_bolt+'&mw=50&mh=50&key_bolt='+item.key_bolt+'&b='+item.brand_url+'&t='+item.type_url+'" alt="'+item.brandName+' '+item.type+'" title="'+item.brandName+' '+item.type+'" />'
                +'</a>'
                +'<div class="name">'
                    +'<a href="'+item.brand_url+'/'+item.type_url+'/'+item.key_product+'">'
                        +item.brandName+' '+item.type+
                    +'</a>'
                +'</div>'
                +'<div class="price">'+item.price+' Ft</div>'
            +'</div>';
   
  $(container).append("#matches-product")
   
}
