function wstawTagi(element,startTag,endTag){
    if(element == 0){
        var textArea = $("textarea.message");
    }else{
        var parent = element.parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
    }
    var len = textArea.val().length;
    var start = textArea[0].selectionStart;
    var end = textArea[0].selectionEnd;
    var selectedText = textArea.val().substring(start, end);
    var replacement = startTag + selectedText + endTag;
    textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}
$(document).ready(function(){ 
    $("div.pick").hide();
    $("div.emotki img").click(function(){
        wstawTagi(0," "+$(this).attr("alt")," ");
        return false;
    });
    $(".editor a,.emotki a").click(function(){return false;});
    
    $("a img.link").click(function(){

	    return false;
	});
	$("a img.clear").click(function(){
        var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        textArea.val(""); 
	    return false;
	});
	$("a img.bold").click(function(){
        wstawTagi($(this),"[b]","[/b]");
	    return false;
	});
	$("a img.right").click(function(){
        wstawTagi($(this),"[right]","[/right]");
	    return false;
	});
	$("a img.left").click(function(){
        wstawTagi($(this),"[left]","[/left]");
	    return false;
	});
	$("a img.justify").click(function(){
        wstawTagi($(this),"[justify]","[/justify]");
	    return false;
	});
	$("a img.center").click(function(){
        wstawTagi($(this),"[center]","[/center]");
        return false;
	});
	$("a img.italic").click(function(){
        wstawTagi($(this),"[i]","[/i]");
        return false;
	});
	$("a img.strikethrough").click(function(){
        wstawTagi($(this),"[s]","[/s]");
        return false;
	});
	$("a img.heading").click(function(){
        wstawTagi($(this),"[size=20][b]","[/b][/size]");
        return false;
	});
	$("a img.bullets").click(function(){
        wstawTagi($(this),"\n[list]\n[*]","\n[/list]\n");
        return false;
	});
	$("a img.numbers").click(function(){
        wstawTagi($(this),"\n[list=1]\n[*]","\n[/list]\n");
        return false;
	});
	$("a img.color").click(function(){
	    var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        var colorPick = $("div.colorPick",parent);
        
        colorPick.show().css("top",$(this).position().top);
	    $("div.picker",colorPick).farbtastic("input.color",colorPick);
	    colorPick.show();
	    $("a.exit",colorPick).click(function(){
	    var val = $("input.color",colorPick).val();
	    wstawTagi($(this),"[color="+val+"]","[/color]");   
	    colorPick.hide();
	      return false;
	    });
        return false;
	});
	$("a img.quote").click(function(){
	    var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        var quotePick = $("div.quotePick",parent);
        
        quotePick.show().css("top",$(this).position().top);
        var len = textArea.val().length;
        var start = textArea[0].selectionStart;
        var end = textArea[0].selectionEnd;
        var selectedText = textArea.val().substring(start, end);
        $("textarea",quotePick).val(selectedText);
	    $("a.exit",quotePick).click(function(){
	        var autor = $("input",quotePick).val();
	        var text = $("textarea",quotePick).val();
    	    if(selectedText == ""){
                wstawTagi($(this),"[quote=\""+autor+"\"]"+text+"","[/quote]");   
    	    }else{
    	      var replacement = "[quote=\""+autor+"\"]" + text + "[/quote]";
              textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
    	    }
    	    quotePick.hide();
    	    $("input",quotePick).val("");
            $("textarea",quotePick).val("");
             return false;
	    });
        return false;
	});
	$("a img.href").click(function(){
	    
	    var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        var hrefPick = $("div.hrefPick",parent);
        
        hrefPick.show().css("top",$(this).position().top);
        
        var len = textArea.val().length;
        var start = textArea[0].selectionStart;
        var end = textArea[0].selectionEnd;
        var selectedText = textArea.val().substring(start, end);
	     $("a.exit",hrefPick).click(function(){
    	    var link = $("input",hrefPick).val(); 
            if(selectedText == ""){
                wstawTagi($(this),"[url="+link+"]"+link+"","[/url]");   
    	    }else{
    	        wstawTagi($(this),"[url="+link+"]","[/url]");  
    	    }
    	    hrefPick.hide();
    	    $("input",hrefPick).val("");
             return false;
	    });
        return false;
	});
	
	$("a img.images").click(function(){
	    var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        var imgPick = $("div.imgPick",parent);
        
        imgPick.show().css("top",$(this).position().top);
         
	     $("a.exit",imgPick).click(function(){
    	    var link = $("input",imgPick).val(); 
    	    wstawTagi($(this),"[img]"+link+"","[/img]");  
    	    imgPick.hide();
    	    $("input",imgPick).val("");
             return false;
	    });
        return false;
	});
	
	
	$("a img.preview").click(function(){
	    var parent = $(this).parents("ul.for-editor-message");
        var textArea = $("textarea.message",parent);
        var previewPick = $("div.previewPick",parent);
        
        previewPick.show().css("top",$(this).position().top);
        
	    $("div.bbcode-message",previewPick).html(text);
	    $("a.exit",previewPick).click(function(){
	        previewPick.hide();
             return false;
	    });
        return false;
	});
    
});
