// JavaScript Document

onload = function() {  // initialise required scripts
	scrollScript(); // make the scroller draggable
	scrollWatcher(); // start up a looping script that checks if the page scrolls
}
var scrolltimer = null; // variable used in looping scripts
var scrollWatcherTimer = null; // variable used in scrolling repair script
var mouseLocV = 0; // variable used for paging up & down
var isMouseDown = 0; // variable used for scroller rollover
var isMouseOver = 0; // variable used for scroller rollover
var menuHeight = document.getElementById("Menu").offsetHeight; // the height of the menu
var contentHeight = document.getElementById("centreContent").offsetHeight; // the height of the page content
if (contentHeight > menuHeight) { // work out which height should be used in calculations (highest wins!)
	everythingHeight = contentHeight;
} else {
	everythingHeight = menuHeight;
}
getScreenData(); // get various screen measurements that can be changed by the user

function getScreenData() { // get various screen measurements that can be changed by the user
	windowHeight = document.getElementById("scrollArea").offsetHeight; // The total height of the window
	scrollAreaHeight = windowHeight - 198; // window height less the height of 2 scroll buttons and scroller
	if (scrollAreaHeight < 1) {
		scrollAreaHeight = 1; // make sure it doesn't go all funny
	}
	document.getElementById("pageUpDownButton").style.height = scrollAreaHeight + 48 + "px"; // scroll area including the scroller height
}

onresize = function() { // maintain all layers' relative placement when the window is resized
	g = document.getElementById("scroller").offsetTop / scrollAreaHeight; // get relative position of scroller before resizing 
	getScreenData(); // update the screen info
	h = Math.round(scrollAreaHeight * g); // the new position relative to the old one
	if (h < 0) { // if the scroller is trying to move above the scroll area
		h = 0; // keep it at the top
	}
	if (h > scrollAreaHeight) { // if the scroller is trying to move below the scroll area
		h = scrollAreaHeight; // keep it at the top
	}
	document.getElementById("scroller").style.top = h + "px"; //put the scroller in the correct position
	scrollContents(); // line everything up accordingly
	scrollScript(); // line everything up accordingly
}

function scrollScript() {  
	Drag.init(document.getElementById("scroller"),null,0,0,0,scrollAreaHeight); //make the scroller div draggable    
	document.getElementById("scroller").onDrag = function (x,y) { scrollContents(); } //add ondrag function and move everything accordingly
}

function scrollWatcher() {
	clearTimeout(scrollWatcherTimer);
	if (document.getElementById("entireWindow").scrollTop != 0) { // maintain proper positioning of elements when jumping to anchors, etc.
		document.getElementById("centreContent").style.top = document.getElementById("centreContent").offsetTop - document.getElementById("entireWindow").scrollTop + "px"; // move the content to be in the right position when the window is moved back
		document.getElementById("entireWindow").scrollTop = 0 + "px"; // move the window back to 0 px
		document.getElementById("scroller").style.top = Math.round(- document.getElementById("centreContent").offsetTop * (scrollAreaHeight / (contentHeight - windowHeight))) + "px"; // place the scroller in the correct place
		if (document.getElementById("scroller").style.top > scrollAreaHeight + 75) { // if the scroller has gone below the permissable area (eg in a search)
			document.getElementById("scroller").style.top = scrollAreaHeight + 75 + "px"; // put the scroller to the bottom of the permissable area
		}
		if (document.getElementById("scroller").style.top < 0) { // if the scroller has gone above the permissable area (eg in a search)
			document.getElementById("scroller").style.top = 0 + "px"; // put the scroller to the top of the permissable area
		}
		scrollContents(); // line the layers up with the scroller
	}
	scrollWatcherTimer = setTimeout("scrollWatcher()", 10); // repeat
}

function pageUpDown(e) {
	clearTimeout(scrolltimer);
	clearTimeout(scrollWatcherTimer);
	if (mouseLocV == 0) { // The following bit saves the mouse position
		if (document.all) {
			mouseLocV = window.event.y; // The current vertical mouse position
		} else {
			mouseLocV = e.pageY; // The current vertical mouse position
		}
	}
	if (mouseLocV > windowHeight - (75 + 24)) { // make sure the scroller doesn't go too far down
		mouseLocV = windowHeight - (75 + 24);
	}
	if (mouseLocV < (75 + 24)) { // make sure the scroller doesn't go too far up
		mouseLocV = (75 + 24);
	}
	if (mouseLocV > document.getElementById("scroller").offsetTop + 75 + 24) { // if the mouse is below halfway down the scroller when clicked, move downwards
		if (contentHeight + (document.getElementById("centreContent").offsetTop - windowHeight) > windowHeight) { // if there's more than a window's worth of content offscreen (below)
			document.getElementById("scroller").style.top = document.getElementById("scroller").offsetTop + Math.round(scrollAreaHeight / (everythingHeight / windowHeight)) + "px"; //this needs to change
		} else {
			document.getElementById("scroller").style.top = mouseLocV - (75 + 24) + "px"; //otherwise put the scroller where the mouse is
		}
	}
	if (mouseLocV < document.getElementById("scroller").offsetTop + 75 + 24) { // if the mouse is above halfway down the scroller when clicked, move upwards
		if (document.getElementById("centreContent").offsetTop < -windowHeight) {// if there's more than a window's worth of content offscreen (above)
			document.getElementById("scroller").style.top = document.getElementById("scroller").offsetTop - Math.round(scrollAreaHeight / (everythingHeight / windowHeight)) + "px"; //this needs to change
		} else {
			document.getElementById("scroller").style.top = mouseLocV - (75 + 24) + "px"; //otherwise put the scroller where the mouse is
		}
	}
	scrollContents(); // scroll the contents to keep up with the scroller
	scrolltimer = setTimeout("pageUpDown()", 1); // repeat
}

function scrollContents() { 
	clearTimeout(scrollWatcherTimer);
	theScrollerPosition = document.getElementById("scroller").offsetTop; // where the scroller is at the moment
	if (windowHeight < menuHeight) { // if the menu needs scrolling
		menuScrollTo =  -(theScrollerPosition * (menuHeight - windowHeight) / scrollAreaHeight); // work out how much to scroll the menu
		document.getElementById("leftColumn").style.top = menuScrollTo + "px"; // then scroll the menu
	} else { // if no scrolling is necessary
		document.getElementById("leftColumn").style.top = 0 + "px"; //ensure the menu stays at the top of the page
	}
	if (windowHeight < document.getElementById("centreContent").offsetHeight) { // if the content needs scrolling
		contentScrollTo = -(theScrollerPosition * (contentHeight - windowHeight) / scrollAreaHeight); // work out how much to scroll the content
		document.getElementById("centreContent").style.top = contentScrollTo + "px"; // then scroll the content
	} else { // if no scrolling is necessary
		document.getElementById("centreContent").style.top = 0 + "px"; //ensure the content stays at the top of the page
	}
}
		
function scrollAway(amount){ // Scroll up & down, using buttons
	clearTimeout(scrolltimer);	
	clearTimeout(scrollWatcherTimer);
	a = parseInt(amount); // the speed of movement
	scrollerTop = document.getElementById("scroller").offsetTop + a; // the current location of the scroller
	if (scrollerTop > scrollAreaHeight) { scrollerTop = scrollAreaHeight; }
		if (a > 0) { // if the scroller should move downwards
			if (scrollerTop < scrollAreaHeight) {// if the scroller isn't at the bottom, move down by 'a'
				document.getElementById("scroller").style.top = scrollerTop + "px"; 
			} else {
				document.getElementById("scroller").style.top = scrollAreaHeight + "px";  // otherwise just position it at the bottom
			}
		}
		if (a < 0) { // if the scroller should move upwards
			if (scrollerTop > 0) {
				document.getElementById("scroller").style.top = scrollerTop + "px";   // if the scroller isn't at the top, move up by 'a'
			} else {
				document.getElementById("scroller").style.top = 0 + "px";  // otherwise just position it at the top
			}
	}
	scrollContents(); // scroll the contents to keep up with the scroller
	scrolltimer = setTimeout("scrollAway(a)", 1); // repeat
}

function stopScroll() { // Stop all scrolling
	mouseLocV = 0 ;
	clearTimeout(scrolltimer);
	scrollWatcher();
}

function mouseStatus(a, b) { // Stop the scroller rollover image flickering when dragged
	if (a == 0 && b == 0) {MM_swapImgRestore();MM_displayStatusMsg('You\'re monkeying around at the Baboon Saloon!');return document.MM_returnValue; }
	else {MM_swapImage('scroller','',scrollerAddress + 'TopOfPage2.gif',1);MM_displayStatusMsg('Drag To Scroll');return document.MM_returnValue; }
}