// Written by Jason John Jaeger those functions that are not, are credited.
// Last updated March 14th 2007.
var minHeight = 580;
var speed = 20;//smaller == faster
var increment = 20;//smaller == slower but smoother


var wheelIncrement = 25;//smaller == slower but smoother
var nonWheelSpeed = 20;
var nonWheelIncrement = 25;

var scrollDown_Intervals = Array();
var scrollUp_Intervals = Array();

var shouldScroll = true;

function initialize(){
	hideAllSubMenus();
	initScrollBar();
	
}

function initScrollBar(){
	var scrollContainerObj = getObject('scrollerContainer_1');
	var sliderObj = getObject('slider_1');
	var containerHeight = findObjHeight(scrollContainerObj);
	var sliderHeight = findObjHeight(sliderObj);
	var tolerance = 0;
	var upBtnObj = getObject('upButton_1');
	var downBtnObj = getObject('downButton_1');
	var scrollBarBGobj = getObject('scrollBarBG');
	var scrollHandleObj = getObject('scrollHandle');
	//if(sliderHeight + tolerance > containerHeight){
	if(sliderHeight > containerHeight){
		//show scrollbar
		upBtnObj.style.visibility = 'visible';
		downBtnObj.style.visibility = 'visible';
		scrollBarBGobj.style.visibility = 'visible';
		scrollHandleObj.style.visibility = 'visible';
		shouldScroll = true;
	}else{
		//hide scrollbar
		upBtnObj.style.visibility = 'hidden';
		downBtnObj.style.visibility = 'hidden';
		scrollBarBGobj.style.visibility = 'hidden';
		scrollHandleObj.style.visibility = 'hidden';
		shouldScroll = false;
	}
	
//~~[init scroll handle]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	var percentOf = parseFloat((sliderHeight*100)/containerHeight);
	//alert(percentOf);
	var scrollBarHeight = findObjHeight(scrollBarBGobj);
	//scrollHandleObj.style.height = (scrollBarHeight-60)-(percentOf-100)+'px';//<--Why does 60 work?
	
	
	var percentDiff = 100 -(percentOf - 100);
	//var scrollArea = scrollBarHeight ;//<--32 for the scroll buttons' combined height
	var newHeight = parseFloat((scrollBarHeight * percentDiff)/100);
	
	
	if(newHeight < 20){ 
		//alert('test');
		newHeight = 20; 
	}
	scrollHandleObj.style.height = newHeight+'px';
	
	//scrollHandleObj.style.height = newHeight - ((newHeight * 5.324)/100)+'px';//<--subtracts 5.32% from height
	
	//var overlapHeight = sliderHeight - containerHeight;
	//scrollHandleObj.style.height = scrollBarHeight-overlapHeight-percentDiff+'px';//<--minus 16 for bottom scroll btn height
	
	
	
	
	
}



function onHandleDrag(newHandleTop){
		//~~~~~~~~~~~
		var parentObj = getObject('scrollerContainer_1');
		var scrollHandleObj = getObject('scrollHandle');
		var scrollContainerObj = getObject('scrollerContainer_1');
		var scrollBarBGobj = getObject('scrollBarBG');
		var sliderObj = getThingObj('slider_1');
		
		var sliderHeight = findObjHeight(sliderObj);
		var scrollContainerHeight = findObjHeight(scrollContainerObj);
		var sliderStrokeLength = sliderHeight - scrollContainerHeight;
		var scrollHandleHeight = findObjHeight(scrollHandleObj);
		var scrollBarHeight = findObjHeight(scrollBarBGobj);
		var scrollHandleStrokeLength = scrollBarHeight - scrollHandleHeight;
		
		var handleTop = findPosY(scrollHandleObj);
		var handlePercent = (100*(newHandleTop-16))/scrollHandleStrokeLength;
		
		var newScrollTop = (sliderStrokeLength * -handlePercent)/100;
		
		
		//var upBtnObj = getObject('upButton_1');
		//var downBtnObj = getObject('downButton_1');
		//var upBtnBottom = findPosY(upBtnObj) + findObjHeight(upBtnObj);
		//var downBtnTop = findPosY(downBtnObj);
		var handleBottom = newHandleTop + scrollHandleHeight;
		
		 
		
		if(newHandleTop < 16 ){
			scrollHandleObj.style.top = 16+'px'
			sliderObj.style.top = 0 +'px';
			
		}else if(handleBottom > (scrollBarHeight+16)){
			scrollHandleObj.style.top = scrollBarHeight + 16 - scrollHandleHeight +'px'
			
			var upperOverlap = findPosY(scrollContainerObj) - findPosY(sliderObj);
			sliderObj.style.top = -upperOverlap +'px';
		}else{
			scrollHandleObj.style.top = newHandleTop+'px'
			sliderObj.style.top = newScrollTop  +'px';
		}
		
		//~~~~~~~~~~~
}



/** This is high-level function; REPLACE IT WITH YOUR CODE.
 * It must react to delta being more/less than zero.
 * The scrollwheel code is from http://adomas.org/javascript-mouse-wheel/
 */
function handle(delta) {
	if (delta < 0)
		//scrollDown('slider_1','upButton_1');
		callScrollDown('downButton_1', true);
	else
		//scrollUp('slider_1','upButton_1');
		callScrollUp('upButton_1','container', true);
}

function wheel(event){
	if(shouldScroll){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		if (delta)
			handle(delta);
	        if (event.preventDefault)
	                event.preventDefault();
	        event.returnValue = false;
	}
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;



function clearTheIntervals(){//###########################################################
	//if(scrollDown_Intervals){
		for (x in scrollDown_Intervals){clearInterval(scrollDown_Intervals[x]);	}//end for
	//}//end if
	//if(scrollUp_Intervals){	
		for (x in scrollUp_Intervals){	clearInterval(scrollUp_Intervals[x]);	}//end for
	//}//end if
}//end function

function callScrollUp(button, imageFaderId, wheel){//#########################################################
	if (wheel){ 
		increment= wheelIncrement;
	}else{
		increment= nonWheelIncrement;
	}
	
	clearTheIntervals();
	var buttonId = getThingId(button);
	var buttonObj = getThingObj(button);
	//var sliderId = buttonObj.nextSibling.id;
	var imageFaderObj = getThingObj(imageFaderId);
	var sliderArray = getElementsByClassName(imageFaderObj, "div", "slider");
	var sliderId = getThingId(sliderArray[0]);
	//alert(buttonId+'  '+sliderId);
	if(wheel){
		scrollUp(sliderId, buttonId);
		
	}else{
		scrollDown_Intervals[sliderId] = setInterval("scrollUp('"+sliderId+"','"+buttonId+"')",speed);
	}
}//end function

function callScrollDown(button, wheel){//#######################################################
	if (wheel){ 
		increment= wheelIncrement;
	}else{
		increment= nonWheelIncrement;
	}
	//alert('scroll down called');
	clearTheIntervals();
	var buttonId = getThingId(button);
	var buttonObj = getThingObj(button);
	var sliderId = buttonObj.previousSibling.id;
	if(wheel){
		scrollDown(sliderId,buttonId);
	}else{
		scrollDown_Intervals[sliderId] = setInterval("scrollDown('"+sliderId+"','"+buttonId+"')",speed);
	}
}//end function

function scrollUp(sliderId,buttonId){
	//alert(sliderId);
	var sliderObj = getThingObj(sliderId);
	var buttonObj = getThingObj(buttonId);
	var parentObj = buttonObj.parentNode;
	
	var parentY = findPosY(parentObj);
	var currentButtonY = findPosY(buttonObj);
	var sliderCurrentY = findPosY(sliderObj);
	var buttonHeight = findObjHeight(buttonObj);
	var endY = sliderCurrentY-buttonHeight+increment;
	//alert(parentObj.id);
	//alert(currentButtonX+' || '+sliderCurrentX+' || '+sliderWidth+' || '+endX);
	if(currentButtonY > endY){
		
		sliderObj.style.top = sliderCurrentY-parentY+increment+'px';
		
		//~~~~~~~~~~~
		var scrollHandleObj = getObject('scrollHandle');
		//var scrollHandleCurrentTop = findPosY(scrollHandleObj);
		
		//alert('test');
		var scrollContainerObj = getObject('scrollerContainer_1');
		var sliderHeight = findObjHeight(sliderObj);
		var scrollContainerHeight = findObjHeight(scrollContainerObj);
		var sliderStrokeLength = sliderHeight - scrollContainerHeight;
		var upperOverlap = findPosY(scrollContainerObj) - findPosY(sliderObj);
		var sliderPercent = (100*upperOverlap)/sliderStrokeLength;
		var scrollHandleHeight = findObjHeight(scrollHandleObj);
		var scrollBarBGobj = getObject('scrollBarBG');
		var scrollBarHeight = findObjHeight(scrollBarBGobj);
		var scrollHandleStrokeLength = scrollBarHeight - scrollHandleHeight;
		var newHandleTop = ((scrollHandleStrokeLength * sliderPercent)/100)+16;//<-- plus 16 for the top scroll btn
		if(newHandleTop < nonWheelIncrement ){newHandleTop = 16;}
		scrollHandleObj.style.top = newHandleTop+'px';
		//alert(newHandleTop);
		
		
		//scrollHandleObj.style.top = (scrollHandleCurrentTop-parentY) - (increment)+'px';
		//~~~~~~~~~~~
	}else{	clearTheIntervals();	}//end if
}//end move function

function scrollDown(sliderId,buttonId){//################################################
	var sliderObj = getThingObj(sliderId);
	var buttonObj = getThingObj(buttonId);
	var parentObj = buttonObj.parentNode;
	var parentY = findPosY(parentObj);
	var currentButtonY = findPosY(buttonObj);
	var sliderCurrentY = findPosY(sliderObj);
	var sliderHeight = findObjHeight(sliderObj);
	var endY = sliderCurrentY+sliderHeight-increment ;
	if(currentButtonY < endY){
		sliderObj.style.top = sliderCurrentY-parentY-increment+'px';
		
				//~~~~~~~~~~~
		var scrollHandleObj = getObject('scrollHandle');

		var scrollContainerObj = getObject('scrollerContainer_1');
		var sliderHeight = findObjHeight(sliderObj);
		var scrollContainerHeight = findObjHeight(scrollContainerObj);
		var sliderStrokeLength = sliderHeight - scrollContainerHeight;
		var upperOverlap = findPosY(scrollContainerObj) - findPosY(sliderObj);
		var sliderPercent = (100*upperOverlap)/sliderStrokeLength;
		var scrollHandleHeight = findObjHeight(scrollHandleObj);
		var scrollBarBGobj = getObject('scrollBarBG');
		var scrollBarHeight = findObjHeight(scrollBarBGobj);
		var scrollHandleStrokeLength = scrollBarHeight - scrollHandleHeight;
		var newHandleTop = ((scrollHandleStrokeLength * sliderPercent)/100)+16;//<-- plus 16 for the top scroll btn
		if(newHandleTop < nonWheelIncrement){newHandleTop = 16;}
		if(newHandleTop + scrollHandleHeight > scrollBarHeight){
			
			newHandleTop = scrollHandleStrokeLength +16;
		}
		scrollHandleObj.style.top = newHandleTop+'px';
		
		//~~~~~~~~~~~
		
	}else{	clearTheIntervals();	}//end if
}//end function

function getObject(id,silent){//###############################################################
	if (document.all) {	obj = document.all[id];	}
		else if (document.getElementById && !document.all) {	obj = document.getElementById(id);	} 
	if(silent != "silent"){
		if (!obj) {	alert(id+" is an unrecognized ID");
					return false;	}
	}//end silent if
	return obj;
}//end function

function idOrObj(thing){//#####################################################################
	var whatItIs = null;
	if(getObject(thing,'silent')){
		whatItIs = 'id';
	}else if(thing.attributes.length >0){
	   whatItIs = 'object';
	} else{ return false;	}//end if elseif else statement
	return whatItIs;
}//end function

function getThingObj(thing){//##################################################################
	var obj = null;
	if (idOrObj(thing) == 'id'){
		obj = getObject(thing);
		return obj;
	} else if (idOrObj(thing) == 'object'){
		obj = thing;
		return obj;
	} else {
		//alert('Sorry, but '+thing+' is neither an object nor an id.');
		return false;
	}//end if else if
}//end function

function getThingId(thing){//##################################################################
	//updated Nov 24th 2006 to create and assign a random id if an object does not have one
	var id = null;
	if (idOrObj(thing) == 'id'){
		id = thing;
		return id;
	} else if (idOrObj(thing) == 'object'){
		if(thing.id){
			id = thing.id;
			return id;
		}else{ 	
			//if the object does not have and id, then give it one!
			var randomNumber=Math.floor(Math.random()*100001);
			var newId = null;
			if(thing.className){
				newId = thing.className+'_'+randomNumber;
			} else{ 
				newId = randomNumber;
			}//end if else
			if(newId){thing.id = newId;}//end if
			return newId;
		}//end if else
	} else {
		//alert('Sorry, but '+thing+' is neither an id nor an object.');
		return false;
	}//end if else if
}//end function

function getHeight(thing){//###############################################################
	// Created Nov 24th 2006. Based off of findObjHeight.
	// Improvements over findObjHeight: 
	//   --> Uses new getThingId which assigns a random id to objects with no id!
	//   --> Automatic Self Calibration!
	//   --> Can use either an object or an id for the thing
	var obj = getThingObj(thing);
	var id = getThingId(thing);
	if(obj.offsetHeight){ 
		obj_height = obj.offsetHeight; }
	else if(obj.style.pixelHeight){ 
		obj_height = obj.pixelHeight; 
	}//end if
	//--Calibrate!------------------------	
	if(!getHeightOffsetArray[id]){
		obj.style.height = 10+'px';
		if(obj.offsetHeight){ cal_height = obj.offsetHeight; }
		else if(obj.style.pixelHeight){ cal_height = obj.pixelHeight; } 
		getHeightOffsetArray[id]= 10 - cal_height;
	}//end if
	//--Apply Calibration!-------------
	obj_height = obj_height + getHeightOffsetArray[id];
	obj.style.height = obj_height+'px';
	return obj_height;
}//end function

function getWidth(thing){//###############################################################
	// Created Dec 9th 2006. Based off of getHeight.
	// Improvements over findObjWidth: 
	//   --> Uses new getThingId which assigns a random id to objects with no id!
	//   --> Automatic Self Calibration!
	//   --> Can use either an object or an id for the thing
	var obj = getThingObj(thing);
	var id = getThingId(thing);
	if(obj.offsetWidth){ 
		obj_width = obj.offsetWidth; }
	else if(obj.style.pixelWidth){ 
		obj_width = obj.pixelWidth; 
	}//end if
	//--Calibrate!------------------------	
	if(!getWidthOffsetArray[id]){
		obj.style.width = 10+'px';
		if(obj.offsetWidth){ cal_width = obj.offsetWidth; }
		else if(obj.style.pixelWidth){ cal_width = obj.pixelWidth; } 
		getWidthOffsetArray[id]= 10 - cal_width;
	}//end if
	//--Apply Calibration!-------------
	obj_width = obj_width + getWidthOffsetArray[id];
	obj.style.width = obj_width+'px';
	return obj_width;
}//end function

function findObjHeight(thing){//###############################################################
	if(thing.offsetHeight){
		thing_height = thing.offsetHeight;
	}
	else if(thing.style.pixelHeight){
		thing_height = thing.pixelHeight;
	} 
	return thing_height;
}//end function
	
function findObjWidth(thing){//################################################################
	if(thing.offsetWidth){
		thing_width = thing.offsetWidth;
	}
	else if(thing.style.pixelWidth){
		thing_width = thing.pixelWidth;
	} 
	return thing_width;
}//end function
	
function findPosY(obj)  {//####################################################################
	// by Peter-Paul Koch & Alex Tingle
	// http://blog.firetree.net/2005/07/04/javascript-find-position/
	// http://www.quirksmode.org/js/findpos.html
	var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }//end function
  
function findPosX(obj){//######################################################################
	// by Peter-Paul Koch & Alex Tingle
	// http://blog.firetree.net/2005/07/04/javascript-find-position/
	// http://www.quirksmode.org/js/findpos.html
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }//end function
  
function getElementsByClassName(oElm, strTagName, strClassName){//###############################
	/* this awesome getElementsByClassName function was written by:
	Jonathan Snook, http://www.snook.ca/jonathan
	with add-ons by:
	Robert Nyman, http://www.robertnyman.com
	and be found at http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
	*/
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){arrReturnElements.push(oElement);}//end if 
    }//end for
    return (arrReturnElements);
}//end function