 if (!dk) {
  var dk = {} ;
  dk.hintzmann = {} ;
  dk.hintzmann.SimpleTextareaEditor = {};
} else if (!dk.hintzmann) {
  dk.hintzmann = {};
  dk.hintzmann.SimpleTextareaEditor = {};
} else {
  dk.hintzmann.SimpleTextareaEditor = {};
}
var ste = dk.hintzmann.SimpleTextareaEditor;

ste.cfg = {
  panelSuffix		: "_editorPanel" 
}
/* Is method used in SimpleTextareaEditor supported by browser ? */
ste.isSupported = (document.createElement &&
                  document.getElementById && 
                  document.getElementsByTagName);

ste.STEditor = function(sID) {
	if (!ste.isSupported) return;
	
	this.id = sID;
	this._textarea = document.getElementById(sID);
	if (this._textarea == null) { return null; }
	//	if (!((this._textarea.setSelectionRange) || (document.selection))) { return null; }
	
	var sPanel_id = this.id + ste.cfg.panelSuffix;
	this._panel = document.getElementById(sPanel_id);
	if ( this._panel == null ) {
		this._panel = document.createElement("div")
		this._panel.id = sPanel_id;
		this._textarea.parentNode.insertBefore(this._panel, this._textarea);
	}
	
	this.setCaretToPos = ste._setCaretToPos;
	this.setCaretToEnd = ste._setCaretToEnd;
	this.setSelectionRange = ste._setSelectionRange;
	this.replaceSelection = ste._replaceSelection;
}
ste.STEditor.prototype.makeButton = function(sValue, oFunc, sTitle, sAccesskey) {
	var oButton;
	oButton = document.createElement("input");
	oButton.type = "button";
	oButton.value = sValue;
	oButton.title = sTitle;
	oButton.accessKey = sAccesskey;
	oButton.onclick = oFunc;
	this._panel.appendChild(oButton)
}
ste.STEditor.prototype.makeSelect = function(aValue, oFunc, sTitle, sAccesskey) {
	var oSelect;
	oSelect = document.createElement("select");
	oSelect.title = sTitle;
	oSelect.accessKey = sAccesskey;
	oSelect.onchange = oFunc;
	this._panel.appendChild(oSelect)

	var oOption;
	var sText
	for (var x = 0; x < aValue.length; x=x+2) {
		oOption = document.createElement("option");
		oOption.value = aValue[x];
		sText = document.createTextNode(aValue[x+1]);
		oOption.appendChild(sText)
		oSelect.appendChild(oOption)
	}
}
ste.STEditor.prototype.makeSeperator = function() {
	/*
	var oSep;
	oSep = document.createElement("hr"); 
	this._panel.appendChild(oSep);
	*/
	oSep = document.createElement("input"); 
	oSep.type = "button";
	oSep.value = "";
	oSep.className = "seperator";
	this._panel.appendChild(oSep)
}
ste.STEditor.prototype.setHTML = function (oTextarea, replaceString) {
	var replaceStringPrefix = "";
	var replaceStringSuffix = "";
	var aReplaceString = replaceString.split("%%");
	replaceStringPrefix = aReplaceString[0];
	if (aReplaceString.length>1) {
		replaceStringSuffix = aReplaceString[1];
	}
	if (oTextarea.setSelectionRange) { // Mozilla
		var sText = ste.getSelection(oTextarea);
		ste._replaceSelection(oTextarea, replaceStringPrefix+sText+replaceStringSuffix);
	}
	else if (document.selection) { // IE
		var range = document.selection.createRange();

		if (range.parentElement() == oTextarea) {
			var isCollapsed = range.text == '';
			var sNewText = replaceStringPrefix+range.text+replaceStringSuffix;
			range.text = sNewText;

			if (!isCollapsed)  {
				range.moveStart('character', -sNewText.length);
				range.select();
			} 
		}	else {
			
			oTextarea.focus(); //in case no selection
			var i = oTextarea.value.length+1; //textarea value
			theCaret = document.selection.createRange().duplicate();
			while (theCaret.parentElement() == oTextarea && theCaret.move("character", 1) == 1) {
				--i;
			}
			var tmp = oTextarea.value.split("\n").length-1;
			
			if (i-tmp == 0) {
				oTextarea.value = oTextarea.value + replaceStringPrefix + replaceStringSuffix;
				ste._setCaretToEnd(oTextarea);
			} else {
				ste._setCaretToPos(oTextarea,i-tmp);
				ste._setSelectionRange(oTextarea,i-tmp-1,i-tmp);
				range = document.selection.createRange();
				var sNewText = replaceStringPrefix+range.text+replaceStringSuffix;
				range.text = sNewText;
				var replaceString = replaceStringPrefix+replaceStringSuffix;
				ste._setCaretToPos(oTextarea,i-tmp+replaceString.length);
			}
		}
	} else {
		oTextarea.value = oTextarea.value + replaceStringPrefix + replaceStringSuffix;
	}
}
ste._setSelectionRange = function (input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}
ste._replaceSelection = function (input, replaceString) {
	if (input.setSelectionRange) 
	{
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)
							+ replaceString
							+ input.value.substring(selectionEnd);
		if (selectionStart != selectionEnd) // has there been a selection
			ste._setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
		else // set caret
			ste._setCaretToPos(input, selectionStart + replaceString.length);
	}
	else if (document.selection) 
	{
		var range = document.selection.createRange();
		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;
			if (!isCollapsed)  { 
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}
ste.getSelection = function (oTextarea) {
	var sText = "";
	if (oTextarea.setSelectionRange) { // Mozilla
		var selectionStart = oTextarea.selectionStart;
		var selectionEnd = oTextarea.selectionEnd;
		sText = oTextarea.value.substring(selectionStart, selectionEnd);
	} 
	else if (document.selection) { // IE
		var range = document.selection.createRange();
		if (range.parentElement() == oTextarea) {
			sText = range.text;
		}
	}
	return sText;
}
ste._setCaretToPos = function (input, pos) {
	ste._setSelectionRange(input, pos, pos);
}
ste._setCaretToEnd = function (oInput) {
	ste._setSelectionRange(oInput, oInput.value.length, oInput.value.length);
}
ste._preview = function(sHTML) {
	myWindow = window.open("", "previewWindow", '')
	myWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n');
	myWindow.document.write('<html>\n');
	myWindow.document.write('<head>\n');
	myWindow.document.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n');
	myWindow.document.write('<link href="/main.css" rel="stylesheet" type="text/css" />');
	myWindow.document.write('<title>TAEditor - Preview</title>\n');
	myWindow.document.write('</head>\n');
	myWindow.document.write('<body>\n');
	myWindow.document.write(''+sHTML.replace(/[\n]/g, "<br />")+'');
	myWindow.document.write('<body>\n');
	myWindow.document.write('</html>\n');
	myWindow.document.close()
}
ste._help = function() {
	if (typeof(helpURL) == "undefined") {
		alert("Hjælpefil ikke angivet");
	} else {
		//helpURL = "/";
		window.open(helpURL, "Hjælp", "height=400,width=300,resizable=yes,scrollbars=yes,status=no,toolbar=no");
	}
}
ste.STEditor.prototype.buildEditor = function() {
	var self = this;
	var oTextarea = document.getElementById(self.id);
	
	this.makeButton(	"Afsnit"
								, function()	{ 
														self.setHTML(oTextarea, '<p>%%</p>\n');
													}
								, "[p] Insæt afsnit"
								, "p" );
	
	/*
	this.makeButton(	"blockquote"
								, function()	{ 
														self.setHTML(oTextarea, '<blockquote>%%</blockquote>\n');
													}
								, "[q] Insert blockquate"
								, "q" );
	*/
	/*							
	this.makeSelect(	["", "Headings", "<h1>%%</h1>", "Heading 1", "<h2>%%</h2>", "Heading 2", "<h3>%%</h3>", "Heading 3", "<h4>%%</h4>", "Heading 4", "<h5>%%</h5>", "Heading 5", "<h6>%%</h6>", "Heading 6"]
								, function()	{ 
															if (this.selectedIndex!= -1) {
																var sHeading = this.options[this.selectedIndex].value;
																if (sHeading == '') return;
																self.setHTML(oTextarea, sHeading)
															}
													}
								,"[s] Stronger emphasis text" 
								,"s" );
	this.makeSeperator();
	*/
	this.makeButton(	"Overskrift"
								, function()	{ 
														self.setHTML(oTextarea, '<h3>%%</h3>');
													}
								, "[b] Overskrift til afsnit (<h3>)"
								, "b" );
	this.makeButton(	"Kursiv"
								, function()	{ 
														self.setHTML(oTextarea, '<em>%%</em>');
													}
								,"[e] Kursiv tekst (<em>)" 
								,"e" );
	this.makeButton(	"Fed"
								, function()	{ 
														self.setHTML(oTextarea, '<strong>%%</strong>');
													}
								,"[s] Fed tekst (<strong>)" 
								,"s" );
	this.makeButton(	"Ny Linie"
								, function()	{ 
														self.setHTML(oTextarea, '%%<br />\n');
													}
								, "[b] Insæt tvungent linieskift (<br>)"
								, "b" );
	/*
	this.makeButton(	"acronym"
								, function()	{ 
														self.setHTML(oTextarea, '<acronym title="">%%</acronym>');
													}
								,"[a] Insert an acronym (e.g., WAC, radar, etc.). &lt;acronym title='World Wide Web'&gt;WWW&lt;/acronym&gt;" 
								,"a" );

	this.makeButton(	"link"
								, function()	{ 
														self.setHTML(oTextarea, '<a href="">%%</a>');
													}
								,"[l] Insert a link" 
								,"l" );
	this.makeButton(	"img"
								, function()	{  
														self.setHTML(oTextarea, '%%<img src="" width="" height="" alt="" />\n');
													}
								,"[i] Insert image tag" 
								,"i" );
	*/
	this.makeSeperator();
	this.makeButton(	"Vis"
								, function()	{ 
														ste._preview(oTextarea.value);
													}
								,"Vis siden (Åbner i nyt vindue)" 
								,"" );
	this.makeButton(	"Hjælp"
								, function()	{ 
														ste._help();
													}
								,"Vis hjælpesiden (Åbner i nyt vindue)" 
								,"" );
} 
/**
* Editor til kalender-items
*/
ste.STEditor.prototype.buildEditor_kalender = function() {
	var self = this;
	var oTextarea = document.getElementById(self.id);
	
	this.makeButton(	"Afsnit"
								, function()	{ 
														self.setHTML(oTextarea, '<p>%%</p>\n');
													}
								, "[p] Insæt afsnit"
								, "p" );
	
	this.makeButton(	"Overskrift"
								, function()	{ 
														self.setHTML(oTextarea, '<h3>%%</h3>');
													}
								, "[b] Overskrift til afsnit (<h3>)"
								, "b" );
	this.makeButton(	"Kursiv"
								, function()	{ 
														self.setHTML(oTextarea, '<em>%%</em>');
													}
								,"[e] Kursiv tekst (<em>)" 
								,"e" );
	this.makeButton(	"Fed"
								, function()	{ 
														self.setHTML(oTextarea, '<strong>%%</strong>');
													}
								,"[s] Fed tekst (<strong>)" 
								,"s" );
	this.makeButton(	"Centrer"
								, function()	{ 
														self.setHTML(oTextarea, '<span class="center">%%</span>');
													}
								,"[s] Centrer tekst" 
								,"s" );
	this.makeButton(	"Ny Linie"
								, function()	{ 
														self.setHTML(oTextarea, '%%<br />\n');
													}
								, "[b] Insæt tvungent linieskift (<br>)"
								, "b" );
	this.makeSeperator();
	this.makeButton(	"Vis"
								, function()	{ 
														ste._preview(oTextarea.value);
													}
								,"Vis siden (Åbner i nyt vindue)" 
								,"" );
	this.makeButton(	"Hjælp"
								, function()	{ 
														ste._help();
													}
								,"Vis hjælpesiden (Åbner i nyt vindue)" 
								,"" );
} 
