// Made by geeeet@ghtml.com
// Keep these two lines and you're free to use this code

// Known bugs :
// If ie4.5 mac, please press apple-t to remove sidebar, otherwise everything is pushed 20px to the right...

// Corrected bugs :
// 25.01.2001 - When the height of the span "scrollContent" was less than the height of the span "contentClip" a javascript error occured, function changed : move()
// 21.02.2001 - Scrolling text wasn't selectable in ie, function changed : move()
// 05.03.2001 - Ie x and y coordinates was wrong when page was scrolled, function changed : getMouse()

// 19.04.2001 - Finally able to remove browser-scrollbar if content is longer than the browser is high:
// Just put this in the style-tag right before the end head-tag:
// body {margin-left:0; margin-right:0; margin-top:0; margin-bottom:0; width:100%;height:100%;overflow:hidden}

// Touch me here :-)
var upH = 5; // Height of up-arrow
var upW = 17; // Width of up-arrow
var downH = 5; // Height of down-arrow
var downW = 17; // Width of down-arrow
var dragH = 17; // Height of scrollbar
var dragW = 17; // Width of scrollbar
var scrollH = 343; // Height of scrollbar
var speed = 5; // Scroll speed

// And now... go to the bottom of the page...

// Browser detection
var ie4 = document.all ? true:false;
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;

var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick

var clickUp = false; // If click on up-arrow
var clickDown = false; // If click on down-arrow
var clickDrag = false; // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

var timer = setTimeout("",500); // Repeat variable
var upL; // Up-arrow X
var upT; // Up-arrow Y
var downL; // Down-arrow X
var downT; // Down-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentT; // Content layer Y;
var contentH; // Content height
var contentClipH; // Content clip height
var scrollLength; // Number of pixels scrollbar should move
var startY; // Keeps track of offset between mouse and span

// Mousedown
function down(e){

	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);
	startY = (mouseY - dragT);

	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
	//if(mouseX >= upL && (mouseX <= (upL + upW)) && (mouseY <= (upT + upH))){
		clickUp = true;
		return scrollUp();
	}	
	// Else if click on down-arrow
	 else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
	//else if(mouseX >= downL && (mouseX <= (downL + downW)) && (mouseY <= (downT + downH))){

	// 1
	// 1
	// 0
	// 1


		clickDown = true;
		return scrollDown();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
		// If click above drag
		if(mouseY < dragT){
			clickAbove = true;
			clickUp = true;
			return scrollUp();
		}
		// Else click below drag
		else{
			clickBelow = true;
			clickDown = true;
			return scrollDown();
		}
	}
	// If no scrolling is to take place
	else{
		return true;
	}
}

// Drag function
function move(e){
	if(clickDrag && contentH > contentClipH){
		getMouse(e);
		dragT = (mouseY - startY);
		
		if(dragT < (rulerT))
			dragT = rulerT;		
		if(dragT > (rulerT + scrollH - dragH))
			dragT = (rulerT + scrollH - dragH);
		
		contentT = ((dragT - rulerT)*(1/scrollLength));
		contentT = eval('-' + contentT);

		moveTo();
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	clickUp = false;
	clickDown = false;
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	return true;
}

// Reads content layer top
function getT(){
	if(ie4)
		contentT = document.all.scrollContent.style.pixelTop;
	else if(nn4)
		contentT = document.contentClip.document.scrollContent.top;
	else if(dom)
		contentT = parseInt(document.getElementById("scrollContent").style.top);
}

// Reads mouse X and Y coordinates
function getMouse(e){
	if(ie4){
		if(screen.height == 600) mouseY = (event.clientY + document.body.scrollTop) + 190;
		else mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
}

// Moves the layer
function moveTo(){
	if(ie4){
		document.all.scrollContent.style.top = contentT + "px";
		document.all.ruler.style.top = dragT + "px";
		//document.all.drag.style.top = parseInt(dragT2 - contentT + dragT2) + "px";
		//document.all.drag.style.top = parseInt(dragT2 + scrollLength) + "px";

		if(location.href == "http://www.smithsflourmill.co.uk/walsall")
		{
			document.all.drag.style.top = parseInt((dragT2 - contentT) * (323/(contentH - contentClipH)) + dragT2-50)  + "px";
		}
		else
		{
			document.all.drag.style.top = parseInt((dragT2 - contentT) * (323/(contentH - contentClipH)) + dragT2-4)  + "px";
		}
	}
	else if(nn4){
		document.contentClip.document.scrollContent.top = contentT;
		document.ruler.top = dragT;
		document.drag.top = dragT;
	}
	else if(dom){
		document.getElementById("scrollContent").style.top = contentT + "px";
		//document.getElementById("drag").style.top = dragT + "px";
		
		document.getElementById("ruler").style.top = dragT + "px";
		//document.getElementById("drag").style.top = parseInt((dragT - contentT) * (323/(contentH - contentClipH)) + dragT2-4)  + "px";
		document.getElementById("drag").style.top = dragT-223 + "px";
	}
}

// Scrolls up
function scrollUp(){
	getT();
	
	if(clickAbove){
		if(dragT <= (mouseY-(dragH/2)))
			return up();
	}
	
	if(clickUp){
		if(contentT < 0){		
			dragT = dragT - (speed*scrollLength);
			
			if(dragT < (rulerT))
				dragT = rulerT;
				
			contentT = contentT + speed;
			if(contentT > 0)
				contentT = 0;
			
			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(){
	getT();

	if(clickBelow){
		if(dragT >= (mouseY-(dragH/2)))
			return up();
	}

	if(clickDown){
		if(contentT > -(contentH - contentClipH)){		
			dragT = dragT + (speed*scrollLength);
			if(dragT > (rulerT + scrollH - dragH))
				dragT = (rulerT + scrollH - dragH);
			
			contentT = contentT - speed;
			if(contentT < -(contentH - contentClipH))
				contentT = -(contentH - contentClipH);

			moveTo();
			timer = setTimeout("scrollDown()",25);
		}
	}
	return false;
}




//-------------------------------------------
// Real positions
//-------------------------------------------
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	return curtop;
}
//-------------------------------------------

// Preload
function eventLoader(){

		// quit if this function has already been called
  //  if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    //arguments.callee.done = true;


	if(ie4){
		// Up-arrow X and Y variables
		upL = findPosX(document.all.up);
		upT = findPosY(document.all.up);

		// Down-arrow X and Y variables
		downL = findPosX(document.all.down);
		downT = findPosY(document.all.down);

		// Scrollbar X and Y variables
		//dragL = document.all.drag.style.pixelLeft;
		//dragT = document.all.drag.style.pixelTop;
		dragL = findPosX(document.all.drag);
		dragT = findPosY(document.all.drag);
		dragT2 = document.all.drag.style.pixelTop;
		//window.alert(dragL+'-'+dragT);

		// Ruler Y variable
		rulerT = findPosY(document.all.ruler);

		// Height of content layer and clip layer
		contentH = parseInt(document.all.scrollContent.scrollHeight);
		contentClipH = parseInt(document.all.contentClip.style.height);

		var windowWidth = document.body.offsetWidth;
		var windowHeight = document.body.offsetHeight;
	}
	else if(nn4){
		// Up-arrow X and Y variables		
		upL = findPosX(document.up);
		upT = findPosY(document.up);

		// Down-arrow X and Y variables
		downL = findPosX(document.down);
		downT = findPosY(document.down);

		// Scrollbar X and Y variables
		dragL = document.drag.left;
		dragT = document.drag.top;		

		// Ruler Y variable
		rulerT = document.ruler.top;

		// Height of content layer and clip layer
		contentH = document.contentClip.document.scrollContent.clip.bottom;
		contentClipH = document.contentClip.clip.bottom;

		var windowWidth = window.innerWidth;
		var windowHeight = window.innerHeight;
	}
	else if(dom){
		// Up-arrow X and Y variables
		upL = findPosX(document.getElementById("up"));
		upT = findPosY(document.getElementById("up"));

		// Down-arrow X and Y variables
		downL = findPosX(document.getElementById("down"));
		downT = findPosY(document.getElementById("down"));

		// Scrollbar X and Y variables
		//dragL = parseInt(document.getElementById("drag").style.left);
		//dragT = parseInt(document.getElementById("drag").style.top);
		dragL = findPosX(document.getElementById("drag"));
		dragT = findPosY(document.getElementById("drag"));
		dragT2 = document.getElementById("drag").style.top;

		// Ruler Y variable
		//rulerT = parseInt(document.getElementById("ruler").style.top);
		rulerT = findPosY(document.getElementById("ruler"));

		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("scrollContent").offsetHeight);
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
		document.getElementById("scrollContent").style.top = 0 + "px";		

		var windowWidth = window.innerWidth;
		var windowHeight = window.innerHeight;
	}
	// Number of pixels scrollbar should move
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));


	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	}
	//window.onresize = reloadPage;
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;


			if (document.all&&document.getElementById) {
				navRoot = document.getElementById("nav");
				for (i=0; i<navRoot.childNodes.length; i++) {
					node = navRoot.childNodes[i];
					if (node.nodeName=="LI") {
						node.onmouseover=function() {
							this.className+=" over";
						}
						node.onmouseout=function() {
							this.className=this.className.replace(" over", "");
						}
					}
				}
			}
		/*
		function setWindowWidth()
		*/
}


			function switchImage(img,imgfull,text,link,id) {
	if (link == 1) {
		document.getElementById('page-images').innerHTML='<a href="/enlarge-image'+imgfull+'" onclick="popupWindow(\'/enlarge-image'+imgfull+'\',400,400);return false"><img src="'+img+'" alt="'+text+'" /></a>';
	} else {
		document.getElementById('page-images').innerHTML='<img src="'+img+'" alt="'+text+'" />';
		document.getElementById(id).style.color='#F18A00';
		document.all.id.style.color='#F18A00';
	}
}

function rollImage(img,text) {
	document.getElementById('page-images').innerHTML="<img src="+img+" />";
}

// reloads page to position the layers again
function reloadPage(){

	if(windowWidth == window.innerWidth && windowHeight == window.innerHeight)
	{
		return false;
	}
	else
	{
		location.reload();
	}
}
