/*Jules Gilson javascript scrolling functions copyright EightBall360
inputs required:
var scrollingDiv = "id of div to scroll";
var scrollWhenOver = "id of the highest level container to be over to allow scrolling";
	will check up through the dom to see if element over when scrolling is permitted to scroll
var scrollerContainer = "id of the container the whole scroller fits in for visibility changes";
var scrollHandleID = "id of the scroller handle that will move and can be dragged";
var scrollerTrackLength = pixel length of the scroller track background;
var scrollerHandleHeight = pixel height of the scrolling handle;
var scrollViewPortHeight = pixel height of the content div viewport;
*/
var scrollingDiv = "contentContent";
var scrollWhenOver = "contentContentContainer";
var scrollerContainer = "scrollerContainer";
var scrollHandleID = "scrollerHandle";
var scrollerTrackLength = 230;
var scrollerHandleHeight = 26;
var scrollViewPortHeight = 275;

// set up vars
var handleID;
var mouseStY = 0;
var handleYpos = 0;
var scrollRepeat = null;

var scrollerMvDist = scrollerTrackLength-scrollerHandleHeight;

//helper funcs
function E(el) {
	return document.getElementById(el);	
}

function jg_scrolling_vis_change(el,toState){
	var eMent = E(el);
	eMent.style.visibility = toState;
}

function jg_el_check(el,chkEl) {
	if (el.id == chkEl)
	{
		return true;
	}
	else
	{
		var loopEl = el;
		while (!loopEl.tagName || (loopEl.tagName.toLowerCase() != "body" && loopEl.tagName.toLowerCase() != "html")) // loop up through dom until get to body tag - skipping any with no tagName
		{
			loopEl = loopEl.parentNode;
			if (loopEl.id == chkEl)
			{
				return true;
			}
		}
	}
	return false;
}

function jg_handleDown(e) {
	if (e == null)
	{
	 	var e = window.event; // populate e if IE
	}
	var evEl = e.target != null ? e.target : e.srcElement; // create a func var containing event element
	if ((e.button == 1 && window.event != null || e.button == 0) && jg_el_check(evEl,scrollHandleID)) // if left mouse button down - 1 for IE, 0 else
	{
		mouseStY = e.clientY; //get the mouse Y position from the event position
		handleYpos = (isNaN(parseInt(evEl.style.top)) || parseInt(evEl.style.top) == null) ? 0 : parseInt(evEl.style.top); // assign the current handle pos to a var - check if a number or set to zero
		handleID = evEl; //store the handles ID in the global var to pass to functions
		document.onmousemove = jg_handleDrag; // call the drag function when mouse is moved
		document.body.focus(); //move focus to the body for below
		document.onselectstart = function () { return false; }; //prevent any selections being made from the drag in IE
		return false; // prevent any selections on other browsers
	}
}

function jg_handleDrag(e) {
	if (e == null)
	{
		var e = window.event; // populate e if IE
	}
	if (handleYpos + e.clientY - mouseStY >= scrollerMvDist)
	{
		handleID.style.top = scrollerMvDist+'px'; //if handle start pos + mouse move offset is greater than max of scroller - set to max
	}
	else if (handleYpos + e.clientY - mouseStY <= 0)
	{
		handleID.style.top = '0px'; //if handle start pos + mouse move offset is less than 0 - set to 0
	}
	else
	{
		handleID.style.top = (handleYpos + e.clientY - mouseStY) + 'px'; // else just apply the mouse offset to the handle position
	}
	// perform the movement of the content div
	var hiddenHeight = E(scrollingDiv).offsetHeight - scrollViewPortHeight; // get the height of the content not showing
	var scrollToPos = (hiddenHeight/100) * (parseInt((parseInt(handleID.style.top)/scrollerMvDist)*100)) //scroll the content the same %age as the scroller handle position
	E(scrollingDiv).style.top = (isNaN(parseInt(scrollToPos)) || parseInt(scrollToPos) == null) ? '0px' : '-' + parseInt(scrollToPos) + 'px';
}

function jg_handleUp() {
	// check if handle was being dragged
	if (handleID != null)
	{
		document.onmousemove = null; // remove mousemove handler set
		document.onselectstart = null; // remove onselectstart to give back selections
		handleID = null; // clear the procedure
	}
}

function jg_arrowScrollStart(direction) {
	var currentScrollPos  = E(scrollingDiv).offsetTop;
	var contentHeight = E(scrollingDiv).offsetHeight - scrollViewPortHeight;
	contentHeight = -contentHeight;
	if (direction == 'up' && currentScrollPos < 0)
	{
		if (currentScrollPos+10 > 0)
		{
			E(scrollingDiv).style.top = '0px';
		}
		else
		{
			E(scrollingDiv).style.top = (currentScrollPos+10) + 'px';
		}
	}
	if (direction == 'down' && currentScrollPos > contentHeight)
	{
		if (currentScrollPos-10 < contentHeight)
		{
			E(scrollingDiv).style.top = contentHeight + 'px';
		}
		else
		{
			E(scrollingDiv).style.top = (currentScrollPos-10) + 'px';
		}
	}
	// set the scrolling handle position
	// get the percentage of the content scrolled using Math.abs to convert to absolute (positive) numbers
	var newScrollPercent = parseInt((Math.abs(E(scrollingDiv).offsetTop) / Math.abs(contentHeight)) *100);
	E(scrollHandleID).style.top = parseInt((scrollerMvDist/100)*newScrollPercent) + "px";
	
	// test if at bottom or top and kill the interval and return from function
	if (E(scrollingDiv).offsetTop==0 || E(scrollingDiv).offsetTop == contentHeight)
	{
		clearInterval(scrollRepeat);
		scrollRepeat=null;
		return false;
	}
	// set-up the repeat
	if (scrollRepeat==null)
	{
		scrollRepeat = setInterval("jg_arrowScrollStart('" + direction + "')",50)
	}	
}

function jg_arrowScrollStop() {
	clearInterval(scrollRepeat);
	scrollRepeat=null;
	return false;
}

function jg_mouseScroll(delta) {
	var currentScrollPos  = E(scrollingDiv).offsetTop;
	var contentHeight = E(scrollingDiv).offsetHeight - scrollViewPortHeight;
	contentHeight = -contentHeight;
	if (delta < 0)
		if (currentScrollPos-20 < contentHeight)
		{
			E(scrollingDiv).style.top = contentHeight + 'px';
		}
		else
		{
			E(scrollingDiv).style.top = (currentScrollPos-10) + 'px';
		}
	else
		if (currentScrollPos+20 > 0)
		{
			E(scrollingDiv).style.top = '0px';
		}
		else
		{
			E(scrollingDiv).style.top = (currentScrollPos+10) + 'px';
		}
	// set the scrolling handle position
	// get the percentage of the content scrolled using Math.abs to convert to absolute (positive) numbers
	var newScrollPercent = parseInt((Math.abs(E(scrollingDiv).offsetTop) / Math.abs(contentHeight)) *100);
	E(scrollHandleID).style.top = parseInt((scrollerMvDist/100)*newScrollPercent) + "px";
}

function jg_mWheelCapture(e){
	var delta = 0;
	if (e == null)
	{
	 	var e = window.event; // populate e if IE
	}
	var localHandleID = e.target != null ? e.target : e.srcElement; // create a func var containing handle element - so that other events not effected
	if (jg_el_check(localHandleID,scrollWhenOver)) //check we are over content
	{
		if (e.wheelDelta) //ie & opera event data representing wheel movement
		{
			delta = e.wheelDelta/120; // should provide +1 or -1
			if (window.opera) delta = -delta; // opera reverses sign
		} 
		else if (e.detail) // FF wheel data
		{
			delta = -e.detail/3;
		}		
		if (delta)
		{
			jg_mouseScroll(delta); // do the scroll
			// prevent default event handler
			if (e.preventDefault) // for browsers that support w3c method
			{
				e.preventDefault();
			}
			e.returnValue = false; //for everything else
		}
	}
}

function jg_scroller_init() {
	if(E(scrollingDiv).offsetHeight>scrollViewPortHeight)
	{
		jg_scrolling_vis_change(scrollerContainer,"visible");
		// set up custom event handlers
		document.onmousedown = jg_handleDown;
		document.onmouseup = jg_handleUp;
		if (window.addEventListener)
		{
			window.addEventListener('DOMMouseScroll', jg_mWheelCapture, false);
		}	
		window.onmousewheel = document.onmousewheel = jg_mWheelCapture;
	}
	else
	{
		jg_scrolling_vis_change(scrollerContainer,"hidden");
		// remove custom event handlers
		document.onmousedown = null;
		document.onmouseup = null;
		if (window.removeEventListener)
		{
			window.removeEventListener('DOMMouseScroll', jg_mWheelCapture, false);
		}	
		window.onmousewheel = document.onmousewheel = null;
	}
}