var dom = document.getElementById;
var iex = document.all;
var ns4 = document.layers;
var docLoaded = false;
var currentContent = null;

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;

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Scroll Functions


function initScroller(){

   fixColumns();
   
    
	scrollSpeed = 6;   // scrolling speed
	dragHeight  = 21;  // Height of scrollbar drag
	trackHeight = maxHeight-50; // 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("navigation"); // 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("navigation_content");
	 
}
function loadContent(name){
	if(!docLoaded) return;
	if(currentContent!=null){
		contentObj.setTop(0);
		contentObj.hideVis();
	}
	currentContent = name;
 
	contentObj = getElement(currentContent,"navigation");
	contentHeight = contentObj.getHeight(); // Height of the content div
	//contentHeight=634;
	contentLength = contentHeight-contentMaskHeight; // Adjusted content height
	scrollLength = trackLength/contentLength; // Height difference between the scrollbar track and the content
	
	contentObj.showVis();
	dragObj.setTop(trackTop);
	//alert(contentHeight);
	//contentHeight=600;
	
	if(contentHeight<=contentMaskHeight){
		trackObj.hideVis();
		upObj.hideVis();
		downObj.hideVis();
		dragObj.hideVis();
	}else{
		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);
	
	//alert(contentObj.css.top);
	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;


var maxHeight = 0;
var defaultHeight = 352;

	function fixColumns(){
		var h1 = document.getElementById("navigation").offsetHeight;
		var h2 = document.getElementById("col2").offsetHeight;
		var h3 = document.getElementById("content").offsetHeight;
	
		var max = h3;
		
		if (max < defaultHeight) {
	        max = defaultHeight;
	        document.getElementById("content").style.height = max + "px";
		} else {
		     var _down = getElement("down");
		     _down.setTop(max-20);
		}
		
		maxHeight = max;
		/*if(h2 > max) max = h2;
		if(h3 > max) max = h3;
	 */
		document.getElementById("navigation").style.height = max + "px";
		document.getElementById("col2").style.height = max + "px";
		//document.getElementById("col3").style.height = max + "px";
		
		
//document.getElementById("content").onclick=function() {document.location=next;};
//document.getElementById("navigation").onclick=function() {document.location=next;};
    
	}
	
	function email(){
		
	  _url = document.location.href;
	  _base = _url.substring(0, _url.indexOf("/",8));
	  _new_url ="";
	 
  //	_new_url= _base + "/mailto_email_page.asp?url="+escape(document.location.href);
	//setTimeout("location.href = _new_url;", 2000);
	
	_body = "\n"; //""Hello \n\n";//I recommend this tip -- " + document.title;
  _body += "Information von www.Gradwohl.info: " + location.href; 
  _body += "\n\nE-Mail Text\n";
  _body += "\n\n"; 
	
  mail_str = "mailto:office@gradwohl.info?subject=Information von www.Gradwohl.info";// + escape(document.title);
  mail_str += "&body=" + escape (_body);
  
  location.href = mail_str;

}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
		createCookie(name,"",-1);
}

function setOuterTop() {
	el = document.getElementById("outer");
	_top = el.offsetTop;
	createCookie("outertop", _top, 1);
}

function getOuterTopPosition() {
	
	_val = readCookie("outertop");
	
	if (_val == null) return "";
	else return "position:absolute; top:"+ _val + "px";
	
} 

var BO = new Object(); 
function detectBrowser() { 
    BO["ie"] =  false /*@cc_on || true @*/; 
    BO["ie4"]  =  BO["ie"] && (document.getElementById==null); 
    BO["ie5"]  =   BO["ie"] && (document.namespaces==null) && (!BO["ie4"]) ; 
    BO["ie6"]  =  BO["ie"] && (document.implementation!=null) && (document.implementation.hasFeature!=null) 
    BO["ie55"]  =  BO["ie"] && (document.namespaces!=null) && (!BO["ie6"]); 
    BO["ns4"]  = !BO["ie"] &&  (document.layers !=null) &&  (window.confirm !=null) && (document.createElement ==null); 
    BO["opera"] =  (self.opera!=null); 
    BO["gecko"] =  (document.getBoxObjectFor!=null); 
    BO["khtml"] = (navigator.vendor =="KDE"); 
    BO["konq"] =  ((navigator.vendor == 'KDE')||(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)); 
    BO["safari"] = (document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName); 
    BO["safari1.2"] = (parseInt(0).toFixed==null) && (BO["safari"] && (window.XMLHttpRequest!=null)); 
    BO["safari2.0"] = (parseInt(0).toFixed!=null) && BO["safari"] && !BO["safari1.2"] ; 
    BO["safari1.1"] = BO["safari"] && !BO["safari1.2"]  &&!BO["safari2.0"]; 
} 