(function($){
    $.fn.extend({
        insertAtCaret: function(myValue, dontFocus){
            return this.each(function(i){
                if (document.selection)
                {
                    // IE
                    this.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                    if (dontFocus == undefined)
                        this.focus();
                }
                else if (this.selectionStart || this.selectionStart == "0")
                {
                    // FF, WebKit
                    var startPos = this.selectionStart;
                    var endPos = this.selectionEnd;
                    var scrollTop = this.scrollTop;
                    this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos,this.value.length);
                    if (dontFocus == undefined)
                        this.focus();
                    this.selectionStart = startPos + myValue.length;
                    this.selectionEnd = startPos + myValue.length;
                    this.scrollTop = scrollTop;
                }
                else
                {
                    this.value += myValue;
                    if (dontFocus == undefined)
                        this.focus();
                }
            })
        },
        wrapAtCaret: function(open, close){
            return this.each(function(i){
                if (document.selection)
                {
                    // IE
                    this.focus();
                    sel = document.selection.createRange();
                    sel.text = open + sel.text + close;
                    this.focus();
                }
                else if (this.selectionStart || this.selectionStart == "0")
                {
                    // FF, WebKit
                    var startPos = this.selectionStart;
                    var endPos = this.selectionEnd;
                    var scrollTop = this.scrollTop;
                    this.value = this.value.substring(0, startPos) + open + this.value.substring(startPos, endPos) + close + this.value.substring(endPos,this.value.length);
                    this.focus();
                    this.selectionStart = startPos + (open.length + endPos - startPos + close.length);
                    this.selectionEnd = startPos + (open.length + endPos - startPos + close.length);
                    this.scrollTop = scrollTop;
                }
                else
                {
                    this.value += open + close;
                    this.focus();
                }
            })
        }
    });
})(jQuery);

$(function(){
    // Тэги
    $.each(["b", "i", "s", "u"], function(i, tag){
        $(".tag-helper-" + tag).click(function(){
            $(".comments-form textarea").wrapAtCaret("<" + tag + ">", "</" + tag + ">");
            return false;
        });
    });
    $(".tag-helper-a").click(function(){
        $(".comments-form textarea").wrapAtCaret("<a href=\"\">", "</a>");
        return false;
    });
    $(".tag-helper-img").click(function(){
        $(".comments-form textarea").wrapAtCaret("<img src=\"", "\" />");
        return false;
    });
    $(".tag-helper-pre").click(function(){
        $(".comments-form textarea").wrapAtCaret("<pre>", "</pre>");
        return false;
    });
    $(".tag-helper-quote").click(function(){
        $(".comments-form textarea").wrapAtCaret("<quote>", "</quote>");
        return false;
    });
    
    // Загрузка изображений
    $(".image-uploader").click(function(e){
        document.domain = "thelogin.ru";
        window.open("http://i.thelogin.ru/?from=thelogin.ru", "", "width=400,height=300,left=" + e.screenX + ",top=" + (e.screenY - 350) + ",scrollbars=yes,resizable=yes,toolbar=no,location=yes");
        return false;
    });
    
    // Цитата выделенного
    var quoteSelected = function(){
        var selectedText = "";
        if (window.getSelection)
        {
            selectedText = window.getSelection();
        }
        else if (document.getSelection)
        {
            selectedText = document.getSelection();
        }
        else if(document.selection)
        {
            selectedText = document.selection.createRange().text;
        }

        if (selectedText != "")
        {
            $(".comments-form textarea").insertAtCaret("<quote>" + selectedText + "</quote>\n", true);
        }

        return false;
    };
    $(document).bind("keydown", "Ctrl+Return", quoteSelected);
    $(".comment .quote-selected").click(function(){
        $(this).addClass("clicked");
        quoteSelected();

        return false;
    });
    $(".comment .quote-comment").click(function(){
        $(this).addClass("clicked");
        $(".comments-form textarea").insertAtCaret("<quote>" + $(this).parent().parent().find(".text").data("plaintext") + "</quote>\n", true);

        return false;
    });
});

