function TMDomInit() {
	var agent = navigator.userAgent.toLowerCase();
	var os = navigator.platform.toLowerCase();
	var mac = (os.indexOf('mac') != -1) ? true : false;
	var isW3CDOM = (document.getElementById) ? true : false;
	if (window.opera && isW3CDOM) {
		isOPERA = true;
		isOPERA6 = (document.createElement && !document.addEventListener) ? true : false;
		isOPERA7 = (document.createElement && document.addEventListener) ? true : false;
	} else {
		isOPERA = false;
		isOPERA6 = false;
		isOPERA7 = false;	
	}
	if (document.all && isW3CDOM && !isOPERA) {
		isIE = true;
		isIE5 = (!document.createEventObject && !mac) ? true : false;
		isIE55 = (document.createEventObject && !document.compatMode && !mac) ? true : false;
		isIE6 = (document.compatMode && !mac) ? true : false;
		isIE5MAC = (mac) ? true : false;
	} else {
		isIE = false;
		isIE5 = false;
		isIE55 = false;
		isIE6 = false;
		isIE5MAC = false;
	}
	isKHTML = (isW3CDOM && document.clientWidth) ? true : false;
	isGECKO = (isW3CDOM && !isKHTML && !isIE && !isOPERA) ? true : false;
	isGECKOPR = (isGECKO && (agent.indexOf('rv') == -1) && !document.compatMode) ? true : false;
	isDOM = (isGECKO || isKHTML || isIE || isOPERA6 || isOPERA7) ? true : false;
}

TMDomInit();

function TMSucheElem(obj) {
	if (isDOM) {
		var a = arguments;
		if (typeof obj == 'string') {
			if (a.length == 1) {
				return document.getElementById(obj);
			} else if (a.length == 3 && typeof a[1] == 'string' && typeof a[2] == 'number') {
				if (a[1].toLowerCase() == 'name') {
					return document.getElementsByName(obj)[a[2]];
				} else if (a[1].toLowerCase() == 'tagname') {
					return document.getElementsByTagName(obj)[a[2]];
				}
			}
		} else if (typeof obj == 'object') {
			return obj;
		}
	}
}

function TMCSS(obj) {
	if (isDOM) {
		var a = arguments;
		if (typeof obj == 'string') {
			if (a.length == 1) {
				return TMSucheElem(obj).style;
			} else if (a.length == 3 && typeof a[1] == 'string' && typeof a[2] == 'number') {
				return TMSucheElem(obj,a[1],a[2]).style;
			}
		} else if (typeof obj == 'object') {
			return obj.style;
		}
	}
}
				
function TMCSSVisibility() {
	if (isDOM) {
		var a = arguments;
		for (var i = 0; i<a.length-1; i+=2) {
			if (typeof a[i] == 'string' || typeof a[i] == 'object') {
				var objCSS = TMSucheElem(a[i]).style;
				if (a[i+1].toLowerCase() == 'ein') {
					objCSS.visibility = 'visible';
				} else {
					objCSS.visibility = 'hidden';	
				}
			}
		}
	}
}

function TMCSSDisplay() {
	if (isDOM) {
		var a = arguments;
		for (var i = 0; i<a.length-1; i+=2) {
			if (typeof a[i] == 'string' || typeof a[i] == 'object') {
				TMSucheElem(a[i]).style.display = a[i+1];
			}
		}
	}
}

function TMCSSPosition() {
	if (isDOM) {
		var a = arguments;
		for (var i = 0; i<a.length-2; i+=3) {
			if (typeof a[i] == 'string' || typeof a[i] == 'object') {
				var objCSS = TMSucheElem(a[i]).style;
				objCSS.left = a[i+1] + 'px';
				objCSS.top = a[i+2] + 'px';
			}
		}
	}
}

function TMCSSClip() {
	if (isDOM) {
		var a = arguments;
		for (var i = 0; i<a.length-4; i+=5) {
			if (typeof a[i] == 'string' || typeof a[i] == 'object') {
				TMSucheElem(a[i]).style.clip = 'rect('+ a[i+1] +'px, ' + a[i+2] + 'px, ' + a[i+3] + 'px, ' + a[i+4] + 'px)';
			}
		}
	}
}

function TMCSSZindex() {
	if (isDOM) {
		var a = arguments;
		for (var i = 0; i<a.length-1; i+=2) {
			if (typeof a[i] == 'string' || typeof a[i] == 'object') {
				TMSucheElem(a[i]).style.zIndex = a[i+1];
			}
		}
	}
}

function TMGetElemPosY(el) {
	var yPosEl = 0, element = el;
	while (typeof element == 'object') {
		yPosEl += element.offsetTop;
		if (element.tagName.toLowerCase() == 'body' || ((isIE6 && document.documentElement) && element.tagName.toLowerCase() == 'html')) {
			element = 0;
		}
		if (typeof(el.offsetParent) == 'object') {
			element = element.offsetParent;
		}
	}
	return yPosEl;
}
				
function TMGetElemPosX(el) {
	var xPosEl = 0, element = el;
	while (typeof element == 'object') {
		xPosEl += element.offsetLeft;
		if (element.tagName.toLowerCase() == 'body' || ((isIE6 && document.documentElement) && element.tagName.toLowerCase() == 'html')) {
			element = 0;
		}
		if (typeof(el.offsetParent) == 'object') {
			element = element.offsetParent;
		}
	}
	return xPosEl;
}

function TMErmittleMasse(obj){
	if (isDOM) {
		var obj = TMSucheElem(obj);
		var masse = new Object();
		masse.b = obj.offsetWidth;
		masse.h = obj.offsetHeight;
		return masse;
	}
}

function TMErstelleEvent(obj, handler, funktion){
	if (isDOM) {
		var a = arguments;
		var obj = TMSucheElem(obj);
		if (a.length == 4) {
			if (obj.addEventListener) {
				return obj.addEventListener(handler, funktion, a[3]);
			}
			else 
				if (obj.attachEvent) {
					return obj.attachEvent('on' + handler, funktion);
				}
		}
		else {
			return obj['on' + handler] = funktion;
		}
	}
}

function TMEntferneEvent(obj, handler){
	if (isDOM) {
		var a = arguments;
		var obj = TMSucheElem(obj);
		if (a.length == 4) {
			if (obj.removeEventListener) {
				return obj.removeEventListener(handler, a[2], a[3]);
			}
			else 
				if (obj.detachEvent) {
					return obj.detachEvent('on' + handler, a[2]);
				}
		}
		else {
			return obj['on' + handler] = null;
		}
	}
}

function TMUnterbindeStandard(e) {
	var e = e ? e : event;
	if (e.preventDefault) {
		e.preventDefault();
	} else if (isIE) {
		e.returnValue = false;
	} else {
		return false;
	}
}

function TMUnterbindeAusbreitung(e) {
	var e = e ? e : event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else if (isIE) {
		e.cancelBubble = true;
	}
}

function TMSucheMaus(e) {
	if (isDOM) {
		var xOffset, yOffset;
		var e = e ? e : window.event;
		if (isIE) {
			if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				xOffset = document.documentElement.scrollLeft;
				yOffset = document.documentElement.scrollTop;
			} else if (document.body) {
				xOffset = document.body.scrollLeft;
				yOffset = document.body.scrollTop;
			}
		} else {
			xOffset = window.pageXOffset;
			yOffset = window.pageYOffset;
		}
		var mausPos = new Object();
		mausPos.x = e.clientX;
		mausPos.y = e.clientY;
		mausPos.xo = mausPos.x + xOffset;
		mausPos.yo = mausPos.y + yOffset;
		return mausPos;
	}
}

function TMSchreibeZeile(text) {
	if (isDOM) {
		document.writeln(text);
	}
}

/************************************************************************************************************/

TMScrollLeiste = function(){
	if (isDOM) {
		var a = arguments;
		this.objekt = a[0] + 'objekt';
		eval(this.objekt + '= this');
		this.gesamtBox = $(a[0]);
		this.inhalt = $(a[1]);
		this.leistenBox = $(a[2]);
		this.leiste = $(a[3]);
		this.greifer = $(a[4]);
		/*
		this.pfeil01 = TMSucheElem(a[5]);
		this.pfeil02 = TMSucheElem(a[6]);
		this.springPfeil = TMSucheElem(a[7]);
		*/
		this.scrollVertikal = (a[8] == 'vertikal') ? true : false;
		this.scrollTimeout = a[9];
		this.scrollSpeed = a[10];
		this.mausradSpeed = a[11];
		if (a.length == 20) {
			this.bildwechsel = true;
			this.greiferBild_n = a[12];
			this.greiferBild_o = a[13];
			/*this.pfeil01Bild_n = a[14];
			 this.pfeil01Bild_o = a[15];
			 this.pfeil02Bild_n = a[16];
			 this.pfeil02Bild_o = a[17];
			 this.springPfeilBild_n = a[18];
			 this.springPfeilBild_o = a[19];*/
		}
		else {
			this.bildwechsel = false;
		}
		this.scrollSchleife = null;
		this.inhaltMaxPos = 0;
		this.greiferMinPos = 0;
		
		if (this.scrollVertikal) {
			this.inhaltPos = parseInt(this.inhalt.style.top) ? parseInt(this.inhalt.style.top) : 0;
			this.greiferPos = parseInt(this.greifer.style.top) ? parseInt(this.greifer.style.top) : 0;
			this.inhaltHoehe = this.inhalt.offsetHeight;
			this.gesamtBoxHoehe = this.gesamtBox.offsetHeight;
			this.inhaltMinPos = -this.inhaltHoehe + this.gesamtBoxHoehe;
			this.leistenHoehe = this.leiste.offsetHeight;
			this.greiferHoehe = this.greifer.offsetHeight;
			this.greiferMaxPos = this.leistenHoehe - this.greiferHoehe;
			this.scrollBereich = this.greiferMaxPos / (this.inhaltHoehe - this.gesamtBoxHoehe);

			if (this.inhaltHoehe > this.gesamtBoxHoehe) {
				this.leistenBox.style.visibility = 'visible';
			}
		}
		else {
			this.inhaltPos = parseInt(this.inhalt.style.left) ? parseInt(this.inhalt.style.left) : 0;
			this.greiferPos = parseInt(this.greifer.style.left) ? parseInt(this.greifer.style.left) : 0;
			this.inhaltBreite = this.inhalt.offsetWidth;
			this.gesamtBoxBreite = this.gesamtBox.offsetWidth;
			this.inhaltMinPos = -this.inhaltBreite + this.gesamtBoxBreite;
			this.leistenBreite = this.leiste.offsetWidth;
			this.greiferBreite = this.greifer.offsetWidth;
			this.greiferMaxPos = this.leistenBreite - this.greiferBreite;
			this.scrollBereich = this.greiferMaxPos / (this.inhaltBreite - this.gesamtBoxBreite);
			if (this.inhaltBreite > this.gesamtBoxBreite) {
				this.leistenBox.style.visibility = 'visible';
			}
		}
		var obj = this;
		/*TMErstelleEvent(this.pfeil01,'mousedown',function(e) { obj.scrollenbeginnen(obj.scrollSpeed,e); });
		 TMErstelleEvent(this.pfeil01,'mouseup',function() { obj.scrollenbeenden(); });
		 TMErstelleEvent(this.pfeil01,'mouseout',function() { obj.scrollenbeenden(); });
		 TMErstelleEvent(this.pfeil02,'mousedown',function(e) { obj.scrollenbeginnen(-obj.scrollSpeed,e); });
		 TMErstelleEvent(this.pfeil02,'mouseup',function() { obj.scrollenbeenden(); });
		 TMErstelleEvent(this.pfeil02,'mouseout',function() { obj.scrollenbeenden(); });*/
		TMErstelleEvent(this.greifer, 'mousedown', function(e){
			obj.ziehenbeginnen(e);
		});
		/*TMErstelleEvent(this.springPfeil,'mousedown',function(e) { obj.hochspringen(e); });*/
		this.mausradPos = this.inhaltPos / this.mausradSpeed;
		TMErstelleEvent(this.gesamtBox, 'mousewheel', function(e){
			obj.mausradscrollen(e);
		});
		if (isGECKO) {
			TMErstelleEvent(this.gesamtBox, 'DOMMouseScroll', function(e){
				obj.mausradscrollen(e);
			}, false);
		}
		else {
			TMErstelleEvent(this.gesamtBox, 'mousewheel', function(e){
				obj.mausradscrollen(e);
			});
		}
		if (this.bildwechsel) {
			this.bilderladen();
		}
	}
};

TMScrollLeiste.prototype = {

	positionieren: function(){
		if (this.scrollVertikal) {
			this.inhaltPosX = 0;
			this.inhaltPosY = this.inhaltPos;
			this.greiferPosX = 0;
			this.greiferPosY = this.greiferPos;
		}
		else {
			this.inhaltPosX = this.inhaltPos;
			this.inhaltPosY = 0;
			this.greiferPosX = this.greiferPos;
			this.greiferPosY = 0;
		}
		TMCSSPosition(this.inhalt, this.inhaltPosX, this.inhaltPosY);
		TMCSSPosition(this.greifer, this.greiferPosX, this.greiferPosY);
	},
	
	scrollenbeginnen: function(speed, e){
		if (this.bildwechsel) {
			if (speed > 0) {
				this.pfeil = this.pfeil01;
				this.pfeilBild = this.pfeil01Bild_o
			}
			else {
				this.pfeil = this.pfeil02;
				this.pfeilBild = this.pfeil02Bild_o;
			}
			this.bilderwechseln(this.pfeil, this.pfeilBild);
		}
		this.scrollSchleife = window.setInterval(this.objekt + '.scrollen(' + speed + ')', this.scrollTimeout);
		TMUnterbindeStandard(e);
	},
	
	scrollen: function(speed){
		this.inhaltPos += speed;
		if (this.inhaltPos < this.inhaltMinPos) {
			this.inhaltPos = this.inhaltMinPos;
			this.greiferPos = this.greiferMaxPos;
			window.clearInterval(this.scrollSchleife);
			this.scrollSchleife = null;
		}
		else 
			if (this.inhaltPos > this.inhaltMaxPos) {
				this.inhaltPos = this.inhaltMaxPos;
				this.greiferPos = this.greiferMinPos;
				window.clearInterval(this.scrollSchleife);
				this.scrollSchleife = null;
			}
			else {
				this.greiferPos = Math.round(-this.inhaltPos * this.scrollBereich);
			}
		if (isIE6) {
			this.mausradPos = this.inhaltPos / this.mausradSpeed;
		}
		this.positionieren();
	},
	
	scrollenbeenden: function(){
		if (this.bildwechsel) {
			this.bilderwechseln(this.pfeil01, this.pfeil01Bild_n, this.pfeil02, this.pfeil02Bild_n);
		}
		if (this.scrollSchleife) {
			window.clearInterval(this.scrollSchleife);
			this.scrollSchleife = null;
		}
	},
	
	ziehenbeginnen: function(e){
		if (this.bildwechsel) {
			this.bilderwechseln(this.greifer, this.greiferBild_o);
		}
		this.mausPos = this.scrollVertikal ? TMSucheMaus(e).yo : TMSucheMaus(e).xo;
		this.ziehenPos = this.mausPos - this.greiferPos;
		var obj = this;
		TMErstelleEvent(document, 'mousemove', function(e){
			obj.ziehen(e);
		});
		TMErstelleEvent(document, 'mouseup', function(){
			obj.ziehenbeenden();
		});
		TMUnterbindeStandard(e);
	},
	
	ziehen: function(e){
		this.mausPos = this.scrollVertikal ? TMSucheMaus(e).yo : TMSucheMaus(e).xo;
		this.greiferPos = this.mausPos - this.ziehenPos;
		if (this.greiferPos > this.greiferMaxPos) {
			this.greiferPos = this.greiferMaxPos;
			this.inhaltPos = this.inhaltMinPos;
		}
		else 
			if (this.greiferPos < this.greiferMinPos) {
				this.greiferPos = this.greiferMinPos;
				this.inhaltPos = this.inhaltMaxPos;
			}
			else {
				this.inhaltPos = Math.round(-this.greiferPos * (1 / this.scrollBereich));
			}
		if (isIE6) {
			this.mausradPos = this.inhaltPos / this.mausradSpeed;
		}
		this.positionieren();
		TMUnterbindeStandard(e);
	},
	
	ziehenbeenden: function(){
		if (this.bildwechsel) {
			this.bilderwechseln(this.greifer, this.greiferBild_n);
		}
		TMEntferneEvent(document, 'mousemove');
		TMEntferneEvent(document, 'mouseup');
	},
	
	hochspringen: function(e){
		if (this.bildwechsel) {
			this.bilderwechseln(this.springPfeil, this.springPfeilBild_o);
			var obj = this;
			TMErstelleEvent(this.springPfeil, 'mouseup', function(){
				obj.bilderwechseln(obj.springPfeil, obj.springPfeilBild_n);
			});
			TMErstelleEvent(this.springPfeil, 'mouseout', function(){
				obj.bilderwechseln(obj.springPfeil, obj.springPfeilBild_n);
			});
		}
		this.greiferPos = this.greiferMinPos;
		this.inhaltPos = this.inhaltMaxPos;
		if (isIE6) {
			this.mausradPos = this.inhaltPos / this.mausradSpeed;
		}
		this.positionieren();
		TMUnterbindeStandard(e);
	},
	
	mausradscrollen: function(e){
		e = e ? e : window.event;
		var delta = 0;
		if (e.wheelDelta) {
			delta = e.wheelDelta / 120;
			if (window.opera) {
				delta = -delta;
			}
		}
		else 
			if (e.detail) {
				delta = -e.detail / 3;
			}
		if (delta) {
			this.mausradPos += delta;
			this.inhaltPos = Math.round(this.mausradPos * this.mausradSpeed);
			if (this.inhaltPos < this.inhaltMinPos) {
				this.inhaltPos = this.inhaltMinPos;
				this.greiferPos = this.greiferMaxPos;
				this.mausradPos = this.inhaltPos / this.mausradSpeed;
			}
			else 
				if (this.inhaltPos > this.inhaltMaxPos) {
					this.inhaltPos = this.inhaltMaxPos;
					this.greiferPos = this.greiferMinPos;
					this.mausradPos = this.inhaltPos / this.mausradSpeed;
				}
				else {
					this.greiferPos = Math.round(-this.inhaltPos * this.scrollBereich);
				}
			this.positionieren();
			//TMUnterbindeStandard(e)
			e.returnValue = false;
		}
	},
	
	bilderladen: function(){
		this.bilder = new Array(this.greiferBild_o, this.pfeil01Bild_o, this.pfeil02Bild_o, this.springPfeilBild_o);
		this.bild = new Array();
		for (var i = 0; i < this.bilder.length; i++) {
			this.bild[i] = new Image();
			this.bild[i].src = this.bilder[i];
		}
	},
	
	bilderwechseln: function(){
		var a = arguments;
		for (var i = 0; i < a.length - 1; i += 2) {
			a[i].setAttribute('src', a[i + 1]);
		}
	}
};

window.addEvent('load', function(){
	if ($("scroll_container") && ($("scroll_text").offsetHeight-50 > $("scroll_container").offsetHeight)) {
		var scroller_main = new TMScrollLeiste('scroll_container', 'scroll_text', 'scrollbarbox', 'scrolltrack', 'scrollgreifer', 'scrollpfeiloben', 'scrollpfeilunten', 'springpfeil', 'vertikal', 15, 5, 10, '/fileadmin/magura/img/grafiken/v_greifer.png', '/fileadmin/magura/img/grafiken/v_greifer_o.png', '/fileadmin/magura/img/grafiken/v_pfeil_a.png', '/fileadmin/magura/img/grafiken/v_pfeil_a_o.png', '/fileadmin/magura/img/grafiken/v_pfeil_b.png', '/fileadmin/magura/img/grafiken/v_pfeil_b_o.png', '/fileadmin/magura/img/grafiken/v_top_jump.png', '/fileadmin/magura/img/grafiken/v_top_jump_o.png');
	}
	if ($("scroll_container_videolist") && ($("scroll_text_videolist").offsetHeight-50 > $("scroll_container_videolist").offsetHeight)) {
		var scroller_main = new TMScrollLeiste('scroll_container_videolist', 'scroll_text_videolist', 'scrollbarbox_videolist', 'scrolltrack_videolist', 'scrollgreifer_videolist', 'scrollpfeiloben', 'scrollpfeilunten', 'springpfeil', 'vertikal', 15, 5, 10, '/fileadmin/magura/img/grafiken/v_greifer.png', '/fileadmin/magura/img/grafiken/v_greifer_o.png', '/fileadmin/magura/img/grafiken/v_pfeil_a.png', '/fileadmin/magura/img/grafiken/v_pfeil_a_o.png', '/fileadmin/magura/img/grafiken/v_pfeil_b.png', '/fileadmin/magura/img/grafiken/v_pfeil_b_o.png', '/fileadmin/magura/img/grafiken/v_top_jump.png', '/fileadmin/magura/img/grafiken/v_top_jump_o.png');
	}	
});

