// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 01/17/03 ---------------------------------------------------------

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Cross-Browser Functions

var dom = document.getElementById;
var iex = document.all;
var ns4 = document.layers;

function addEvent(event,method){
	this[event] = method;
	if(ns4) this.captureEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function removeEvent(event){
	this[event] = null;
	if(ns4) this.releaseEvents(Event[event.substr(2,event.length).toUpperCase()]);
}
function getElement(name,nest){
	nest = nest ? "document."+nest+"." : "";
	var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+"document."+name) : false;
	el.css = ns4 ? el : el.style;
	el.hideVis = function(){el.css.visibility="hidden"};
	el.showVis = function(){el.css.visibility="visible"};
	el.getTop = function(){return parseInt(el.css.top) || 0};
	el.setTop = function(y){el.css.top = ns4 ? y: y+"px"};
	el.getHeight = function(){return ns4 ? el.document.height : el.offsetHeight};
	el.getClipHeight = function(){return ns4 ? el.clip.height : el.offsetHeight};
	el.addEvent = addEvent;
	el.removeEvent = removeEvent;
	return el;
}
function getMouse(e){
	return iex ? event.clientY : e.pageY;
}

document.addEvent = addEvent;
document.removeEvent = removeEvent;

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// ImageSwap Functions

function newImage(src){
	img = new Image();
	img.src = src;
	return img;
}
function imageSwap(img,obj,div){
	try {
		obj = eval(obj);
		if(document.layers && div != null){
			document.layers[div].document.images[img].src = obj.src;
		}else{
			document.images[img].src = obj.src;
		}
	} catch (e) {}
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Scroll Functions

var currentContent = null;
var docLoaded = false;
var aboutOff = newImage("img/nav_about.gif");
var aboutOn = newImage("img/nav_about_on.gif");
var workOff = newImage("img/nav_work.gif");
var workOn = newImage("img/nav_work_on.gif");
var clientsOff = newImage("img/nav_clients.gif");
var clientsOn = newImage("img/nav_clients_on.gif");
var contactOff = newImage("img/nav_contact.gif");
var contactOn = newImage("img/nav_contact_on.gif");

function initScroller(trackHeight, startObj){
	scrollSpeed = 6; // scrolling speed
	dragHeight = 21; // Height of scrollbar drag
	trackHeight = trackHeight; // Height of scrollbar track
	trackObj = getElement("track"); // Reference to the scrollbar track div
	upObj = getElement("up"); // Reference to the up arrow div
	downObj = getElement("down"); // Reference to the down arrow div
	dragObj = getElement("drag"); // Reference to the scrollbar drag div
	contentMaskObj = getElement("contentMask"); // Reference to the content mask div
	trackTop = dragObj.getTop(); // Scrollbar top contraint
	trackLength = trackHeight-dragHeight; // Adjusted track height
	trackBottom = trackTop+trackLength; // Scrollbar bottom contraint
	contentMaskHeight = contentMaskObj.getClipHeight();// Height of the div that masks the content div
	scrollTimer = null;
	trackObj.addEvent("onmousedown",scrollJump);
	upObj.addEvent("onmousedown", function(){scroll(scrollSpeed);return false});
	upObj.addEvent("onmouseup", stopScroll);
	upObj.addEvent("onmouseout", stopScroll);
	downObj.addEvent("onmousedown", function(){scroll(-scrollSpeed);return false});
	downObj.addEvent("onmouseup", stopScroll);
	downObj.addEvent("onmouseout", stopScroll);
	dragObj.addEvent("onmousedown", startDrag);
	if(iex) dragObj.addEvent("ondragstart", function(){return false});
	docLoaded = true;
	loadContent(startObj);
}

function loadContent(number){
	if(!docLoaded) return;
	if(currentContent!=null){
		highlight(number);
		imageSwap(currentContent, currentContent+"Off", "nav");
		contentObj.setTop(0);
		contentObj.hideVis();
	}
	currentContent = "divTabContent"+number;
	imageSwap(number, number+"On", "nav");
	contentObj = getElement(currentContent,"contentMask");
	contentHeight = contentObj.getHeight(); // Height of the content div
	contentLength = contentHeight-contentMaskHeight; // Adjusted content height
	scrollLength = trackLength/contentLength; // Height difference between the scrollbar track and the content
	contentObj.showVis();
	dragObj.setTop(trackTop);
	if(contentHeight<=contentMaskHeight){
/*		trackObj.hideVis();
		upObj.hideVis();
		downObj.hideVis();*/
		dragObj.hideVis();
		upObj.addEvent("onmousedown", function(){return false});
		downObj.addEvent("onmousedown", function(){return false});
/*		contentMaskObj.width = contentMaskObj.width + 16;
		contentObj.width = (contentObj.width + 16);
*/
	}else{
		upObj.addEvent("onmousedown", function(){scroll(scrollSpeed);return false});
		downObj.addEvent("onmousedown", function(){scroll(-scrollSpeed);return false});
		trackObj.showVis();
		upObj.showVis();
		downObj.showVis();
		dragObj.showVis();
	}

}
function startDrag(e){
	dragStartMouse = getMouse(e); // Holds the starting y mouse position
	dragStartOffset = dragObj.getTop(); // Holds the starting top position of the scrollbar drag
	document.addEvent("onmousemove", drag);
	document.addEvent("onmouseup", stopDrag);
	return false;
}
function stopDrag(){
	document.removeEvent("onmousemove");
	document.removeEvent("onmouseup");
}
function drag(e){
	var currentMouse = getMouse(e);
	var mouseDifference = currentMouse-dragStartMouse;
	var dragDistance = dragStartOffset+mouseDifference;
	var dragMovement = (dragDistance<trackTop) ? trackTop : (dragDistance>trackBottom) ? trackBottom : dragDistance;
	dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-trackTop)*(1/scrollLength);
	contentObj.setTop(contentMovement);
	return false;
}
function scroll(speed){
	var contentMovement = contentObj.getTop()+speed;
	var dragMovement = trackTop-Math.round(contentObj.getTop()*(trackLength/contentLength));
	if(contentMovement > 0){
		contentMovement = 0;
	}else if(contentMovement < -contentLength){
		contentMovement = -contentLength;
	}
	if(dragMovement < trackTop){
		dragMovement = trackTop;
	}else if(dragMovement > trackBottom){
		dragMovement = trackBottom;
	}
	contentObj.setTop(contentMovement);
	dragObj.setTop(dragMovement);
	scrollTimer = window.setTimeout("scroll("+speed+")",25);
}
function stopScroll(){
	if(scrollTimer){
		window.clearTimeout(scrollTimer);
		scrollTimer = null;
	}
}
function scrollJump(e){
	var currentMouse = getMouse(e);
	var dragDistance = currentMouse-(dragHeight/2);
	var dragMovement = (dragDistance<trackTop) ? trackTop : (dragDistance>trackBottom) ? trackBottom : dragDistance;
	dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-trackTop)*(1/scrollLength);
	contentObj.setTop(contentMovement);
	return false;
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Utility Functions

function hideScrollbars(){
	if(document.getElementsByTagName){
		document.getElementsByTagName("body")[0].style.overflow = "hidden";
	}
}
function fixNetscape4(){
	if(ns4origWidth != window.innerWidth || ns4origHeight != window.innerHeight){
		window.location.reload();
	}	
}
if(document.layers){
	ns4origWidth = window.innerWidth;
	ns4origHeight = window.innerHeight;
	window.onresize = fixNetscape4;
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||

// window.onload = initScroller;


// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Reiter einfärben
// ||||||||||||||||||||||||||||||||||||||||||||||||||
var path = "img/";
normal = new Array();
active = new Array();

normal['left'] = newImage(path+"reiter_left-b.png");
normal['right'] = newImage(path+"reiter_right-b.png");
normal['back'] = newImage(path+"reiter_back-b.png");
normal['middle'] = newImage(path+"reiter_middle-bb.png");

active['left'] = newImage(path+"reiter_left-r.png");
active['right'] = newImage(path+"reiter_right-r.png");
active['middle0'] = newImage(path+"reiter_middle-rb.png");
active['middle1'] = newImage(path+"reiter_middle-br.png");
active['back'] = newImage(path+"reiter_back-r.png");

function highlight(tabNr) {
	var types= new Array();
	types['m'] = 'middle';
	types['b'] = 'back';
	types['l'] = 'left';
	types['r'] = 'right';
	for (var i = 0; i < window.document.images.length; i++) {
		var imageName = window.document.images[i].name;
		var name = imageName.split('_');
		if (name[0] == "tab") {
//			alert(name[1] + "--" + types[name[1]]);
			var belongs = false;
			for (var j = 2; j < name.length; j++) {
				if (name[j] == tabNr) {
					belongs = j - 2;
				}
			}
//			alert(belongs);
			if (belongs === false) {
				setNormal(imageName, types[name[1]]);
			} else {
				setActive(imageName, types[name[1]], belongs);
			}
		}
	}
}

function setActive(imageName, type, source) {
//	alert('active: '+imageName+", "+type+", "+source);
	if (type == 'middle') {
		type = type+source;
	}
	window.document.images[imageName].src = active[type].src;
}

function setNormal(imageName, type) {
//	alert('normal: '+imageName+", "+type);
	window.document.images[imageName].src = normal[type].src;
}
