var scrollerHTMLadded = false; //Flag to determine whether or not the scrolling layer has already been added.

var checkScrollInterval;
var scrollTimer;

var scrollerVisible = false;

//Build an array that stores the background colours. In Safari, these colours will be set on the scroller div to essentially hide the scrollbars that Safari leaves in place.
var bgcolours = Array();
bgcolours["green"] = '#028067';
bgcolours["jumpstart"] = '#423325';
bgcolours["everyone"] = '#B14F80';
bgcolours["hightech"] = '#165293';
bgcolours["contact"] = '#4F3C8B';

window.onload = function() {
	//First, check top make sure the proper copy div actually exists.
	if (document.getElementById('copy')) {
		//Reset the ovewflow property of the copy layer to none because we're handling the scrolling with Javascript.
		document.getElementById('copy').style.overflow = 'hidden';

		checkScrolling();
		
		checkScrollInterval = setInterval('checkScrolling();', 100);
	}
}

function checkScrolling() {
	//Check to see if the copy layer has more content than is currently viewable.
	if (parseInt(document.getElementById('copy').offsetHeight) < parseInt(document.getElementById('copy').scrollHeight)) {
		if (!scrollerVisible) insertScroller();
	}
	else {
		if (scrollerVisible) hideScroller();
	}
}

function insertScroller() {
	if (!scrollerHTMLadded) {

		var scrollObj = document.createElement('div');
		scrollObj.id = "scroller";
		
		var scrollUpObj = document.createElement('a');
		scrollUpObj.id = "scrollup";
		scrollUpObj.title = "Scroll Up";
		scrollUpObj.innerText = "Scroll Up";
		scrollUpObj.href = '#';
		scrollUpObj.onclick = function(){return false};
		scrollUpObj.onmouseover = scrollUp;
		scrollUpObj.onmouseout = stopScrolling;
		
		var scrollDownObj = document.createElement('a');
		scrollDownObj.id = "scrolldown";
		scrollDownObj.title = "Scroll Down";
		scrollDownObj.innerText = "Scroll Down";
		scrollDownObj.href = '#';
		scrollDownObj.onclick = function(){return false};
		scrollDownObj.onmouseover = scrollDown;
		scrollDownObj.onmouseout = stopScrolling;

		scrollObj.appendChild(scrollUpObj);
		scrollObj.appendChild(scrollDownObj);		
		
		document.getElementById('content').appendChild(scrollObj);

		scrollerHTMLadded = true;		
	}
	else {
		document.getElementById('scroller').style.display = "block";
	}
	
	//Modify the margin for the copy div to "make room" for the scroll layer.
	document.getElementById('copy').style.marginRight = '42px';

	//Becasue Safari can't dynamically change the overflow property on a layer, we'll need to physically place the scroller on top of the scrollbars that Safari leaves in place.
	if (String(navigator.userAgent).indexOf('Safari') > -1) {
		document.getElementById('copy').style.marginRight = '26px';
		document.getElementById('copy').style.paddingRight = '26px';
		
		//Set the background colour of the scroller div based on the class name of the parent element (the Content div)
		document.getElementById('scroller').style.backgroundColor = bgcolours[document.getElementById('copy').parentNode.className];
		
		//Reset the height and positioning of the scroller to cover the scrollbars.
		document.getElementById('scroller').style.height = document.getElementById('copy').offsetHeight+'px';
		document.getElementById('scroller').style.bottom = 0;
	}
	
	scrollerVisible = true;
	
	checkScrollPosition();
}

function hideScroller() {
	if (scrollerHTMLadded) {
		document.getElementById('scroller').style.display = "none";
		
		//Reset the copy width.
		document.getElementById('copy').style.marginRight = '0';

		if (String(navigator.userAgent).indexOf('Safari') > -1) {
			document.getElementById('copy').style.paddingRight = '8px';
		}
		
		//make sure that the scrolltop position is set to 0 so that we don't lose the text...
		document.getElementById('copy').scrollTop = 0;
	}
	
	scrollerVisible = false;
}

function checkScrollPosition() {
	//Check to see where the current scroll position is. Show/Hide the scroll up/down arrows if we're at the top/bottom.
	if (document.getElementById('copy').scrollTop == 0) {
		//hide the scroll up arrow
		document.getElementById('scrollup').style.display = 'none';
	}
	else {
		//show the scroll up arrow
		document.getElementById('scrollup').style.display = 'block';
	}

	if (document.getElementById('copy').scrollTop == (document.getElementById('copy').scrollHeight - document.getElementById('copy').offsetHeight)) {
		//hide the scroll down arrow
		document.getElementById('scrolldown').style.display = 'none';
	}
	else {
		//show the scroll down arrow
		document.getElementById('scrolldown').style.display = 'block';
	}
}

function scrollDown() {
	if (document.getElementById('copy').scrollTop != (document.getElementById('copy').scrollHeight - document.getElementById('copy').offsetHeight)) {
		document.getElementById('copy').scrollTop = parseInt(document.getElementById('copy').scrollTop) + 3;
		
		checkScrollPosition();
		
		scrollTimer = setTimeout('scrollDown();', 40);
	}
	else {
		stopScrolling();
	}
}

function scrollUp() {
	if (document.getElementById('copy').scrollTop != 0) {
		document.getElementById('copy').scrollTop = parseInt(document.getElementById('copy').scrollTop) - 3;
		
		checkScrollPosition();
		
		scrollTimer = setTimeout('scrollUp();', 40);
	}
	else {
		stopScrolling();
	}
}

function stopScrolling() {
	clearTimeout(scrollTimer);
}







