
/*
Dieses JS File includiert die Möglichkeit Text zu markieren und diesen dann an ein Hilfstool zu senden
*/

//standard Hilfsfunktionen

function eventAdder(objAttrib,handler,addFunction)
{
	if ((!document.all)&&(document.getElementById))
	{       
		objAttrib.setAttribute(handler,addFunction);   
	}    
	//workaround for IE 5.x   
	
	if ((document.all)&&(document.getElementById))
	{       
		objAttrib[handler]=new Function(addFunction);   
	}
}

function attAdder(objAttrib,name,value)
{
	
	for (var k=0;k<objAttrib.attributes.length;k++)
	{
		if (objAttrib.attributes[k].nodeName == name)
		{
			objAttrib.attributes[k].nodeValue = value;
			return;
		}
	}
	
	objAttrib.setAttribute(name,value);
}

function styAdder(objAttrib,name,value)
{
	if (! objAttrib.style)
		return;
	var str = "objAttrib.style."+name+" = '"+value+"';";
	eval(str);
}



// Starte die Überwachung

function startSelectionControl()
{
	var selectionTrigger = window.setInterval("checkselection()", 50);
}



function checkselection()
{
	if (window.getSelection) 
		sel = window.getSelection();   
	else if (document.getSelection) 
		sel= document.getSelection();
	else if (document.selection) 
		sel = document.selection.createRange().text;			
		
	if (sel != aktSelection)
	{
		
		if (sel != "")
		{
			aktSelection = sel+ "";
			showSelectorDiv();
		}
		else
		{
			hideSelectorDiv();
		}
	}
	
	
}


function createCheckLayer()
{
	var mainDiv = document.createElement("DIV");
	styAdder(mainDiv,"backgroundImage","url(/images/pfeil_rechts_submit.gif)");
	styAdder(mainDiv,"backgroundRepeat","no-repeat");
	styAdder(mainDiv,"backgroundPosition","bottom right");
	styAdder(mainDiv,"backgroundColor","#E6EBF1");
	styAdder(mainDiv,"borderBottom","1px solid #B9C1C8");
	styAdder(mainDiv,"color","#003F74");
	styAdder(mainDiv,"paddingLeft","5px");
	styAdder(mainDiv,"paddingRight","16px");
	styAdder(mainDiv,"paddingTop","2px");
	styAdder(mainDiv,"paddingBottom","2px");
	styAdder(mainDiv,"whiteSpace","nowrap");
	styAdder(mainDiv,"fontFamily","arial,helvetica, sans serif");
	styAdder(mainDiv,"fontSize","11px");
	

	styAdder(mainDiv,"position","absolute");
	styAdder(mainDiv,"top","100px");
	styAdder(mainDiv,"left","100px");
	styAdder(mainDiv,"visibility","hidden");
	
	var myB = document.createElement("B");
	styAdder(myB,"fontFamily","times new roman");
	styAdder(myB,"fontSize","11px");
	styAdder(myB,"lineHeight","8px");
	styAdder(myB,"backgroundColor","white");
	styAdder(myB,"fontWeight","bold");
	styAdder(myB,"border","1px solid #003f74");
	styAdder(myB,"padding","0px 6px 0px 6px");
	styAdder(myB,"marginRight","5px");
	
	mainDiv.appendChild(myB);
	
	var myT = document.createTextNode("i");
	myB.appendChild(myT);
	
	var myA = document.createElement("A");
	attAdder(myA,"href","javascript:makeSelectionCheck()");
	
	mainDiv.appendChild(myA);
	var myT2 = document.createTextNode("Begriff erklären");
	
	myA.appendChild(myT2);
	
	return mainDiv;

}

function myInit()
{
	mySelectorDiv = createCheckLayer();

	myD = document.getElementsByTagName("body")[0];
	
	myD.appendChild(mySelectorDiv);
	
	hideSelectorDiv();
	
	document.onmousemove = storePosition;

	if ( typeof(table_row_class_toggle) == "function")
	{
		table_row_class_toggle();
	}
}




function storePosition(e)
{
	checkerX = -1;
	checkerY = -1;
	offsetY = 30;
	if (e)
	{
		offsetY = 10;
		checkerX = e.pageX;
		checkerY = e.pageY +offsetY ;
		checkerY = checkerY+"px";
		checkerX = checkerX+"px";
	}
	else
	{
		offsetY = 10;
		checkerX = event.clientX;
		checkerY = event.clientY +offsetY + document.documentElement.scrollTop  ;
	}
}

function showSelectorDiv()
{
	styAdder(mySelectorDiv,"top",checkerY);
	styAdder(mySelectorDiv,"left",checkerX);
	styAdder(mySelectorDiv,"visibility","visible");
}

function hideSelectorDiv()
{
	window.setTimeout("trueHider()",300);
}

function trueHider()
{
	styAdder(mySelectorDiv,"visibility","hidden");
	aktSelection = "";
}

function makeSelectionCheck()
{
	window.open("handle?action=checkMe&ignore=true&selTransSearch="+aktSelection,"mySelection","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=795,height=595");
}


if (document.addEventListener)
{
	document.addEventListener("DOMContentLoaded", myInit, false);
}

function selTrans_checkMyState()
{
	if (document.readyState)
	{
		if (document.readyState != "complete")
		{
			window.setTimeout("selTrans_checkMyState()",100);
		}
		else
		{
			myInit();
		}
	}
}

selTrans_checkMyState();


function AttachEvent(obj,evt,fnc,useCapture)
    {
      if (!useCapture) useCapture=false;
      if (obj.addEventListener)
      {
        obj.addEventListener(evt,fnc,useCapture);
        return true;
      } 
      else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
      else
      {
        MyAttachEvent(obj,evt,fnc);
        obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
      }
    } 
  
  
    function initalizeFocusListener() 
    {
      if (typeof(document.activeElement) == "undefined")
      {
        document.activeElement = document.body;
        var all = document.body.getElementsByTagName('*');
        for (var i = 0; i < all.length; i++) 
        {
          functionBody = "storeActive(this);";
          var oldOnFocus = all[i].getAttribute('onfocus');
          if (oldOnFocus)
          {
            functionBody += oldOnFocus;
          }
          all[i].onfocus = new Function ('event', functionBody);
        }
      }
      if (document.all)
      {
        var help = document.getElementsByTagName("BODY")[0];
        AttachEvent(help,'keypress',checkKey);
      }
    }
                    
    function storeActive(element)
    {
      document.activeElement = element;
    }              
    
    function checkKey(e)
    {
      //alert(window.event.keyCode);
      if (e.keyCode == 13)
      {
        trySmartSubmit();	  
      }
    }
    
    function trySmartSubmit()
    {
      var myElement = document.activeElement;
      
      pass = false;
      
      var theElement = myElement;
      while (theElement = theElement.parentNode)
      {
        if (typeof(theElement.tagName) == "undefined") continue;
        var help = theElement.tagName.toLowerCase();
        
        if (help == "form")
        {
          pass = true;
          myElement = theElement;
        }
      }
      
      if (pass)
      {
        //Form gefunden -> submit
        var alternate = myElement.getAttribute("alternateSubmitFunction");
        if (alternate != null)
        {
          if (alternate == "none")
          {
            //do nothing
          }
          else
          {
            eval(alternate);
          }
        }
        else
        {
          myElement.submit();
        }
      }
    }

    AttachEvent(window,'keypress',checkKey);
    AttachEvent(window,'load',initalizeFocusListener);

//INIT

// Starte die Überwachung

startSelectionControl();
var aktSelection = "";


