var is_ie = navigator.appVersion.indexOf('MSIE') != -1 ? true : false;
var is_saf = navigator.appVersion.indexOf('Safari') != -1 ? true : false;
var former_class;

function _i()
{
	var li = gT($('mainnavi'), 'li');
	if(li.length)
	{
		for(var i=0; i<li.length; i++)
		{
			if(li[i].id && $('sub'+getId(li[i].id)))
			{
				li[i].onmouseover = function() { drop(this.id); }
				li[i].onmouseout = function() { hide(this.id); }
			}
		}
	}
}

function drop(id)
{
	var nid = getId(id);
	/* Dropdown holen */
	$('sub'+nid).style.left = (is_ie ? $(id).offsetLeft + 50 : $(id).offsetLeft) + 'px';
	$('sub'+nid).style.top = $(id).offsetTop + $(id).offsetHeight + (is_ie ? 128 : (is_saf ? -2 : -1)) + 'px';
	if($('sub'+nid).offsetWidth < ($(id).offsetWidth - 8)) {
		$('sub'+nid).style.width = ($(id).offsetWidth - 8) + 'px';
	}
	/* Menuepunkt hervorheben */
	former_class = $(id).firstChild.className;
	$(id).firstChild.className = 'active';
}

function hide(id)
{
	var nid = getId(id);
	/* Dropdown verstecken */
	$('sub'+nid).style.left = '-999em';
	/* Menuepunkt zuruecksetzen */
	if(former_class != 'active') {
		$(id).firstChild.className = '';
	}
}

function getId(id) {
	return id.substring(1);
}

function insert(aTag, eTag, tarea) {
  var input = document.getElementById(tarea);
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
 /* Einfügen des Formatierungscodes */
     var range = document.selection.createRange();
     if(range.text.length>0){
       if (range.text.charAt(range.text.length-1)==" "){
         range.moveEnd("character" ,-1);
       }
     }
     var insText = range.text;
     range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length);
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
         /* Einfügen des Formatierungscodes */
     var start = input.selectionStart;
     var end = input.selectionEnd;
     if(start!= end){
      if (input.value.substring(end-1, end)==" "){
         var end = input.selectionEnd-1;
       }
     }
     var insText = input.value.substring(start, end);
     //console.log("-",insText,"-",start,end);
     input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfuegen an Position"+" (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Text eingeben"+":");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}

function ubb(tag, tarea){
  var input = document.getElementById(tarea);
  input.focus();
  // Browser ermitteln, Auswahl auswerten -------------
  //ie
  if(typeof document.selection != 'undefined') {
    var browser = "ie"
    var range = document.selection.createRange();
    if (range.text.charAt(range.text.length-1)==" "){
      range.moveEnd("character" ,-1);
    }
    var selection = range.text;
  }
  //gecko
  else if(typeof input.selectionStart != 'undefined'){
    var browser = "gecko"
    var start = input.selectionStart;
    var end = input.selectionEnd;
	  if (input.value.substring(end-1, end)==" " && start!=end){
      var end = input.selectionEnd-1;
    }
    var selection = input.value.substring(start, end);
  } else {
    var browser = "other"
  }
  // Code erstellen ------------------------------------------
  switch(tag){

    case "link":
    url = prompt("Webadresse"+":", "http://");
    if (selection == ""){
      wert = prompt("Text des Links"+":", "");
    } else {
      wert = selection;
    }
    code = (wert != null && url != null) ? "<a href=\""+url+"\">"+wert+"</a>" : "";
    break;

    case "img":
    url = prompt("Webadresse zum Bild"+":", "http://");
    code = (url != null) ? "<img src=\""+url+"\" />" : "";
    break;

    case "color":
    col = prompt("Textfarbe (z.B. red oder #ff0000)"+":", "");
    if (selection == ""){
      wert = prompt("Text"+":", "");
    } else {
      wert = selection;
    }
    code = (wert != null && col != null) ? "<span style=\"color:"+col+"\">"+wert+"</span>" : "";
    break;
  }
  // Code einfügen ------------------------------------------
  if (code.length > 0) {
    switch (browser){
      case "ie":
        range.select();
        range.text = code;
      break
      case "gecko":
        input.value = input.value.substr(0, start) + code + input.value.substr(end);
      break
      case "other":
        document.getElementById(tarea).value += code+" ";
      break
    }
  }
  document.getElementById(tarea).focus();
}

function list(tarea)
{
  listentry = "init";
  thelist = "";
  entries = "";
  cnt = 1;
  while ((listentry != "") && (listentry != null))
{
  listentry = prompt(+cnt+". "+"Element (Beenden mit 'Abbrechen')"+":", "");
  if ( (listentry != "") && (listentry != null) )
{
  listentry = "<li>"+listentry+"</li>\n";
  entries = entries+listentry;
  thelist = "<ul>\n"+entries+"</ul> ";
}
  cnt++;
}
  document.getElementById(tarea).value += thelist;
  document.getElementById(tarea).focus();
}

