/* ************************************************************************************ *
 * FADENKREUZ                                                                           *
 *                                                                                      *
 * Das Script kann frei verwendet werden, dieser Kommentar, der URL sowie die Nennung   *
 * des Nicks müssen jedoch erhalten bleiben.                                            *
 *                                                                                      *
 *                                                                Quelle: www.quaese.de *
 *                                                                     (c) Quaese, 2007 *
 * ************************************************************************************ */

/* *** [START - Fadenkreuz-Script] **************************************************************** */
// Fadenkreuz
function Fadenkreuz(blnShowInfo, strTransImg){

  this.Delay  = 10;                       // Verzögerungszeit beim Neuzeichnen des Fadenkreuzes
  this.Thickness = 1;                     // Breite der Fadenkreuzlinien
  this.WidthInfoBox  = 60;								// Breite der Infobox (Pixelwerte)
  this.HeightInfoBox = 20;								// Höhe der Infobox (Pixelwerte)
  this.MarginInfoBox = 5;									// Abstand der Infobox zum Dokumentenrand



  var objThis  = this;                    // Closure
  this.hTimer  = null;                    // Timerhandle
  this.objBody = null;                    // Variable für IE-Body-Objekt
  this.hasBeenStarted = false;
  this.mouseOffset = 1;
  this.isKonqueror = false;

	this.idSuffix = new Date().getTime();   // ID-Suffix
  this.idHor  = "id_h_" + this.idSuffix;  // ID für das horizontale Element
  this.idVert = "id_v_" + this.idSuffix;  // ID für das vertikale Element
  this.idDescX = "idDescX";               // ID für Zahlenwert-Box in vertikaler Richtung
  this.idDescY = "idDescY";               // ID für Zahlenwert-Box in horizontaler Richtung

  this.TransImg = (typeof strTransImg != "undefined")? strTransImg : "transparenz_80prozent.png";
  this.blnShowInfo = (typeof blnShowInfo != "undefined")? true : false;
  this.objDescX = this.objDescY = null;

  // Passendes Body-Element des IE ermitteln (Quirksmode/Standardsmode)
  if(document.all && !window.opera)
  	this.objBody = (window.document.compatMode == "CSS1Compat") ? window.document.documentElement
                                                                : window.document.body || null;

  // Dokumentgrössen bestimmen
  if(window.innerWidth){                            // Non-IEs
    this.bodyWidth  = window.innerWidth;
    if(window.scrollMaxY){                          // Gecko-Engines
    	this.bodyHeight = window.innerHeight + window.scrollMaxY;
    	this.bodyWidth = window.innerWidth + window.scrollMaxX;
    }else{                                          // Opera, Konqueror ...
	    if(window.getComputedStyle){                  // Opera
	      this.bodyHeight = parseInt(window.getComputedStyle(document.getElementsByTagName("body")[0], null).getPropertyValue("height"));
	      // Gesamtbreite des Dokuments (Body) ermitteln
        this.bodyWidth  = document.getElementsByTagName("body")[0].scrollWidth;
        // Falls Anzeigeprobleme bestehen => nächste Zeile auskommentieren
        //document.getElementsByTagName("body")[0].style.width = this.bodyWidth + "px";
	    }else if(this.objBody.scrollHeight){          // Konqueror
      	this.bodyHeight = this.objBody.scrollHeight;
      	this.bodyWidth  = this.objBody.scrollWidth;
        this.isKonqueror = true;
      }else{                                        // Sonstige
	      alert("Ihr Browser unterstützt die notwendigen Eigenschaften zum Anzeigen des Fadenkreuzes leider nicht!");
	      return;
	    }
    }
  }else if(this.objBody != null && !window.opera){  // IEs
    this.bodyWidth  = this.objBody.scrollWidth;
    this.bodyHeight = this.objBody.scrollHeight;
  }else{                                            // Sonstige
    this.bodyHeight = this.bodyWidth = 0;
    alert("Ihr Browser unterstützt die notwendigen Eigenschaften zum Anzeigen des Fadenkreuzes leider nicht!");
    return;
  }

  // Fadenkreuzelemente erstellen
  this.horLinie  = initFadenkreuz(this.idHor, 1);
  this.vertLinie = initFadenkreuz(this.idVert, 1);

  // Falls bereits ein MouseMove-Handler existiert
  if(typeof document.onmousemove == "function")
  	document.save = document.onmousemove;

  document.onmousemove = function(objEvt){
  	// Falls eine bereits bestehende MouseMove-Funktion ausgeführt werden muss
	  if(typeof document.save == "function")
	    document.save();

    objEvt = (window.event)? window.event : objEvt;
    objThis.mouseX = (document.all && !window.opera && !objThis.isKonqueror)? (window.event.clientX+objThis.objBody.scrollLeft) : objEvt.pageX;
    objThis.mouseY = (document.all && !window.opera && !objThis.isKonqueror)? (window.event.clientY+objThis.objBody.scrollTop) : objEvt.pageY;

    // Funktion zum Anzeigen des Fadenkreuzes aufrufen
    objThis.showCrosshairs();
  }

  this.stopIt();

  // Initialisieren der Fadenkreuzelemente
  function initFadenkreuz(strId, intThickness){
  	var objTemp = document.createElement("div");
    objTemp.id = strId;

    // Style-Objekt des temporären Elements bearbeiten
    objTemp.style.position = "absolute";
    objTemp.style.top = "0";
    objTemp.style.left = "0";
    objTemp.style.margin = "0";
    objTemp.style.padding = "0";
    objTemp.style.fontSize = "1px";
    objTemp.style.lineHeight = "0em";
    objTemp.style.zIndex = "99";

    // Falls es sich um die vertikale Linie handelt
    if(strId == objThis.idVert){
      objTemp.style.height = objThis.bodyHeight + "px";
      objTemp.style.width = "1px";
      objTemp.style.borderLeft = intThickness + "px solid #369";

      // Infobox für vertikale Linie
      var objDesc = document.createElement("div");
      objDesc.id = objThis.idDescX;
	    objDesc.style.position = "absolute";
	    objDesc.style.top = objThis.MarginInfoBox + "px";
	    objDesc.style.left = parseInt(objTemp.style.left) + "1px";
    	objDesc.style.zIndex = "101";
      objThis.objDescX = objDesc;
    // Falls es sich um die horizontale Linie handelt
    }else if(strId == objThis.idHor){
      objTemp.style.height = "1px";
      objTemp.style.width = objThis.bodyWidth + "px";
      objTemp.style.borderTop = intThickness + "px solid #a00";

      // Infobox für horizontale Linie
      var objDesc = document.createElement("div");
      objDesc.id = objThis.idDescY;
	    objDesc.style.position = "absolute";
	    objDesc.style.top = (parseInt(objTemp.style.top) + 2) + "px";
	    objDesc.style.right = objThis.MarginInfoBox + "px";
    	objDesc.style.zIndex = "100";
      objThis.objDescY = objDesc;
    }

    // Beschreibungsbox formatieren
    objDesc.style.width = objThis.WidthInfoBox + "px";
    objDesc.style.height = objThis.HeightInfoBox + "px";
    objDesc.style.border = "1px solid #ccc";
    objDesc.style.backgroundColor = "#efefef";
    if(typeof objDesc.style.filter != "undefined")
	    objDesc.style.filter = "alpha(opacity=80)";
    /*else if(typeof objDesc.style.MozOpacity != "undefined")
    	objDesc.style.MozOpacity = 0.8;
    else if(typeof objDesc.style.opacity != "undefined")
    	objDesc.style.opacity = 0.8;*/
    else{
	    objDesc.style.backgroundColor = "transparent";
	    objDesc.style.backgroundImage = "url("+objThis.TransImg+")";
    }
    objDesc.style.padding = "0 0px";
    objDesc.style.fontFamily = "Arial, serif";
    objDesc.style.fontSize = "11px";
    objDesc.style.lineHeight = "20px";
    objDesc.style.textAlign = "center";

    // Element in Dokumentenbaum einhängen
    document.getElementsByTagName("body")[0].appendChild(objTemp);
		// Falls gewünscht, Infobox in Dokumentenbaum einhängen
    if(objThis.blnShowInfo)
	    document.getElementsByTagName("body")[0].appendChild(objDesc);

    return objTemp;
  }
}

Fadenkreuz.prototype.startIt = function(){
	this.hasBeenStarted = true;
	document.getElementsByTagName("body")[0].style.cursor = 'crosshair';
}

Fadenkreuz.prototype.stopIt = function(){
	document.getElementsByTagName("body")[0].style.cursor = '';

  this.horLinie.style.top = "-200px";
  this.vertLinie.style.left = "-200px";
	this.hasBeenStarted = false;

  // Falls gewünscht -> Beschreibungsboxen positionieren
  if(this.blnShowInfo){
	  this.objDescX.style.left = (parseInt(this.vertLinie.style.left) + this.MarginInfoBox) + "px";
	  this.objDescX.innerHTML = "";
	  this.objDescY.style.top  = (parseInt(this.horLinie.style.top) + this.MarginInfoBox) + "px";
	  this.objDescY.innerHTML = "";
  }
}

Fadenkreuz.prototype.showCrosshairs = function(){
	// Falls das Fadenkreuz angezeigt werden soll
	if(this.hasBeenStarted){
	  this.horLinie.style.top   = (this.mouseY + this.mouseOffset) + "px";
	  this.vertLinie.style.left = (this.mouseX + this.mouseOffset) + "px";

    // Falls gewünscht -> Beschreibungsboxen positionieren
	  if(this.blnShowInfo){
	    var intScrollTop = (typeof window.pageYOffset != "undefined")? window.pageYOffset : this.objBody.scrollTop;
	    var intScrollLeft = (typeof window.pageXOffset != "undefined")? window.pageXOffset : this.objBody.scrollLeft;

	    // Breite und Höhe des Anzeigebereichs ermitteln
	    var intScreenHeight = (typeof window.innerHeight != "undefined")? parseInt(window.innerHeight) : this.objBody.offsetHeight;
	    var intScreenWidth = (typeof window.innerWidth != "undefined")? parseInt(window.innerWidth) : this.objBody.offsetWidth;

	    // Prüfen, ob sich Mauszeiger über der Hälfte der Breite im Anzeigebereich befindet
	    var intDist = ((parseInt(this.vertLinie.style.left)-intScrollLeft) > (intScreenWidth/2))? -(this.WidthInfoBox + this.MarginInfoBox) : this.MarginInfoBox ;
	    this.objDescX.style.left = (parseInt(this.vertLinie.style.left) + intDist) + "px";
	    this.objDescX.style.top  = (intScrollTop + this.MarginInfoBox) + "px";
	    this.objDescX.innerHTML = (parseInt(this.vertLinie.style.left) - this.mouseOffset) + "px";

	    // Prüfen, ob sich Mauszeiger über der Hälfte der Höhe im Anzeigebereich befindet
	    var intDist = ((parseInt(this.horLinie.style.top)-intScrollTop) > (intScreenHeight/2))? -(this.HeightInfoBox + this.MarginInfoBox) : this.MarginInfoBox;
	    this.objDescY.style.top  = (parseInt(this.horLinie.style.top) + intDist) + "px";                // Infobox hor. an Linie ausrichten
	    this.objDescY.style.left  = (intScrollLeft + intScreenWidth - (this.WidthInfoBox + 25)) + "px"; // Infobox immer am rechten Rand ausrichten
	    this.objDescY.innerHTML = (parseInt(this.horLinie.style.top) - this.mouseOffset) + "px";        // Pixelwert in Infobox schreiben
	  }
	}
}

Fadenkreuz.prototype.startIt_ = function(){
  this.horLinie.style.top   = this.mouseY + "px";
  this.vertLinie.style.left = this.mouseX + "px";

  var objThis = this;

  this.hTimer = window.setTimeout(function(){objThis.startIt();}, objThis.Delay);
}

Fadenkreuz.prototype.stopIt_ = function(){
	var objThis = this;
	if(this.hTimer != null)
  	window.clearTimeout(objThis.hTimer);
}
/* *** [ENDE  - Fadenkreuz-Script] **************************************************************** */
