/*	Versieht ein DIV-Element mit Animation zum Ein- und Ausblenden

*	@package	DirMenu

*	@author	Fabian Kunz

*	@version	1.0

*	@access	public

*	@since		Javascript 1.2

*

*	@param		string	id ID-Attribut des DIV-Tags

*	@param		string	position Positionierung des Elements: "absolute" oder "relative"

*						Bei "absolute" dürfen für das umgebende Element keine Paddings gesetzt sein (falsche Koordinatenberechnung bei IE)

*	@param		string	animation Ausgangspunkt für Einblendung (Ausblendung erfolgt in umgekehrter Richtung). Mögliche Werte: lefttop, top, righttop, left, right, leftbottom, bottom, rightbottom

*	@param		int		distance Strecke ab der Animation mit Höchstgeschwindigkeit abläuft

*	@param		int		maxspeed maximales Bewegungsinkrement

*	@param		int		interval Zeitinterval in ms

*/

function SlideWin(id, position, animation, distance, maxspeed, interval) {

	

	/*	Entfernt führende leere Textknoten und Umbrüche im ersten Kindknoten

	*	@param	node	obj Ausgangspunkt

	*	@return	node	"gereinigtes" Objekt

	*/

	this.cleanUp = function(obj){

		

		// zuerst leere Textknoten, Kommentare und Umbrüche entfernen

		while(((obj.firstChild.nodeType==3) && (obj.firstChild.data.search(/\S/)==-1)) || obj.firstChild.nodeType==8 || (obj.firstChild.tagName && obj.firstChild.tagName.toLowerCase()=="br"))	{	

			obj.removeChild(obj.firstChild)

		}

		// dann innerhalb des Textes Leerzeichen Umbrüche und Tabs entfernen (primär für Mozilla Browser)

		if (obj.firstChild.nodeType==3) {

			text=obj.firstChild.data

			while (text.charCodeAt(0)<33) {text=text.substr(1)}

			obj.firstChild.data=text

		}

		return (obj)

	}

	

	/* Setzt Position

	* @param	int		x Koordinate, auf die das Objekt gesetzt werden soll

	* @param	int		y Koordinate, auf die das Objekt gesetzt werden soll

	*/

	this.setPosition = function(x, y) {



		this.node.style.width = x + "px"

		this.node.style.left = (this.width -  x) * this.reversex + this.left + "px"

		this.node.firstChild.style.left = (x - this.width) * this.reversex + "px"

		this.node.style.height = y + "px"

		this.node.style.top = (this.height -  y) * this.reversey + this.top + "px"

		this.node.firstChild.style.top = (y - this.height) * this.reversey + "px"

	}

	

	/* Setzt Ausgangs- und Endposition und startet Einblendung. Einblendung von x0 nach x1 und von y0 nach y1

	*/

	this.show = function() {

		if (this.status != "out" && this.handler == null) {

			this.x0 = this.startx

			this.x1 = this.width

			this.y0 = this.starty

			this.y1 = this.height

			this.dir = 1		// Richtungsfaktor

			this.start()

		}

	}

	

	/* Setzt Ausgangs- und Endposition und startet Ausblendung

	*/

	this.hide = function() {

		if (this.status != "in" && this.handler == null) {

			this.x0 = this.width

			this.x1 = this.startx

			this.y0 = this.height

			this.y1 = this.starty

			this.dir = -1		// Richtungsfaktor

			this.start()

		}

	}

	

	/* Setzt Fenster sofort auf Startposition zurück (ohne Animation)

	*/

	this.endPos = function() {

		if (this.status != "out" && this.handler == null) {

			this.setPosition(this.width, this.height)

			this.status = "out"

		}

	}

	

	/* Setzt Fenster sofort auf Startposition zurück (ohne Animation)

	*/

	this.startPos = function() {

		if (this.status != "in" && this.handler == null) {

			this.setPosition(this.startx, this.starty)

			this.status = "in"

		}

	}

	

	/*  Startet Animation

	*/

	this.start = function() {

		this.setPosition(this.x0, this.y0)

		this.speed = this.calculateSpeed()

		this.handler = setInterval("SlideWinAnimate('" + this.id + "')", this.interval)

	}

	

	/* Berechnet den Index für Geschwindigkeitsdaten aus der noch zurückzulegenden Distanz (Bremsung)

	* @return	int		Index für increment und interval

	*/

	this.calculateSpeed = function() {

		var distance =  (this.movex != 0) ? Math.abs(this.x1-this.x0) : Math.abs(this.y1-this.y0)

		

		// Lineare Geschwindigkeitsberechnung

		return (distance >= this.distance) ? this.maxspeed : Math.floor((distance/ this.distance) * (this.maxspeed - 1) + 1)

	}



	

	/* Initialisieren

	*/

	this.id = id

	this.node = document.getElementById(id)

	this.node.SlideWin = this

	this.node = this.cleanUp(this.node)

	this.node.style.overflow = "hidden"

	

	this.animation = animation

	if (position == "relative") {

		this.left = 0

		this.top = 0

	}

	else {

		this.left = this.node.offsetLeft

		this.top = this.node.offsetTop

	}

	this.width = this.node.offsetWidth

	this.height = this.node.offsetHeight

	this.maxspeed = maxspeed

	this.distance = distance

	this.interval = interval

	this.status = "in"

	this.startx =  this.width

	this.starty = this.height

	this.reversex = this.reversey = 0

	this.movex = this.movey = 0			// Faktor für hor. und vert. Bewegung

	

	// Startbedingungen für horizontale Bewegung setzen

	if (animation.indexOf('left') > -1) {

		this.reversex =  0

		this.startx =  0

		this.movex = 1

	}

	else if (animation.indexOf('right') > -1) {

		this.reversex =  1

		this.startx =  0

		this.movex = 1

	}

	

	// Startbedingungen für vertikale Bewegung setzen

	if (animation.indexOf('top') > -1) {

		this.reversey =  0

		this.starty =  0

		this.movey = 1

	}

	else if (animation.indexOf('bottom') > -1) {

		this.reversey =  1

		this.starty =  0

		this.movey = 1

	}

	

	// Korrektur für 45° Animationen

	if (this.movey * this.movex == 1) {

		

		// wenn Breite grösser als Höhe, Startposition um Differenz nach rechts verschieben

		this.startx = (this.width - this.height) * (this.width > this.height)

	

		// wenn Höhe grösser als Breite, Startposition um Differenz nach unten verschieben

		this.starty = (this.height - this.width) * (this.height > this.width)

	}

	

	this.handler = null

	this.setPosition(this.startx, this.starty)

	this.node.style.visibility = "visible"

	

}



/* Führt die Bewegung aus und beendet sie, wenn Ziel erreicht

*/

function SlideWinAnimate(id) {

	var obj =  document.getElementById(id).SlideWin

	var newspeed

	

	// Neue Koordinaten

	obj.x0 += obj.dir * obj.movex * obj.speed

	obj.y0 += obj.dir * obj.movey * obj.speed

	

	// Ziel erreicht?

	if (obj.x0 * obj.dir * obj.movex >= obj.x1 || obj.y0 * obj.dir * obj.movey >= obj.y1) {

		obj.setPosition(obj.x1, obj.y1)

		clearInterval(obj.handler)

		obj.status = (obj.dir == 1) ? "out" : "in"

		obj.handler = null

	}

	// Neue Position setzen und Geschwindigkeitberechnung

	else {

		obj.setPosition(obj.x0, obj.y0)

		obj.speed = obj.calculateSpeed()

	}

}


