var bbcode = {

/* ---------------------------------------------------------------- */

	/* Properties */

	active : true,

	targetSelector : "#formular-nachricht-zelle",
	textareaSelector : "#formular-nachricht",

	id : "bbcodeButtons",

	selfVersion : "self812",

	uri : /^(https?|ftp|news|nntp|mailto|telnet|gopher|data|file|about|chrome|feed|irc|mms|skype):/,

	ref : /^http:\/\/de\.selfhtml\.org\//,

	buttons : {
		various : [
			"Link", "Bild"
		],
		code : [
			"HTML", "CSS", "JavaScript", "PHP", "Perl", "XML", "SQL"
		]
	},


/* ---------------------------------------------------------------- */

	/* Methods */

	insertButtons : function () {

		if (!bbcode.active) return;

		var target = $(bbcode.targetSelector).get(0);
		if (!target) return;

		var div = document.createElement("div");
		div.id = bbcode.id;
		target.insertBefore(div, target.firstChild);

		var strongElement = document.createElement("strong");
		strongElement.appendChild(document.createTextNode("Formatierungen einfügen:"));
		div.appendChild(strongElement);

		div.appendChild(document.createElement("br"));

		for (var type in bbcode.buttons) {
			var buttonList = bbcode.buttons[type];
			for (var i = 0, title; title = buttonList[i]; i++) {
				var button = document.createElement("input");
				button.type = "button";
				button.value = title;
				button.id = "bb" + title;
				if (type == "code") {
					button.title = title + "-Codeblock einfügen";
					button.onclick = function () {
						bbcode.insertAtPosition("[code lang=" + this.value.toLowerCase() + "]", "[/code]");
					};
				} else if (type == "various" && title == "Link") {
					button.onclick = bbcode.insertLink;
				} else if (type == "various" && title == "Bild") {
					button.onclick = bbcode.insertImage;
				} else {
					button.onclick = function () {
						bbcode.insertAtPosition("[" + this.value.toLowerCase() + "]", "[/" + this.value.toLowerCase() + "]");
					};
				}
				div.appendChild(button);
			}
		}

	},

/* ---------------------------------------------------------------- */

	getSelectionText : function (input) {

		var selectionText = "";

		if (document.selection && document.selection.createRange) {
			selectionText = document.selection.createRange().text;
		} else if (typeof input.selectionStart == "number" && typeof input.selectionEnd == "number") {
			selectionText = input.value.substring(input.selectionStart, input.selectionEnd);
		}

		return selectionText;

	},

/* ---------------------------------------------------------------- */

	insertAtPosition : function (startTag, endTag, encloseSelection) {

		if (encloseSelection == undefined) {
			encloseSelection = true;
		}

		var input = $(bbcode.textareaSelector).get(0);
		input.focus();

		var selection, selectionText = "";

		if (document.selection && document.selection.createRange) {

			selection = document.selection.createRange();
			if (encloseSelection) {
				selectionText = selection.text;
			}
			selection.text = startTag + selectionText + endTag;

		} else if (typeof input.selectionStart == "number" && typeof input.selectionEnd == "number") {

			var oldScrollTop = input.scrollTop;

			if (encloseSelection) {
				selectionText = input.value.substring(input.selectionStart, input.selectionEnd);
			}

			var before = input.value.substring(0, input.selectionStart),
				enclosedSelection = startTag + selectionText + endTag,
				after = input.value.substring(input.selectionEnd, input.value.length);

			input.value = before + enclosedSelection + after;
			input.selectionStart = input.selectionEnd = (before + enclosedSelection).length;

			input.scrollTop = oldScrollTop;

		} else {
			input.value += startTag + "Text" + endTag;
		}

		input.focus();
	},

/* ---------------------------------------------------------------- */

	insertLink : function () {

		var input = $(bbcode.textareaSelector).get(0);
		input.focus();

		var link = {
			uri : "",
			title : ""
		};

		var selectionText = bbcode.getSelectionText(input);

		if (selectionText.length > 0) {
			if (bbcode.uri.test(selectionText)) {
				link.uri = selectionText;
			} else {
				link.title = selectionText;
			}
		}

		if (!link.uri) {
			link.uri = prompt("URI eingeben:", "http://");
			if (link.uri == undefined) return;
		}

		if (!link.title) {
			link.title = prompt("Linktitel eingeben (leer lassen für keinen Titel):", "");
			if (link.title == undefined) return;
		}

		/* Encode characters which are not allowed in the URI query part */
		var queryPartStart = link.uri.indexOf("?");
		if (queryPartStart != -1) {
			var queryPart = link.uri.substring(queryPartStart + 1);
			var newQueryPart = queryPart.replace(/[^a-z0-9;/?:@&=+$,-_.!~*'()%]/gi, function (char) {
				return encodeURIComponent(char);
			});
			if (newQueryPart != queryPart) {
				link.uri = link.uri.substring(0, queryPartStart + 1) + newQueryPart;
			}
		}

		link.uri = bbcode.ref.test(link.uri) ?
			link.uri.replace(bbcode.ref, "ref:" + bbcode.selfVersion + ";") :
			"link:" + link.uri;

		if (link.title.length > 0)
			link.title = "@title=" + link.title.replace(/]/g, "\\]");

		link.startTag = "[" + link.uri + link.title;
		link.endTag = "]";

		bbcode.insertAtPosition(link.startTag, link.endTag, false);

	},

/* ---------------------------------------------------------------- */

	insertImage : function (input) {

		var input = $(bbcode.textareaSelector).get(0);
		input.focus();

		var image = {
			uri : "",
			alt : ""
		};

		var selectionText = bbcode.getSelectionText(input);

		if (/https?:\/\//.test(selectionText)) {
			image.uri = selectionText;
		} else {
			image.alt = selectionText;
		}

		if (!image.uri) {
			image.uri = prompt("URI eingeben:", "http://");
			if (image.uri == undefined) return;
		}

		if (!image.alt) {
			image.alt = prompt("Alternativtext eingeben (leer lassen für keinen Alternativtext):", "");
			if (image.alt == undefined) return;
		}

		if (image.alt.length > 0)
			image.alt = "@alt=" + image.alt.replace(/]/g, "\\]");

		bbcode.insertAtPosition("[image:" + image.uri + image.alt, "]", false);

	}

}

/* ---------------------------------------------------------------- */

$(document).ready(bbcode.insertButtons);
