var beta_project = '_default/'; /* mimo beta projekty ponechat prázdné */

jQuery.noConflict();

jQuery(document).ready(function() {
  if(jQuery('#discussion')[0]) {
    jQuery('#link-discussion-form').click(function() {
      jQuery('#discussion form').slideDown('fast');
      return false;
    });
    jQuery('.link-reply').live('click', reply);
    jQuery('.link-delete').live('click', function() {
      deleteComment(jQuery(this).closest('.comment').attr('id').match(/\d+$/), parseInt(jQuery(this).closest('.comment').attr('class').match(/\d+$/))-1);
      hideReactions(jQuery(this).closest('.comment').attr('id').match(/\d+$/), parseInt(jQuery(this).closest('.comment').attr('class').match(/\d+$/)));
      return false;
    });
    jQuery('.link-show-reactions').live('click', function() {
      showReactions(jQuery(this).closest('.comment').attr('id').match(/\d+$/), parseInt(jQuery(this).closest('.comment').attr('class').match(/\d+$/))+1);
      jQuery(this).hide();
      jQuery(this).next().show();
      return false;
    });
    jQuery('.link-hide-reactions').live('click', function() {
      hideReactions(jQuery(this).closest('.comment').attr('id').match(/\d+$/), parseInt(jQuery(this).closest('.comment').attr('class').match(/\d+$/)));
      jQuery(this).hide();
      jQuery(this).prev().show();
      return false;
    });
  }
  jQuery('.poll form').addClass('vote-links'); /* odkomentovat, pokud mají být odpovědi odkazy */
  jQuery('.poll .vote-links label').click(function() {
    pollVote(jQuery(this).parents('ul').next().val(), jQuery(this).prev().val());
  });
  jQuery('#nav > ul > li').hover(
    function(){
      jQuery(this).children('ul').show();
    },
    function(){
      jQuery(this).children('ul').hide();
    }
  );
});

function reply() {
  var id = jQuery(this).closest('.comment').attr('id').match(/\d+$/);
  jQuery('#discussion form').slideDown('fast');
  jQuery('#input-id').attr('value', id);
  jQuery('html, body').animate({
    scrollTop: jQuery("#discussion").offset().top
  }, 'fast');
  return false;
}

function showReactions(id, level) {
  jQuery.ajax({
    type: 'POST',
    url: '/' + beta_project + 'ajax-diskuze',
    data: 'id=' + id + '&level=' + level,
    dataType: 'json',
    success: function(data) {
      jQuery('#comment-' + id).after(data);
    }
  });
}

function hideReactions(id, level) {
  for(i=1; i>0; i++) {
    if(jQuery('#comment-' + id).nextAll('.level' + (level+i))[0]) {
      jQuery('#comment-' + id).nextAll('.level' + (level+i) + ':gt(0)').each(function() {
        if(parseInt(jQuery(this).prev().attr('class').match(/\d+$/))>=(level+i)) {
          jQuery(this).remove();
        }
      });
      jQuery('#comment-' + id).nextAll('.level' + (level+i) + ':first').remove();
    } else {
      break;
    }
  }
}

function deleteComment(id, level) {
  jQuery.ajax({
    type: 'POST',
    url: '/' + beta_project + 'ajax-diskuze',
    data: 'id=' + id + '&delete=1',
    dataType: 'json',
    success: function(data) {
      jQuery('#comment-' + id).prevAll('.level' + level + ':first').find('.comment-info').html(data);
      jQuery('#comment-' + id).remove();
    }
  });
}

function pollVote(poll, answer) {
  if(jQuery('#poll-' + poll + ' input[name=answer]:checked')[0]) {
    var answer = jQuery('#poll-' + poll + ' input[name=answer]:checked').val();
  }
  if(answer) {
    jQuery.ajax({
      type: 'POST',
      url: '/' + beta_project + 'ajax-ankety',
      data: 'poll=' + poll + '&answer=' + answer,
      dataType: 'json',
      success: function(data){
        jQuery('#poll-' + poll + ' ul').html(data);
        jQuery('#submit-vote-' + poll).remove();
      }
    });
  }
  return false;
}

