// Made by geeeet@ghtml.com
// Keep these two lines and you're free to use this code

// Other adjustments and additions by lbarton_98@yahoo.com, 03-14-03 and then 03-17-04

// Browser sensing
var ie4 = document.all ? true:false;
var dom = document.getElementById ? true:false;
var nn4 = false;

// Mouse position per onClick()
var mouseY;
var mouseX;
var direction = 1;	// 1 is next, 0 is prev

// Mouse clicks relative to arrows
var clickUp      = false; 
var clickDown    = false;
var clickLeft    = false;
var clickRight   = false;

// Mouse clicks relative to drag
var clickDrag    = false;

// Mouse clicks relative to scrollbar
var clickAbove   = false;
var clickBelow   = false;
var clickToLeft  = false;
var clickToRight = false;


// Controller for loops and mouseUp
var notLooping = true;
var counter = 0;


// Hit detection bools
var x_within_target = false;
var y_within_target = false;

// Hit detection vars
var xRight = 0;
var widthRight = 0;
var widthLeft = 0;
var scrollRoom = 0;
var overRight = 0;

// Physical property vars
var upH     = 18;   		// Height of up-arrow
var upW     = 12;  		 	// Width of up-arrow
var downH   = 18; 			// Height of down-arrow
var downW   = 12; 			// Width of down-arrow

var leftH   = 18; 			// Height of left-arrow
var leftW   = 12;  			// Width of left-arrow
var rightH  = 18; 			// Height of right-arrow
var rightW  = 12; 			// Width of right-arrow

var dragH   = 18; 			// Height of scrollbar
var dragW   = 60; 			// Width of scrollbar

var scrollH = 12; 			// Height of scrollbar
var scrollW = 586; 			// Horizontal width of scroll pane (182)

var speed   = 10; 			// Scroll speed
var loopTime = 20;			// Loop time for animation

var pageHMax = 0;			// Total horizontal "pages" in in one page
var pageHNum = 1;			// Current horizontal "page" in view
var nextSection = 2;		// Section counter

var contentClipW = 586;		// Hard code the width of the window pane


var storyL = 0;             // Left position of story div
var storyT = 0;             // Top position of story div

// Offsets from top left corner of pageWrapper
var offsetWrapperX = 202;
var offsetWrapperY = 127;

// Keep page counts and contentL in sync
//var contentPosArr = new Array (0, 0, -776, -1580, -2370, -3160, -3950, -4740, -5530, -6320, -7110, -7900, -8690);
var contentPosArr = new Array();
contentPosArr[1]  = new Array(0, 0, -588, -1176, -1764, -2352, -2936, -3524);


// Physical positioning vars
var upL;    				// Up-arrow X
var upT;    				// Up-arrow Y
var downL;  				// Down-arrow X
var downT;  				// Down-arrow Y

var leftL;  				// Left-arrow X
var leftT;  				// Left-arrow Y
var rightL; 				// Right-arrow X
var rightT; 				// Right-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 thisContentH = 289;		// Mac IE workaround to hardcode value

var contentL; 				// Content layer X
var contentW; 				// Content width

var nextContentL;			// Calculated value for the next x-pos of the content div

var scrollLengthV; 			// Number of pixels vertical scrollbar should move
var scrollLengthH; 			// Number of pixels horizontal scrollbar should move

var startY; 				// Keeps track of offset between mouse and span
var startX; 				// Keeps track of offset between mouse and span


// Timer vars
var timer = setTimeout("",500); 	// Repeat variable
var looper = setTimeout("",500); 	// Loop variable
var timer_0 = setTimeout ("", 1);   // Preload variable


function floorNum()
{
	contentL = Math.round(contentL);
	dragL = Math.round(dragL);
}


// Stop everything upon mouseUp
function up()
{

	//alert ("dragL, dragLact, contentL = " + dragL + ", " + document.all.drag.style.left + ", " + contentL);

	if (notLooping)
	{	
		clearTimeout(timer);
		clearTimeout(looper);

		clickUp      = false;
		clickDown    = false;
		clickLeft    = false;
		clickRight   = false;

		clickDrag    = false;

		clickAbove   = false;
		clickBelow   = false;
		clickToLeft  = false;
		clickToRight = false;
	
		x_within_target = false;
		y_within_target = false
	}

	return true;
}



// Mouse detection
function getMouse(e)
{
	if (ie4)
	{
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if (nn4 || dom)
	{
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
    
    // Adjust for position element that is not absolute, based on top 0, left 0
    mouseX = mouseX - offsetWrapperX;
    mouseY = mouseY - offsetWrapperY;
}



// Hit detection
function hitTarget (mouseX, mouseY, minX, minY, targetWidth, targetHeight)
{

    // Determine where the top-left corner of story is
   // getCoordsStory();
    //alert ("story_x, story_y = " + storyL + ", " + storyT);
    

    //alert ("mouse_x, mouse_y = " + mouseX + ", " + mouseY);
	x_within_target = ((mouseX >= minX) && (mouseX <= (minX + targetWidth)));
	y_within_target = ((mouseY >= minY) && (mouseY <= (minY + targetHeight)));
		
	return ((x_within_target) && (y_within_target))
}


// HELPER FUNCTION
function resetParams()
{
	if (ie4)
	{
		contentL = document.all.content.style.pixelLeft;
		dragL = document.all.drag.style.pixelLeft;
	}
	else if (nn4)
	{
		contentL = document.content.left;
		dragL = document.drag.left;
	}
	else if (dom)
	{
		contentL = parseInt(document.getElementById('content').style.left);
		dragL = parseInt(document.getElementById('drag').style.left);
	}

}


// Moves the layer
function moveTo()
{
	// If numbers are bad, reset them
	if (isNaN(contentL) || isNaN(dragL))
	{
		alert();
		
		resetParams();
		
		notLooping = true;
		
		// Reset timers
		clearTimeout(timer);
		clearTimeout(looper);
	}


	floorNum();

	
	if (ie4)
	{
		document.all.content.style.left = contentL;
		document.all.ruler.style.left = dragL;
		document.all.drag.style.left = dragL;
			
		document.all.content.style.top = contentT;
		document.all.ruler.style.top = dragT;
		document.all.drag.style.top = dragT;
	}
	else if (nn4)
	{
		document.content.left = contentL;
		document.ruler.left = dragL;
		document.drag.left = dragL;
			
		document.content.top = contentT;
		document.ruler.top = dragT;
		document.drag.top = dragT;
	}
	else if (dom)
	{
		document.getElementById('content').style.left = contentL + "px";
		document.getElementById('drag').style.left = dragL + "px";
		document.getElementById('ruler').style.left = dragL + "px";
		
		document.getElementById('content').style.top = contentT + "px";
		document.getElementById('drag').style.top = dragT + "px";
		document.getElementById('ruler').style.top = dragT + "px";
	}	
}



// Drag function
function move(e)
{
	if ((clickDrag) && (contentL <= 0))
	{
		getMouse(e);
		dragL = (mouseX - startX);
		
		if (dragL < rulerL) dragL = rulerL;		
		
		if (dragL > (rulerL + scrollW - dragW)) dragL = (rulerL + scrollW - dragW);
		
		contentL = ((dragL - rulerL)*(1/scrollLengthH));
		contentL = eval('-' + contentL);

		moveTo();
		
		// So ie-pc doesn't select gifs
		if (ie4) return false;
	}
}


// Reads content layer left and top
function getCoords()
{
	if (ie4)
	{
		contentL = document.all.content.style.pixelLeft;
		contentT = document.all.content.style.pixelTop;
	}
	else if (nn4)
	{
		contentL = document.content.left;
		contentT = document.content.top;
	}
	else if (dom)
	{
		contentL = parseInt(document.getElementById('content').style.left);
		contentT = parseInt(document.getElementById('content').style.top);
	}
    
    //alert ("contentL, contentT = " + contentL + ", " + contentT);
}

// Reads content layer left and top
function getCoordsStory()
{
    if (ie4)
    {
        storyL = document.all.story.style.pixelLeft + document.body.scrollLeft;
        storyT = document.all.story.style.pixelTop + document.body.scrollTop;
    }
    else if (nn4)
    {
        storyL = document.story.left + document.body.scrollLeft;
        storyT = document.story.top + document.body.scrollTop;
    }
    else if (dom)
    {
        storyL = parseInt(document.getElementById('story').style.left) + document.body.scrollLeft;
        storyT = parseInt(document.getElementById('story').style.top) + document.body.scrollTop;
    }
    
    //alert ("storyL, storyT = " + storyL + ", " + storyT);
    
    
}



// Move right one unit
function scrollRight()
{
	getCoords();
	
	if (clickRight) {if (dragL >= (mouseX-(dragW/2))) return up();}
	
	overRight = contentW - contentClipW;
	scrollRoom = rulerL + scrollW - dragW;
	
	if (clickRight)
	{
		if (contentL < overRight)
		{		
			dragL = dragL + (speed*scrollLengthH);
			if (dragL > scrollRoom) 
			{				
				dragL = scrollRoom;
			}
			
			contentL = contentL - speed;
			if (contentL < -(overRight))
			{
				contentL = -(overRight);
			}
			
			moveTo();
			timer = setTimeout("scrollRight()",5);
		}
	}
	
	return false;	
}


// Move left one unit
function scrollLeft()
{
	getCoords();
	
	if (clickLeft) {if (dragL <= (mouseX-(dragW/2))) return up();}
		
	if (clickLeft)
	{
		if (contentL < 0)
		{				
			dragL = dragL - (speed*scrollLengthH);
			if (dragL < rulerL) dragL = rulerL;
			
			contentL = contentL + speed;
			if (contentL > 0) contentL = 0;
			
			moveTo();
			timer = setTimeout("scrollLeft()",5);
		}
	}
	
	return false;
}

function prepMove()
{
	// Reset timers
	clearTimeout(timer);
	clearTimeout(looper);
	
	// Clean numbers
	floorNum();

	// Determine current page number per contentL and pageHMax
    pageHMax = Math.floor(contentW / scrollW);
	pageHNum = Math.floor((Math.abs(contentL) / scrollW)) + 1;	
}

// Determine animation parameters going to next page
function nextH()
{
	direction = 1;
	prepMove();
	
	if (pageHNum < pageHMax)
	{
		var nextPage = pageHNum + 1;
		temp_next_page = nextPage;
		
		// If already in an animation, increment to the nextPage to allow successive loops
		if ((notLooping == false) && (nextPage < pageHMax))	{nextPage++}
		nextContentL = contentPosArr[this_page][nextPage];					
		//if (test_form == 1) do_watch();	
		
		// Trigger the animation
		clickRight = true;
		notLooping = false;
		quad_init (loopTime, contentL, nextContentL, pageHNum, nextPage, pageHMax, direction);
	}
}

// Determine animation parameters going to prev page
function prevH()
{
	direction = 0;
	prepMove();
	
	//if (pageHNum > 1)	
	if (contentL < 0)
	{
		// Check to see if in the middle of a page
		if (contentL < contentPosArr[this_page][pageHNum]) {var nextPage = pageHNum;}
		else if (contentL == contentPosArr[this_page][pageHNum]) {var nextPage = pageHNum - 1;}
		
		temp_next_page = nextPage;
		
		// If already in an animation, increment to the nextPage to allow successive loops
		if ((notLooping == false) && (nextPage > 1)) {nextPage--}
		
		nextContentL = contentPosArr[this_page][nextPage];
		
		// Testing
		var string1 = 	"<p class=\"tableText\">from prev :: " + 
						loopTime + 	", " + 
	 					contentL + 	", " + 
						nextContentL + 	", " + 
						pageHNum + 	", " + 
						nextPage + 	", " + 
						pageHMax + 	", " + 
						"0</p>";
					
		//if (test_form == 1) do_watch();	
		
		// Trigger the animation
		clickLeft = true;
		notLooping = false;
		quad_init (loopTime, contentL, nextContentL, pageHNum, nextPage, pageHMax, direction);
	}

}



var hide  = true;

function showhide(obj)
{
    var x = document.getElementById('testP');
    hide = !hide;
    x.style.visibility = (hide) ? 'hidden' : 'visible';
    setLyr(obj,'testP');
}

function setLyr(obj,lyr)
{
    var coors = findPos(obj);
    if (lyr == 'testP') coors[1] -= 50;
    var x = document.getElementById(lyr);
    x.style.top = coors[1] + 'px';
    x.style.left = coors[0] + 'px';
}

function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}




// Target hit detections determinations
function down(e)
{
	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	
	// Find the mouse
	getMouse(e);
	
	// Determine offsets from mouse to object
	startY = (mouseY - dragT);
	startX = (mouseX - dragL);
	
	// Set hit detection ranges
	xRight = dragL + dragW;
	widthRight = rightL - xRight;
	widthLeft = dragL - rulerL;
	clickFast = false;
	
   //alert ((mouseX - offsetWrapperX)  + "," + (mouseY - offsetWrapperY) + "," + dragL + "," + dragT + "," +  dragW + "," + dragH);    
    
	// Perform hit detections --- (mouseX, mouseY, minX, minY, targetWidth, targetHeight)
	if (hitTarget (mouseX, mouseY, leftL, leftT, leftW, leftH))
	{
		// Hit left arrow (this is actually hidden for this piece -- it's not the prev button)
		prevH()
	}	
	else if (hitTarget (mouseX, mouseY, rightL, rightT, rightW, rightH))
	{
		// Hit right arrow (this is actually hidden for this piece -- it's not the prev button)
		nextH()
	}
	else if (hitTarget (mouseX, mouseY, 0, 275, 15, 15))
	{
		// Hit prev arrow
		prevH()
	}	
	else if (hitTarget (mouseX, mouseY, 567, 275, 15, 15))
	{
		// Hit next arrow
		nextH()
	}
	else if (hitTarget (mouseX, mouseY, dragL, dragT, dragW, dragH))
	{
		// Hit drag
		clearTimeout(looper);
		notLooping = true;
		clickDrag = true;
		return false;
        
       
	}
	else if (hitTarget (mouseX, mouseY, xRight, rulerT, widthRight, dragH))
	{
		// Hit right of drag
		nextH()
	}
	else if (hitTarget (mouseX, mouseY, rulerL, rulerT, widthLeft, dragH))
	{
		// Hit left of drag
		prevH()
	}
	else
	{
		// Nothing was hit so no scrolling
		return true;
	}
    
   //alert (mouseX + "," + mouseY + "," + dragL + "," + dragT + "," +  dragW + "," + dragH);
}





// Property functions
function getProps ()
{
    temp_arr = new Array();
    temp_arr = findPos("pageWrapper");
   // alert ("top, left :: " + temp_arr[1] + ", " + temp_arr[0]);

	// Get property values for horizontal scrolling
	if (ie4)
	{
		// Left-arrow X and Y variables
    		leftL = document.all.left.style.pixelLeft;
		leftT = document.all.left.style.pixelTop;		

		// Right-arrow X and Y variables
		rightL = document.all.right.style.pixelLeft;
		rightT = document.all.right.style.pixelTop;
		
		// Hard code to avoid a bug
		document.all.drag.style.pixelLeft = 0;
		document.all.drag.style.pixelTop = 259;
		document.all.ruler.style.pixelLeft = document.all.drag.style.pixelLeft;
		document.all.ruler.style.pixelTop = document.all.drag.style.pixelTop;

		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft;
		dragT = document.all.drag.style.pixelTop;		

		// Ruler X and Y variables
		rulerL = document.all.ruler.style.pixelLeft;
		rulerT = document.all.ruler.style.pixelTop;
		
		// Content Left and Top properties
		contentL = document.all.content.style.pixelLeft;
		contentT = document.all.content.style.pixelTop;
		
		// Temporary Workaround for Mac IE
		if ((is_ie) && (is_mac))
		{
			contentH = thisContentH;
			contentW = this_content_w;
		}
		else
		{
			contentH = parseInt(document.all.content.scrollHeight);
			contentW = parseInt(document.all.content.scrollWidth);
			//alert (contentW);
		}
	}
	else if (nn4)
	{
		// Left-arrow X and Y variables
		leftL = document.left.left;
		leftT = document.left.top;

		// Right-arrow X and Y variables
		rightL = document.right.left;
		rightT = document.right.top;
		
		// Hard code to avoid a bug
		document.drag.left = 0;
		document.drag.top = 259;		
		document.ruler.left = document.drag.left;
		document.ruler.top = document.drag.top;
		
		// Scrollbar X and Y variables
		dragL = document.drag.left;
		dragT = document.drag.top;
			
		// Ruler X and Y variables
		rulerL = document.ruler.left;
		rulerT = document.ruler.top;
		
		// Content Left and Top properties
		contentL = document.content.left;
		contentT = document.content.top;
		
		// Content Width and Height variables (add a margin for the width)
		contentH = document.content.clip.bottom;
		contentW = document.content.clip.right;
	}
	else if (dom)
	{	
		// Left-arrow X and Y variables
		leftL = parseInt(document.getElementById('left').style.left);
		leftT = parseInt(document.getElementById('left').style.top);
	
		// Right-arrow X and Y variables
		rightL = parseInt(document.getElementById('right').style.left);
		rightT = parseInt(document.getElementById('right').style.top);
		
		// Hard code to avoid a bug
		document.getElementById('drag').style.left = 0 + "px";
		document.getElementById('drag').style.top = 259 + "px";		
		document.getElementById('ruler').style.left = document.getElementById('drag').style.left;
		document.getElementById('ruler').style.top = document.getElementById('drag').style.top;
				
		// Scrollbar X and Y variables    
		dragL = parseInt(document.getElementById('drag').style.left);
		dragT = parseInt(document.getElementById('drag').style.top);
  	
        
		// Ruler X and Y variables
		rulerL = parseInt(document.getElementById('ruler').style.left);
		rulerT = parseInt(document.getElementById('ruler').style.top);
		
		// Content Left and Top properties
		contentL = parseInt(document.getElementById('content').style.left);
		contentT = parseInt(document.getElementById('content').style.top);
		
		// Content Width and Height variables (add a margin for the width)
		contentH = parseInt(document.getElementById('content').offsetHeight);
		contentW = parseInt(document.getElementById('content').offsetWidth);
		
		// Conversions
		document.getElementById('content').style.top = 0  + "px";
		document.getElementById('content').style.left = 0  + "px";
	}
	
	// Click (Hit Target) range adjustments
	leftL = rulerL;
	rightL = rulerL + 586;
	
	// Number of pixels scrollbar should move horizontally dynamically
	scrollLengthH = ((scrollW-dragW)/(contentW-contentClipW));
	

	
	//alert ("dragL, dragT = " + dragL + ", " + dragT);
}


// NN4 Trap
function reloadPage()
{
	location.reload();
}

// NN4 Trap
function initEvents()
{
	if (nn4)
	{
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
}

// Reveal scrolling elements after page has loaded
function show_scrolling_elements()
{	
	if (ie4)
	{
		document.all.imgScrollGutter.style.visibility = 'visible';
		document.all.drag.style.visibility = 'visible';
		document.all.ruler.style.visibility = 'visible';
		
		document.all.navPrev.style.visibility = 'visible';
		document.all.navNext.style.visibility = 'visible';
	}
	else if (nn4)
	{
		document.layers['imgScrollGutter'].visibility = 'visible';	
		document.layers['drag'].visibility = 'visible';
		document.layers['ruler'].visibility = 'visible';
		
		document.layers['navNext'].visibility = 'visible';			
		document.layers['navPrev'].visibility = 'visible';
	}
	else if (dom)
	{
		document.getElementById('imgScrollGutter').style.visibility = 'visible';
		document.getElementById('drag').style.visibility = 'visible';
		document.getElementById('ruler').style.visibility = 'visible';
		
		document.getElementById('navPrev').style.visibility = 'visible';
		document.getElementById('navNext').style.visibility = 'visible';	
	}
}

// Script controller
function scroll_control()
{
	// Get the content properties to control the scrolling math
	getProps();
		
	// Initializes event capturing
	initEvents ();
	
	// Show scrolling elements now that the page has loaded
	show_scrolling_elements();
	
	// Start mouse functions now that scrolling elements are shown
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	
	// Trigger the initial move to initialize page
	moveTo();
	
	// Set notLooping flag
	notLooping = true;
	
	// Show scrolling elements now that the page has loaded
	show_scrolling_elements();
	
	
	//if (test_form == 1) do_watch();

}

function pre_scroll_control()
{
	// Browser detection for DOM behavior -- use a mix of feature sensing and browser version
	var browserVersion = parseInt(navigator.appVersion);
	ie4 = document.all ? true:false;
	dom = document.getElementById ? true:false;
	nn4 = false;
	if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {nn4 = true}
	
	scroll_control();	
}


// Browser detection
var agt       = navigator.userAgent.toLowerCase(); var is_major  = parseInt(navigator.appVersion); var is_minor  = parseFloat(navigator.appVersion); 
var is_nav    = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1) &&
                 (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && 
                 (agt.indexOf('webtv') == -1));
var is_nav4up = (is_nav && (is_major >= 4)); var is_nav5up = (is_nav && (is_major >= 5)); var is_nav6up = (is_nav5up && (agt.indexOf('netscape/7.') != -1));
var is_nn4    = (is_nav4up && !is_nav5up && !is_nav6up); var is_nn6    = (is_nav4up &&  is_nav5up && !is_nav6up);
var is_ie     = (agt.indexOf("msie") != -1); var is_ie3 = (is_ie && (is_major < 4)); var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.")  == -1));
var is_ie4up  = (is_ie && (is_major == 4));  var is_ie5 = (is_ie && (is_major == 4) &&  (agt.indexOf("msie 5.0") != -1)); 
var is_ie5up  = (is_ie && (is_major == 4) && ((agt.indexOf("msie 5.1") != -1) || (agt.indexOf("msie 5.5") != -1))); 
var is_ie6    = (is_ie && (is_major == 4) &&  (agt.indexOf("msie 6.")  != -1));
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5up && !is_ie6);
var is_opera  = (agt.indexOf('opera')  != -1); var is_safari = (agt.indexOf('safari') != -1);
var is_win    = ((agt.indexOf("win") != -1) || (agt.indexOf("16bit")!=-1)); var is_mac    = (agt.indexOf("mac")  != -1);
var is_mac10  = (agt.indexOf("mac os x") != -1)
var is_unix   = ((agt.indexOf("x11") != -1) && !is_win && !is_mac);
var nn4_bug   = false;
