$().ready(function() {
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/script/tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist,file",

		// Theme options
		theme_advanced_buttons1 : 
"save,undo,redo,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,styleselect,formatselect,",
		theme_advanced_buttons2 : 
"bold,italic,underline,removeformat,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,|,link,unlink,image,file,media",
		theme_advanced_buttons3 : 
"tablecontrols,|,visualaid,visualchars,nonbreaking,pagebreak,template,code",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal: false,
		theme_advanced_resizing_min_height: '500px',
		theme_advanced_resizing_max_height: '1000px',
		height: '600px',
		width: '100%',

		content_css : '/css/backend/tiny_mce.css',
                                                                     
		// Drop lists for link/image/media/template dialogs
		external_link_list_url : '/script/lists/links.js',
		external_image_list_url : '/script/lists/images.js',
		media_external_list_url : '/script/lists/media.js',

		// Replace values for the template plugin
		template_external_list_url : '/script/lists/templates.js',
		template_cdate_classes : 'cdate',
		template_mdate_classes : 'mdate',
		template_selected_content_classes : 'selcontent',
		template_cdate_format : '%d.%m.%Y : %H:%M:%S',
		template_mdate_format : '%d.%m.%Y : %H:%M:%S',
		template_replace_values : {
			username : 'Some User',
			staffid : '991234'
		},

		save_onsavecallback: 'save_callback',
		save_enablewhendirty: true,

		paste_auto_cleanup_on_paste: true,
		paste_preprocess: 'paste_preprocess',
		paste_strip_class_attributes: 'all',
		accessibility_warnings: false,
		file_browser_callback: 'filebrowser'
	});
});
function paste_preprocess(pl,o){
	s = o.content;
	s = s.replace(/<h(\d)[^>]*><a[^>]*>([^<]*)<\/a><\/h\d>/g,'<h$1>$2</h$1>');
	s = s.replace(/<\/?div[^>]*>/g,'').replace(/ rel="nofollow"/g,'');
	s = s.replace(/<\/ul>\s*<ul>/g,'').replace(/<\/ol>\s*<ol>/g,'');
	s = s.replace(/<p[^>]*>\s*/g,'<p>').replace(/\s*<\/p>\s*/g,'</p>\n');
	//s = s.replace(/<img[^>]*>/g,''); //fix autoimport
	s = s.replace(/bdquo/g,'WTF');
	o.content = s;
}
function save_callback(e){
	e.setProgressState(1);
	$.ajax({
		type: 'POST',
		url: jQuery(location).attr('href')+'&save',
		data: { content: e.getContent() },
		success: function(data, textStatus, jqXHR){
			e.setProgressState(0);
			//todo: save updated content (grey save button)
		}
	});
}
function filebrowser(field_name, url, type, win) {
	tinyMCE.activeEditor.windowManager.open({
		file : "/script/filebrowser/mfm.php?field=" + field_name + "&url=" + url + "",
		title : 'File Browser',
		width : 640,
		height : 450,
		resizable : "no",
		inline : "yes",
		close_previous : "no"
	}, {
		window : win,
		input : field_name
	});
	return false;
}


