$(function(){

  qt = [[1,1,1,1,1,1,1,1],
        [2,2,2,1,1,1,3,3],
        [3,2,5,2,5,3,5,3],
        [5,2,5,2,7,2,5,2],
        [2,2,5,2,5,8,5,8],    //5
        [1,18,11,2,20,12,3,18],
        [11,1,18,2,2,0,13,19],
        ['a1','a2','a3','a4','a5','a6','a7','a8'],
        ['19','a13','21','20','a14','1','23','a15'],
        ['a16','a16','a17','a23','a19','a19','a19','a16'],  //10
        ['a16','a24','a21','a22','a23','a22','a21','a24'],
        ['a25','a26','a27','a28','a29','a30','a31','a32'],
        ['8','1','25','26','2','27','28','3'],
        ['a34','a33','a15','a36','a37','a38','a39','a40'],
        ['a39','a43','a42','a44','a23','a46','a47','a48'],
        ['a43','a49','a33','a51','a52','a31','a53','a16'],
        ['a39','a25','a55','a56','a57','a58','a59','a60'],
        ['a4','a17','a23','a23','a4','a17','a17','a23'],
        ['24','11','17','9','16','13','15','14'],
        ['a16','a66','a23','a66','a61','a32','a20','a42'],
        ['a39','19','a62','a34','23','a64','a25','19']];
        
  at = [[1,2,3,4,5,6,7,8],
        [1,5,7,3,9,11,12,13],
        [3,11,4,12,5,2,7,8],
        [3,4,5,6,1,2,12,7],
        [4,8,2,1,11,12,7,14], // 5
        [2,8,12,13,17,6,7,16],
        [2,3,10,8,9,1,13,14],
        ['a9','a10','a4','a11','a8','a12','a13','a15'],
        ['9','18','20','2','21','22','11','23'],
        ['a14','a2','a57','a11','a66','a64','a20','a15'],  //10
        ['a9','a10','a16','a11','a53','a47','a13','a15'],
        ['a35','a10','a4','a11','a8','a12','a44','a45'],
        ['27','28','4','5','8','4','24','29'],
        ['a25','a14','a4','a11','a37','a41','a13','a45'],
        ['a30','a48','a50','a11','a43','a26','a17','23'],     //15
        ['a60','a33','a41','a16','a40','a28','a39','a43'],
        ['a55','a53','a39','a45','a57','a60','a31','a43'],
        ['a25','a13','a3','a4','a8','a19','a49','a20'],
        ['9','11','12','20','1','10','14','24'],
        ['a67','a66','23','a17','a15','a42','a13','a11'],
        ['a23','2','13','a54','a65','a64','a28','a18']];
        
  answ = Array(8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8);
  abs = 1260;
  A = {};

  $('button').button();
  $('input[type=submit]').button();
  
  $('#begin').click(function(){
    $.post('begin.php',{},function(data){
      $('#start').fadeOut('500', function(){
        $("#top").fadeIn();
        load_q(0);
        A.inter = setInterval("chng_time()",1000);
      });
      
    });
  });
  //alert($('#neww,#new').attr('val'));
  $('#neww,#new').click(function(){
    $.post('new.php',{},function(){
      window.location.reload()
    });
  });

 $('#send_code').click(function(){
    $.post('pay.php',{code:$("#code").val()},function(data){
      if (data=='ok') {
        window.location.reload()
      } else if (data=="er1") {
        alert('Nekorekts kods!');
      } else if (data=='er2') {
        alert('Nepareizs kods!');
      }
    },"html");
  });


})

chng_time = function(){
  if (abs == 0) { 
    $('#cont').fadeOut(function(){
      end_test();
      clearInterval(A.inter);
    }); 
  } else {
    sec = abs % 60;
    if (sec<=9) { sec = '0'+sec; }
    min = ((abs - sec) / 60); 
    abs = abs - 1;     
    $('#time').html(min+':'+sec);
  }
}

load_q = function(id){
  $("#qq").html((id+1)+"/21");
  $("#q_cont td").each(function(i){
    $(this).html('<img src="img/'+qt[id][i]+'.jpg">');
  }); 
  $("#a_cont td").each(function(i){
    $(this).html('<img qid="'+id+'" val="'+i+'" class="answ" src="img/'+at[id][i]+'.jpg">');
  });
if (id<20) {
  $("#q_cont2 td").each(function(i){
    $(this).html('<img src="img/'+qt[id+1][i]+'.jpg">');
  }); 
  $("#a_cont2 td").each(function(i){
    $(this).html('<img  src="img/'+at[id+1][i]+'.jpg">');
  });
}
  $(".answ").click(function(){
    save_answ($(this).attr('qid'),$(this).attr('val'));
  });
  $('#cont').fadeIn();
}

save_answ = function(qid,a){
  answ[qid] = a;
  $('#cont').fadeOut(function(){
    if (qid==20) {
      end_test();
    } else {
      load_q(1*(qid)+1);
    }  
  }); 
}

end_test = function(){
  $.post('end.php',{answ:answ},function(data){
    $("#top").fadeOut();
    if (data>0) { dd = data; } else { dd = 85; }
    if (data!='timeout') $("#iqrep").html($("#iqrep").html()+"<strong>"+data+"</strong>"); 
    iqg(dd);  
    $('#end').fadeIn('500', function(){
      //alert(data);
    });   
  }) 
}

restart_test = function(){
  $("#top").fadeOut();
  $("#iqval").html('');   
  $('#iq').fadeIn('500', function(){ });  
}

iqg = function(iq){
  if (iq=='zem 85') iq = 85;
  iqpos = 4*(iq-40) - 10;
  setTimeout("move_bg(0,"+iqpos+")",10);
}

move_bg = function(d,iqpos){
// console.log(d+' '+iqpos);
 if (d<iqpos) {
   $("#iqg").css('background-position',(d+1)+'px 0px');
   setTimeout("move_bg("+(d+1)+","+iqpos+")",10);
 }  
}


