//*****************************************************************************************************************************
// Objektinitialisierung
// DynamicLayers
//*****************************************************************************************************************************
// init Global
var GlobalTime = new Date();
var GlobalStarttime = GlobalTime.getTime()
var GlobalErrornumber = 0
var GlobalErrorlist = '"Alertme" - FehlerListe:<br>----------------------------<br>'
var GlobalXoffset = 0
var GlobalYoffset = 0
var GlobalXpage = 789
var GlobalYpage = 571
var GlobalXscreen = screen.availWidth
var GlobalYscreen = screen.availHeight
var isNS4 = (document.layers)? true:false
var isIE4 = (document.getElementById)? true:false
function DynamicLayer(Name){
	// Eigenschaften
		if (isNS4) {
                this.css 		= document.layers[Name]
				this.object		= this.css
				this.xpos 		= this.css.left
        		this.ypos 		= this.css.top
				
        		this.clipx1 	= this.css.clip.left 
				this.clipy1 	= this.css.clip.top 
				this.clipx2 	= this.css.clip.right 
				this.clipy2 	= this.css.clip.bottom 
		}
        else if (isIE4) {
       			this.css 		= document.getElementById(Name).style
				this.object 	= document.getElementById(Name)
				this.xpos 		= this.css.pixelLeft
        		this.ypos 		= this.css.pixelTop
				this.clipx1 	= 0
				this.clipx2 	= this.css.pixelWidth
				this.clipy1 	= 0
				this.clipy2		= this.css.pixelHeight
		}
		
		this.zorig				= this.css.zIndex
		
		this.dragable = false
		this.draginside = false
		this.dragx1 = 0-GlobalXoffset
		this.dragy1 = 0-GlobalYoffset
		this.dragx2 = GlobalXscreen-GlobalXoffset
		this.dragy2 = GlobalYscreen-GlobalYoffset
		this.clicked = ""
		this.dragtargeted = false
		this.dragorigx = this.xpos
		this.dragorigy = this.ypos
		this.dragtargetx = 0
		this.dragtargety = 0
		this.dragtargetevent = ""
		
		this.layername 			= Name 
		this.name 				= Name.substring(0,1).toUpperCase() + Name.substring(1,Name.length).toLowerCase() 
		this.moveing 			= false
		this.wipeing 			= false
		this.movestop			= false
		this.scalestop			= false
		this.tfadeing			= false
		this.tfadestop			= false
		this.movetimer			= new Array()
		this.movetimercounter	= 0
		this.FaderTimer			= new Array()
		this.fadetimercounter	= 0
		this.moveinside			= false
		
	// Methoden
		this.Show 				= LayerShow
		this.ShowNow 			= LayerShowNow
		this.Hide 				= LayerHide
		this.HideNow 			= LayerHideNow
		this.Set 				= LayerSet
		this.SetNow 			= LayerSetNow
		this.SetInside			= LayerSetInside
		this.SetInsideNow		= LayerSetInsideNow
		this.Clip 				= LayerClip
		this.ClipNow 			= LayerClipNow
		this.Wipe 				= LayerWipe
		this.WipeNow 			= LayerWipeNow
		this.WipeLoop			= LayerWipeLoop
		this.Move 				= LayerMove
		this.ReMove 			= LayerReMove
		this.MoveNow 			= LayerMoveNow
		this.MoveLoop 			= LayerMoveLoop
		this.MoveInside 		= LayerMoveInside
		this.MoveInsideNow 		= LayerMoveInsideNow
		this.LayerWrite 		= LayerLayerWrite
		this.LoadSource 		= LayerLoadSource
		this.Stop				= LayerStop
		this.SetZindex			= LayerSetZindex
		this.SetZindexNow		= LayerSetZindexNow
		this.SetImageSource = LayerSetImageSource
		this.GetImageSource = LayerGetImageSource
		
		this.MouseActive		= LayerMouseActive
		this.MouseActiveNow		= LayerMouseActiveNow
		this.MouseInactiveNow	= LayerMouseInactiveNow
		this.MouseDragLimit		= LayerMouseDragLimit
		this.MouseDragLimitPage	= LayerMouseDragLimitPage
		this.MouseDragInside 	= LayerMouseDragInside
		this.MouseDragNormal	= LayerMouseDragNormal
		this.MouseClick			= LayerMouseClick
		this.MouseDragTarget    = LayerMouseDragTarget
		this.MouseDragOrig    = LayerMouseDragOrig
}
//*****************************************************************************************************************************
// Imagesource
//*****************************************************************************************************************************
function LayerSetImageSource(bildname,bildsrc){
		if (isNS4) {
				eval("bildobjekt = document." + this.layername + ".document." + bildname)
		}
        else if (isIE4) {
				eval("bildobjekt = document.all." + bildname)
		}	
		bildobjekt.src = bildsrc	
}
function LayerGetImageSource(bildname){
		if (isNS4) {
				eval("bildobjekt = document." + this.layername + ".document." + bildname)
		}
        else if (isIE4) {
				eval("bildobjekt = document.all." + bildname)
		}	
		return bildobjekt.src
}
//*****************************************************************************************************************************
// Z-Index
//*****************************************************************************************************************************
function LayerSetZindex(delay,nummer){
		window.setTimeout (this.name + ".SetZindexNow(" + nummer + ")", delay)		
}
function LayerSetZindexNow(nummer){
		if (nummer>=0){this.css.zIndex = nummer}
		if (nummer<0){
			this.css.zIndex = this.zorig
		}
		
}
//*****************************************************************************************************************************
// Show/Hide
//*****************************************************************************************************************************
function LayerShow(delay) {
		window.setTimeout (this.name + ".ShowNow()", delay)		
}
function LayerShowNow() {
		this.css.visibility = "visible"
}
function LayerHide(delay) {
		window.setTimeout (this.name + ".HideNow()", delay)		
}
function LayerHideNow() {
		this.css.visibility = "hidden"
}
//*****************************************************************************************************************************
// Set
//*****************************************************************************************************************************
function LayerSet(delay,x,y,inside) {
		inside=(inside)? true:false
		window.setTimeout (this.name + ".SetNow(" + x + "," + y + "," + inside + ")", delay)        
}				
function LayerSetNow(x2,y2,inside) {
		inside=(inside)? true:false
		if (inside){
			x1 = this.xpos
			y1 = this.ypos
			cx1 = this.clipx1
			cy1 = this.clipy1
			cx2 = this.clipx2
			cy2 = this.clipy2
			this.ClipNow(cx1+(x1-x2),cy1+(y1-y2),cx2+(x1-x2),cy2+(y1-y2)) 
		}
        this.xpos = x2
        this.ypos = y2
        this.css.left = (this.xpos) + GlobalXoffset
        this.css.top = (this.ypos) + GlobalYoffset
}				
//*****************************************************************************************************************************
// Move
// Verschiebt von A nach B
//*****************************************************************************************************************************
function LayerReMove(start,stop,x2,y2,how,inside){
		inside=(inside)? true:false
		if (!how) how=4
		this.Stop()
		eval(this.name + '.Move(' + start + ',' + stop + ',' + x2 + ',' + y2 + ',' + how + "," + inside + ')')
}
function LayerMove(start,stop,x2,y2,how,inside){
		inside=(inside)? true:false
		if (!how) how=4
		duration = stop-start
		this.movetimercounter = this.movetimercounter+1
		this.movetimer[this.movetimercounter] = window.setTimeout (this.name + ".MoveNow(" + (duration) + "," + x2 + "," + y2 + "," + how + "," + inside + ")",start)		
		//alertme(this.name + ": Starter set to :" + start + ' /Counter:' + this.movetimercounter + ' /#' + this.movetimer[this.movetimercounter])
}
function LayerMoveNow(duration,x2,y2,how,inside){
		inside=(inside)? true:false
		if (!how) how=4
		if (this.moveing == false){
			this.moveing = true;
			time = new Date();
			starttime = time.getTime();
			this.MoveLoop(starttime, duration, this.xpos, this.ypos, x2, y2, how, inside)
		}				
		else{
			// alert and Try again
			//alertme(this.layername + "Moved noch! ...waiting to loop")
			window.setTimeout (this.name + ".MoveNow(" + duration + "," + x2 + "," + y2 + "," + how + "," + inside + ")",50)				
		}
}				
function LayerMoveLoop (starttime,duration,x1,y1,x2,y2,how,inside){
		time = new Date();
		now = time.getTime()
		if (this.movestop == false){
			if (now <= starttime + duration ){
				factor = ease(starttime,now,duration,how)
				x = x1 + (x2-x1)*factor
				y = y1 + (y2-y1)*factor
				this.SetNow(x,y,inside)
				window.setTimeout (this.name + ".MoveLoop(" + starttime + "," + duration + "," + x1 + "," + y1 + "," + x2 + "," + y2 + "," + how + "," + inside + ")", 50)
			}
			else {
				// Fertig
				this.SetNow(x2,y2,inside);
				this.moveing = false
			}	
		}
		else {
			// Stopped !
			//alertme(this.name + "Stopped!")
			this.movestop = false
			this.moveing = false
		}
}
// **********************************************************************
// MoveInside
// Inhalt wird im aktuellen Clipping verschoben
//***********************************************************************
function LayerSetInside(delay,x2,y2){
		this.Set(x2,y2,true)
}
function LayerSetInsideNow(x2,y2){
		this.SetNow(x2,y2,true)
}
function LayerMoveInside(start,stop,x2,y2,how){
		if (!how) how=4
		this.Move(start,stop,x2,y2,how,true)
}
function LayerMoveInsideNow(duration,x2,y2,how){
		if (!how) how=4
		this.MoveNow(duration,x2,y2,how,true)
}
function LayerReMoveInside(start,stop,x2,y2,how){
		if (!how) how=4
		this.ReMove(start,stop,x2,y2,how,true)
}
// **********************************************************************
// Stopper
// Layer-moves anhalten und alle Events löschen
//***********************************************************************
function LayerStop(){
	//Stop all aktive Move-Timeouts
		//alertme ("try to delete all " + this.movetimercounter + " MoverTimers for:" + this.name )
	 	if (this.movetimercounter > 0){
			for(i = 1; i <= this.movetimercounter; i++){
			//alertme("Clear move : #" + i + "internal#" + this.movetimer[i])
			window.clearTimeout(this.movetimer[i])
			}
		}
		this.movetimercounter = 0
	// Stop if allready moving
		//alertme ("try to stop:" + this.name + "ismoveing:" + this.moveing)
		if (this.moveing == true){this.movestop = true}
}
//*****************************************************************************************************************************
// Clip 
//*****************************************************************************************************************************
function LayerClip(delay,x1,y1,x2,y2) {
		window.setTimeout (this.name + ".ClipNow(" + x1 + "," + y1 + "," + x2 + "," + y2 + ")",delay)		
}
function LayerClipNow(x1,y1,x2,y2) {
        this.clipy1 = y1
		this.clipx2 = x2
		this.clipy2 = y2
		this.clipx1 = x1 
		if (isNS4) {
			this.css.clip.top = y1
			this.css.clip.right = x2
			this.css.clip.bottom = y2
			this.css.clip.left = x1
		}
        else if (isIE4) {
			this.css.clip = "rect("+y1+"px "+ x2 +"px "+ y2 +"px "+x1+"px)"
		}				
}
// **********************************************************************
// Wipe 
// Clipt von einem Bereich zum anderen
//***********************************************************************
function LayerWipe(start,stop,x3,y3,x4,y4,how){
		if (!how) how=4
		duration = stop-start
		//Setze timer
		window.setTimeout (this.name + ".WipeNow(" + duration + "," + x3 + "," + y3 + "," + x4 + "," + y4 + "," + how + ")", start)
}				
function LayerWipeNow(duration,x3,y3,x4,y4,how){
		if (!how) how=4
		if (this.wipeing == false){
			this.wipeing = true;
			time = new Date();
			starttime = time.getTime();
			this.WipeLoop(starttime,duration,this.clipx1,this.clipy1,this.clipx2,this.clipy2,x3,y3,x4,y4,how); 
		}				
		else{
			// alert and Try again
			//alertme(this.layername + "Wipet noch!")
			window.setTimeout (this.name + ".WipeNow(" + duration + "," + x3 + "," + y3 + "," + x4 + "," + y4 + "," + how + ")", 50)
		}
}				
function LayerWipeLoop(starttime,duration,x1,y1,x2,y2,x3,y3,x4,y4,how){
		time = new Date();
		now = time.getTime()
		if (now <= starttime + duration){
			factor = ease(starttime,now,duration,how)
			this.ClipNow(x1 + (x3-x1)*factor, y1 + (y3-y1)*factor, x2 + (x4-x2)*factor, y2 + (y4-y2)*factor)
			window.setTimeout (this.name + ".WipeLoop(" + starttime + "," + duration + "," + x1 + "," + y1 + "," + x2 + "," + y2 + "," + x3 + "," + y3 + "," + x4 + "," + y4 + "," + how + ")", 50)
		}
		else{
			this.wipeing = false
			this.ClipNow(x3,y3,x4,y4)
			}
}
//*****************************************************************************************************************************
// Layer-writer
// Inhalt wird komplett ersetzt
// Keine CSS einsetzen!!!
//*****************************************************************************************************************************
function LayerLayerWrite(content){
		if (isNS4) {
				eval("layer = document." + this.layername + ".document")
				layer.open()
                layer.write(content)
                layer.close()
		}
        else if (isIE4) {
				document.all[this.layername].innerHTML = content
		}	
}
//*****************************************************************************************************************************
// Layer-Loader
// Inhalt wird komplett durch html-Seite ersetzt
//*****************************************************************************************************************************
function LayerLoadSource(url) {
		//alertme(this.name + ":  Load url :" + url)
		if (isNS4) {
                //breite = this.clipx2
                //alertme(breite)
				this.object.load(url,this.clipx2)
		}
        else if (isIE4) {
				GlobalLoaderBuffer = this.layername
				parent.bufferFrame.document.location = url
		}
}
//*****************************************************************************************************************************
//
// Mouse & Drag Event Handlers
//
//*****************************************************************************************************************************
// Globale Variablen
var GlobalMouseX = 0
var GlobalMouseY = 0
var GlobalMouseStatus = ''
var GlobalMouseAction = ''
var GlobalMouseXstart = 0
var GlobalMouseYstart = 0
var GlobalMouseDragging = false
var GlobalMouseDragOffsetX = 0
var GlobalMouseDragOffsetY = 0
var GlobalMouseDragCounter = 0
var GlobalMouseDragObjects = new Array()
var GlobalMouseDragObj = 0
var GlobalMouseClicktime
//***********************************************************************
// Mouse-Events von Dokument-Ebene Einfangen
function InitMouse(){
		document.onmousedown = MouseDown
        document.onmousemove = MouseMove		
		document.onmouseup = MouseUp
		if (isNS4) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)
}
//***********************************************************************
function MouseDown(ereignis){
		//nur linker Button
		if ((isNS4 && ereignis.which!=1) || (isIE4 && event.button!=1)) return true
		// Position der Maus
		GlobalMouseX= (isNS4)? ereignis.pageX-GlobalXoffset : event.x+document.body.scrollLeft-GlobalXoffset
		GlobalMouseY= (isNS4)? ereignis.pageY-GlobalYoffset : event.y+document.body.scrollTop-GlobalYoffset
		GlobalMouseStatus = 'Down'
		// Remember Mousecoords & time		
		GlobalMouseXstart = GlobalMouseX
		GlobalMouseYstart = GlobalMouseY
		time = new Date();
		GlobalMouseClicktime = time.getTime()
		// Dragable Elements? Wenn GlobalDragCounter=0 wirds uebersprungen!
		for(i = 1; i <= GlobalMouseDragCounter; i++){
			// Dragable Element getroffen ?
			GlobalMouseDragObj = eval(GlobalMouseDragObjects[i])
			if (GlobalMouseDragObj.dragable==true && GlobalMouseX>=GlobalMouseDragObj.xpos+GlobalMouseDragObj.clipx1 && GlobalMouseX<=GlobalMouseDragObj.xpos+GlobalMouseDragObj.clipx2 && GlobalMouseY>=GlobalMouseDragObj.ypos+GlobalMouseDragObj.clipy1 && GlobalMouseY<=GlobalMouseDragObj.ypos+GlobalMouseDragObj.clipy2){
				// getroffen!
				//alertme("getroffen: " + GlobalMouseDragObj.name)
				GlobalMouseDragging = true			
				GlobalMouseDragOffsetX = GlobalMouseX-GlobalMouseDragObj.xpos
				GlobalMouseDragOffsetY = GlobalMouseY-GlobalMouseDragObj.ypos
				// falls dragtargeted
				if (GlobalMouseDragObj.dragtargeted == true){
					// anhalten (falls grad unterwegs zurueck)
					GlobalMouseDragObj.Stop()
				}
				MouseCoords()
				return false
			}
		}
		MouseCoords()
		return true
}
//***********************************************************************
function MouseMove(ereignis){
		if ((isNS4 && ereignis.which!=1) || (isIE4 && event.button!=1)) return true
		GlobalMouseX= (isNS4)? ereignis.pageX-GlobalXoffset : event.x+document.body.scrollLeft-GlobalXoffset
		GlobalMouseY= (isNS4)? ereignis.pageY-GlobalYoffset : event.y+document.body.scrollTop-GlobalYoffset
		// Coordinatenanzeige
		MouseCoords()
		// falls Dragging:
		if (GlobalMouseDragging == true){
			//Position des Elements:
			Xpos = GlobalMouseX-GlobalMouseDragOffsetX
			Ypos = GlobalMouseY-GlobalMouseDragOffsetY
			// Limits erreicht?
			if (Xpos <= GlobalMouseDragObj.dragx1){Xpos = GlobalMouseDragObj.dragx1}
			if (Xpos >= GlobalMouseDragObj.dragx2){Xpos = GlobalMouseDragObj.dragx2}
			if (Ypos <= GlobalMouseDragObj.dragy1){Ypos = GlobalMouseDragObj.dragy1}
			if (Ypos >= GlobalMouseDragObj.dragy2){Ypos = GlobalMouseDragObj.dragy2}
			if (GlobalMouseDragObj.draginside == true){GlobalMouseDragObj.SetInsideNow(Xpos,Ypos)}
			if (GlobalMouseDragObj.draginside == false){GlobalMouseDragObj.SetNow(Xpos,Ypos)}
			return false
		}
		else{
			return true
		}
}
//***********************************************************************
function MouseUp(ereignis){
		if ((isNS4 && ereignis.which!=1) || (isIE4 && event.button!=1)) return true
		GlobalMouseX= (isNS4)? ereignis.pageX-GlobalXoffset : event.x+document.body.scrollLeft-GlobalXoffset
		GlobalMouseY= (isNS4)? ereignis.pageY-GlobalYoffset : event.y+document.body.scrollTop-GlobalYoffset
		GlobalMouseStatus = 'Up'		
		// wurde geklickt?
		time = new Date();
		now = time.getTime()
		// unter 300ms und innerhalb von 10 pixeln gilt als geklicked
		if ((now-GlobalMouseClicktime) <= 300 && Math.abs(GlobalMouseXstart-GlobalMouseX)<=10 && Math.abs(GlobalMouseYstart-GlobalMouseY)<=10){
			GlobalMouseAction="Klicked" 
			if (GlobalMouseDragging==true){
				// zurueckschieben falls leicht verrutscht
				GlobalMouseDragObj.SetNow(GlobalMouseXstart-GlobalMouseDragOffsetX,GlobalMouseYstart-GlobalMouseDragOffsetY)
				//event ausloesen
				eval(GlobalMouseDragObj.clicked)
			}
		}
		else{
			GlobalMouseAction="Dragged"
			//targeted ?
			if (GlobalMouseDragObj.dragtargeted == true){
				//liegts es jetzt richig?
				toleranz = 5
				if (GlobalMouseDragObj.xpos>=GlobalMouseDragObj.dragtargetx-toleranz && GlobalMouseDragObj.xpos<=GlobalMouseDragObj.dragtargetx+toleranz && GlobalMouseDragObj.ypos>=GlobalMouseDragObj.dragtargety-toleranz && GlobalMouseDragObj.ypos<=GlobalMouseDragObj.dragtargety+toleranz){
					// liegt im toleranzbereich!
					//alertme("ist drin")
					GlobalMouseDragObj.MoveNow(100,GlobalMouseDragObj.dragtargetx,GlobalMouseDragObj.dragtargety,2)
					eval (GlobalMouseDragObj.dragtargetevent)
				}
				else{
					//alertme("falsch abgelegt")
					GlobalMouseDragObj.MoveNow(1000,GlobalMouseDragObj.dragorigx,GlobalMouseDragObj.dragorigy,2)
				}
			}
		
		}
		GlobalMouseDragging = false			
		MouseCoords()
		return true
}
//***********************************************************************
function LayerMouseActive(delay){
	window.setTimeout (this.name + ".MouseActiveNow()", delay)
}
//***********************************************************************
function LayerMouseActiveNow(){
	GlobalMouseDragCounter++
	GlobalMouseDragObjects[GlobalMouseDragCounter] = this.name
	this.dragable = true
	this.dragorigx = this.xpos
	this.dragorigy = this.ypos
}
//***********************************************************************
function LayerMouseDragTarget(x,y,event){
	this.dragtargeted = true
	this.dragtargetx = x
	this.dragtargety = y
	this.dragtargetevent = event
}
//***********************************************************************
function LayerMouseDragOrig(x,y){
	this.dragtargeted = true
	this.dragorigx = x
	this.dragorigy = y
}
//***********************************************************************
function LayerMouseDragLimit(x1,y1,x2,y2){
	this.dragx1 = x1
	this.dragy1 = y1
	this.dragx2 = x2
	this.dragy2 = y2
}
//***********************************************************************
function LayerMouseDragLimitPage(){
	this.dragx1 = this.clipx1
	this.dragy1 = this.clipy1
	this.dragx2 = GlobalXpage-this.clipx2
	this.dragy2 = GlobalYpage-this.clipy2
}
//***********************************************************************
function LayerMouseDragInside(){
	this.draginside = true
}
//***********************************************************************
function LayerMouseDragNormal(){
	this.draginside = false
}
//***********************************************************************
function LayerMouseInactiveNow(){
	this.dragable = false
	// So gehts auch, aber der name sollte noch aus array GlobalDragObjects(is n String!!!) entfernt werden...
}
//***********************************************************************
function LayerMouseClick(Ereignis){
	this.clicked = Ereignis
}
//*****************************************************************************************************************************
//  
// Berechnungsfunktionen mit Rueckgabewert 
//  
//*****************************************************************************************************************************
//***********************************************************************
// Ease - Bewegungsablaeufe 
// ***********************************************************************
function ease(starttime,now,duration,welcher){
		switch(welcher){
			case 1: //Linear
			factor = (now - starttime)/duration
			break;
			case 2: //Ease out
   			factor = Math.sin(((now - starttime)/duration) * (0.5*Math.PI))
			break;
			case 3: //Ease in
   			factor = 1-(Math.cos(((now - starttime)/duration) * (0.5*Math.PI)))
			break;
			case 4: //Ease in & out
   			factor = (Math.cos((((now - starttime)/duration) * Math.PI)+Math.PI)+1)/2
			break;
			case 5: //Ease out mit bounce
   			factor = Math.sin(((now - starttime)/duration) * 2)*1.1
			break;
			case 6: //Ease in & out mit bounce
   			factor = ((Math.sin((((now - starttime)/duration) * 4)-2)*1.1)+1)/2
			break;
			
			default://Linear
			factor = (now - starttime)/duration
			break;
		}
		return factor;
} 

