var pozicia = 0;
var refresh_spravy_time = 1000;
var old_max_id = 0;
var spr_limit = 0;

function zmen_sprava(ID, pocet, sid)
{
  for (x=0; x<=pocet-1; x++)
  {
  	but = "but_" + x;
  	pop = "popis_" + x;
  	if (x==ID)
  	{
  		document.getElementById(pop).style.display = "block";
  		document.getElementById(but).style.background = "Gray";
  		ZapisCookie('sprava_id',sid,"");
  	}
  	else
  	{
  	  	document.getElementById(pop).style.display = "none";
  		document.getElementById(but).style.background = "#EBEBEB";	
  	}
  }
}

function zmen_stra(typ,pocet,max)
{
  if (!pozicia) pozicia = pocet-max+1;
  
  if (max<pocet)
  {
  	if ((typ == 1) && (pozicia == 1)) {}
  	if ((typ == 1) && (pozicia >1))
  	{ 
  		pozicia--;
  		prva = "riadok_" + pozicia;
  		pos = pozicia + max;
  		posledna = "riadok_" + pos;
  		document.getElementById(prva).style.display = "block";
  		document.getElementById(posledna).style.display = "none";
  	}
  	if ((typ == 2) && (pozicia+max-1 >= pocet)) {}
  	if ((typ == 2) && (pozicia+max-1 < pocet))
  	{
  		prva = "riadok_" + pozicia;
  		pos = pozicia + max;
  		posledna = "riadok_" + pos;
  		document.getElementById(prva).style.display = "none";
  		document.getElementById(posledna).style.display = "block";
  		pozicia++;
  	}
  }
}

function createRequestObject()
{
    var request_;
    
    if(typeof XMLHttpRequest != "undefined"){
        request_ = new XMLHttpRequest();
    }
    else if(typeof window.ActiveXObject != "undefined"){
        try {
            request_ = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        }
        catch(e){
            try {
                request_ = new ActiveXObject("MSXML2.XMLHTTP");
            }
            catch(e){
                try {
                    request_ = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    request_ = null;
                }
            }
        }
    }
    return request_;    
}

function send_xmlhttprequest(url,typ,div,wr,id)
{
    http_request = createRequestObject()
    http_request.open('GET', url);
    http_request.onreadystatechange = function()
     {
        if (http_request.readyState == 4) 
        {
            if (http_request.status == 200 || http_request.status == 304) 
            {
                var vrat = http_request.responseText;
                if (typ == 'S')
                {
                    var zapis_hod = "<div class=hod>Priemerné hodnotenie tejto správy je " + vrat+ ". Ďakujeme Vám.</div>";
                    var horne = document.getElementById('hlasovanie_00');
                    div.innerHTML = zapis_hod;
                    if (id == old_max_id) horne.innerHTML = zapis_hod;
                }
                if (typ == 'D') div.innerHTML = "<div class=hod>Priemerné hodnotenie tejto analýzy je " + vrat+ ". Ďakujeme Vám.</div>";
                if (typ == 'K') div.innerHTML = "<div class=hod>Priemerné hodnotenie tohto komentára je " + vrat+ ". Ďakujeme Vám.</div>";
                if (wr)
                {
                    my_div = document.getElementById('hlasovanie_00');
                    my_div.innerHTML = "<div class=hod>Priemerné hodnotenie tejto správy je " + vrat+ ". Ďakujeme Vám.</div>";
                }
            }
        }
     }   
     http_request.send(null);   
}

function hlasuj_id(ako,id,os,typ)
{    
	cook = "hlasovanie_id_" + typ + '_' + id;
	hl_div = document.getElementById('hlasovanie_' + id);
	dlzka = 1000*24*60*60;
    if ((typ == 'D') || (typ == 'K')) dlzka = dlzka*30;
	
	link = 'hlasuj.php?id=' + id + '&typ=' + typ + '&ako=' + ako + '&os=' + os;
	send_xmlhttprequest(link,typ,hl_div,'',id);
	ZapisCookie(cook,'1',dlzka);
}

function hlasuj_id_sp(ako,id,os,typ,id2)
{    
    cook = "hlasovanie_id_" + typ + '_' + id;
    hl_div = document.getElementById('hlasovanie_' + id);
    dlzka = 1000*24*60*60;
    if ((typ == 'D') || (typ == 'K')) dlzka = dlzka*30;
    
    link = 'hlasuj.php?id=' + id + '&typ=' + typ + '&ako=' + ako + '&os=' + os;
    send_xmlhttprequest(link,typ,hl_div,id2,id);
    ZapisCookie(cook,'1',dlzka);
}

function zmen_limit(limit)
{
  old_max_id = 0;
  spr_limit = limit; 
}

function nacitaj_spravy()
{   
   http_request = createRequestObject();
   
   var _url = "get_spravy.php";
   var _param = "max=" + old_max_id + "&limit=" + spr_limit;
    
   http_request.open('POST', _url + '?' + _param, true); 
       
   http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1250");
   http_request.send(_param);
      
   http_request.onreadystatechange = function() 
   {       
      if (http_request.readyState == 4) 
      {
         var response = http_request.responseText;
         if (response)
         {
             var myindex = response.indexOf('###');
             old_max_id = response.substring(0,myindex);
             var text = response.substring(myindex+3);
             var myindex = text.indexOf('###');
             prva_sprava = text.substring(0,myindex);
             var new_text = text.substring(myindex+3);
             document.getElementById('prva_ajax').innerHTML = prva_sprava;
             document.getElementById('spravy_ajax').innerHTML = new_text;
         }
      }
   }
}   

function go_spravy()
{
    nacitaj_spravy();
    window.setTimeout("go_spravy()", refresh_spravy_time);
}