/****NOTES**********************************************************************
 
 This script will scroll the browser window fully in either horizontal direction.
 
*******************************************************************************/

/*******************************************************************************/
steps = 15;
speed = 20;

//flag to tell when menu is moving
inMotion = false;

		function alignMenu(){
			
			//****TEST FOR MENU THAT IS STILL MOVING****************************/
			if(inMotion){return false;}
			
			//****get the difference in states**********************************/
			
			hDiff = 67;
			
			
			//******************************************************************/


			//****test for direction********************************************/
			
			dir = (sansPX(document.getElementById('per_nav_sub').style.top) < 0) ? 'top' : 'bottom';
			
			if(dir == 'top'){
				//swap image to off state
				document.getElementById('per_nav_sub_trigger').src = baseurl+'images/global/pn_more_on.gif';
				//increase teh clip height
				document.getElementById('per_nav_sub_clip').style.height = '69px';
				
				//if the current size is greater than our object, no need to go any further
				if(currentTop() < hDiff){
					//get the scroll on
					b = Math.abs(currentTop());
					t = 0;
					d = steps;
					c = hDiff;
					
					inMotion = true;
					
					scrolling = setInterval(scrollTop, speed);
				}else{
					return false;
				}
			
			}else if(dir == 'bottom'){
				//swap image to off state
				document.getElementById('per_nav_sub_trigger').src = baseurl+'images/global/pn_more.gif';
				
				if(currentTop() > -hDiff){

					//get the scroll on
					b = -(Math.abs(currentTop()));
					t = 0;
					d = steps;
					c = -hDiff;
					
					inMotion = true;
					
					scrolling = setInterval(scrollBottom, speed);
				}else{
					return false;
				}
			
			}else{
				return false;
			}
			
			//******************************************************************/
			
		}
		
		function scrollTop(){
			if(t < d){
				t++;
				withEase = easeInOut(t, b, c, d);
				document.getElementById('per_nav_sub').style.top = withEase +'px';
			}else{
				clearInterval(scrolling);
				//reset position
				document.getElementById('per_nav_sub').style.top = '0px';
				inMotion = false;
			}
		}
		
		function scrollBottom(){
			if(t < d){
				t++;
				withEase = easeInOut(t, b, c, d);
				document.getElementById('per_nav_sub').style.top = withEase +'px';
			}else{
				clearInterval(scrolling);
				//reset position
				document.getElementById('per_nav_sub').style.top = '-'+hDiff+'px';
				//decrease teh clip height
				document.getElementById('per_nav_sub_clip').style.height = '19px';
				inMotion = false;
			}
		}
		
		/****EASING FUNCTION***************************************************/
		
		///////////// SINUSOIDAL EASING: sin(t) ///////////////
		// sinusoidal easing in - accelerating from zero velocity
		// t: current time, b: beginning value, c: change in position, d: duration
		// sinusoidal easing in/out - accelerating until halfway, then decelerating
		function easeInOut (t, b, c, d){
			return Math.ceil(-c/2 * (Math.cos(Math.PI*t/d) - 1) - (b));
		}
		
		/****GET THE CURRENT POSITION******************************************/
		
		function currentTop(){
			var tempTop = document.getElementById('per_nav_sub').style.top;
			if(tempTop == ''){return 0;}
			else{
			var asInt = tempTop.substring(0, tempTop.length-2);
			return asInt;
			}
			
		}
		
		/****GET THE MEASURE SANS PX*******************************************/
		
		function sansPX(str){
			var tempMeasure = str;
			if(tempMeasure == ''){return 0;}
			else {
				if(tempMeasure.substring(tempMeasure.length-2, tempMeasure.length) == 'px'){
					var asInt = tempMeasure.substring(0, tempMeasure.length-2);
					return asInt;
				}else{
					return tempMeasure;
				}
			}
			
		}
		