/*
Datei bietet alle notwendigen Funktionen, um die Conversationen auf MuR zuverlässig zu managen
 */
var content;

function cancelEdit() {
    document.getElementById('edit-post-id').value = "";

    SwitchEditor();
    document.getElementById('post_text').value = "";
    SwitchEditor();

    document.getElementById('create-post-submit').style.display="block";
    document.getElementById('edit-post-submit').style.display="none";
}
function removePost(postId, modul) {
    var result = confirm('Diesen Kommentar wirklich verstecken?');

   	if (result == true) {
        jQuery.post("/engine/comment/remove/"+postId+"/", {modul:modul}, function() { window.location.reload(); })
   	} else {
   		return false;
   	}
}
function editPost(postId) {
    id = "post-raw-" + postId;

    var content = document.getElementById(id).innerHTML;

    document.getElementById('edit-post-id').value = postId;

    if(document.getElementById('create-post-submit') != undefined) {
        document.getElementById('create-post-submit').style.display="none";
    }
    if(document.getElementById('edit-post-submit') != undefined) {
        document.getElementById('edit-post-submit').style.display="block";
    }


    SwitchEditor();
    document.getElementById('post_text').value = content;
    SwitchEditor();

    self.location.href="#post-editor";

}
function quotePost(postId) {
    id = "post-raw-" + postId;

    var content = document.getElementById(id).innerHTML;
    var author =  document.getElementById(id).title;
    // content = content.replace(/\[(.*?)\]/gi,"");

    var bbCode = "[quote="+author+"]" + content + "[/quote]";

    SwitchEditor();
    document.getElementById('post_text').value = bbCode + "\n" + document.getElementById('post_text').value;
    SwitchEditor();

    self.location.href="#post-editor";
}
function postHide(postId) {

    if(confirm("Diesen Post wirklich verstecken?") == true) {
        jQuery.post(window.location.href, "action=delete_post&postid=" + postId, function(data) {
           // console.log(data.postid);
            jQuery('#post-container-' + data.postid).slideUp();
        }, 'json');
    }

}
var oldTitle = false;
var thread_id;

/* Stammtisch Thread-Editor */
function activateStammtischTitleEditor(param_thread_id) {
    if(oldTitle !== false) {
        return;
    }

    thread_id = param_thread_id;
    var threadTitle = jQuery("#forum_thread_title").html();
    oldTitle = threadTitle;

    jQuery("#forum_thread_title").html("<input type='text' name='forum_thread_title_input' id='forum_thread_title_input' value='" + threadTitle + "'><input type='button' name='' onclick='saveStammtischTitleEditor(this);' value='Speichern'><input type='button' name='' onclick='cancelStammtischTitleEditor(this);' value='Abbrechen'>");

    jQuery("#forum_title_editor_icon").css("display", "none");

}
function cancelStammtischTitleEditor() {
    jQuery("#forum_thread_title").html(oldTitle);
    jQuery("#forum_title_editor_icon").css("display", "inline");
    oldTitle = false;

}
function saveStammtischTitleEditor() {
    jQuery.post("/engine/stammtisch/changethreadtitle/", {thread_id:thread_id, title:jQuery('#forum_thread_title_input').val()}, function() {
        oldTitle = false;

        jQuery("#forum_thread_title").html(jQuery('#forum_thread_title_input').val());
        jQuery("#forum_title_editor_icon").css("display", "inline");
    });
}

function activateTitleEditor(param_thread_id) {
    if(oldTitle !== false) {
        return;
    }

    thread_id = param_thread_id;
    var threadTitle = jQuery("#forum_thread_title").html();
    oldTitle = threadTitle;

    jQuery("#forum_thread_title").html("<input type='text' name='forum_thread_title_input' id='forum_thread_title_input' value='" + threadTitle + "'><input type='button' name='' onclick='saveTitleEditor(this);' value='Speichern'><input type='button' name='' onclick='cancelTitleEditor(this);' value='Abbrechen'>");

    jQuery("#forum_title_editor_icon").css("display", "none");

}
function cancelTitleEditor() {
    jQuery("#forum_thread_title").html(oldTitle);
    jQuery("#forum_title_editor_icon").css("display", "inline");
    oldTitle = false;

}
function saveTitleEditor() {
    jQuery.post("/engine/forum/changetitle/", {thread_id:thread_id, title:jQuery('#forum_thread_title_input').val()}, function() {
        oldTitle = false;

        jQuery("#forum_thread_title").html(jQuery('#forum_thread_title_input').val());
        jQuery("#forum_title_editor_icon").css("display", "inline");
    });
}
String.prototype.nl2br = function() {
	var br;
	if( typeof arguments[0] != 'undefined' ) {
		br = arguments[0];
	}
	else {
		br = '<br />';
	}
	return this.replace( /\r\n|\r|\n/g, br );
}

String.prototype.br2nl = function() {
	var nl;
	if( typeof arguments[0] != 'undefined' ) {
		nl = arguments[0];
	}
	else {
		nl = '';
	}
	return this.replace( /\<br(\s*\/|)\>/g, nl );
}


