jQuery.fn.tinyEdit = function(settings) {
    settings = jQuery.extend({
        tags: { strong: "bold", em: "italic", blockquote: "blockquote" }
    }, settings);
    return this.each(function(i) {
        if (!jQuery(this).is("textarea")) {
            return false;
        }
        var pos = i + jQuery.tE.editors.length;
        jQuery.tE.editors[pos] = this;
        $cur = jQuery(this).before("<ul class='tinyEdit'></ul>").prev();
        $cur.append('<li class="formatMsg">Format Tools:</li>');
        var tags = settings.tags;
        for (var tag in tags) {
        	$cur.append("<li></li>")
        	.find("li:last").append("<a href='#'></a>")
        	.find("a").attr("title",tag).html(tags[tag])
        	.click(function() {
                jQuery.tE.insertTag(jQuery(this).attr("title"), pos);
                return false;
        	});
        }
    });
};
jQuery.tE = {
    editors: [],
    insertTag: function(tag, i) {
        var e = jQuery.tE.editors[i];
        if (document.selection) {
            highlighted = document.selection.createRange();
            if (highlighted.text.length > 0) {
                highlighted.text = ["<", tag, ">", highlighted.text, "</", tag, ">"].join("");
            }
        } else if (e.selectionStart || e.selectionStart == 0) {
            var alpha = e.selectionStart;
            var omega = e.selectionEnd;
            e.value = [e.value.substr(0, alpha), "<", tag, ">", e.value.substr(alpha, omega - alpha), "</", tag, ">", e.value.substr(omega, e.value.length)].join("");
        }
    }
};
