function showCommentForm(parentId){
  setDefault('commentContent', 'Kommentar*');
  jQuery("#commentErrorContent").hide();
  jQuery("#commentErrorName").hide();
  jQuery("#commentErrorEmail").hide();
  fbId = FB.Connect.get_loggedInUser();
  if(fbId) {
    fbConnected(fbId);
  } else {
    fbDisconnected(fbId);
  }
  
  document.getElementById("parentComment").value = parentId;
  showPopUp('#writeComment');
}

function submitComment() {

  var fbId = jQuery("#fbId").val();
  var postToFb = jQuery("#postToFb:checked");
  if(FB.Connect.get_loggedInUser() && fbId && postToFb && (!publishStreamPermission)){
    FB.Connect.showPermissionDialog("publish_stream", function(result) {
      submitCommentQuery();
    }, true);
  } else {
    submitCommentQuery();
  }
}
function submitCommentQuery(){
  var parentId = jQuery("#parentComment").val();
  var comment = jQuery("#commentContent").val();
  comment = encodeURIComponent(comment);
  var email = jQuery("#commentEmail").val();
  email = encodeURIComponent(email);
  var oname = jQuery("#commentName").val();
  oname = encodeURIComponent(oname);
  postToFb = jQuery('#postToFb').attr('checked') ? '&postToFb=1' : '';
  notify = jQuery('#notify').attr('checked') ? '&notify=1' : '';
  
  jQuery.ajax({
    type: "GET",
    dataType: 'json',
    url: "newComment.php",
    data: 'parent='+parentId+'&comment='+comment+'&email='+email+'&name='+oname+'&fbId='+fbId+postToFb+notify,
    success: function(data) {
      jQuery("#commentForm").show();
      jQuery("#submittingIndicator").hide();
      if(data.error) {
        if(data.errorComment) {
          jQuery("#commentErrorContent").show();
        }
        if(data.errorName) {
          jQuery("#commentErrorName").show();
        }
        if(data.errorEmail) {
          jQuery("#commentErrorEmail").show();
        }
      } else {
/*
        var commentBoxBig = document.getElementById("ajaxCommentsBig");
        var commentBoxSmall = document.getElementById("ajaxCommentsSmall");
        commentBoxBig.innerHTML = data.comments;
        commentBoxSmall.innerHTML = data.comments;
*/

        jQuery("#ajaxNrComments").html("&#124; &raquo; "+data.nrComments+" Kommentare zu dieser Seite");
        jQuery("#ajaxCommentsBig").html(data.comments);
//        jQuery("#ajaxCommentsSmall").html(data.comments);
/*
        if(FB.Connect.get_loggedInUser()) {
          var attachment = {'name': '{*actor*} hat einen Kommentar zur Ottogroup Karriere Seite geschrieben.',
          'href' : 'http://www.ottogroup-karriere.de'};
          var links = {'text': 'Jetzt besuchen',
          'href' : 'http://www.ottogroup-karriere.de'};
          FB.Connect.streamPublish('', attachment, links);
          }
*/          
          
          hideAllPopUp();
        if ( FB.XFBML.Host.parseDomTree )
          setTimeout( FB.XFBML.Host.parseDomTree, 0 );
//      FB.XFBML.Host.refresh();
      }
    }
  });
  jQuery("#commentErrorContent").hide();
  jQuery("#commentErrorName").hide();
  jQuery("#commentErrorEmail").hide();
  jQuery("#commentForm").hide();
  jQuery("#submittingIndicator").show();
  return false;
}

function showDeleteForm(commentId, isFb){
  jQuery(".deleteFormReset").hide();
  isFb = typeof(isFb) == 'undefined' ? false : isFb >0;
  fbUser = FB.Connect.get_loggedInUser();
  
  if(isFb){
    if(fbUser) {
      jQuery('#deleteFormFB').show();
    } else {
      jQuery('#deleteLogin').show();
    }
  } else {
    jQuery('#deleteEmailContainer').show();
  }
  jQuery("#deleteSendButton").show();
  document.getElementById("deleteId").value = commentId;
  showPopUp('#deleteComment');
  return false;
}

function deleteComment(){
  jQuery("#deleteErrorNotFound").hide();
  jQuery("#deleteErrorWrongUser").hide();
  jQuery("#deleteErrorWrongEmail").hide();
  jQuery("#deleteErrorNotLoggedIn").hide();
  jQuery("#deleteErrorNotLoggedIn").hide();
  jQuery("#deleteCloseButton").hide();
  jQuery("#deleteSendButton").show();
  var commentId = jQuery("#deleteId").val();
  var email = jQuery("#deleteEmail").val();
  email = encodeURIComponent(email);
  jQuery.ajax({
    type: "GET",
    dataType: 'json',
    url: "deleteComment.php",
    data: 'cId='+commentId+'&email='+email,
    success: function(data) {
      jQuery("#deletingIndicator").hide();
      if(data.error) {
        if(data.notFound) {
          jQuery("#deleteErrorNotFound").show();
        }
        if(data.wrongUser) {
          jQuery("#deleteErrorWrongUser").show();
        }
        if(data.wrongEmail) {
          jQuery("#deleteErrorWrongEmail").show();
          jQuery("#deleteEmailContainer").show();
        }
        if(data.notLoggedIn) {
          jQuery("#deleteErrorNotLoggedIn").show();
        }
      } else {
        if(data.emailSent) {
          jQuery("#deleteEmailSent").show();
          jQuery("#deleteSendButton").hide();
          jQuery("#deleteCloseButton").show();
        } else {
/*          
          var commentBoxBig = document.getElementById("ajaxCommentsBig");
          var commentBoxSmall = document.getElementById("ajaxCommentsSmall");
          commentBoxBig.innerHTML = data.comments;
          commentBoxSmall.innerHTML = data.comments;
*/          
          jQuery("#ajaxCommentsBig").html(data.comments);
          jQuery("#ajaxNrComments").html("&#124; &raquo; "+data.nrComments+" Kommentare zu dieser Seite");
//          jQuery("#ajaxCommentsSmall").html(data.comments);
          hideAllPopUp();
          if ( FB.XFBML.Host.parseDomTree )
            setTimeout( FB.XFBML.Host.parseDomTree, 0 );
//          FB.XFBML.Host.refresh();
        }
      }
    }
  });
  jQuery("#deletingIndicator").show();
  return false;
}
  
  
function setDefault(elemId, defaultText) {
  var node = jQuery('#'+elemId);
  node.blur(function(event) {
    var node = jQuery(this);
    if(node.val() == '') {
      node.val(defaultText);
      node.addClass('defaultInputValue');
    }
  });
  node.focus(function(event) {
    var node = jQuery(this);
    node.removeClass('defaultInputValue');
    if(node.val() == defaultText) {
      node.val('');
    }
  });
  node.val(defaultText);
  node.addClass('defaultInputValue');
}

function showBigComments() {
  id = 'div#ajaxCommentsBig div.box-content-small';
  var viewportHeight = window.innerHeight ? window.innerHeight : jQuery(window).height();  
  popupHeight = (viewportHeight - 150);
  jQuery(id).css({"height": popupHeight });
  showPopUp('#commentBox', true);
  
}
