// JavaScript Document

$(document).ready(function(){



$.jheartbeat.set({
   url: "ajax.get.challenge-credits.php", // The URL that jHeartbeat will retrieve
   delay: 15000, // How often jHeartbeat should retrieve the URL
   div_id: "ch-cr" // Where the data will be appended.
});

//$.jheartbeat.set({
//   url: "ajax.get.challenges.php", // The URL that jHeartbeat will retrieve
//   delay: 10000, // How often jHeartbeat should retrieve the URL
//   div_id: "ch-t" // Where the data will be appended.
//});






 });


var httpRequest;





function divswitch(divName)
   {
     var divik = document.getElementById(divName);
     if (divik.style.display=="none") {
	     document.getElementById(divName).style.display = "block";
	     document.getElementById(divName+"-link").style.display = "none";
     } else {
	     document.getElementById(divName).style.display = "none";
	     document.getElementById(divName+"-link").style.display = "block";
     }
   }


function livewin()
    {
     
    var castka = document.getElementById("kolik").value;
    var kurs = document.getElementById("krz").value;
    var Pattern = new RegExp("^[0-9]{1,5}$");
    var match = Pattern.exec(castka);
    document.cookie=("cstk="+castka);
    if (match) {
	    document.getElementById("lw").innerHTML = "<b>Možná výhra "+Math.round(castka*kurs)+" K</b>&nbsp;";
    } else {
	    document.getElementById("lw").innerHTML = "";
    }

    }


function hilight(id)
    {
     
     document.getElementById("row_"+id).style.background="#CDEEFD";
    }
    
    
    function nolight(id)
    {
     
     document.getElementById("row_"+id).style.background="#FFFFFF";
    }
    
function closediv(id)
   {
     var divik = document.getElementById("sik_"+id);
     divik.style.display="none";
   }

function ajaxAction(id,p)
   {
     var divik = document.getElementById("sik_"+id);
     //alert(id+" "+f+" "+td+" "+action);
     //document.getElementById("sik_"+id).style.display="block";
     if (divik.style.display == "block") {
	     divik.style.display="none";
	     return;
     } else {
	     divik.style.display="block";
     }
     
     if (id > -1)
     {
        var link;
        link = "stats.php?ItemId="+id+"&p="+p;
	
	//alert(link);
        if (window.ActiveXObject)
        {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
          httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("get", link, true);
        httpRequest.onreadystatechange= function () {processRequest("sik_"+id); } ;
        httpRequest.send(null);
      }
      else
      {
        // just delete the content when no article selected
        //document.getElementById(session-tags).innerHTML = "";
        
      }
      
   }
   
   function processRequest(targetDiv)
   {
	var loading = document.getElementById(targetDiv);
	   
      if (httpRequest.readyState == 4)
      {
        if(httpRequest.status == 200)
        {
          var contentViewer = document.getElementById(targetDiv);
          contentViewer.innerHTML = httpRequest.responseText;
	  //loading.innerHTML = "";
	  
        }
        else
        {
            //alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
      } else {
        //loading.innerHTML = "<img src='img/wait.gif'>";
        loading.innerHTML = "<p style='text-align:center;'>Nahrávám statistiky<br /><br /><img src='../img/ex/load.gif'></p>";
        }      
      
   }

//pridavani a odebirani z bufferu
function ar_match(id,tip,mode)
   {
     //var divik = document.getElementById("sik_"+id);
     //alert(id+" "+f+" "+td+" "+action);
     //document.getElementById("sik_"+id).style.display="block";
     //divik.style.display="block";
     
     if (id > 0)
     {
        var link;
	var amount = document.getElementById("kolik").value;
	
	if(mode == 2) {
		$("#p-"+id+"-1").removeClass("ot");
		$("#p-"+id+"-2").removeClass("ot");
	}
	if(mode == 1) {
		if(tip == 1) {
			$("#p-"+id+"-1").addClass("ot");
			$("#p-"+id+"-2").removeClass("ot");
		}
		if(tip == 2) {
			$("#p-"+id+"-2").addClass("ot");
			$("#p-"+id+"-1").removeClass("ot");
		}
	}
        link = "update_buffer.php?matchId="+id+"&tip="+tip+"&mode="+mode+"&amy="+amount;
	
	//alert(link);
        if (window.ActiveXObject)
        {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
          httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("get", link, true);
        httpRequest.onreadystatechange= function () {processBuRequest("buffalist",id,tip); } ;
        httpRequest.send(null);
      }
      else
      {
        // just delete the content when no article selected
        //document.getElementById(session-tags).innerHTML = "";
        
      }
      
   }
   
   function processBuRequest(targetDiv,id,tip)
   {
	var loading = document.getElementById("working");
	var tik_stat = document.getElementById("nt"+tip+"_"+id);
	var other_tik = (tip == 1) ? document.getElementById("nt2"+"_"+id) : document.getElementById("nt1"+"_"+id);
	   
      if (httpRequest.readyState == 4)
      {
        if(httpRequest.status == 200)
        {
          var contentViewer = document.getElementById(targetDiv);
          contentViewer.innerHTML = httpRequest.responseText;
	  loading.innerHTML = "";
	  //tik_stat.innerHTML = "na tiketu";
	  //other_tik.innerHTML = "";
	  livewin();
	  
        }
        else
        {
            //alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
      } else {
        //loading.innerHTML = "<img src='img/wait.gif'>";
        loading.innerHTML= "<img src='../img/ex/load.gif'>";
        //tik_stat.innerHTML= "sázím...";
        }      
      
   }

   

