$(function(){
    $('.add_fast_link').click(function(){
        var mess = $(this).closest('.right');
        $('.enter_fast_title', mess).show();
        return false;
    });

    $('form.add_fast_link_form INPUT[type=submit]').click(function(){
        var form = $(this).closest('FORM');
        if ($('TEXTAREA[name=title]', form).val()!=''){
            $.post(
                '/forum/addfastlink/',
                {
                comment_id: $('INPUT[name=comment_id]', form).val(),
                title: $('TEXTAREA[name=title]', form).val()
                },
                function(result){
                    if (result!='error'){
                        window.location.reload();
                    }else{
                        form.closest('.ui-widget').html(result);
                    }
                }
            );
        }
        return false;
    });

    $('.add_fast_link_form .cancel').click(function(){
        $(this).closest('.enter_fast_title').hide();
        return false;
    });
});

    function paste(html, editor) {
		editor = tinyMCE.get(editor);
		editor.getBody().focus();
		editor.execCommand('mceInsertRawHTML', false, html); 
		return false;
	}
	
	function ans(to) {
		var text= '<p>&nbsp;</p><p><b>to ' + to + ':</b><br>&nbsp;</p>';
		paste(text,'text');	
		editor.getBody().focus();
	}
	
	function Quote(user, quoted) {
		var text;
		if (window.getSelection) {
			text = window.getSelection();
		}else if (document.selection && document.selection.createRange) {
			var range = document.selection.createRange();
    		text = range.text;
		}

		if (text == '') {
			text = quoted;
		}

		
		if (text) {
			text = text.replace(/\n/igm, ' ');
			text = text.replace(/<blockquote>.*?<\/blockquote>/igm, '');
			text = text.replace(/<p>(\s|&nbsp;)*<\/p>/igm, '');
			text = '<p>&nbsp;</p><p><blockquote><div><b>' + user + ':</b><br>'+ text + '</div></blockquote></p><p>&nbsp;</p>'; 
			paste(text,'text');		
			return;
//			html = tinyMCE.getContent('text') || "<p></p>";
			editor = tinyMCE.get('text');
			editor.execCommand('mceInsertContent', false, '<p>&nbsp;</p><p><blockquote><div><b>' + user + ':</b><br>'+ text + '</div></blockquote></p><p>&nbsp;</p>');
//			moveCursorToEnd('text');                                        
			tinyMCE.execCommand('mceFocus', false, 'text');
			editor.getBody().focus();
		} else {
//			alert('Для вставки цитаты - выделите любой текст на странице и нажмите на ссылку "цитировать" еще раз.')
		}
		return false;
	}


		tinyMCE.init({
			language : "ru",
			mode : "specific_textareas",
			textarea_trigger : 'wysiwyg',

			relative_urls : false,
			remove_script_host : false,
//			document_base_url : "/",
			content_css : "/templates/editor.css",
			
			theme : "advanced",
 			plugins: "paste,contextmenu,searchreplace,table", 
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,sub,sup,|,image,link,unlink,|,code",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",

			extended_valid_elements : "div[class],span[id],object[width|height|id|data|type],param[name|value],embed[flashVars|servicesURL|base|bgcolor|name|seamlesstabbing|swLiveConnect|pluginspage|src|type|wmode|width|height]",

			accessibility_focus : false,
			gecko_spellcheck : true,
			forced_root_block : 'p',
			width : "100%"
		});

