//FINAL.js

 (function($){ 
  $.fn.naseptavac = function(options) {  

   var defaults = {  
    serviceUriNapoveda: "/service/web.vrat.napoveda.vyhledavani.php",
    serviceUriNapoveda2: "/service/web.vyhledavani.naseptavac.php",
    maxPocetNaseptavani: 20, //maximalni pocet pro naseptavani
    minDelkaNaseptavani: 3,
    autoSkryvani: 30//sekundy

   }; 
   var nastaveni = $.extend(defaults, options);  




   return this.each(function() { 
      var obj;
        obj = $(this); 
        obj.find('input[type="text"]').attr('autocomplete','off');

        var pos_top = obj.find('input[type="text"]').position();
        var input_h = obj.find('input[type="text"]').height();

	jQuery(obj).find('input').keyup(function() {
		obj.trigger('naseptavej');  
	}); 
	jQuery(this).find('input').focus(function() {
		obj.trigger('naseptavej');  
	});       
	obj.bind('naseptavej',function(event) {
		var hodnota_vyhledavani = obj.find('input[type="text"]').val() ;
		
		if(hodnota_vyhledavani.length >= nastaveni.minDelkaNaseptavani ){
                        jQuery(this).find('.napoveda').remove();

                        
			jQuery(this).find('input[type="text"]').after('<div class="napoveda"></div>');
			     obj.find('.napoveda').css('left', pos_top.left+'px');
			     obj.find('.napoveda').css('top', (input_h+pos_top.top)+'px');
			     //obj.find('.napoveda').css('margin-left', pos_top.left+'px');
			     //obj.find('.napoveda').css('margin-top', (input_h+pos_top.top)+'px');
			
			
				
		             jQuery.ajax({
		                url: nastaveni.serviceUriNapoveda2 + '?c=' + nastaveni.maxPocetNaseptavani + '&q=' + hodnota_vyhledavani,
		                cache: false,
		                dataType: "xml",
		                success: function(xmlSource){ 
		                  //akce   
		                  jQuery(xmlSource).find("seznam").each(function(){
		                      obj.find('.napoveda').append('<ul class="napoveda"></ul>');
		                      
		                      //reklama
		                      jQuery(xmlSource).find("polozka_reklama").each(function(){
		                       obj.find('.napoveda ul').append('<li class="vyhledavani_reklama"/>');
		                       obj.find('.napoveda ul li:last').append('<a href="' + jQuery(this).find('redirect').text() + '" />');
		                       obj.find('.napoveda ul li:last a').append(jQuery(this).find('nazev').text());
		                       obj.find('.napoveda ul li:last a').append('<img src="' + jQuery(this).find('counter').text() + '" height="1" width="1" />');
		                       //obj.find('.napoveda ul li:last a').append(' <span class="typ">[reklama]</span>');
		                      }); 				      
				      
				      
				      //naseptavac
				      jQuery(xmlSource).find("polozka").each(function(){
		                       obj.find('.napoveda ul').append('<li class="vyhledavani_napoveda"/>');
		                       obj.find('.napoveda ul li:last').append('<a href="' + jQuery(this).find('uri').text() + '" />');
		                       obj.find('.napoveda ul li:last a').append(jQuery(this).find('nazev').text());
		                       obj.find('.napoveda ul li:last a').append(' <span class="typ">['+jQuery(this).find('typ').text()+']</span>');
		                      });
                
		                  });
		                  //zavrenu
		                       obj.find('.napoveda ul').append('<li class="close"/>');
		                       obj.find('.napoveda ul li:last').append('<a href="#close" />');
		                       obj.find('.napoveda ul li:last a').append('<span class="txt">zavřít</span><span class="ico">&nbsp;</span>');
		                      //auto zavreni
		                      setTimeout(function() {
		                      		//obj.find('.napoveda').remove();
						  obj.find('.napoveda').slideUp('slow', function() {
						  	obj.find('.napoveda').remove();
						  });
		                      		
		                      }, nastaveni.autoSkryvani*1000) 


		               }
		              }); 

		}else{
			//odstraneni - malo znaku
			obj.find('.napoveda').remove();
		}
  
  		//manualni zavreni
		jQuery(this).find('.napoveda ul .close a').live('click', function() { 
			  obj.find('.napoveda').slideUp(150, function() {
			  	obj.find('.napoveda').remove();
			  });
			return false;
		});   
  
        	
    	}); 
    }); 
     
  }; 
})(jQuery);       
      
