var sending = false;

$(document).ready(function(){
  // Komentarze i oceny
  catchUpComments();
  catchUpVotes();
})

catchUpComments = function() {
  $(".comment-author").corner("top");
  setTimeout(checkIfAddComment, 100);
  $("#sendComment").click(function(e){
    sending = true;
    $("#sendComment").attr({'disabled':'disabled'});
    $("#form-error").hide();
    showIndicator(e);
    $.post('/c/comment.php', $('#comment-form').fastSerialize(), function(answer){
     if (answer=="[OK]") {
       $("#name").val('');
       $("#comment").val('');
       $('#comments').load('/z-comments.php'+location.search, function(){
         hideIndicator();
         catchUpComments();
         $('#votes').load('/z-votes.php'+location.search, catchUpVotes);
       });
     }
     else {
       hideIndicator();
       $("#form-error").text(answer).show().corner();
     }
     sending = false;
    });
    return false;
  });
}

var rateStars = {};
restoreRating = function() {
  for (i=1;i<6;i++) $("#votes img[rel="+i+"]").attr({'src':rateStars[i]});
}
catchUpVotes = function() {
  for (i=1;i<6;i++) rateStars[i] = $("#votes img[rel="+i+"]").attr('src');
  $("#votes img").bind("mouseout", restoreRating);
  $("#votes img").bind("mouseover", function(){
    $(this).css('cursor', 'pointer');
    sel = $(this).attr('rel');
    for (i=1;i<=sel;i++) $("#votes img[rel="+i+"]").attr({'src':'http://g.miplo.pl/rr.gif'});
    for (i=sel+1;i<6;i++) $("#votes img[rel="+i+"]").attr({'src':'http://g.miplo.pl/r0.gif'});
  });
  $("#votes img").click(function(e){
    $(this).attr({'disabled':'disabled'});
    showIndicator(e);
    $.post('/c/rate.php', {'s': $('#s').val(), 'e': $('#e').val(), 'vote': $(this).attr('rel')}, function(answer){
     if (answer=="[OK]") {
       $('#comments').load('/z-comments.php'+location.search, catchUpComments);
       $('#votes').load('/z-votes.php'+location.search, hideIndicator);
     }
     else {
       hideIndicator();
       restoreRating();
       alert(answer);
     }
    });
    return false;
  });
}

checkIfAddComment = function() {
  if ($("#name").val()!='' && $("#comment").val()!='' && !sending)
    $('#sendComment').removeAttr('disabled');
  else
    $('#sendComment').attr({'disabled':'disabled'});
  setTimeout(checkIfAddComment, 100);
}

$.fn.fastSerialize = function() {
    var a = [];
    $('input,textarea,select,button', this).each(function() {
        var n = this.name;
        var t = this.type;
        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;
        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );
        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }
        a.push({name: n, value: this.value});
    });
    return a;
};