var isDOM=(document.getElementById?true:false); 
var isIE4=((document.all&&!isDOM)?true:false);
var isNS4=(document.layers?true:false);
var isIE=(navigator.userAgent.indexOf("MSIE")>-1)?true:false;
var isFF=(navigator.userAgent.indexOf("Firefox")>-1)?true:false;
var isNS=(navigator.userAgent.indexOf("Netscape")>-1)?true:false;
//showElementsIn(navigator);

var nameInt = 0;
function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
}
function getImgRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.images[id];
}
var intervalManagerObj = new IntervalManager();


var eventHandler = null;
function init_old() {
	intervalManagerObj = new IntervalManager();
	naviBar = new NavigationBar("topBar",navDefinition1);
	naviBar2= new NavigationBar("bottomBar",navDefinition2);
	scroller1 = new Scroller(scrollerDefinition1);
	scroller1.init();
	


}

function IntervalManager() {
	this.interval = {};
	this.intervalRef = {};
	this.object = {}
	this.setInterval = function (interval) {
	  var name = interval.name;
		if (!this.object[interval.object.name]) this.object[interval.object.name]={}
		this.object[interval.object.name][name]=interval;
		this.interval[name] = interval;
		this.intervalRef[name] = window.setInterval("intervalManagerObj.doInterval(\""+name+"\")",interval.intervalLength);
	}
	this.terminateInterval = function(interval) {
		window.clearInterval(this.intervalRef[interval.name]);
		this.intervalRef[interval.name]=null;
		this.interval[name]=null;
		if (interval.method_terminate) interval.object[interval.method_terminate](interval.method_arg);
	}
	this.doInterval = function(name) {
		if (this.interval[name])	{
			this.interval[name].object[this.interval[name].method](this.interval[name]);
		}
	}
	this.terminateAllIntervals = function(object) {
		if (this.object[object.name]) {
			for (var name in this.object[object.name]) {
				var interval = this.object[object.name][name];
				this.terminateInterval(interval);
			}
			this.object[object.name]=null;
		}
	}
}
function cos1(c,t,x) {
	return (c-t)*Math.cos(30*x*x*x*x)*(Math.cos(3.14*(-x*x+2*x))+1)*0.5+t;
}
function lin1(c,t,x) {
	return (c-t)*(1-x)+t;
}
function cos2(c,t,x) {
	return (x-(x*x))*100+(c-t)*Math.cos(30*x*x*x*x)*(Math.cos(3.14*(-x*x+2*x))+1)*0.5+t;
}

function Interval(object,method,method_arg,method_terminate,intervalLength) {
	this.t=0;
	this.name=getName();
	this.object = object;
	this.method = method;
	this.method_arg=method_arg;
	this.method_terminate = method_terminate;
	this.intervalLength=intervalLength;
}

function NavigationBar(name,navigationDefinition) {
	this.name=getName();
	this.ticketManager = null;
	this.def=navigationDefinition;
	this.ref = getRef(name);
	
	this.move = function(targetPosition) {
		if (this.ticketManager == null) this.ticketManager = new TicketManager(this);
		this.ticketManager.buildTickets(targetPosition);
		
		return true;
	}
	this.executeTicket = function(interval) {
		var ticket = interval.method_arg;
		interval.t++;
		
		
		
		var distanceX = this.def[ticket.getCurrentXLabel()]-this.def[ticket.getTargetXLabel()];
		var distanceY = this.def[ticket.getCurrentYLabel()]-this.def[ticket.getTargetYLabel()];
		var distanceW = this.def[ticket.getCurrentWLabel()]-this.def[ticket.getTargetWLabel()];
		var distanceH = this.def[ticket.getCurrentHLabel()]-this.def[ticket.getTargetHLabel()];
			
		if (distanceX==0 && distanceY==0 && distanceW==0 && distanceH ==0) intervalManagerObj.terminateInterval(interval);
		
		var steps=this.def["time"+ticket.train]/this.def["intervalLength"+ticket.train];
		var functionArg=interval.t/steps;
		
		if (this.def["function"+ticket.train+"X"]) this.ref.style.left	= Math.round(window[this.def["function"+ticket.train+"X"]](this.def[ticket.getCurrentXLabel()],this.def[ticket.getTargetXLabel()],functionArg))+"px";
		if (this.def["function"+ticket.train+"Y"]) this.ref.style.top	= Math.round(window[this.def["function"+ticket.train+"Y"]](this.def[ticket.getCurrentYLabel()],this.def[ticket.getTargetYLabel()],functionArg))+"px";
		if (this.def["function"+ticket.train+"W"]) this.ref.style.width = Math.round(window[this.def["function"+ticket.train+"W"]](this.def[ticket.getCurrentWLabel()],this.def[ticket.getTargetWLabel()],functionArg))+"px";
		if (this.def["function"+ticket.train+"H"]) this.ref.style.height= Math.round(window[this.def["function"+ticket.train+"H"]](this.def[ticket.getCurrentHLabel()],this.def[ticket.getTargetHLabel()],functionArg))+"px";
		
		if (interval.t==steps) {
			this.ref.style.left = this.def[ticket.getTargetXLabel()]+"px";
			this.ref.style.top = this.def[ticket.getTargetYLabel()]+"px";
			this.ref.style.width = this.def[ticket.getTargetWLabel()]+"px";
			this.ref.style.height = this.def[ticket.getTargetHLabel()]+"px";
			intervalManagerObj.terminateInterval(interval);
		}
	
	}
	this.ticketExecuted = function (ticket) {
		this.ticketManager.ticketExecuted();
	}
	function TicketManager(navBar) {
		this.navBar = navBar;
		this.firstTicket = null;
		this.lastTicket = null;
		this.ticket = {};
		this.cf=this.navBar.def.l1_start;
		this.cs=this.navBar.def.l2_start;
		this.ticketExecuted = function() {
			this.firstTicket = this.firstTicket.next;
			if (!this.firstTicket) {
				this.lastTicket=null;
				if (this.cf==0 && this.cs==0) {
					this.navBar.ref.style.visibility="hidden";
				}
			} else {
				this.executeTicketQueue(); 
			}
		}
		this.executeTicketQueue = function () {
			this.navBar.ref.style.visibility="visible";
			this.cf=this.firstTicket.tf;
			this.cs=this.firstTicket.ts;
			intervalManagerObj.setInterval(
				new Interval(
					this.navBar,
					"executeTicket",
					this.firstTicket,
					"ticketExecuted",
					this.navBar.def["intervalLength"+this.firstTicket.train]
				)
			);
		}
		this.queueTicket = function (ticket) {
			if (this.firstTicket==null) {
				this.firstTicket = ticket;
				this.lastTicket = ticket;
				this.executeTicketQueue();
			} else {
				this.lastTicket.next = ticket;
				this.lastTicket = ticket;
			}
		}
		this.buildTickets = function(targetPosition){
			var ticket = null;
			if (this.firstTicket) {
				this.firstTicket.next = null;
				this.lastTicket = this.firstTicket;
			}
			if (this.cf == targetPosition.split("_")[0]) {
				if (this.cs != targetPosition.split("_")[1]) {
					// andere zweite Ebene -> an richtige zweite Ebene fahren
					ticket = new Ticket();
					ticket.cf=this.cf;
					ticket.cs=this.cs;
					ticket.tf=this.cf;
					ticket.ts=targetPosition.split("_")[1];
					ticket.train="SecondLevel";
			//		alert(this.navBar.name+" an richtige zweite Ebene fahren");
					this.queueTicket(ticket);
				}
			} else {
				if (this.cs != 0) {
					//hochfahren
					ticket = new Ticket();
					ticket.cf=this.cf;
					ticket.cs=this.cs;
					ticket.tf=this.cf;
					ticket.ts=0;
					ticket.train="SecondLevel";
//					alert(this.navBar.name+" hochfahren");

					this.queueTicket(ticket);
				} 
				//erste Ebene wechseln
					ticket = new Ticket();
					ticket.cf=this.cf;
					ticket.cs=0;
					ticket.tf=targetPosition.split("_")[0];
					ticket.ts=0;
					
					ticket.train="FirstLevel";
		//			alert(this.navBar.name+" ersteEbene wechseln");
					this.queueTicket(ticket);
				if (0 != targetPosition.split("_")[1]) {
					//von 0 an richtige zweite Ebene fahren.
					ticket = new Ticket();
					ticket.cf=targetPosition.split("_")[0];
					ticket.cs=0;
					ticket.tf=targetPosition.split("_")[0];
					ticket.ts=targetPosition.split("_")[1];
					ticket.train="SecondLevel";
		//			alert(this.navBar.name+" von 0 an richtige Stelle der zweiten Ebene fahren");
					this.queueTicket(ticket);
				}
			}
		}
	}
	function Ticket() {
		this.cf=null;
		this.cs=null;
		this.tf=null;
		this.ts=null;
		this.next=null;
		this.train="";
		this.getCurrentXLabel = function () {
			var retVal = "x"+this.cf+"_"+this.cs;
			return retVal;
		}
		this.getCurrentYLabel = function () {
			var retVal = "y"+this.cf+"_"+this.cs;
			return retVal;
		}
		this.getTargetXLabel = function () {
			var retVal = "x"+this.tf+"_"+this.ts;
			return retVal;
		}
		this.getTargetYLabel = function () {
			var retVal = "y"+this.tf+"_"+this.ts;
			return retVal;
		}
		this.getCurrentWLabel = function () {
			var retVal = "w"+this.cf+"_"+this.cs;
			return retVal;
		}
		this.getCurrentHLabel = function () {
			var retVal = "h"+this.cf+"_"+this.cs;
			return retVal;
		}
		this.getTargetWLabel = function () {
			var retVal = "w"+this.tf+"_"+this.ts;
			return retVal;
		}
		this.getTargetHLabel = function () {
			var retVal = "h"+this.tf+"_"+this.ts;
			return retVal;
		}

	}
}
function getName() {
	nameInt++;
	return "n"+nameInt;
}



function Scroller(scrollerDef) {
	this.name = scrollerDef.name;
	this.scrollerDef = scrollerDef;
	this.ctRef = null;
	this.sbRef = null;
	this.sbHeight=null;
	this.ctHeight=null;
	this.sbStartY=null;
	
	/*this.upDivRef = getRef(name+"UpDiv");
	this.upImgRef = getImgRef(name+"UpImg");
	this.downDivRef = getRef(name+"DownDiv");
	this.downImgRef = getImgRef(name+"DownImg");
	*/
	
	this.contentTop=0;
	this.scrollBarTop=0;
	this.offsetContentTop=0;
	this.offsetScrollBarTop=0;
	this.interval = null;
	this.init = function() {
		this.ctRef = getRef(this.scrollerDef.nameContentDiv);
		//alert(this.scrollerDef.nameScrollerNoBarDiv);
		this.sbRef = getRef(this.scrollerDef.nameScrollerBarDiv);
		this.sbRefBg = getRef(this.scrollerDef.nameScrollerBgDiv);
		this.sbRefBgNoScrollbar = getRef(this.scrollerDef.nameScrollerNoBarDiv);
		this.ctHeight = parseInt(this.ctRef.offsetHeight);
		
		this.sbHeight = parseInt(this.scrollerDef.sbMaxHeight*(this.scrollerDef.clipHeight/this.ctHeight));
		this.sbRef.style.height=this.sbHeight+"px";
		this.offsetContentTop=parseInt(this.ctRef.offsetTop);
		//this.offsetScrollBarTop=parseInt(this.sbRef.offsetTop);
		this.offsetScrollBarTop=0;
		this.drawContent();
		this.drawBar();
		if (this.sbHeight<this.scrollerDef.sbMaxHeight) {
			//this.sbRef.style.visibility = "visible";
			this.sbRefBg.style.visibility = "visible";
		} else {
			this.sbRefBgNoScrollbar.style.visibility = "visible";
		}
		this.ctRef.style.visibility = "visible";
		
	}
	this.top = function() {
		this.contentTop = 0;
		this.scrollBarTop = 0;
		this.drawContent();
		this.drawBar();
	}
	this.upStart = function() {
		intervalManagerObj.setInterval(new Interval(this,"up",{t:0},null,15));
	}
	this.upStop = function() {
		intervalManagerObj.terminateAllIntervals(this);
	}
	this.up = function(interval) {
		if (this.contentTop>=0) {
			this.upStop();
			this.contentTop = 0;
			this.scrollBarTop = 0;
		} else {
			this.contentTop++;
			this.scrollBarTop = -Math.round((this.scrollerDef.sbMaxHeight-this.sbHeight)*this.contentTop/(this.ctHeight-this.scrollerDef.clipHeight));
		}
		this.drawContent();
		this.drawBar();
	}
	this.downStart = function() {
		intervalManagerObj.setInterval(new Interval(this,"down",{t:0},null,15));
	}
	this.downStop = function () {
		intervalManagerObj.terminateAllIntervals(this);
	}
	this.down = function(interval){
		if (this.contentTop<=(this.scrollerDef.clipHeight-this.ctHeight)) {
			this.downStop();
			this.contentTop = this.scrollerDef.clipHeight-this.ctHeight;
			this.scrollBarTop = this.scrollerDef.sbMaxHeight-this.sbHeight;
		} else {
			this.contentTop--;
			this.scrollBarTop = -Math.round((this.scrollerDef.sbMaxHeight-this.sbHeight)*this.contentTop/(this.ctHeight-this.scrollerDef.clipHeight));
		}
		this.drawContent();
		this.drawBar();
	}
	this.barMouseMove = function(event) {
		
		//alert(this.scrollBarTop);
		if (!this.sbStartY) {
			this.sbStartY=parseInt(event.clientY);
		}
		var delta = parseInt(event.clientY) - this.sbStartY;
		if ((this.scrollBarTop + delta) < 0 ) {
			//Anschlag oben
			this.scrollBarTop = 0;
			this.contentTop = 0;
		} else if ((this.scrollBarTop + delta) > (this.scrollerDef.sbMaxHeight-this.sbHeight)) {
			//Anschlag unten
			this.scrollBarTop = this.scrollerDef.sbMaxHeight-this.sbHeight;
			this.contentTop = this.scrollerDef.clipHeight-this.ctHeight;
		} else {
			//alles im grünen Bereich
			this.sbStartY=parseInt(event.clientY)
			this.scrollBarTop += delta;
			this.contentTop-=Math.round((this.ctHeight-this.scrollerDef.clipHeight)*(delta)/(this.scrollerDef.sbMaxHeight-this.sbHeight));	
		}
		
		//getRef("output").innerHTML = "event.clientY: "+event.clientY+"<br>this.scrollBarTop: "+this.scrollBarTop+"<br>this.sbStartY: "+this.sbStartY;
		
		//alert(this.ctHeight+"\n"+this.scrollerDef.clipHeight+"\n"+this.sbStartY+"\n"+event.clientY+"\n"+this.scrollerDef.sbMaxHeight+"\n"+this.sbHeight);
		this.drawContent();
		this.drawBar();
		
	}
	this.barMouseDown = function() {
		
		if (!document.eventMapping) document.eventMapping = {};
		document.eventMapping.mousemove = {object:this,method:"barMouseMove"};
		document.onmousemove = handleEvent;
		document.eventMapping.mouseup = {object:this,method:"barMouseUp"};
		document.onmouseup = handleEvent;


	}
	this.barMouseUp = function() {
		this.sbStartY=null;
		//alert(this.scrollBarTop);
		document.onmousemove = null;
		document.onmouseup = null;
		document.eventMapping.mousemove = null;
		document.eventMapping.mouseup = null;
	}
	this.drawContent = function() {
		this.ctRef.style.top=(this.offsetContentTop+this.contentTop)+"px";
		var t = -this.contentTop;
		var b = -this.contentTop+this.scrollerDef.clipHeight;
		var l =0;
		var r = this.scrollerDef.clipWidth;
		this.ctRef.style.clip="rect("+t+"px "+r+"px "+b+"px "+l+"px)";
		//this.ctRef.style.visibility = "visible;"
	}
	this.drawBar = function() {
		this.sbRef.style.top=(this.offsetScrollBarTop+this.scrollBarTop)+"px";
		//this.sbRef.style.visibility = "visible";
	}
}
function handleEvent(event) 
{
	if (isIE) {
		if (!event) event=window.event;
		if (event.srcElement.eventMapping) event.srcElement.eventMapping[event.type].object[event.srcElement.eventMapping[event.type].method](event);
		else if(document.eventMapping) if(document.eventMapping[event.type]) document.eventMapping[event.type].object[document.eventMapping[event.type].method](event);
	} 
  //else if (isNS||isFF) {
  else {

		if (!event) event=window.event;
		if (event.target.eventMapping) event.target.eventMapping[event.type].object[event.target.eventMapping[event.type].method](event);
		else if(event.target.ownerDocument.eventMapping) if(event.target.ownerDocument.eventMapping[event.type]) event.target.ownerDocument.eventMapping[event.type].object[event.target.ownerDocument.eventMapping[event.type].method](event);
	}
}

function showElementsIn (object) {
	var output="";
	var n=5; 
	var i=0;
for (property in object){
		if (i<n) {output += property+": "+object[property]+"<br><br>"; i++} else {output += property+": "+object[property]+"<br><br>";i=0;}
	}
/*	for (property in object){
		if (i<n) {output += property+": "+object[property]+"\t\t"; i++} else {output += property+": "+object[property]+"\n";i=0;}
	}
*/	if (output=="") output = "No Object: "+object;
	//alert (output);
	var win = window.open();
	win.document.write(output);
	
	//document.write(output);
}

function show_error(text)
{
	switch (browser) {
		case "IE":
			break;
		default:
			break;
	}
}

