// v1.0 4LevelWebs

var thumbHeight,setTop,thumbTrackHeight,thumbContainerDiv,thumbDiv,viewPortDiv,thumbWay,thumbTop,viewPortHeight,contentDiv,contentHeight,contentWay,scrollProportion,mouseYInitial,thumbTopInitial;
var mUp = 'onmouseup', mDown = 'onmousedown', mOut = 'onmouseout', mMove = 'onmousemove';
var viewPort;
var content = 'contentToScroll';
var thumbTrack = 'thumbContainer';
var thumb = 'thumb';
var contentTopCache = 0; // cache for contenTop
var contentHeightCache = 0; // cache for contenHeight
var thumbTopCache = 0; // cache for thumbTop
var thumbHeightCache = 0; // cache for thumbHeight
var scrollBar = false;
var scrollingNow = 0;

function resetScroll(contentSpecificViewport) {
	setupScroll(contentSpecificViewport);
}

function makeDivObjects(id, d) { // makes div's of var's
	var x;
	if(!d) d = document;
 	if(!(x = d[id]) && d.all) x = d.all[id];
	if(!x && d.getElementById) x = d.getElementById(id);
	return x;
}

function startProcessing(event,funct) {
	this[event] = funct;
}
function stopProcessing(event) {
	this[event] = null;
}

function defineBasics() { // set thumb.style.height and other layout styles
	thumbHeight = Math.round(document.getElementById(viewPort).offsetHeight/document.getElementById(content).offsetHeight*document.getElementById(viewPort).offsetHeight);
	if (document.getElementById(content).offsetHeight < document.getElementById(viewPort).offsetHeight) {
		document.getElementById(thumbTrack).style.display = 'none';
		if(activeContent == 'c1w') document.getElementById(viewPort).style.width = '442px';
		else document.getElementById(viewPort).style.width = '600px';
	}
	else {
		document.getElementById(thumbTrack).style.display = 'block';
		if(activeContent == 'c1w') document.getElementById(viewPort).style.width = '424px';
		else document.getElementById(viewPort).style.width = '585px';
		scrollBar = true;
	}
	if(thumbHeight <= 11) thumbHeight = parseInt(document.getElementById(thumb).style.width);
	else document.getElementById(thumb).style.height = thumbHeight + 'px';
	return thumbHeight;
}
function processDiv(i) {
	var divObject,divObjectStyle;
	if((divObject = makeDivObjects(i)) != null) {
		divObjectStyle = divObject.style; // define div.style
	}
	divObject.getTop = function() {return parseInt(divObjectStyle.top)||0};
	divObject.setTop = function(y) {divObjectStyle.top = y+'px'};
	divObject.getHeight = function() {return divObject.offsetHeight};
	divObject.setHeight = function(y) {return divObjectStyle.height = y+'px'};
	divObject.startProcessing = startProcessing;
	divObject.stopProcessing = stopProcessing;
	return divObject; // return the value to calling function
}
function catchEvent(e) {
	return document.all?event.clientY:e.pageY;
}
function setupScroll(contentSpecificViewport) {
	viewPort = contentSpecificViewport;
	thumbHeight = defineBasics();
	setTop = null; //?
	thumbTrackHeight = parseInt(document.getElementById(thumbTrack).offsetHeight);
	thumbContainerDiv = processDiv(thumbTrack);
	thumbDiv = processDiv(thumb);
	viewPortDiv = processDiv(viewPort);
	if(thumbTopCache != 0 && contentDiv.getHeight() <= viewPortHeight) {
		contentDiv.setTop(0);
		thumbDiv.setTop(0);
		contentTopCache = 0;
		contentHeightCache = 0;
		thumbTopCache = 0;
		thumbHeightCache = 0;
	}
	else if(thumbTopCache != 0 && contentDiv.getHeight() > viewPortHeight) {
		if(contentDiv.getHeight() >= contentHeightCache) {
			thumbDiv.setTop(thumbTopCache*contentHeight/contentDiv.getHeight());
		}
		else {
			if((contentDiv.getHeight()+contentDiv.getTop()) < viewPortHeight) {
				contentDiv.setTop(-contentTopCache+(viewPortHeight-contentDiv.getHeight()+contentDiv.getTop()));
				thumbDiv.setTop(thumbTrackHeight-thumbDiv.getHeight());
			}
		}
		thumbTopCache = thumbDiv.getTop();
		contentTopCache = contentDiv.getTop();
	}
	else if(thumbTopCache == 0) thumbTop = thumbDiv.getTop(); // initial value or chached
	thumbWay = thumbTrackHeight-thumbHeight;
	viewPortHeight = viewPortDiv.getHeight();
	thumbDiv.startProcessing(mDown,startScroll);
	LC(content);
}
function LC(content) {
	contentDiv = processDiv(content);
	contentHeight = contentDiv.getHeight();
	contentWay = contentHeight - viewPortHeight; // set contentway-to-go
	scrollProportion = thumbWay/contentWay; // proportion/factor
}
function startScroll(thumb) { // called by thumb.onmousedown
	mouseYInitial = catchEvent(thumb); // initial value
	thumbTopInitial = thumbDiv.getTop(); // initial value
	if(activeContent == 'c2b') thumbDiv.style.backgroundImage='url(./fileadmin/templates/grfx/thumb_1b.gif)';
	else thumbDiv.style.backgroundImage='url(./fileadmin/templates/grfx/thumb_1.gif)';
	document.startProcessing(mMove,doScroll);
	scrollingNow = 1;
	return false;
}
function pauseScroll() {
	thumbDiv.style.backgroundImage='url(./fileadmin/templates/grfx/thumb.gif)';
	document.stopProcessing(mMove);
	document.stopProcessing(mUp);
	scrollingNow = 0;
}
function doScroll(thumb) { 
	var mouseYCurrent = catchEvent(thumb);
	var mouseDistance = mouseYCurrent-mouseYInitial;
	var thumbDistance = thumbTopInitial+mouseDistance;
	var thumbTopCurrent = (thumbDistance<thumbTop)?thumbTop:(thumbDistance>thumbWay)?thumbWay:thumbDistance; //
	thumbDiv.setTop(thumbTopCurrent);
	var contentTopNext = -(thumbTopCurrent-thumbTop)*(1/scrollProportion);
	contentDiv.setTop(contentTopNext);
// set caches
	contentTopCache = contentTopNext;
	contentHeightCache = contentHeight;
	thumbTopCache = thumbTopCurrent;
	thumbHeightCache = thumbHeight;
	document.startProcessing(mUp,pauseScroll);
	return false
}
document.startProcessing = startProcessing;
document.stopProcessing = stopProcessing;

function openClose(which) {
	if(activeContent == 'c4w') {
		if(document.getElementById(which).offsetHeight == '90') document.getElementById(which).style.height = '100%';
		else document.getElementById(which).style.height = '90px';
	}
	else {
		if (document.getElementById(which+'List').style.display=='none') {
			document.getElementById(which+'List').style.display='block';
			document.getElementById(which+'Switch').src='./fileadmin/templates/grfx/close_1.gif';
		}
		else if (document.getElementById(which+'List').style.display=='block') {
			document.getElementById(which+'List').style.display='none';
			document.getElementById(which+'Switch').src='./fileadmin/templates/grfx/open.gif';
		}
	}
	setupScroll(viewPort);
}
var iconState='';
var iconSrc='';
function onMouse(which,action) {
	if(which == 'thumb' && scrollingNow == 0) {
		if(action=='over') {
			if(activeContent == 'c2b') document.getElementById(which).style.backgroundImage='url(./fileadmin/templates/grfx/thumb_1b.gif)';
			else document.getElementById(which).style.backgroundImage='url(./fileadmin/templates/grfx/thumb_1.gif)';
		}
		else document.getElementById(which).style.backgroundImage='url(./fileadmin/templates/grfx/thumb.gif)';
	}
	else if(which != 'thumb') {
		iconSrc=document.getElementById(which+'Switch').src;
		if(iconSrc.indexOf('close')!=-1) iconState='close';
		else iconState='open';
		if(action=='over') {
			document.getElementById(which+'Switch').src='./fileadmin/templates/grfx/'+iconState+'_1.gif';
		}
		else if(action=='out') {
			document.getElementById(which+'Switch').src='./fileadmin/templates/grfx/'+iconState+'.gif';
		}
	}
}