// JavaScript Document

	window.onload = start;
	
	function start() {	
		scrollListener(); //Init mousewheel event
		scrollbar(); //Show scrollbar and set scrollbox height
		moveScrollbox(); //Set scrollbox position
		checkIfAnchors(); //Check if page has anchors and change href to onclick
		buttonScroll(); //Event for scrolling with four buttons
	}
	
	function scrollListener() {	  	  
		//adding the event listerner for Mozilla
		if(window.addEventListener) { 
			document.addEventListener('DOMMouseScroll', moveObject, false);
		}
		//for IE/OPERA etc
		document.onmousewheel = moveObject;
	}
	
	
	function scrollbar() {
		var barDiv = document.getElementById("scrollbar");
		barDiv.style.visibility = "visible";
		var barHeight = 366;
		var frameHeight = document.getElementById('content_frame').offsetHeight;
		var offHeight = document.getElementById('content').offsetHeight;
		
		var m = offHeight / barHeight;
		var boxHeight = Math.ceil(frameHeight / m);
		document.getElementById('scrollbox').style.height = boxHeight + 'px';
	}
	
	
	function moveObject(event) {
		var delta = 0;
		if (!event) event = window.event;
		// normalize the delta
		if (event.wheelDelta) {
			// IE & Opera
			delta = event.wheelDelta / Math.abs(event.wheelDelta)
		} else if (event.detail) { // W3C
			delta = -event.detail / 3;
		}
		moving(delta*4);
	}
	
	
	function moving(delta) {  
		var currPos=document.getElementById('content').offsetTop;
		var frameHeight = document.getElementById('content_frame').offsetHeight;
		var offHeight = document.getElementById('content').offsetHeight;
		
		if (frameHeight < offHeight) {
			//calculating the next position of the object
			currPos=parseInt(currPos)+(delta*20);
			//moving the position of the object
			if (currPos > 0) {
				currPos = 0;	  
			} else if (currPos <= -(offHeight - frameHeight) ) {
				currPos = -(offHeight - frameHeight);
			}
			document.getElementById('content').style.top=currPos+"px";	
			moveScrollbox();
		}
	}
	
	function moveScrollbox() {
		var top = -document.getElementById('content').offsetTop;
		var height = document.getElementById('content').offsetHeight;
		var x = height/366;
		var boxHeight = parseInt(top/x) + 12;
		document.getElementById('scrollbox').style.top = boxHeight + 'px';		
	}
	
	function checkIfAnchors() {
		var p = document.getElementById('anchorLinks');
		if (p) {
			var html = p.innerHTML;
			var finder = html.search(/href="\#/i); //i is for case-insensitiv
			while (finder >= 0) {
				var html_temp = html.substring(finder+6); //Find first href="#
				var srch = html_temp.search(/"/); //Find next " after first href="#
				var value = html.substring(finder+7, finder+6+srch); //Find text where anchor goes to
				html = html.substring(0, finder) + ' onclick="anchorScroll(\'' + value + '\')" ' + html_temp.substring(srch+1);
				finder = html.search(/href="/i); //Search for more anchors
			}
			p.innerHTML = html;
		}
	}
	
	
	function anchorScroll(id) {		
		var currPos=document.getElementById('content').offsetTop;
		var frameHeight = document.getElementById('content_frame').offsetHeight;
		var offHeight = document.getElementById('content').offsetHeight;
		
		//calculating the next position of the object
		currPos = -document.getElementById(id).offsetTop;
		//moving the position of the object
		if (currPos > 0) {
			currPos = 0;	  
		} else if (currPos <= -(offHeight - frameHeight) ) {
			currPos = -(offHeight - frameHeight);
		}		
		document.getElementById('content').style.top=currPos+"px";	
		moveScrollbox();
	}
	

	function buttonScroll() {
		document.onkeydown = function(e){
			var e=window.event || e
//			alert(e.keyCode);
			switch(e.keyCode) {
			case 36: 	//Home
						document.getElementById('content').style.top=0+"px";
						moveScrollbox();
						break;
			case 33: 	//Page Up
						moving(20);
						moveScrollbox();
						break;
			case 34: 	//Page Down
						moving(-20);
						moveScrollbox();
						break;
			case 35: 	//End
						var frameHeight = document.getElementById('content_frame').offsetHeight;
						var offHeight = document.getElementById('content').offsetHeight;
						var pos = -(offHeight - frameHeight);
						document.getElementById('content').style.top=pos+"px";
						moveScrollbox();
						break;
			case 38: 	//Arrow up
						moving(1);
						moveScrollbox();
						break;
			case 40: 	//Arrow down
						moving(-1);
						moveScrollbox();
						break;
			}	
		}
	}

