/*
Livenet.fr
*/
function confirm2(title,msg,callback) {
  $('#confirm')
    .jqmShow()
    .find('h1.jqmConfirmH1')
      .html(title)
    .end()
    .find('p.jqmConfirmMsg')
      .html(msg)
    .end()
    .find(':submit:visible')
      .click(function(){
        if(this.value == 'créer un compte')
          (typeof callback == 'string') ?
            window.location.href = callback :
            callback();
        $('#confirm').jqmHide();
      });
}

 $(document).ready(function() {
   $('#comment').submit(function() {
     if(!m_name){
        $('#confirm').jqm({overlay: 38, modal: true, trigger: false});
        confirm2('Attention&nbsp;!','<br />Vous devez être membre Livenet pour pouvoir envoyer des commentaires.<br /><br /><br />','http://www.livenet.fr/inscription');
     }else
     if($('#com_text').val().length>500){
       alert('Le texte ne doit pas dépasser les 500 caractères');
       $('#com_text').focus().css('border','3px solid red');
     }else
     if($('#com_text').val().length==0 ){
       alert('Vous devez entrer un commentaire');
       $('#com_text').focus().css('border','3px solid red');
     }else
      $.ajax({
         type: "POST",
         url: p_url+'/ajax_post_comment/'+t+type,
         data: {'content':$('#com_text').val()},
         success: function(msg){
           alert( msg );
           $comment = $('#com_text').val();
           $comment = $comment.replace(/<\/?[^>]+>/gi, '');
           $('#comment_preview').hide().html( '<div class="pr_co"><div>Par '+m_name+'</div>' + $comment + '</div>' ).fadeIn('slow');
         }
       });
     return false;
   });
   $('.delete').each(function(){
     $(this).click(function(){
       $(this).parent().parent().parent().hide();
       $.get(p_url+'/ajax_del_comment/'+$(this).parent().parent().parent().attr('id')+'/'+t);
       return false;
     });
   });
   $('.validate').each(function(){
     $(this).click(function(){
       $(this).parent().parent().parent().removeClass('pr_co2');
       $.get(p_url+'/ajax_val_comment/'+$(this).parent().parent().parent().attr('id')+'/'+t);
       $(this).html('');
       return false;
     });
   });
   $('.block').each(function(){
     $(this).click(function(){
       $.get(p_url+'/ajax_block_comment/'+$(this).parent().parent().parent().attr('id')+'/'+$(this).attr('title')+'/'+t);
       $(this).parent().parent().parent().hide();

       alert('Le membre a été bloqué, il ne pourra plus vous envoyer de commentaires');
       return false;
     });
   });
   
 $(".update").click(function() {
  var i = $(this).attr('title');
  var obj = "#comm_"+i;
  var obj_edit = "#comm_edit_"+i;
  comment = $(obj).html();
  comment = comment.replace(/<\/?[^>]+>/gi, '');
  var textarea = '<textarea rows="2" cols="68" id="comm_edit_'+i+'">'+comment+'</textarea><br />';
  var button	 = '<input type="button" value="Valider" class="saveButton" /> ou <input type="button" value="Annuler" class="cancelButton" />';
  var revert = $(obj).html();
  $(obj).html(textarea+button);
  $('.saveButton').click(function(){comm_saveChanges(obj_edit, false, i);});
  $('.cancelButton').click(function(){comm_saveChanges(obj_edit, revert, i);});
  return false;

  });
     
 });

function comm_saveChanges(obj, cancel, n) {
  if(!cancel) {
  var text = $(obj).val();
      $.ajax({
         type: "POST",
         url: p_url+'/ajax_update_comment/'+n+"/"+t,
         data: {'content':text},
         success: function(msg){
           $("#comm_"+n).html(text);
             alert( msg );
         }
       });
  }
  else {
    var text = cancel;
    $("#comm_"+n).html(text);
  }
}