$(document).ready(function() {
	$('textarea.tinymce, textarea.editor').tinymce({
		// Location of TinyMCE script
		script_url : '/assets/tiny_mce/tiny_mce.js',
		
		mode : "exact",
		elements : 'nourlconvert',
		convert_urls : false,
		document_base_url : site_url,


		// 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",
		//plugins : "autoresize,inlinepopups",
		plugins : "advlink,paste,template,inlinepopups,advimage,table,media",
		height: '300px',
		paste_auto_cleanup_on_paste: true,
		paste_strip_class_attributes: 'all',
		paste_remove_spans: true,
		paste_remove_styles: true,
		

		// Theme options
		//theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		//theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		//theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",

		theme_advanced_buttons1 : "formatselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,|,image,media,link,unlink,|,template",
		theme_advanced_buttons2 : "tablecontrols,|,hr,cleanup",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		//theme_advanced_statusbar_location : "bottom",
		//theme_advanced_resizing : true,

		//theme_advanced_toolbar_location : "external",
		theme_advanced_blockformats : "p,h1,h2,h3",
		
		// Example content CSS (should be your site CSS)
		content_css : "/assets/css/editor.css",
		
		template_templates : [{
			title : "Indhold med venstre kolonne",
			src : "/assets/tiny_mce/templates/test.htm",
			description : "Indhold med venstre kolonne"
		}],

		
		// Drop lists for link/image/media/template dialogs
		//template_external_list_url : "lists/template_list.js",
		external_link_list_url : "http://admin.bubble.dk/content/link/tinymce",
		external_image_list_url : "/media/file/tinymce/image"
		//media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		//template_replace_values : {
		//	username : "Some User",
		//	staffid : "991234"
		//},

		//handle_event_callback : caller,
		

		//setup : function(ed){
			//ed.onPreInit.add(function(ed, e){alert('onPreInit')});
			//ed.onBeforeRenderUI.add(function(ed, e){alert('onBeforeRenderUI')});
			//ed.onPostRender.add(function(ed, e){alert('onPostRender')});
			//ed.onRemove.add(function(ed, e){alert('onRemove')});
			//ed.onActivate.add(function(ed, e){alert('onActivate')});
			//ed.onDeactivate.add(function(ed, e){alert('onDeactivate')});
			//ed.onClick.add(function(ed, e){alert('onClick')});
			//ed.onEvent.add(function(ed, e){alert('onEvent')});
			
			//ed.onExecCommand.add(function(ed, e){myCustomInitInstance(ed.id)});

			
			
			//ed.onInit.add(function(ed) { blurevent(ed); });
			//ed.onNodeChange.add(function(ed, e){myCustomInitInstance(ed.id)});
		//}
	});
});

$(document).ready(function() {
	$('textarea.editor-simple').tinymce({
		// Location of TinyMCE script
		script_url : '/assets/tiny_mce/tiny_mce.js',
		
		mode : "exact",
		elements : 'nourlconvert',
		convert_urls : false,
		document_base_url : site_url,


		// 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",
		//plugins : "autoresize,inlinepopups",
		plugins : "advlink,paste,inlinepopups",
		height: '200px',
		paste_auto_cleanup_on_paste: true,
		paste_strip_class_attributes: 'all',
		paste_remove_spans: true,
		paste_remove_styles: true,
		

		// Theme options
		//theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		//theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		//theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",

		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,bullist,numlist,|,link,unlink",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		//theme_advanced_statusbar_location : "bottom",
		//theme_advanced_resizing : true,

		//theme_advanced_toolbar_location : "external",
		theme_advanced_blockformats : "p,h1,h2,h3",
		
		// Example content CSS (should be your site CSS)
		content_css : "/assets/css/editor.css",
		
		template_templates : [{
			title : "Indhold med venstre kolonne",
			src : "/assets/tiny_mce/templates/test.htm",
			description : "Indhold med venstre kolonne"
		}],

		
		// Drop lists for link/image/media/template dialogs
		//template_external_list_url : "lists/template_list.js",
		external_link_list_url : "http://admin.bubble.dk/content/link/tinymce",
		external_image_list_url : "/media/file/tinymce/image"
		//media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		//template_replace_values : {
		//	username : "Some User",
		//	staffid : "991234"
		//},

		//handle_event_callback : caller,
		

		//setup : function(ed){
			//ed.onPreInit.add(function(ed, e){alert('onPreInit')});
			//ed.onBeforeRenderUI.add(function(ed, e){alert('onBeforeRenderUI')});
			//ed.onPostRender.add(function(ed, e){alert('onPostRender')});
			//ed.onRemove.add(function(ed, e){alert('onRemove')});
			//ed.onActivate.add(function(ed, e){alert('onActivate')});
			//ed.onDeactivate.add(function(ed, e){alert('onDeactivate')});
			//ed.onClick.add(function(ed, e){alert('onClick')});
			//ed.onEvent.add(function(ed, e){alert('onEvent')});
			
			//ed.onExecCommand.add(function(ed, e){myCustomInitInstance(ed.id)});

			
			
			//ed.onInit.add(function(ed) { blurevent(ed); });
			//ed.onNodeChange.add(function(ed, e){myCustomInitInstance(ed.id)});
		//}
	});
});

$(window).click(function(){
	myCustomInitInstance(null);
});

var active_editor_id = null;

function blurevent(ed){
 tinyMCE.dom.Event.add(ed.getWin(), "blur", function(e){
	 $('#' + ed.id + "_external").hide();
	 active_editor_id = null;
 });
}

$(window).scroll(function(){
	myCustomInitInstance(active_editor_id);
});

function caller(e, ed) {
	myCustomInitInstance(ed.id);
}

function myCustomInitInstance(editor_id) {
	active_editor_id = editor_id;
	
	if(active_editor_id != null) {
		var mce_table = $('#' + active_editor_id + '_tbl');
		var mce_toolbar = $('#' + active_editor_id + '_external');

		var toTopOfPage = $(this).scrollTop();
		var toolbarHeight = 28;
		var layoutHeight = mce_table.height();
		var from = mce_table.offset().top - 28;
		var to = from + layoutHeight - 1;

		$('.mceExternalToolbar').parent().css('background-color', 'red');

		if(toTopOfPage < from) {
			mce_toolbar.css('position', 'absolute');
			mce_toolbar.css('top', '-' + toolbarHeight + 'px');
			mce_toolbar.css('left', 'auto');
			mce_toolbar.css('right', '0px');	
		} else if(toTopOfPage > to) {
			mce_toolbar.css('position', 'absolute');
			mce_toolbar.css('top', layoutHeight - toolbarHeight - 1);
			mce_toolbar.css('left', 'auto');
			mce_toolbar.css('right', 0);
		} else {
			var left = mce_toolbar.offset().left;
			mce_toolbar.css('position', 'fixed');
			mce_toolbar.css('top', 0);
			mce_toolbar.css('left', left);
			mce_toolbar.css('right', 'auto');
		}
	}
}
