//  Generates the box element containing the spec chars.
function createSpecCharBox() {
    var boxDiv = document.createElement("div");
    boxDiv.setAttribute('id', 'editMenu');
    // boxDiv.setAttribute('id', 'customBar');
    // boxDiv.setAttribute('style', 'margin-top:5px;border:1px solid #aaaaaa;padding:3px');

    var html = '<a href="http://hu.wikipedia.orghttps://wikines.com/hu/Wikipédia:Különleges_karakterek"' +
    ' title="Wikipédia:Különleges karakterek">Különleges karakterek:</a>:' +
    "<a href=\"javascript:insertTags('„','','')\">„</a> ·" +
    "<a href=\"javascript:insertTags('”','','')\">“</a> ·" +
    "<a href=\"javascript:insertTags('–','','')\">– <small>(gondolatjel)</small></a> ·" +
    "<a href=\"javascript:insertTags('„','”','')\">„”</a> ·" +
    "<a href=\"javascript:insertTags('»','«','')\">»«</a> ·" +
    "<a href=\"javascript:insertTags('²','','')\">²</a> " +
    "<a href=\"javascript:insertTags('³','','')\">³</a> " +
    "<a href=\"javascript:insertTags('½','','')\">½</a> ·" +
    "<a href=\"javascript:insertTags('€','','')\">€</a> ·" +
    "<a href=\"javascript:insertTags('í','','')\">í</a> ·" +
    "<a href=\"javascript:insertTags(']','')\">]</a> " +
    "<a href=\"javascript:insertTags('|','','')\">|</a> " +
    "<a href=\"javascript:insertTags('{'+'{','}}','')\">{{}}</a> ·" +
    "<a href=\"javascript:insertTags('{'+'{en}}','','')\"><small>angol link</small></a> ·" +
    "<a href=\"javascript:insertTags('{'+'{' + 'torles}}','','')\"><small>törlésre javasolt</small></a> ·" +
    "<a href=\"javascript:insertTags('--\176\176\176\176','','')\"><small>aláírás</small></a> ·" +
    "<a href=\"javascript:translateInfoboxCountryEnHu()\"><small>ország</small></a> ·" +
    "<a href=\"javascript:fixLatin1Chars()\"><small>kalapos</small></a> ·" +
    "<a href=\"javascript:fixHungarianQuotes()\"><small>idézőjel</small></a> ·" +
    "<a href=\"javascript:fixExternalLinksSectionTitle()\"><small>külső</small></a> ·" +
    "<a href=\"javascript:translateDatesEnHu()\"><small>dátum</small></a> ·" +
    "<a href=\"javascript:insertCustomForm(customReplaceForm())\"><small>egyedi</small></a>"
    "</div>"; 

   return boxDiv;
}

function replaceRegexp(replaceWhat, replaceWith, modifiers) {
        if (modifiers == null) {
		modifiers = "";
	}
	var txtarea = document.editform.wpTextbox1;
	// IE
	if(document.selection  && !is_gecko) {
		var theSelection = document.selection.createRange().text;		
		txtarea.focus();
		theSelection = theSelection.replace(replaceWhat, replaceWith);
		document.selection.createRange().text = theSelection;
	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
                // This is probably more complicated than it needs to be
 		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		var scrollTop=txtarea.scrollTop;

		var pattern = new RegExp(replaceWhat, modifiers);
		var myText = (txtarea.value).replace(pattern, replaceWith);

		txtarea.value = myText;		

		txtarea.focus();

		var cPos=startPos;
		txtarea.selectionStart=cPos;
		txtarea.selectionEnd=cPos;
		txtarea.scrollTop=scrollTop;

	// All others
	} else {
		alert("No support for this browser yet");
		return;
	}
	// reposition cursor if possible
	if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
}

// Replaces Latin 1 chars with properly encoded Hungarian Latin 2 equivalents
function fixLatin1Chars() {	
	replaceRegexp("Õ", "\u0150", "g");
	replaceRegexp("Û", "Ű", "g");
	replaceRegexp("õ", "ő", "g");
        replaceRegexp("&#337;", "ő", "g");
	replaceRegexp("&#369;", "ű", "g");
	replaceRegexp("û", "ű", "g");
}


// Replaces English style "" quotes with Hungarian „”
function fixHungarianQuotes() {
	replaceRegexp("\"(*)\"", "„$1”", "mg");
}

// Replaces English style "" quotes with Hungarian „”
function fixExternalLinksSectionTitle() {
	replaceRegexp("=\\s*Külső linkek\\s*=", "=Külső hivatkozások=", "mg");
}

// Replace all occurences of a user-entered regexp with a user entered replacement
function customReplace() {
	var what = prompt('Cseréld ezt:');
	if (what != null) {
		var withWhat = prompt('Cseréld ezt: "' + what + '", erre:');
		if (withWhat != null) {
			replaceRegexp(what, withWhat, "gm");
		}
	}
}

// Translate dates from English format to Hungarian
// Currently it only handles linkified dates
function translateDatesEnHu() {
	var months = [
		,
		,
		,
		,
		,
		,
		,
		,	
		,
		,
		,
		
	];

	for (var i = 0; i < months.length; i++) {
		var enMonth = months;
		var huMonth = months;
		// alert("Replacing " + enMonths + " with " + huMonth);
		// Replace "], ]" with "]. ]."
		// $1 = "15", $2 = "2005"
		replaceRegexp("\\\\],?\\s*\\\\]", "]. ].", "gm");
	}
}

// Translates an English country infobox to Hungarian
function translateInfoboxCountryEnHu() {
	var dict = [
		,	
		,
		,		
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		,
		, 
		, 
		, 
		, 
		, 
		, 
		
	];

	for (var i = 0; i < dict.length; i++) {
		var pair = dict;
		replaceRegexp(pair, pair);
	}

        // In Hungarian, we use &nbsp; for separating digits instead of ,
        replaceRegexp("(\\d+?),(\\d+?)", "$1&nbsp;$2", "g");
	// Decimal separator "." -> ","
	replaceRegexp("(\\d+?)\\.(\\d+?)", "$1,$2", "g");

	// In rankings, get rid of "st", "th", etc.
	replaceRegexp("(\\d+?)st", "$1", "g");
	replaceRegexp("(\\d+?)nd", "$1", "g");
	replaceRegexp("(\\d+?)rd", "$1", "g");
	replaceRegexp("(\\d+?)th", "$1", "g");

	// Get rid of $ prefix in GDP
	replaceRegexp("\\$(\\d+?)", "$1", "g")


        // Get rid of whitespace before "="
        replaceRegexp("(\\s+)=", " =", "g");

        translateDatesEnHu();
}

// Replace all occurences of a user-entered regexp with a user entered replacement
function customReplaceForm() {
  var form =
    'Cseréld ezt a reguláris kifejezést: <input id="customReplaceThis" type="text" size="40"> ' +
    'erre: <input id="customReplaceWith" type="text" size="40"> ' +
    '<input type="submit" value="Csere" onClick="javascript:performCustomReplace()">';
  return form;
}

function performCustomReplace() {
  var replaceThis = document.getElementById('customReplaceThis');
  var replaceWith = document.getElementById('customReplaceWith');
  replaceRegexp(replaceThis.value, replaceWith.value, "gm");
  deleteCustomForm();
}

function insertCustomForm(customForm) {
  var customBar = document.getElementById('editMenu');
  if (customBar == null) return;

  var replaceForm = document.createElement('div');
  replaceForm.setAttribute('id', 'customForm');
  replaceForm.setAttribute('style', 'margin-top:5px;border:1px solid #aaaaaa;padding:3px;text-align:center');
  replaceForm.innerHTML = 
    '<img src="http://upload.wikimedia.org/wikipedia/hu/b/bd/Close.png" align="right" ' +
    'style="margin:2px" onClick="javascript:deleteCustomForm()">' +
    customForm;
  
  var oldForm = document.getElementById('customForm');
  if (oldForm != null) {
    customBar.parentNode.replaceChild(replaceForm, oldForm);
  } else {
    customBar.parentNode.insertBefore(replaceForm, customBar);
  }
}

function deleteCustomForm() {
  var replaceForm = document.getElementById('customForm');
  if (replaceForm != null)
    replaceForm.parentNode.removeChild(replaceForm);
}

// Initializes edit box
function myOnloadHook() {
  // Textarea positioning doesn't work in Opera
  if (is_opera) return;

  // Find the element that comes right after the special character box
  var copywarn = document.getElementById('editpage-copywarn');
  if (copywarn == null) {
      // Not in edit mode
      return;
  }
  var specCharBox = createSpecCharBox();
  var par = copywarn.parentNode;

  par.insertBefore(specCharBox, copywarn);
  
  initjsDOMenu();
}

if (window.addEventListener) window.addEventListener("load", myOnloadHook, false);
else if (window.attachEvent) window.attachEvent("onload", myOnloadHook);