//
// by Gatada Games
//

var scrollDelay = 0; // Should the menu initially be hidden?
var menuShowing = 0; // Is the menu is initially hidden?
var offsetHeight = 120; // The height of the menu to slide into view

// Shared animation array: Animation are synchronous - only one at a time
var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null, locked:0};

/* BODY SCROLLING: To show/hide the menu -- vertical animation only
------------------------------------------------------------------------------------- */  

// Called by onload() for a delayed hiding animation.
function delayedBodyScroll(wait)
{	if(scrollDelay == 1)
	{	scrollanim.timer = setInterval("scrollBody();", wait);
		scrollDelay = -1; // -1 causes timer to be deleted
	}
}

// Scrolls the entire body of the page up/down
function scrollBody()
{	
	if(scrollanim.locked == 1)
	{	return; // Do nothing
	}

	if(scrollDelay == -1)
	{	clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		scrollDelay = 0;
	}
	
	var menuButton = document.getElementById("showHideMenu");
	
	if(menuShowing == 0)
	{	if(menuButton.className == "hidden")
		{
			menuButton.className = "showing";
	
			scrollanim.begin = 0;
			scrollanim.change = -offsetHeight;

			document.getElementById("menu").style.display = "block";
			document.getElementById("menu").style.visibility = "visible";
		}
		else
		{
			menuButton.className = "hidden";
	
			scrollanim.begin = -offsetHeight;
			scrollanim.change = offsetHeight;
		}
	}
	else
	{	if(menuButton.className == "showing")
		{	menuButton.className = "hidden";
	
			scrollanim.begin = 0;
			scrollanim.change = -offsetHeight;

			document.getElementById("menu").style.display = "block";
			document.getElementById("menu").style.visibility = "visible";
		}
		else
		{	menuButton.className = "showing";
	
			scrollanim.begin = -offsetHeight;
			scrollanim.change = offsetHeight;
		}
	}
	
	theScroll = document.body;

	scrollanim.time = 0;
	scrollanim.duration = 25;
	scrollanim.element = document.body;	
    
    scrollanim.timer = setInterval("bodyAnim();", 15);
	scrollanim.locked = 1; // avoiding animation interruption
	
	return false;   
}

// Animation using utility functions
function bodyAnim()
{	
	if (scrollanim.time > scrollanim.duration)
	{	// Stop timer, prevent further scrolling
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		scrollanim.locked = 0; // Ready for next anim
		
		if(document.getElementById("showHideMenu").className == "hidden")
		{	document.getElementById("menu").style.display = "none";
			document.getElementById("menu").style.visibility = "hidden";		
		}	
	}
	else
	{	move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);

		document.body.style.marginTop = move + "px";
		document.getElementById("menu").style.marginTop = move + "px";

		scrollanim.time++;
	}
}


/* UTILITY FUNCTIONS
// ------------------------------------------------------------------------------------ */
// Math functions for animation calucations, thanks to http://www.robertpenner.com/easing/

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

// ----------------------------------------------------------------------------------- */

console.log("Scroller initialized");