i = 0;
currentComicPage = 1;
animationLock = false;

function flipPage(thisDirection)
{
	if(!animationLock)
	{
		rightPage = document.getElementById("rightPageDiv");
		leftPage = document.getElementById("leftPageDiv");
		backGround = document.getElementById("backGroundDiv");
		if(thisDirection==1 && currentComicPage < numPages)
		{
			animationLock = true;
			backGround.style.zIndex = 8;
			currentComicPage++;
			leftPage.style.zIndex = 2;
			backGround.style.right = "0";
			backGround.style.left = "";
			backGround.style.backgroundPosition = "top right";
			flipAnimation = window.setInterval("turnPageForward()",10);
		}
		if(thisDirection==0 && currentComicPage > 1)
		{
			animationLock = true;
			backGround.style.zIndex = 8;
			currentComicPage--;
			rightPage.style.zIndex = 2;
			backGround.style.right = "";
			backGround.style.left = "0";
			backGround.style.backgroundPosition = "top left";
			flipAnimation = window.setInterval("turnPageBackward()",10);
		}
		backGround.style.width = "1px";
		backGround.style.backgroundImage = "url(/images/comic/issue"+issueNum+"bg-"+currentComicPage+".jpg)";
	}
}

function turnPageForward()
{
	if(i > 500)
	{
		window.clearInterval(flipAnimation);
		rightPage.style.backgroundImage = "url(/images/comic/issue"+issueNum+"-"+currentComicPage*2+".jpg)";
		rightPage.style.right = "0";
		rightPage.style.width = "553px";
		leftPage.style.backgroundImage = "url(/images/comic/issue"+issueNum+"-"+(currentComicPage*2-1)+".jpg)";
		leftPage.style.zIndex = rightPage.style.zIndex = 4;
		backGround.style.zIndex = 2;
		i = 0;
		animationLock = false;
	}
	else
	{
		rightPage.style.right = backGround.style.width = 1024*(i/500)+"px";
		rightPage.style.top = 9-30*(Math.sin(3.1416*(i/500)))+"px";
		rightPage.style.width = 512-512*(i/500)+"px";
		i += 10;
	}
}

function turnPageBackward()
{
	if(i > 500)
	{
		window.clearInterval(flipAnimation);
		rightPage.style.backgroundImage = "url(/images/comic/issue"+issueNum+"-"+currentComicPage*2+".jpg)";
		leftPage.style.left = "0";
		leftPage.style.width = "553px";
		leftPage.style.backgroundImage = "url(/images/comic/issue"+issueNum+"-"+(currentComicPage*2-1)+".jpg)";
		leftPage.style.zIndex = rightPage.style.zIndex = 4;
		backGround.style.zIndex = 2;
		i = 0;
		animationLock = false;
	}
	else
	{
		leftPage.style.left =	backGround.style.width = 1024*(i/500)+"px";
		leftPage.style.top = 9-30*(Math.sin(3.1416*(i/500)))+"px";
		leftPage.style.width = 512-512*(i/500)+"px";
		i += 10;
	}
}