$(document).ready(function(){

// generuje tło dla odpowiedniej pozycji menu
var site = parseInt($("#getSiteAttr").text()) - 1;
var a_width = $("#menuTop a:eq("+site+")").width();
$("#menuTop a:eq("+site+")").addClass("menuBackground").css("background-position", -(53 - a_width / 2));
$(".department ul").css("list-style-type","square");


$(".archiveYear").click(function(){
   if($(this).next(".archiveOfYearAll").is(":visible")) {
      $(this).next(".archiveOfYearAll").hide();
      $(".yearBg").removeClass("yearBg");
   }
   else {
      $("div.archiveOfYearAll:visible").hide("normal");
      $(this).next(".archiveOfYearAll").show("fast");
      $(".yearBg").removeClass("yearBg");
      $(this).addClass("yearBg");
   }
});

$("#enquiryForm input[@type='radio']").click(function(){
  if(!$("input[@title='inne']").is(":checked"))
    $("#enquiryForm input[@type='text']").val("");
});

// obsługa zapisu głosu do ankiety (AJAX)
$("img#submitEnquiry").click(function(){
  if($("#enquiryForm input:checked").length == 0)
    alert("Aby zagłosować należy wybrać jedną z odpowiedzi");
  else if($("input[@title='inne']").is(":checked") && $("input[@name='other']").val().length == 0)
    alert("Proszę wpisać swoją odpowiedź");
  else {
    $("#ajaxLoader").show();
    if($("#enquiryForm input[name='other']").length)
      var other_val = $("#enquiryForm input[name='other']").val();
    else
      var other_val = "";
    
    $.post('enquiry.php', { enquiry_id: $("#enquiryForm input:hidden").val(), answer_letter: $("#enquiryForm input:checked").val(), other: other_val }, 
      function(results){ 
        $("#enquiryForm").hide();
        $("#ajaxLoader").hide();
        $("#enquiryDiv").append(results);
        $("#backToVote").show();
      }
    );
  }
});

$("img#submitResults").click(function(){
    $("#ajaxLoader").show();
    $.post('enquiry.php', { enquiry_id: $("#enquiryForm input:hidden").val() }, 
      function(results){
        $("#enquiryForm").hide();
        $("#ajaxLoader").hide();
        $("#enquiryDiv").append(results);
        $("#backToVote").show();
      }
    );
});
$("#backToVote").click(function(){
  $("#enquiryForm").show();
  $(".enquiryResults").remove();
  $(this).hide();
});

// KOMENTARZE
  $('#comments').ajaxForm({
    beforeSubmit: function() {
      $('#comments div:last').append(" <img class='fileNotify' src='images/ajax-loader.gif' />");
    },
    success: function(msg) {
      $(".fileNotify").remove();
      $('#comments').prepend(msg);
      $("#comments span").fadeOut(3000);
      if(msg == "<span style='color:green'>Twój komentarz został zapisany</span>")
        setTimeout("header()", 3000);
      
    }
  })
  $("#comments textarea").autogrow({ lineHeight: 16 });


});

function header() {
  window.location=window.location.href;
}
