//////////////ELEMENT POSITION/////////////
/*
 spezielle Funktinoen und Klassen fr die Startanimation ICR
*/
//position X from parsed Element
function qbepGetAbsoluteX(fp_oElem) {
var x = 0;
var Elem = fp_oElem;
if (!Elem || typeof Elem.offsetParent == "undefined") return;
while (Elem) {
	x += Elem.offsetLeft;
	Elem = Elem.offsetParent;
}
return x;
}

//position Y from parsed Element
function qbepGetAbsoluteY(fp_oElem){
var y = 0;
if (!fp_oElem || typeof fp_oElem.offsetParent == "undefined") return;
while (fp_oElem && typeof fp_oElem.offsetTop == "number") {
	y += fp_oElem.offsetTop;
	fp_oElem = fp_oElem.offsetParent;
}
return y;
}

/////////////////////////////////////////////
// Klasse qbDriveImage
function qbDriveImage () {
  // Klassenvariable
  var oThis = this;
  this.form = ("animation");
  this.element = document.getElementById("preanimation");
  this.element.dimension = new Array(600,300);
  this.sensor = document.getElementById("sensoranimation");
  this.animation = document.getElementById("animationframe");
  this.animation.dimension = new Array(400,240);
  // Unterschied Bild - Sensor
  this.oversize = new Array(this.element.dimension[0]-this.animation.dimension[0],this.element.dimension[1]-this.animation.dimension[1]);
  this.animationX = qbepGetAbsoluteX(this.animation);
  this.animationY = qbepGetAbsoluteY(this.animation);
  this.movestatus = false;

  this.move = function(event){
	//if (oThis.movestatus != true) return;
    // get Mouse position
	aMousePos = MousePos(event);
	mouseInAnimationX = aMousePos[0]-oThis.animationX;
	mouseInAnimationY = aMousePos[1]-oThis.animationY;
	var left = Math.floor((oThis.animation.dimension[0]-mouseInAnimationX)/oThis.animation.dimension[0]*oThis.oversize[0]-oThis.oversize[0]);
	var top = Math.floor((oThis.animation.dimension[1]-mouseInAnimationY)/oThis.animation.dimension[1]*oThis.oversize[1]-oThis.oversize[1]);
	left = (left<-200) ? -200 : (left>0) ? 0 : left;
	top = (top<-60) ? -60 : (top>0) ? 0 : top;
	oThis.sensor.style.left = left+"px";
	oThis.sensor.style.top = top+"px";
	oThis.element.style.left = left+"px";
	oThis.element.style.top = top+"px";
	// return Mouse-Position über dem Element
	return new Array(left, top);
  };

  this.detail = function(event){
	// Position auf dem elmenent-Bild ermitteln
	var elemPosition = oThis.move(event);
    // get Mouse position
	aMousePos = MousePos(event);
	mouseInAnimationX = aMousePos[0]-oThis.animationX;
	mouseInAnimationY = aMousePos[1]-oThis.animationY;
	// relativer Wert ins Formular
	nA = (mouseInAnimationX-elemPosition[0])/oThis.element.dimension[0];
	nB = (mouseInAnimationY-elemPosition[1])/oThis.element.dimension[1];
	document.forms[oThis.form].elements["a"].value = nA;
	document.forms[oThis.form].elements["b"].value = nB;
	sURL = document.forms[oThis.form].action+'?id='+document.forms[oThis.form].elements["id"].value+'&part='+document.forms[oThis.form].elements["part"].value+'&a='+nA+'&b='+nB;
	qbGallery = qbWin(sURL, 920, 700, 'gallery', 'max');
	return false;
  };

  this.moveLock = function(event){
      this.movestatus = true;
  };
  this.moveUnLock = function(event){
      this.movestatus = false;
  };

  // Eventhandler hinzufügen
  this.animation.onmouseover = this.moveLock;
  this.animation.onmouseout = this.moveUnLock;
  this.animation.onmousemove = this.move;
  this.animation.onclick = this.detail;

	// private method
	function MousePos(e){
	if (!e) e = window.event;
	if(!e) return null;
	if (typeof window.pageYOffset != 'undefined'){
		qbepMousePosX = e.pageX;
		qbepMousePosY = e.pageY;
		}
	else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
		qbepMousePosX = window.event.clientX + document.documentElement.scrollLeft;
		qbepMousePosY = window.event.clientY + document.documentElement.scrollTop;
		}
	else if (typeof document.body != 'undefined'){
		qbepMousePosX = window.event.clientX + document.body.scrollLeft;
		qbepMousePosY = window.event.clientY + document.body.scrollTop;
		}
	return new Array(qbepMousePosX, qbepMousePosY)
	}
}

var leftmargin = 0;
var topmargin = 0;
function defaultPosition(){
	leftmargin = Math.round(Math.random()*200);
	topmargin = Math.round(Math.random()*60);
	oElem = document.getElementById("preanimation");
	oElem.style.left = "-"+leftmargin+"px";
	oElem.style.top = "-"+topmargin+"px";
	oElem = document.getElementById("sensoranimation");
	oElem.style.left = "-"+leftmargin+"px";
	oElem.style.top = "-"+topmargin+"px";
}
