// 21.05.2006 Anfasser ist Safari kompatibel
// Opera stellt Elemente mit overflow=hidden immer mit scrollTop, scrollLeft = 0 dar, selbst, wenn sie vorher auf einen anderen Wert positioniert worden sind.

function scrollBars(elm)
{
	try
	{
		this.elm = document.getElementById(elm);
		//this.elm.scrollTop = 800;
		//alert(this.elm.scrollTop + "\n" + (this.elm.scrollHeight - this.elm.offsetHeight)); //debug

		this.elm.obj = this;
		this.id = "sb_" + scrollBars.num++;

		this.elmHeight = this.elm.offsetHeight; //auto
		this.elmWidth = this.elm.offsetWidth; //auto
		this.elmScrollHeight = this.elm.scrollHeight; //auto
		this.elmScrollWidth = this.elm.scrollWidth;	//auto
		
		this.newContentElmHeight;
		this.newContentElmWidth;
		this.newContentElmScrollHeight;
		this.newContentElmScrollWidth;
		
		this.hasVBar = (this.elm.scrollHeight > this.elm.offsetHeight) ? true : false;
		this.hasHBar = false; //(this.elm.scrollWidth > this.elm.offsetWidth) ? true : false;

		this.vBarWidth = 9; //default, custom
		this.hBarHeight = 9; //default, custom
		this.vBarScrollLength;
		this.hBarScrollLength;

		this.vMultiplication; //auto, Multiplikator (Scrollbarhoehe, Scrollheight des Contents)
		this.hMultiplication; //auto, Multiplikator (Scrollbarbreite, Scrollwidth des Contents)

		this.wheelVSpeed = 9; // custom
		this.wheelHSpeed = 9; // custom

		this.contentMargin = 4; //default, custom
		
		this.vEventStartYPos;
		this.vBarStartYPos;
		this.vBarYPos;
		this.intContentVActualPos;
		this.floatContentVActualPos;

		this.hEventStartXPos;
		this.hBarStartXPos;
		this.hBarXPos;
		this.intContentHActualPos;
		this.floatContentHActualPos;
		this.floatHBarActualPos;

		this.contentSpeed = 4;
		
		this.init();
	}
	catch(e)
	{
		try
		{
			this.elm.style.overflow = 'auto';
		}
		catch(e)
		{

		}
		//alert(e);
	}
}
scrollBars.num = 0;
scrollBars.s_b_html = document.getElementsByTagName('html')[0];

scrollBars.prototype.init = s_b_init;
scrollBars.prototype.embedElm = s_b_embedElm;
scrollBars.prototype.createVScrollbar = s_b_createVScrollbar;
scrollBars.prototype.createHScrollbar = s_b_createHScrollbar;
scrollBars.prototype.calculateVBar = s_b_calculateVBar;
scrollBars.prototype.calculateHBar = s_b_calculateHBar;

scrollBars.prototype.registerButton = s_b_registerButton;
scrollBars.prototype.vScroll = s_b_vScroll;
scrollBars.prototype.hScroll = s_b_hScroll;
scrollBars.prototype.stopButtonScroll = s_b_stopButtonScroll;


function s_b_init()
{
	if (this.hasVBar || this.hasHBar) this.embedElm();
	if (this.hasVBar)
	{
		this.createVScrollbar();
		this.calculateVBar();
	}
	if (this.hasHBar)
	{
		this.createHScrollbar();
		this.calculateHBar();
	}
}

function s_b_embedElm()
{
	this.newContentElm = document.createElement('div');
	this.newContentElm.id = this.id + '_autogenerated';
	
	//this.newContentElm.style.backgroundColor = '#dddddd'; //debug
	this.newContentElm.style.position = 'absolute'; //static
	this.newContentElm.style.overflow = 'hidden'; //static
	this.newContentElm.style.margin = '0px'; //static
	this.newContentElm.style.padding = '0px'; //static
	this.newContentElm.style.border = 'none'; //static

	//this.elm.style.position = 'relative'; //static
	this.elm.style.overflow = 'hidden'; //static

	this.newContentElm.style.width = (this.hasVBar) ? (this.elmWidth - this.vBarWidth - this.contentMargin) + 'px' : '100%'
	this.newContentElm.style.height = (this.hasHBar) ? (this.elmHeight - this.hBarHeight - this.contentMargin) + 'px' : this.elmHeight + 'px'
	try //DOM-kompatible Browser mit Range-Implementatoin (document.implementation.hasFeature('Range','2.0')
	{
		this.range = document.createRange();
		this.range.selectNodeContents(this.elm);
		this.range.surroundContents(this.newContentElm);
	}
	catch(e) // IE
	{
		var cn = this.elm.childNodes.length
		for (var i=0; i < cn; i++)
		{
			this.newContentElm.appendChild(this.elm.childNodes[0]);
		}
		this.elm.appendChild(this.newContentElm);
	}
	this.newContentElmHeight = this.newContentElm.offsetHeight; //auto
	this.newContentElmWidth = this.newContentElm.offsetWidth; //auto
	this.newContentElmScrollHeight = this.newContentElm.scrollHeight;
	this.newContentElmScrollWidth = this.newContentElm.scrollWidth;
	this.newContentElm.obj = this;

}

function v_u_perform(elmId)
{
	alert('b')
	//e = ec.convertEvent(e);
	elmId = (this.obj) ? this.obj.elm.id : elmid
	var obj = document.getElementById(elmId).obj.vBar;
	obj.scrollTop -= 1;
	timeID = setTimeout("v_u_perform(" + [document.getElementById(elmId)] + ")","20");
}

function s_b_createVScrollbar()
{
	/*Scrollbar zusammensetzten START*/
	this.vBarArea = document.createElement('div');
		this.vUp = document.createElement('div');
		this.vDown = document.createElement('div');
		this.vBar = document.createElement('div');
			this.vBarContent = document.createElement('div');
				this.vAnfasser = document.createElement('div');
	
	this.vUp.obj = this;
	this.vUp.setAttribute('id',this.id + "_up");
	this.vDown.obj = this;
	this.vDown.setAttribute('id',this.id + "_down");
	
	this.vAnfasser.obj = this;
	this.vBar.obj = this;

	ec.registerEvent(this.vAnfasser,'mousedown',registerVAnfasser,false);
	try
	{
		ec.registerEvent(this.vBar,'mousewheel',registerVWheel,false);
		ec.registerEvent(this.newContentElm,'mousewheel',registerVWheel,false);
	}
	catch(e){}
	
	ec.registerEvent(this.vUp,'mousedown',this.registerButton,false)
	ec.registerEvent(this.vDown,'mousedown',this.registerButton,false)
	
	this.vBarContent.appendChild(this.vAnfasser);
	this.vBar.appendChild(this.vBarContent);
	this.vBarArea.appendChild(this.vUp);
	this.vBarArea.appendChild(this.vBar);
	this.vBarArea.appendChild(this.vDown);

	this.vBarArea.style.position = 'absolute'; //static
	this.vBarArea.style.width = this.vBarWidth + 'px'; //auto
	this.vBarArea.style.right = '0px'; //auto
	this.vBarArea.style.top = '0px'; //auto
	this.vBarArea.style.height = (this.hasHBar) ? this.newContentElm.offsetHeight + this.contentMargin + 'px' : this.newContentElm.offsetHeight + 'px'; //auto

	this.vUp.style.width = '100%'; // static
	this.vUp.style.height = '7px'; //custom
	this.vUp.style.overflow = 'hidden'; //static
	this.vUp.style.backgroundColor = '#cccccc'; //custom
	this.vUp.style.backgroundImage = "url('/art/icon_pfeiloben.gif')";
	
	this.vDown.style.width = '100%'; // static
	this.vDown.style.height = '7px'; //custom
	this.vDown.style.overflow = 'hidden'; //static
	this.vDown.style.backgroundColor = '#cccccc'; //custom
	this.vDown.style.backgroundImage = "url('/art/icon_pfeilunten.gif')";

	this.vAnfasser.style.position = 'relative'; // static
	this.vAnfasser.style.width = '7px'; // static
	this.vAnfasser.style.height = '32px'; //custom
	this.vAnfasser.style.overflow = 'hidden'; //static
	this.vAnfasser.style.backgroundColor = '#e3c796'; //custom
	this.vAnfasser.style.borderLeft = '1px solid #000000'; //custom
	this.vAnfasser.style.borderRight = '1px solid #000000'; //custom
	//this.vAnfasser.style.backgroundImage = "url('res/anfasser.gif')";

	this.vBar.style.height = (parseInt(this.vBarArea.style.height) - parseInt(this.vUp.style.height) - parseInt(this.vDown.style.height)) + 'px'; //auto, custom
	this.vBar.style.position = 'relative'; //static, muss relative sein, um im Explorer den Content beim scrollTop mitzunehmen
	this.vBar.style.width = '100%'; //static
	this.vBar.style.overflow = 'hidden'; //static
	//this.vBar.style.backgroundColor = '#dddddd'; //custom
	
	//Default-Status des Scrollbalkens START
	this.vAnfasser.style.top = parseInt(this.vBar.style.height) - parseInt(this.vAnfasser.style.height) + 'px'; // auto
	this.vBarContent.style.height = parseInt(this.vBar.style.height)*2 - parseInt(this.vAnfasser.style.height) + 'px'
	this.vBarContent.style.width = '100%'; // static
	//this.vBarContent.style.backgroundColor = '#cceeee' //custom;
	//this.vBarContent.style.backgroundImage = "url('res/linie.gif')";
	
	this.elm.appendChild(this.vBarArea)
	this.vBarScrollLength = (this.vBarContent.offsetHeight - this.vAnfasser.offsetHeight) / 2;
	this.vBar.scrollTop = this.vBarScrollLength;
	//Default-Status des Scrollbalkens ENDE
	/*Scrollbar zusammensetzten ENDE*/
}

function s_b_createHScrollbar()
{
	/*Scrollbar zusammensetzten START*/
	this.hBarArea = document.createElement('div');
		this.hLeft = document.createElement('div');
		this.hRight = document.createElement('div');
		this.hBar = document.createElement('div');
			this.hBarContent = document.createElement('div');
				this.hAnfasser = document.createElement('div');
	
	this.hLeft.obj = this;
	this.hLeft.setAttribute('id',this.id + "_left");
	this.hRight.obj = this;
	this.hRight.setAttribute('id',this.id + "_right");

	this.hAnfasser.obj = this;
	this.hBar.obj = this;

	ec.registerEvent(this.hAnfasser,'mousedown',registerHAnfasser,false);
	try
	{
		ec.registerEvent(this.hBar,'mousewheel',registerHWheel,false);
		if (!this.hasVBar) ec.registerEvent(this.newContentElm,'mousewheel',registerHWheel,false);
	}
	catch(e){}

	ec.registerEvent(this.hLeft,'mousedown',this.registerButton,false)
	ec.registerEvent(this.hRight,'mousedown',this.registerButton,false)
	
	this.hBarContent.appendChild(this.hAnfasser);
	this.hBar.appendChild(this.hBarContent);
	this.hBarArea.appendChild(this.hLeft);
	this.hBarArea.appendChild(this.hBar);
	this.hBarArea.appendChild(this.hRight);

	this.hBarArea.style.position = 'absolute'; //static
	this.hBarArea.style.height = this.hBarHeight + 'px'; //auto
	this.hBarArea.style.bottom = '0px'; //auto
	this.hBarArea.style.left = '0px'; //auto
	this.hBarArea.style.width = (this.hasVBar) ? this.newContentElm.offsetWidth + this.contentMargin + 'px' : this.newContentElm.offsetWidth + 'px'; //auto

	this.hLeft.style.height = '100%'; // static
	this.hLeft.style.width = '7px'; //custom
	this.hLeft.style.overflow = 'hidden'; //static
	this.hLeft.style.backgroundColor = '#cccccc'; //custom
	this.hLeft.style.backgroundImage = "url('res/pfeil_left.gif')";
	
	this.hRight.style.height = '100%'; // static
	this.hRight.style.width = '7px'; //custom
		this.hRight.style.position = 'absolute'; //static, special for horizontal
		this.hRight.style.top = '0px'; //static, special for horizontal
		this.hRight.style.right = '0px'; //static, special for horizontal
	this.hRight.style.overflow = 'hidden'; //static
	this.hRight.style.backgroundColor = '#cccccc'; //custom
	this.hRight.style.backgroundImage = "url('res/pfeil_right.gif')";

	this.hAnfasser.style.position = 'relative'; // static
	this.hAnfasser.style.height = '100%'; // static
	this.hAnfasser.style.width = '32px'; //custom
	this.hAnfasser.style.overflow = 'hidden'; //static
	this.hAnfasser.style.backgroundColor = '#ffcccc'; //custom
	this.hAnfasser.style.backgroundImage = "url('res/h_anfasser.gif')";

	this.hBar.style.width = (parseInt(this.hBarArea.style.width) - parseInt(this.hLeft.style.width) - parseInt(this.hRight.style.width)) + 'px'; //auto, custom
		this.hBar.style.position = 'absolute'; //static, special for horizontal
		this.hBar.style.top = '0px'; //static, special for horizontal
		this.hBar.style.left = '7px'; //static, special for horizontal
	this.hBar.style.height = '100%'; //static
	this.hBar.style.overflow = 'hidden'; //static
	//this.hBar.style.backgroundColor = '#dddddd'; //custom
	
	//Default-Status des Scrollbalkens START
	this.hAnfasser.style.left = parseInt(this.hBar.style.width) - parseInt(this.hAnfasser.style.width) + 'px'; // static, muss am Ende 100% sein
	this.hBarContent.style.width = parseInt(this.hBar.style.width)*2 - parseInt(this.hAnfasser.style.width) + 'px'
	this.hBarContent.style.height = '100%'; // static
	//this.hBarContent.style.backgroundColor = '#cceeee' //custom;
	this.hBarContent.style.backgroundImage = "url('res/h_linie.gif')";
	
	this.elm.appendChild(this.hBarArea)
	this.hBarScrollLength = (this.hBarContent.offsetWidth - this.hAnfasser.offsetWidth) / 2;
	this.hBar.scrollLeft = this.hBarScrollLength;
	//Default-Status des Scrollbalkens ENDE
	/*Scrollbar zusammensetzten ENDE*/
}

function s_b_calculateVBar()
{
	//alert(this.newContentElmScrollHeight)
	this.vMultiplication = (this.newContentElmScrollHeight - this.newContentElmHeight) / this.vBarScrollLength;
	//alert('v: '+this.id+"\n"+this.vMultiplication);
}

function s_b_calculateHBar()
{
	this.hMultiplication = (this.newContentElmScrollWidth - this.newContentElmWidth) / this.hBarScrollLength;
	//alert('h: '+this.id+"\n"+this.hMultiplication);
}

/******** ******** ******** ******** ******** ******** ******** ********/
/******Fogende Funktionen sind nicht Methoden des Scollbar-Objektes*****/
/*******************************Anfasser*******************************/

/* ANFASSER START */

function registerVAnfasser(e)
{
	e = ec.convertEvent(e);
	e.preventDefault();
	e.stopPropagation();
	this.obj.vBarStartYPos = this.obj.vBarYPos = this.obj.vBar.scrollTop;
	this.obj.intContentVActualPos = this.obj.newContentElm.scrollTop;
	this.obj.vEventStartYPos = e.pageY;
	
	scrollBars.s_b_html.obj = this.obj;
	
	ec.registerEvent(scrollBars.s_b_html,'mousemove',moveVAnfasser,true);
	ec.registerEvent(scrollBars.s_b_html,'mouseup',unregisterVAnfasser,true);
	ec.registerEvent(scrollBars.s_b_html,'drag',ec.doNothing,true);
}

function moveVAnfasser(e)
{
	e = ec.convertEvent(e);
	e.preventDefault();
	
	var pos = e.pageY - this.obj.vEventStartYPos;
	this.obj.vBarYPos = this.obj.vBarStartYPos - pos;
	//var invertedVBarYPos = this.obj.vBarScrollLength - (this.obj.vBarStartYPos - pos); //Safari wendet hier die Vorrangregeln durch Klammern nicht korrekt an, deshalb zunaechst d berechnen
	var d = this.obj.vBarStartYPos - pos;
	var invertedVBarYPos = this.obj.vBarScrollLength - d; //von der Ausgangsposition gescrollte Pixel des Scrollbalkens
	this.obj.floatContentVActualPos = invertedVBarYPos * this.obj.vMultiplication; //mit Multiplikator fŸr den Content
	this.obj.vBar.scrollTop = this.obj.vBarStartYPos - pos;
	scrollContentV(this.obj.floatContentVActualPos);
	//document.getElementById('ausgabe').innerHTML = "vBarScrollLength: " + this.obj.vBarScrollLength + "<br>this.obj.vBarStartYPos: " + this.obj.vBarStartYPos + "<br>pos: " + pos + "<br>vBarYPos: " + this.obj.vBarYPos + "<br>invertedVBarYPos: " + invertedVBarYPos + "<br>floatContentVActualPos: " + this.obj.floatContentVActualPos + "<br>newContentElm.scrollTop: " + this.obj.newContentElm.scrollTop + "<br>newContentElm.scrollHeight: " + this.obj.newContentElm.scrollHeight + "<br>newContentElm.offsetHeight: " + this.obj.newContentElm.offsetHeight; //debug
}

function unregisterVAnfasser(e)
{
	ec.removeEvent(scrollBars.s_b_html,'mousemove',moveVAnfasser,true)
	ec.removeEvent(scrollBars.s_b_html,'mouseup',unregisterVAnfasser,true)

	//scrollBars.s_b_html.obj = null;
}






function registerHAnfasser(e)
{
	e = ec.convertEvent(e);
	e.preventDefault();
	e.stopPropagation();
	this.obj.hBarStartXPos = this.obj.hBarXPos = this.obj.hBar.scrollLeft;
	this.obj.intContentHActualPos = this.obj.newContentElm.scrollLeft;
	this.obj.hEventStartXPos = e.pageX;
	
	scrollBars.s_b_html.obj = this.obj;
	
	ec.registerEvent(scrollBars.s_b_html,'mousemove',moveHAnfasser,true);
	ec.registerEvent(scrollBars.s_b_html,'mouseup',unregisterHAnfasser,true);
	ec.registerEvent(scrollBars.s_b_html,'drag',ec.doNothing,true);
}

function moveHAnfasser(e)
{
	e = ec.convertEvent(e);
	e.preventDefault();
	
	var pos = e.pageX - this.obj.hEventStartXPos;
	this.obj.hBarXPos = this.obj.hBarStartXPos - pos;
	//var invertedHBarXPos = this.obj.hBarScrollLength - (this.obj.hBarStartXPos - pos); //Safari wendet hier die Vorrangregeln durch Klammern nicht korrekt an, deshalb zunaechst d berechnen
	var d = this.obj.hBarStartXPos - pos;
	var invertedHBarXPos = this.obj.hBarScrollLength - d; //von der Ausgangsposition gescrollte Pixel des Scrollbalkens
	this.obj.floatContentHActualPos = invertedHBarXPos * this.obj.hMultiplication; //mit Multiplikator fŸr den Content
	
	this.obj.hBar.scrollLeft = this.obj.hBarStartXPos - pos;
	scrollContentH(this.obj.floatContentHActualPos);
	//document.getElementById('ausgabe').innerHTML = "this.obj.vBarStartYPos: " + this.obj.vBarStartYPos + "<br>pos: " + pos + "<br>vBarYPos: " + this.obj.vBarYPos + "<br>invertedVBarYPos: " + invertedVBarYPos + "<br>floatContentActualPos: " + this.obj.floatContentActualPos + "<br>newContentElm.scrollTop: " + this.obj.newContentElm.scrollTop + "<br>newContentElm.scrollHeight: " + this.obj.newContentElm.scrollHeight + "<br>newContentElm.offsetHeight: " + this.obj.newContentElm.offsetHeight; //debug
}

function unregisterHAnfasser(e)
{
	//conPosition = (newConPosition < 0) ? 0 : (newConPosition > conScrollLength) ? conScrollLength : newConPosition;
	ec.removeEvent(scrollBars.s_b_html,'mousemove',moveHAnfasser,true)
	ec.removeEvent(scrollBars.s_b_html,'mouseup',unregisterHAnfasser,true)

	scrollBars.s_b_html.obj = null;
}

function scrollContentV(pos)
{
	scrollBars.s_b_html.obj.newContentElm.scrollTop = pos;
}

function scrollContentH(pos)
{
	scrollBars.s_b_html.obj.newContentElm.scrollLeft = pos;
}


/* ANFASSER ENDE */

/* BUTTONS START */

function s_b_registerButton(e)
{
	e = ec.convertEvent(e);
	e.preventDefault();
	scrollBars.s_b_html.obj = this.obj;
	ec.registerEvent(scrollBars.s_b_html,'drag',ec.doNothing,true);
	ec.registerEvent(document.getElementsByTagName('body')[0],'mousemove',ec.doNothing,true);
	ec.registerEvent(scrollBars.s_b_html,'mouseup',scrollBars.s_b_html.obj.stopButtonScroll,true);
	if (e.target.id.indexOf('up') > 0)
	{
		int_scrollTimeId = setInterval("scrollBars.s_b_html.obj.vScroll(" + this.obj.contentSpeed + ")","20");
	}
	else if (e.target.id.indexOf('down') > 0)
	{
		int_scrollTimeId = setInterval("scrollBars.s_b_html.obj.vScroll(" + (-this.obj.contentSpeed) + ")","20");
	}
	else if (e.target.id.indexOf('left') > 0)
	{
		int_scrollTimeId = setInterval("scrollBars.s_b_html.obj.hScroll(" + this.obj.contentSpeed + ")","20");
	}
	else if (e.target.id.indexOf('right') > 0)
	{
		int_scrollTimeId = setInterval("scrollBars.s_b_html.obj.hScroll(" + (-this.obj.contentSpeed) + ")","20");
	}
}

function s_b_vScroll(wert)
{
	scrollBars.s_b_html.obj.newContentElm.scrollTop -= wert;
	scrollBars.s_b_html.obj.floatVBarActualPos = scrollBars.s_b_html.obj.vBarScrollLength - scrollBars.s_b_html.obj.newContentElm.scrollTop / scrollBars.s_b_html.obj.vMultiplication;
	scrollBars.s_b_html.obj.vBar.scrollTop = scrollBars.s_b_html.obj.floatVBarActualPos;
	//document.getElementById('ausgabe').innerHTML = 'vBar.scrollTop: ' + scrollBars.s_b_html.obj.vBar.scrollTop + '<br>newContentElm.scrollTop: ' + scrollBars.s_b_html.obj.newContentElm.scrollTop + '<br>scrollBars.s_b_html.obj.floatVBarActualPos: ' + scrollBars.s_b_html.obj.floatVBarActualPos;
}

function s_b_hScroll(wert)
{
	scrollBars.s_b_html.obj.newContentElm.scrollLeft -= wert;
	scrollBars.s_b_html.obj.floatHBarActualPos = scrollBars.s_b_html.obj.hBarScrollLength - scrollBars.s_b_html.obj.newContentElm.scrollLeft / scrollBars.s_b_html.obj.hMultiplication;
	scrollBars.s_b_html.obj.hBar.scrollLeft = scrollBars.s_b_html.obj.floatHBarActualPos;
}

function s_b_stopButtonScroll()
{
	clearInterval(int_scrollTimeId);
	ec.removeEvent(scrollBars.s_b_html,'drag',ec.doNothing,true);
	ec.removeEvent(document.getElementsByTagName('body')[0],'mousemove',ec.doNothing,true);
	ec.removeEvent(scrollBars.s_b_html,'mouseup',scrollBars.s_b_html.obj.stopButtonScroll,true);
}

/* BUTTONS ENDE */

/* MOUSEWHEEL START */

function registerVWheel(e)
{
	e = ec.convertEvent(e);
	this.obj.newContentElm.scrollTop += e.eDirection * this.obj.wheelVSpeed;
	this.obj.floatVBarActualPos = this.obj.vBarScrollLength - this.obj.newContentElm.scrollTop / this.obj.vMultiplication;
	this.obj.vBar.scrollTop = this.obj.floatVBarActualPos;
}

function registerHWheel(e)
{
	e = ec.convertEvent(e);
	this.obj.newContentElm.scrollLeft += e.eDirection * this.obj.wheelHSpeed;
	this.obj.floatHBarActualPos = this.obj.hBarScrollLength - this.obj.newContentElm.scrollLeft / this.obj.hMultiplication;
	this.obj.hBar.scrollLeft = this.obj.floatHBarActualPos;
}

/* MOUSEWHEEL ENDE */

/* JUMP START */

function jump(obj,target)
{
	window.location.href = target;
	sb2.vBar.scrollTop = sb2.vBarScrollLength - (sb2.newContentElm.scrollTop / sb2.vMultiplication);
}

/* JUMP ENDE */