//var B_Pre_Img = new Array(Book_Image_Sources.length);

var B_MSz,MidOffset,B_Direction=1,Book_Speed=20;
var B_Angle=0;
var B_LIn, B_LIo, B_RIo, B_RIn, B_MI;
var imgHeight, imgWidth, imgLeft;
var direction = 1; //navigate forward
var halfTransition = 0; //for single page view set this to 1

function setPrevPage(dir){
   //current left pages
   B_LIn = document.leftpage;
//   B_LIn.style.zIndex= -9;  //hide the new page to start

   B_LIo = document.prevleftpage;
//   B_LIo.style.zIndex= 0;

   //Get right pages if it is double page view
   if (pgView==2) {
      // user in double page view mode
      B_RIo = document.prevrightpage;
      B_RIn = document.rightpage;
   } else halfTransition = 1;

   B_MI = B_LIo;
   imgHeight = B_MI.height;
   imgWidth  = B_MI.width;
   imgLeft   = B_MI.left;
   
   setDirection(dir);
}

//Based on navigation direction decide the first page for transition
function setDirection(navigationDir){
   direction = navigationDir;
   B_Direction =  1;  //initiate transition

   // decide the first transition page
   if (pgView==2) {
	   if (direction==1) B_MI = document.prevrightpage;
    	  else  {
        	B_MI = document.prevleftpage;
          	B_RIo.style.zIndex = 9;  //Push righ old page to top
      	}
   } else {
		B_MI = document.prevleftpage
   }

   B_MI.style.zIndex=9; //Bring the transition page to front
}


function turnPage(){
   B_MSz=Math.abs(Math.round(Math.cos(B_Angle)*imgWidth));
   MidOffset= imgWidth-B_MSz;

   if (direction==1) {
       // user moving forward
       if (B_Direction==1) {
           // First page transition
           if (pgView==2) B_LIn.style.width = 0+"px";   //hide the new image
		   B_MI.style.width  = B_MSz+"px";
    	   B_MI.style.height = imgHeight+"px";
       } else {
           B_MI.style.left   = MidOffset+"px";
		   B_MI.style.width  = B_MSz+"px";
    	   B_MI.style.height = imgHeight+"px";
       }
	} else {
       // Reverse navigation
       if (B_Direction==1) {
		   B_MI.style.left  = MidOffset+"px";
		   B_MI.style.width = B_MSz+"px";
       	   B_MI.style.height = imgHeight+"px";
       } else {
//           B_RIn.style.width = 0+"px";  //hide the new image
		   B_MI.style.left   = B_RIo.style.left;
		   B_MI.style.width  = B_MSz+"px";
    	   B_MI.style.height = imgHeight+"px";
       }
    }

   B_Angle+=Book_Speed/720*Math.PI;


   if(B_Angle>=Math.PI/2&&B_Direction  && halfTransition != 1){
        // first half transition is complete
        B_Direction=0;      //select second half
   	    B_MI.zIndex = -9;   //pushes current image to back

        if (direction == 1) {
            //forward direction
           	B_LIn.style.width = 0+"px"; //Hide the new page
           	B_MI.src  = B_LIn.src;
        	B_MI.style.zIndex=9;
        } else {
            //backward direction
            B_RIo.style.zIndex = -9;
       		B_RIn.style.width= 0+"px"; //Hide the new right page
            B_MI.src = B_RIn.src;
        	B_MI.style.zIndex=9; //Bring the page to front
        }
   }

   if(B_Angle>=Math.PI || (B_Angle>=Math.PI/2 && halfTransition==1)){
      B_MI.style.zIndex  = 0;  //new page has to be reset to original level
      B_LIo.style.zIndex = -9;
      B_LIn.style.width = imgWidth;
      if (pgView==2) {
         B_RIo.style.zIndex = -9;
         B_RIn.style.zIndex = -1;
         B_RIn.style.width = imgWidth;
      } else {
         B_LIn.style.zIndex = 0;
//       B_LIn.style.width = imgWidth;
      }
   } else {
//      alert("inside 4");
        setTimeout("turnPage()",100)
    }
//
}