bookTitles = new Array(5);
imgTable = new Array(5);

bookTitles[0] = "Christmas 2004";

imgTable[0] = new Array(
"Layouts/00Christmas.jpg",
"Layouts/01Christmas.jpg",
"Layouts/02Christmas.jpg",
"Layouts/03Christmas.jpg",
"Layouts/04Christmas.jpg",
"Layouts/05Christmas.jpg",
"Layouts/06Christmas.jpg",
"Layouts/07Christmas.jpg",
"Layouts/08Christmas.jpg",
"Layouts/09Christmas.jpg",
"Layouts/10Christmas.jpg",
"Layouts/11Christmas.jpg",
"Layouts/12Christmas.jpg",
"Layouts/13Christmas.jpg",
"Layouts/14Christmas.jpg",
"Layouts/15Christmas.jpg"
);

bookTitles[1] = "Schoenberger Family 2005";

imgTable[1] = new Array(
"../images/offwhiteback.gif",
"Layouts/01Scho2005.jpg",
"Layouts/02Scho2005.jpg",
"Layouts/03Scho2005.jpg",
"Layouts/04Scho2005.jpg",
"Layouts/05Scho2005.jpg",
"Layouts/06Scho2005.jpg",
"Layouts/07Scho2005.jpg",
"Layouts/08Scho2005.jpg",
"Layouts/09Scho2005.jpg",
"Layouts/10Scho2005.jpg",
"Layouts/11Scho2005.jpg",
"Layouts/12Scho2005.jpg",
"Layouts/13Scho2005.jpg",
"Layouts/14Scho2005.jpg",
"Layouts/15Scho2005.jpg",
"Layouts/16Scho2005.jpg",
"Layouts/17Scho2005.jpg",
"Layouts/18Scho2005.jpg",
"Layouts/19Scho2005.jpg",
"Layouts/20Scho2005.jpg",
"Layouts/21Scho2005.jpg",
"Layouts/22Scho2005.jpg",
"Layouts/23Scho2005.jpg",
"Layouts/24Scho2005.jpg",
"Layouts/25Scho2005.jpg",
"Layouts/26Scho2005.jpg",
"Layouts/27Scho2005.jpg",
"Layouts/28Scho2005.jpg",
"Layouts/29Scho2005.jpg",
"Layouts/30Scho2005.jpg",
"Layouts/31Scho2005.jpg",
"Layouts/32Scho2005.jpg",
"Layouts/33Scho2005.jpg",
"Layouts/34Scho2005.jpg",
"Layouts/35Scho2005.jpg"
);

bookTitles[2] = "John Robert Baby Scrapbook";

imgTable[2] = new Array(
"../images/offwhiteback.gif",
"Layouts/John01.jpg",
"Layouts/John02.jpg",
"Layouts/John03.jpg",
"Layouts/John04.jpg",
"Layouts/John05.jpg",
"Layouts/John06.jpg",
"Layouts/John07.jpg",
"Layouts/John08.jpg",
"Layouts/John09.jpg",
"Layouts/John10.jpg",
"Layouts/John11.jpg",
"Layouts/John12.jpg",
"Layouts/John13.jpg",
"Layouts/John14.jpg",
"Layouts/John15.jpg",
"Layouts/John16.jpg",
"Layouts/John17.jpg",
"Layouts/John18.jpg",
"Layouts/John19.jpg",
"Layouts/John20.jpg",
"Layouts/John21.jpg",
"Layouts/John22.jpg",
"Layouts/John23.jpg",
"Layouts/John24.jpg",
"Layouts/John25.jpg",
"Layouts/John26.jpg",
"Layouts/John27.jpg"
);

bookTitles[3] = "Schoenberger Family 2006";

imgTable[3] = new Array(
"../images/offwhiteback.gif",
"Layouts/2006Scho00.jpg",
"Layouts/2006Scho01.jpg",
"Layouts/2006Scho02.jpg",
"Layouts/2006Scho03.jpg",
"Layouts/2006Scho04.jpg",
"Layouts/2006Scho05.jpg",
"Layouts/2006Scho06.jpg",
"Layouts/2006Scho07.jpg",
"Layouts/2006Scho08.jpg",
"Layouts/2006Scho09.jpg",
"Layouts/2006Scho10.jpg",
"Layouts/2006Scho15.jpg",
"Layouts/2006Scho16.jpg",
"Layouts/2006Scho11.jpg",
"Layouts/2006Scho12.jpg",
"Layouts/2006Scho13.jpg",
"Layouts/2006Scho14.jpg"
);

bookTitles[4] = "Schoenberger Family 2007";

imgTable[4] = new Array(
"../images/offwhiteback.gif",
"Layouts/2007Scho00.jpg",
"Layouts/2007Scho01.jpg",
"Layouts/2007Scho02.jpg",
"Layouts/2007Scho09.jpg",
"Layouts/2007Scho10.jpg",
"Layouts/2007Scho03.jpg",
"Layouts/2007Scho04.jpg",
"Layouts/2007Scho05.jpg",
"Layouts/2007Scho06.jpg",
"Layouts/2007Scho07.jpg",
"Layouts/2007Scho08.jpg",
"Layouts/2007Scho11.jpg",
"Layouts/2007Scho12.jpg",
"Layouts/2007Scho13.jpg",
"Layouts/2007Scho14.jpg",
"Layouts/2007Scho15.jpg",
"Layouts/2007Scho16.jpg"
);

// gallery variables
var titleSpan;
var countTxt, imgCount, pageNum;
var galleryNum=0;


// 7 variables to control behavior
var Book_Image_Width=432;
var Book_Image_Height=432;
var Book_Border=true;
var Book_Border_Color="gray";
var Book_Speed=15;
var NextPageDelay=10000; //1 second=1000

// Variables for page turn control
var L,M1,R,M2,T, Angle=0,I=0,B_MaxW;
var msize,Stopped=true;ImageArray=new Array(imgTable[0].length);
var prevButton, nextButton, playButton, Direction=1;
var single=false, Fwd=true, state=1;

var debugTxt;

// initialization
if(document.getElementById){
	//debugTxt=document.getElementById("debug");
	//debugTxt.innerHTML="debug1";

	// retrieve correct gallery
	galleryNum = parseInt(getValue("galleryNum"));

	// load correct title
	titleSpan=document.getElementById("galleryTitle");
	titleSpan.innerHTML=bookTitles[galleryNum];

	// load buttons and page counts
	prevButton=document.getElementById("btnPrev");
	nextButton=document.getElementById("btnNext");
	playButton=document.getElementById("btnPlay");
	countTxt=document.getElementById("countText");
	imgCount=imgTable[galleryNum].length;
	pageNum = 1;
	countTxt.innerHTML = "Pages 1 and 2 of "+imgCount;

	// load layouts
	for(i=0;i<imgCount;i+=1){
		ImageArray[i]=new Image();ImageArray[i].src=imgTable[galleryNum][i];
	}
	Book_Div=document.getElementById("Book");
	L=document.createElement("img");Book_Div.appendChild(L);	
	R=document.createElement("img");Book_Div.appendChild(R);
	M1=document.createElement("img");Book_Div.appendChild(M1);
	M2=document.createElement("img");Book_Div.appendChild(M2);	
	L.style.position=R.style.position=M1.style.position=M2.style.position="absolute";
	L.style.zIndex=R.style.zIndex=1;
	M1.style.zIndex=M2.style.zIndex=2;
	L.style.top=R.style.top=M1.style.top=M2.style.top=0+"px";
	L.style.left=M1.style.left=0+"px";
	M2.style.left=R.style.left=Book_Image_Width+1+"px";
	L.style.height=R.style.height=M1.style.height=M2.style.height=Book_Image_Height+"px";
	L.style.width=R.style.width=M1.style.width=M2.style.width=Book_Image_Width+"px";
	if(Book_Border){
		L.style.borderStyle=R.style.borderStyle=M1.style.borderStyle=M2.style.borderStyle="solid";
		L.style.borderWidth=R.style.borderWidth=M1.style.borderWidth=M2.style.borderWidth=1+"px";
		L.style.borderColor=R.style.borderColor=M1.style.borderColor=M2.style.borderColor=Book_Border_Color;
	}
	M1.src=ImageArray[0].src;
	M2.src=ImageArray[1].src;
	L.src=ImageArray[imgCount-2].src;
	R.src=ImageArray[3].src;
	
}


function TurnPage(){
	msize=Math.abs(Math.round(Math.cos(Angle)*Book_Image_Width));
	if(Fwd){
		Midoffset=Direction?Book_Image_Width+1:Book_Image_Width-msize;
		M2.style.width=msize+"px";
		M2.style.left=Midoffset+"px";
	}
	else{
		Midoffset=Direction?Book_Image_Width-msize:Book_Image_Width+1;
		M1.style.width=msize+"px";
		M1.style.left=Midoffset+"px";
	}	
	Angle+=Book_Speed/720*Math.PI;
	if(Angle>=Math.PI/2&&Direction){
		Direction=0;
		if(Fwd){
			M2.style.zIndex=3;
			M2.src=(I==(imgCount-2))?ImageArray[0].src:ImageArray[I+2].src;
		}
		else{
			M1.style.zIndex=3;
			M1.src=(I==0)?ImageArray[imgCount-1].src:ImageArray[I-1].src;
		}
	}
	if(Angle>=Math.PI){
		if(Fwd){
			I=(I==(imgCount-2))?0:I+2;
			T=M1;
			M1=M2;
			M2=R;
			M1.style.zIndex=M2.style.zIndex=2;
			R=T;
			R.style.left=Book_Image_Width+1+"px"
		}
		else{
			I=(I==0)?I=imgCount-2:I-2;
			T=M2;
			M2=M1;
			M1=L;
			M1.style.zIndex=M2.style.zIndex=2;
			L=T;
			L.style.left=1+"px";
		}
		L.style.zIndex=R.style.zIndex=1;
		R.src=(I==(imgCount-2))?ImageArray[1].src:ImageArray[I+3].src;
		L.src=(I==0)?ImageArray[imgCount-2].src:ImageArray[I-2].src;
		Angle=0;
		Direction=1;
		if(Fwd){
			pageNum+=2;
			if(pageNum>imgCount) pageNum = 1;
		}
		else{
			pageNum-=2;
			 if(pageNum<1) pageNum = imgCount-1;
		}
		var pagePlus = pageNum+1;
		countTxt.innerHTML = "Pages "+pageNum+" and "+pagePlus+" of "+imgCount;
		if(single) nextButton.disabled=prevButton.disabled=playButton.disabled=false;
		else setTimeout("TurnPage()",NextPageDelay);
	}
	else setTimeout("TurnPage()",50);
}

function Play(){
	Stopped = !Stopped;
	if (Stopped){
		single=true;
		playButton.disabled=true;
		playButton.value="Play";
	}
	else {
		prevButton.disabled = nextButton.disabled = true;
		playButton.value="Stop";
		single = false;
		Fwd=true;
		TurnPage();
	}
}

function Prev(){
	single = true;
	prevButton.disabled=nextButton.disabled=playButton.disabled=true;
	Fwd=false;
	TurnPage();
}

function Next(){
	single = true;
	prevButton.disabled=nextButton.disabled=playButton.disabled=true;
	Fwd=true;
	TurnPage();
}


function getValue(varname)
{
  // First, we load the URL into a variable
  var url = window.location.href;

  // Next, split the url by the ?
  var qparts = url.split("?");

  // Check that there is a querystring, return "" if not
  if (qparts.length == 0)
  {
    return "";
  }

  // Then find the querystring, everything after the ?
  var query = qparts[1];

  // Split the query string into variables (separates by &s)
  var vars = query.split("&");

  // Initialize the value with "" as default
  var value = "";

  // Iterate through vars, checking each one for varname
  for (i=0;i<vars.length;i++)
  {
    // Split the variable by =, which splits name and value
    var parts = vars[i].split("=");
    
    // Check if the correct variable
    if (parts[0] == varname)
    {
      // Load value into variable
      value = parts[1];

      // End the loop
      break;
    }
  }
  
  // Convert escape code
  value = unescape(value);

  // Convert "+"s to " "s
  value.replace(/\+/g," ");

  // Return the value
  return value;
}
