/*
.---------------------------------------------------------------------------.
|  Software: Web CMS                                                        |
|   Version: $Id:: diskuse.js 1317 2010-01-17 22:50:11Z jakub.boucek      $ |
|   Contact: info@koldasoft.cz                                              |
|      Info: http://www.koldasoft.cz/                                       |
|   Support: http://www.koldasoft.cz/kontakt/                               |
| ------------------------------------------------------------------------- |
|    Author: Koldasoft, s.r.o.                                              |
|    Author: Jakub Bouček, Jiří Kolařík and someone others                  |
| Copyright (c) 2007-2010, Koldasoft, s.r.o., Rights Reserved.              |
| ------------------------------------------------------------------------- |
|   License: Distributed under the Koldasoft Private/Individual License     |
|            == licence provided determined only, no for free use ==        |
| This program is distributed in the hope that it will be useful - WITHOUT  |
| ANY WARRANTY where is not defined by approved contract                    |
'---------------------------------------------------------------------------'

/**
 * Web CMS
 * NOTE: Designed for use with PHP version 5.2 and up
 * @package Web CMS
 * @author Koldasoft, s.r.o.
 * @copyright 2007 - 2010 Koldasoft, s.r.o.
 * @version $Id: diskuse.js 1317 2010-01-17 22:50:11Z jakub.boucek $
 */

$(document).ready(function(){
  var rest_id = parseInt($("#comment_box .rest_id").val());
  
  $("#comment_box .comment").each(function(){
    var comment_box = $(this);
    var comment_vote_box = $(".vote_box", comment_box);
    
    var comment_id = parseInt($(".vote_comment_id", comment_vote_box).val());

    if(isNaN(comment_id))
      return false;
      
    $("a.yes, a.no", comment_vote_box).each(function(){
      var link = $(this);
      link.click(function(){
        comment_vote_value = (link.hasClass("yes") ? 1 : -1);
        vote(comment_vote_value, comment_id, rest_id, comment_vote_box);
        return false;
      });
      
    });
  })
});

function vote(comment_vote_value, comment_id, rest_id, comment_vote_box) {
  comment_vote_box.fadeOut('fast');
  var data = new Object();
  
  data['datatype'] = 'json';
  data['action'] = 'vote_comment';
  data['value'] = comment_vote_value;
  data['comment'] = comment_id;
  data['rest'] = rest_id;
  $.post(
    "/ajax.php",
    data,
    function(data){
      if(data.status) {
        comment_vote_box
          .text(data.msg)
          .fadeIn('fast');
      }
      else {
        comment_vote_box
          .stop();
        comment_vote_box  
          .text("Chyba: " + data.msg)
          .css({color: 'red', opacity: 1})
          .show();
      }      
    },
    'json'
  );
}
