/*------------ ANKETA -------------------------------------*/
  function hlasuj(id,hlas) {    
    if (window.ActiveXObject) {
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
    }
    else
      {
        httpRequest = new XMLHttpRequest();
    }
    httpRequest.onreadystatechange= function () {zpracovat(id,hlas,httpRequest); } ;
    httpRequest.open("GET", "hlasovat.php?anketa="+id+"&hlas="+hlas, true);
    httpRequest.send(null);
  }
  
  function zpracovat(id,hlas,httpRequest) {
    if(httpRequest.readyState == 1 || httpRequest.readyState == 2 || httpRequest.readyState == 3) {
      document.getElementById("cekacka").innerHTML="<img src='img/loader.gif' alt='' />";
    }
    else if (httpRequest.readyState == 4)
    {
      if(httpRequest.status == 200) {
        document.getElementById("pocet"+hlas).innerHTML++;
        document.getElementById("cekacka").innerHTML="<img src='../img/loader.gif' alt='' />";
        
        //znemoznime hlasovani smazanim odkazu
        for (var key in document.getElementById('t-anketa').getElementsByTagName('td')) {
          var val = document.getElementById("t-anketa").getElementsByTagName('td')[key];
          if(val.className=='popis') {
            val.innerHTML = val.firstChild.innerHTML;
          }
        }
        
        //aktualizuji odpovedi
        var odpovedi = httpRequest.responseXML.getElementsByTagName('odpoved');
        var procenta = httpRequest.responseXML.getElementsByTagName('procenta');
        var delka = httpRequest.responseXML.getElementsByTagName('delka');
        for(var i=0; i < odpovedi.length; i++) {
          document.getElementById(odpovedi[i].getAttribute('id')).innerHTML = odpovedi[i].firstChild.data;
        }
        for(var i=0; i < procenta.length; i++) {
          document.getElementById(procenta[i].getAttribute('id')).innerHTML = procenta[i].firstChild.data+"%";
          document.getElementById(delka[i].getAttribute('id')).width=delka[i].firstChild.data;
        }
        document.getElementById("cekacka").innerHTML="";
      }
      else {
        alert("Chyba pri nacitani stanky "+ httpRequest.status);
      }
    }
  }
/*-------------------- VALIDACE FORA-----------------------------------*/

var xhr = vytvorXHR();
var cache = new Array();

function vytvorXHR(){
  var xhr;
  try{
    xhr = new XMLHttpRequest();
  }catch(e){//pro případ starší verze prohlížeče
    var MSXmlVerze = new Array('MSXML2.XML.Http.6.0','MSXML2.XML.Http.5.0','MSXML2.XML.Http.4.0','MSXML2.XML.Http.3.0','MSXML2.XML.Http.2.0','Microsoft.XML.Http');
    for(var i = 0; i < MSXmlVerze.lenght; i ++){
      try{
        xhr = new ActiveXObject(MSXmlVerze[i]);
      }catch(e){
        //vzniklou chybu ignoruji a pokračuji nastavením další verze
      }
    }
  }
  if(!xhr)
    alert("Došlo k chybě při vytváření objektu XMLHttpRequest!");
  else
    return xhr;
}

function vytvorPozadavek(id,hodnota){
if(xhr){
  if(id){
    //zakoduji hodnoty
    id = encodeURIComponent(id);
    hodnota = encodeURIComponent(hodnota);
    //vložím hodnoty do fronty
    cache.push("id="+id+"&hodnota="+hodnota);
  }
  try{
    //pokračovat budu jen v případě že cache není prázdná a objekt
    //XHR nemá co na práci
    if((xhr.readyState == 4 || xhr.readyState == 0)&& cache.length > 0){
      //z cache načtu další hodnotu
      var cacheVstup = cache.shift();
      xhr.open("POST","include/validace.php",true);
      xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      xhr.onreadystatechange = validace;
      xhr.send(cacheVstup);
    }
  }catch(e){
    alert("Nelze se připojik k serveru:\n" + e.toString());
  }
}else{
  alert("Funkce \"precitSoubor()\": chybí objekt XMLHttpRequest");
}
}

function validace(){
  if(xhr.readyState == 4){
    if(xhr.status == 200){
      var XMLRes = xhr.responseXML;
        //zachycení chyb IE a Opery
        if(!XMLRes || !XMLRes.documentElement){
          throw("Chybná struktura XML:\n"+xhr.responseText);
        }
        //chyby firefox
        var rootNodeName = XMLRes.documentElement.nodeName;
        if(rootNodeName == "parsereerror"){
          throw("Chybná struktura XML:\n"+xhr.responseText);
        }
        //čtu dokument, jelikož je vše ok :-)
        xmlRoot = XMLRes.documentElement;
        odpoved = xmlRoot.getElementsByTagName("odpoved")[0].firstChild.data;
        stav = xmlRoot.getElementsByTagName("stav")[0].firstChild.data;
        id = xmlRoot.getElementsByTagName("id")[0].firstChild.data;
        zprava = document.getElementById("err"+id);

        if(odpoved == 0){          
          zprava.innerHTML = stav;
          document.getElementById(id).style.border="1px solid red";
          document.getElementById(id).style.background="#FF7D73";
          
        }else{
          zprava.innerHTML = "";
          document.getElementById(id).style.border="1px solid black";
          document.getElementById(id).style.background="white";
        }
        
        setTimeout("vytvorPozadavek();", 500);
    }else{
      
    }
  }
}

/*mapa - zobrazeni nazvu*/
function vloziNazev(node, text, top, left) {
  while (node.hasChildNodes()) {
    node.removeChild(node.firstChild);
  }
  node.appendChild(document.createTextNode(text));
  
  node.style.left = left+"px";
  node.style.top = top+"px";

} 

/*-------------------------------------------------------------------------------*/
jQuery.noConflict();
jQuery(document).ready(function($){
    $("a.group").fancybox({
        'zoomSpeedIn': 300, 
        'padding': 5,
        'zoomSpeedOut': 300, 
        'hideOnContentClick': true,
        'overlayShow': true,
        'overlayOpacity' : 0.7,
        'overlayColor' : "#E95910",
        'titleShow' : false,
        'autoScale' : false
    }); 
    
    $("#autostart").fancybox({ 
        'zoomSpeedIn': 300, 
        'padding': 0,
        'frameWidth' : 644,
	      'frameHeight' : 484,
        'zoomSpeedOut': 300, 
        'overlayOpacity'	:	0.7,
        'overlayColor' : "#E95910",
        'hideOnContentClick': true,
        'overlayShow': true}).trigger('click');
    
    $("#topslider").easySlider({
      auto: true,
      continuous: true,
      controlsShow: false,
      pause: 4000
    });
    
    $("#poslat-pohled").validate({  
     rules: {  
 
      email_odesilatele: {  
         required: true,  
         email: true  
       },   
       email_prijemce: {  
         required: true,  
         email: true  
       }  
       }  
     });  
    
    $("#poslat-pohled").submit(function() { 
      var isValid = true;
      var jmeno_odesilatele = $('#jmeno_odesilatele').attr('value');  
      var email_odesilatele = $('#email_odesilatele').attr('value');  
      var jmeno_adresata = $('#jmeno_adresata').attr('value');  
      var email_adresata = $('#email_adresata').attr('value');  
      var text = $('#text').val();
      var pohled = $('.pohled').attr('value');  
      
      if(email_odesilatele == "") {
         $('#email_odesilatele').css({"border":"2px solid rgb(205, 10, 10)"});
         isValid = false;
      }
      else if(email_adresata == "") {
         $('#email_adresata').css({"border":"2px solid rgb(205, 10, 10)"});
         isValid = false;
      } 
      if(isValid) {
      $.ajax({  
        type: "POST",  
        url: "sendForm.php",  
        data: "jmeno_odesilatele="+jmeno_odesilatele+"& email_odesilatele="+email_odesilatele+"& jmeno_adresata="+jmeno_adresata+"& email_adresata="+email_adresata+"& text="+text+"& pohled="+pohled,  
        success: function(){  
          $('#poslat-pohled').hide(function(){$('div.success').fadeIn();});  
        }  
      });
      }  
      return false;  
    });
    
  //obchodni sit
  $("#nazevKraje").hide();
    $('#mapa').maphilight({
      'stroke' : true,
      'strokeColor' : 'F29001',
      'fillColor' : 'F29001',
      'fillOpacity' : 0.2
  });
  
  $('map>area').hover(function(){
    $(this).each(function(){
      $("#nazevKraje").show();
    });
  },
  function(){
    $("#nazevKraje").hide();
  });
 
  });
    
  
jQuery(document).ready(function($){
  $("#spoty-2009").click(function(){
    $("#spoty").load("/templates/"+$(this).attr("id")+".tpl");
  });
  
  $("#spoty-2010").click(function(){
    $("#spoty").load("/templates/"+$(this).attr("id")+".tpl");
  });
  
  $("#spoty-radio").click(function(){
    $("#spoty").load("/templates/"+$(this).attr("id")+".tpl");
  });
});
  

  


