/******* Comments ***********/
function updateComments(post_id) {
  new Ajax.Updater('comments_'+post_id, '/blog/update_comments?post_id='+post_id, { method: 'get'});
}

function openComments(post_id) {
  $('commentControl_'+post_id).update("Kommentare ausblenden");
  $('commentControl_'+post_id).setAttribute('href', 'javascript:closeComments('+post_id+')');
  updateComments(post_id);
  $('comments_'+post_id).style.display = 'block';
}

function closeComments(post_id) {
  $('commentControl_'+post_id).update('Kommentare einblenden');
  $('commentControl_'+post_id).setAttribute('href', 'javascript:openComments('+post_id+')');
  $('comments_'+post_id).update('')
  $('comments_'+post_id).style.display = 'none';
}

function loadComment(post_id, comment_id) {
  new Ajax.Updater('comment_'+post_id+'-'+comment_id,  '/blog/load_comment?id='+comment_id, { method: 'get'});
  $('control_'+post_id+"-"+comment_id).update('<a href="javascript:closeComment('+post_id+','+comment_id+')">Weniger</a>');
}

function closeComment(post_id, comment_id) {
  new Ajax.Updater('comment_'+post_id+'-'+comment_id,  '/blog/close_comment?id='+comment_id, { method: 'get'});
  $('control_'+post_id+"-"+comment_id).update('<a href="javascript:loadComment('+post_id+','+comment_id+')">Mehr</a>');
}

function deleteComment(id, post_id) {
  var xmlHttp = createXMLHttpRequest()
  var url = "/blog/delete_comment";
  var data = {'id':id}
  xmlHttp.open("POST", url, false);
  xmlHttp.send(JSON.stringify(data));
  var res = xmlHttp.responseText;
  if (res.search(/^OK/) == -1) {
    alert("Fehler beim Löschen");
  } else {
    alert("Kommentar gelöscht");
    updateComments(post_id);
  }
}

function hideComment(id, post_id, visible) {
  new Ajax.Request("/blog/hide_comment", {
    method: 'post',
    parameters: {id:id, visible:visible},
    onSuccess: function(transport) {
      if (transport.responseText.search(/^OK/) == -1) {
        alert("Fehler beim Verstecken");
      } else {
        alert("Sichtbarkeit geändert");
        updateComments(post_id);
      }
    }});
}
/******************************/

/**** Posts ******************/
function updatePosts(offset) {
  new Ajax.Updater('blogPosts', '/blog/update?offset='+offset, { method: 'get', evalScripts: 'true'});
  new Ajax.Updater('navigation', '/blog/update_navigation?offset='+offset, {method: 'get'});
}

function openNewPost() {
  new Ajax.Updater('blogNewPost', '/blog/new', { method: 'get', evalScripts: 'true', onComplete: function(transport) {tinyMCE.execCommand('mceAddControl', false, 'text');  $('blogNewPost').style.display = 'block';}});

}

function closeNewPost() {
  $('blogNewPost').style.display = 'none';
}


function savePost(id) {
  var title = $('title').value;
  $('blogNewPost').style.display = 'none';
  var inst = tinyMCE.getInstanceById('text');
  var text = inst.getHTML();
  tinyMCE.execCommand('mceRemoveControl', true, 'text')
  var xmlHttp = createXMLHttpRequest()
  var url = "/blog/save";
  var data = {'title':title, 'text':text, 'id':id};
  xmlHttp.open("POST", url, false);
  xmlHttp.send(JSON.stringify(data));
  var res = xmlHttp.responseText;
  if (res.search(/^OK/) == -1) {
    alert("Fehler beim Speichern");
  } else {
    updatePosts(0);
  }
 
}

function editPost(id) {
  new Ajax.Updater('blogNewPost', '/blog/edit?id=' + id, { method: 'get', evalScripts: 'true', onComplete: function(transport) {tinyMCE.execCommand('mceAddControl', false, 'text');  $('blogNewPost').style.display = 'block';}});
}

function deletePost(id) {
  var xmlHttp = createXMLHttpRequest()
  var url = "/blog/delete";
  var data = {'id':id};
  xmlHttp.open("POST", url, false);
  xmlHttp.send(JSON.stringify(data));
  var res = xmlHttp.responseText;
  if (res.search(/^OK/) == -1) {
    alert("Fehler beim Löschen");
  } else {
    alert("Beitrag gelöscht");
    updatePosts(0);
  }
 
}

/*******************************/


/***** Helpers *****/
function setError(element, text) {
  element.update(text);
}


function createXMLHttpRequest() {
  var req = null;
  try {
    req = new ActiveXObject("MSXML2.XMLHTTP");
  }
  catch (err_MSXML2) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (err_Microsoft) {
      if (typeof XMLHttpRequest != "undefined") 
        req = new XMLHttpRequest;
    }
  }
  return req;
}

