function naseptavac_hledani(id_modulu, cfg, id_volbytema)
{
	autocompleteConfig = cfg;

    var hledam = document.getElementById(cfg.el.id).value;
    //potlac_loader = true;
    //odeslatDotazExt("?fce=naseptavac&hledam="+hledam,"naseptavac","naseptavac_prava_ajax")
    var hledamArray = hledam.split(' '); 
    if(hledamArray.length>2){
     alert("Při tvorbě nadpisu je možné použít maximálně dvě slova!");
     document.getElementById(cfg.el.id).value=hledamArray[0]+" "+hledamArray[1];
    }
    else{
      odeslatDotazZModulu("?fce=naseptavac_hledani&hledam="+hledam+"&id_volbytema="+id_volbytema, id_modulu, "naseptavac_prava_ajax");
    }
	
	if ('function' == typeof(autocompleteConfig.complete))
		autocompleteConfig.complete();
}

function naseptavac_trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function naseptavac_ahref(IDstrIds,IDstrNames,IDstrNameshtml,idsi,namesi,joinstrIds,joinstrNames){
    var ahref = document.createElement("a");
    ahref.appendChild(document.createTextNode(namesi));
    //ahref.setAttribute("href", "#");
    ahref.setAttribute("id", idsi);
    ahref.onclick = function(){
        naseptavac_deletePravo(IDstrIds,IDstrNames,IDstrNameshtml,idsi,joinstrIds,joinstrNames);
    }     
    return ahref;
}

function naseptavac_makeNewFileds(IDstrIds,IDstrNames,IDstrNameshtml,newstrIds,newstrNames,joinstrIds,joinstrNames){
        //var strttt = document.createElement("A");
        //strttt.setAttribute("href", "");
        //strttt.onclick = new Function("fce");
        //strttt.className = "clasa"; 
        
        document.getElementById(IDstrNameshtml).innerHTML = "";
        strIds = document.getElementById(IDstrIds).value;
        strNames = document.getElementById(IDstrNames).value;
        var arrNamesHTML = new Array();
        if(strIds!=""){
          var arrIds = strIds.split(joinstrIds);
          var arrNames = strNames.split(joinstrIds);
          var jetam = 0;
          for (var i=0; i < arrIds.length; i++) {
              arrIds[i] = naseptavac_trim(arrIds[i]);
              arrNames[i] = naseptavac_trim(arrNames[i]);
              
              arrNamesHTML[i] = naseptavac_ahref(IDstrIds,IDstrNames,IDstrNameshtml,arrIds[i],arrNames[i],joinstrIds,joinstrNames);      
                            
              if(arrIds[i]==newstrIds){
                jetam = 1;
              } 
          }
        }
        else{
          var arrIds = new Array();
          var arrNames = new Array();
        }
        
        if(jetam!=1){
            if(arrIds.length==0){
              arrIds[0] = newstrIds;
              arrNames[0] = newstrNames;
              
              arrNamesHTML[0] = naseptavac_ahref(IDstrIds,IDstrNames,IDstrNameshtml,arrIds[0],arrNames[0],joinstrIds,joinstrNames);    
            }
            else{
              var delka = arrIds.length;
              arrIds[delka] = newstrIds;
              arrNames[delka] = newstrNames;
            
              arrNamesHTML[delka] = naseptavac_ahref(IDstrIds,IDstrNames,IDstrNameshtml,arrIds[delka],arrNames[delka],joinstrIds,joinstrNames);              
            }
                        
        }
        
        var strIdsNew = arrIds.join(joinstrIds);
        var strNamesNew = arrNames.join(joinstrIds);
               
        document.getElementById(IDstrIds).value = strIdsNew;
        document.getElementById(IDstrNames).value = strNamesNew;
        
        for (var f=0; f < arrNamesHTML.length; f++) {  
          document.getElementById(IDstrNameshtml).appendChild(arrNamesHTML[f]);
          if(arrNamesHTML.length>f+1){
              document.getElementById(IDstrNameshtml).appendChild(document.createTextNode(", "));
          }
        }
}

function naseptavac_deletePravo(IDstrIds,IDstrNames,IDstrNameshtml,delstr,joinstrIds,joinstrNames) {
         document.getElementById(IDstrNameshtml).innerHTML = "";
         strIds = document.getElementById(IDstrIds).value;
         strNames = document.getElementById(IDstrNames).value;
         var x = 0;
         var arrIdsNew = new Array();
         var arrNamesNew = new Array();
         var arrNamesHTMLNew = new Array();          
         var arrIds = strIds.split(',');
         var arrNames = strNames.split(',');
         for (var i=0; i < arrIds.length; i++) {
              arrIds[i] = naseptavac_trim(arrIds[i]);
              if(arrIds[i]!=delstr){
                arrIdsNew[x] = arrIds[i];
                arrNamesNew[x] = arrNames[i];
                arrNamesHTMLNew[x] = naseptavac_ahref(IDstrIds,IDstrNames,IDstrNameshtml,arrIdsNew[x],arrNamesNew[x],joinstrIds,joinstrNames);
                x++;
              } 
         }
         var strIdsNew = arrIdsNew.join(joinstrIds);
         var strNamesNew = arrNamesNew.join(joinstrNames);
         
         document.getElementById(IDstrIds).value = strIdsNew;
         document.getElementById(IDstrNames).value = strNamesNew;

        for (var f=0; f < arrNamesHTMLNew.length; f++) {  
          document.getElementById(IDstrNameshtml).appendChild(arrNamesHTMLNew[f]);
          if(arrNamesHTMLNew.length>f+1){
              document.getElementById(IDstrNameshtml).appendChild(document.createTextNode(", "));
          }
        }                               	
}

function naseptavac_prava_ajax(xmldata){

    var id      = xmldata.getElementsByTagName("id");
    var popis0    = xmldata.getElementsByTagName("popis0");
    var popis1    = xmldata.getElementsByTagName("popis1");
    var url    = xmldata.getElementsByTagName("url");
    var ul = document.getElementById("naseptavac_ul_"+autocompleteConfig.name);
    while (ul.childNodes.length > 0){
      ul.removeChild(ul.childNodes[0]);
    }
    
    document.getElementById('napovedaHlaska').innerHTML = '';
    
    for (var i=0; i < id.length; i++) {
      var li = document.createElement("LI");
      
      var nazorNadpis = '';
      if (popis0[i].childNodes.length>0) nazorNadpis = popis0[i].childNodes[0].nodeValue;
      var nazorText = '';
      if (popis1[i].childNodes.length>0) nazorText = popis1[i].childNodes[0].nodeValue;
       
      li.innerHTML = '<p><strong>'+nazorNadpis+'</strong></p><p>'+nazorText+'</p>';
      li.innerHTML = li.innerHTML+'<p><a href="'+url[i].childNodes[0].nodeValue+'/?nazor_id='+id[i].childNodes[0].nodeValue+'">souhlasím s názorem</a></p>';
      li.id = id[i].childNodes[0].nodeValue;
      ul.appendChild(li);
      /*li.onclick = function() {
        //document.getElementById("obrazek_prava_id").value = this.id;
        document.getElementById(autocompleteConfig.name).value = this.innerHTML;

		if (autocompleteConfig.noList)
		{
			document.getElementById('naseptavac_span_'+autocompleteConfig.name).style.display = 'none';
			document.getElementById('naseptavac_ids_'+autocompleteConfig.name).value = '';
			document.getElementById('naseptavac_popiss_'+autocompleteConfig.name).value = '';
		}

		naseptavac_makeNewFileds(
			"naseptavac_ids_"+autocompleteConfig.name,
			"naseptavac_popiss_"+autocompleteConfig.name,
			"naseptavac_span_"+autocompleteConfig.name,
			this.id,
			this.innerHTML,
			",",
			", "
		);

		if ('function' == typeof(autocompleteConfig.itemClick))
			autocompleteConfig.itemClick();
		
        //document.getElementById("obrazek_prava_span").innerHTML = fotofile_septej_makeNewFileds(document.getElementById("obrazek_prava_span").innerHTML,this.innerHTML,", ");
        //document.getElementById("obrazek_prava_ids").value = fotofile_septej_makeNewFileds(document.getElementById("obrazek_prava_ids").value,this.id,",");
      }*/
      
      
      document.getElementById('napovedaHlaska').innerHTML = '<span class="Red">Zdá se, že už nějaký(á) volič(ka) před Vámi volili stejné či podobné klíčové slovo či sousloví. Možná chcete vyjádřit stejný názor jako oni, podívejte se prosím pro zajímavost níže.</span>';
      
    }  
    
    //potlac_loader = false;  
    //alert(id.length);
}

function naseptavac_disp(st, idPostfix){

    self.act = st;
    var ul = document.getElementById("naseptavac_ul_"+idPostfix);

    if (st)
	{
      clearTimeout(self.timer);
      ul.style.visibility = "visible";
    }

    else
	{
      var f = function(){ul.style.visibility = "hidden";}
      self.timer = setTimeout(f, 500);
    }
}

