$(document).ready(function(){

  // New Comment Funktion
  $('#new_comment_link a').click(function() {
     
    $("#dialog").dialog({
      'height': 300,
      'width': 400,
      'closeOnEscape': 1,
      'modal': 0,
      'closeText': 'Close'
    });


    $.ajax({
      url: $(this).attr('href')+'&eID=new_comment',
      cache: false,
      success: function(html){
        $("#dialog").html(html);
      }
    });

    return false;


  });

  // Show Comments Funktion
  $('#show_comments_link a').click(function() {
    var linktext = $(this).html();
    var r = new RegExp(/\((.+)\)/);
    var count_items = r.exec(linktext)[1];

    if($(this).hasClass('isopen')) {

      $("#comment_list").hide();
      $(this).html('Kommentare ansehen ('+count_items+')');
      $(this).removeClass('isopen');
    } else {
      $.ajax({
        url: $(this).attr('href')+'&eID=show_comments',
        cache: false,
        success: function(html){
          $("#comment_list_row").html(html);
        }
      });

      $(this).html('Kommentare verbergen ('+count_items+')');
      

      var comment_list = $("#comment_list");
      $('#Categories').prepend(comment_list);
      $("#comment_list").show();


      $(this).addClass('isopen');
    }
    return false;

  });

  $('#category_tab_comments').click(function() {
    var linktext = $('#show_comments_link a').html();
    var r = new RegExp(/\((.+)\)/);
    var count_items = r.exec(linktext)[1];


    $('#show_comments_link a').html('Kommentare ansehen ('+count_items+')');
    $('#show_comments_link a').removeClass('isopen');

    $("#comment_list").hide();
  });



  // Like Button Funktion
  $('.video_like a').click(function() {

    insert_vote($(this).attr('href'),'1');

    // Remove Button
    if($(this).parent().hasClass('single_like')) {
      $(this).parent().attr('onmouseover', 'SetDisplay(\'VoteTomorrow\',\'block\');');
      $(this).parent().attr('onmouseout', 'SetDisplay(\'VoteTomorrow\',\'none\');');
      $(this).replaceWith('<img src="fileadmin/templates/image/ButtonLikeMedium_de.png" />');
      

    } else {
     $(this).parent().attr('onmouseover', 'SetDisplay(\'VoteTomorrow\',\'block\');');
     $(this).parent().attr('onmouseout', 'SetDisplay(\'VoteTomorrow\',\'none\');');
     $(this).replaceWith('<img src="fileadmin/templates/image/ButtonLike_de.png" />');
     
    }
    
    return false;
  });



});

function insert_vote(url1,useurl) {

    if(useurl) {
      var url = url1;
    } else {
      var url = document.URL;
    }      

    $("#votedialog").dialog({
      'height': 190,
      'width': 400,
      'closeOnEscape': 1,
      'modal': 0,
      'closeText': 'Close',
      'dialogClass': 'votebox'
    });
    
    $.ajax({
      url: url+'&eID=send_vote',
      cache: false,
      success: function(html){
        $("#votedialog").html(html);
      }
    });
 
}


// Insert the Comments
function insert_comment() {

  var video = $("#insert_comment_form > input[name='tx_xozmmvideos_pi1[video]']").val();
  var comment = $("#insert_comment_form > textarea[name='tx_xozmmvideos_pi1[comment]']").val();
  var comment2 = comment.replace(/\n/g, '<br />');

  var linktext = $('#show_comments_link a').html();
  var r = new RegExp(/\((.+)\)/);
  var count_items = r.exec(linktext)[1];


  $.ajax({
    url: 'index.php?id=1&eID=insert_comment',
    cache: false,
    type: 'POST',
    data: ({
      'tx_xozmmvideos_pi1[comment]' : comment2,
      'tx_xozmmvideos_pi1[video]' : video
    }),
    success: function(html){
      $("#dialog").html(html);
    }
  });
  if( $('#comment_list').is(':visible') ) {
       
    $.ajax({
      url: 'index.php?id=1&eID=show_comments&tx_xozmmvideos_pi1[video]='+video,
      cache: false,
      success: function(html){
        $("#comment_list .comment_list_row").html(html);
      }
    });

    $('#show_comments_link a').html('Kommentare verbergen ('+(count_items*1+1)+')');
  } else {
    $('#show_comments_link a').html('Kommentare ansehen ('+(count_items*1+1)+')');
  }

  return false;
}

