
// Some global instances
var tinymce;
var tinyMCE;
var WM;


// Dialog Object
var WMDialog = {
	keyHandler: null
}

// init references
if(typeof(frameElement) != "undefined" && frameElement) {
	if(frameElement.IPopup) {
		WM = frameElement.IPopup.WindowManager;
		if(document.title)
			WM.setTitle(frameElement.IPopup.id, document.title);	
		frameElement.IPopup.params.Dialog = WMDialog;
	}
}

if(!tinymce) {
		document.write('<script type="text/javascript" src="/ib/core/libraries_ext/tinymce/jscripts/tiny_mce/tiny_mce_src.js"></script>');
}
if(!WM) {
		document.write('<script type="text/javascript" src="/ib/core/libraries_ext/tinymce/jscripts/tiny_mce/plugins/inlinepopups/standalone_src.js"></script>');
}


// init events
if(typeof(frameElement) != "undefined" && frameElement) {
	frameElement.onresize = function(pmEvent) {
			document.fire("ib:resize");
		};
}
Event.observe(window, "resize", function(e) {
		document.fire("ib:resize");
	}
);

document.observe('keydown', function(pmEvent) {

		var frontWindow;
		if(WM && WM.windows) {
			// Find front most window
			var index=0;
			$H(WM.windows).each(function(w) {
				if (w.value.zIndex > index) {
					frontWindow = w.value;
					index = frontWindow.zIndex;
				}
			});
		}


		// Dialog vorhanden
		if(frontWindow) {

			// ESC, Strg+W
			var escapeKey;
			if (pmEvent.DOM_VK_ESCAPE) {  // mozilla
				escapeKey = pmEvent.DOM_VK_ESCAPE;
			} else { // ie
				escapeKey = 27;
			}
			if(pmEvent.keyCode == escapeKey || (pmEvent.ctrlKey && 87 == pmEvent.keyCode)) {
				frontWindow.close();
				pmEvent.stop();
				return;
			}

		}


		// old wack
		/*
			schleife ueber alle event-handler
			1: behandelt, aber weiter loopen (keine defaults wie reload oder save abfangen)
			2: behandelt, schleife abbrechen
		*/


		// Dialog vorhanden
		if(frontWindow) {
			if(frontWindow.params && frontWindow.params.Dialog && frontWindow.params.Dialog.keyHandler) {
				frontWindow.params.Dialog.keyHandler(pmEvent);
			}
			return;
		}
		

		if(WMDialog.keyHandler)
			WMDialog.keyHandler(pmEvent);




		if(pmEvent.ctrlKey && pmEvent.shiftKey) { 
			if(80 == pmEvent.keyCode) { // p
				document.location.href = document.location.href + "&Mode=preview";
				pmEvent.stop();
			}
			if(69 == pmEvent.keyCode) { // e
				document.location.href = document.location.href + "&Mode=edit";
				pmEvent.stop();
			}
			if(82 == pmEvent.keyCode) { // r
				location.reload();
				//document.getElementById("rAdministrationBar_XML").src='/ib/site/generators/rCacheRestart.php';
				pmEvent.stop();
			}
		}
		if(pmEvent.ctrlKey && typeof(rWACK_Session) != 'undefined') {
			if(83 == pmEvent.keyCode) {	// s
				pmEvent.returnValue = false; // sonst kommt dumpfer Ton
				pmEvent.stop();
			}
			if(87 == pmEvent.keyCode) {	// w
				pmEvent.stop();
			}
		}

		if(pmEvent.ctrlKey) { // Strg+S
			if(83 == pmEvent.keyCode) {
				//pmEvent.returnValue = false; // sonst kommt dumpfer Ton
				//pmEvent.stop();
			}
		}

		//document.fire("ib:keydown", { keyCode: pmEvent.keyCode, ctrlKey: pmEvent.ctrlKey });
	}
);


function fnEditTinyMCE(pmId) { // text_basic
	var s = tinyMCE.settings;

	tinyMCE.init();
	tinyMCE.settings = ({
		// General options
		mode : "none",
		language : "de",
		theme : "advanced",
		convert_urls : false,

		plugins : "style,roccasimage,inlinepopups,paste",

		// Theme options
		theme_advanced_buttons1 : "cut,copy,pastetext,pasteword,|,bullist,numlist,|,bold,italic,underline,strikethrough,|,sub,sup,charmap,removeformat", // "styleselect"
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		
		skin_variant : "intern",
		fix_table_elements : true,
		
		theme_advanced_blockformats : "<?php echo $formats ?>", 
		
		theme_advanced_source_editor_width: 600,
		theme_advanced_source_editor_height: 480,
		
		valid_child_elements : "strong/b/em/i[%itrans|#text],li[ul|ol|%itrans|#text],body[%btrans|#text]",

		accessibility_warnings : false,

		setup : function(ed) {
				ed.addShortcut('ctrl+s', 'Speichern',
					function() {
						if(document.forms.rCard) {
							document.forms.rCard.action.value='Save';
							document.forms.rCard.submit();
						}
					}
				);
				ed.addShortcut('ctrl+d', 'Durchgestrichen', 'Strikethrough');
				ed.addShortcut('ctrl+shift+t', 'Textkoerper', ['FormatBlock', false, 'p']);
				ed.addShortcut('ctrl+shift+l', 'Aufzaehlung', ['InsertUnorderedList', false, '']);
				ed.addShortcut('ctrl+shift+o', 'Nummerierung', ['InsertOrderedList', false, '']);
				ed.addShortcut('ctrl+shift+z', 'Formatierung entfernen', 'RemoveFormat');
			},
		
		content_css : "/ib/site/generators/rStylesheet.php?NOOUTPUT=1"

	});
	tinyMCE.execCommand('mceAddControl', true, pmId);
	
	tinyMCE.settings = s;
}



