// JavaScript Document
function PageRotater(radius,x,y,pageWidth,homePage,inFunc,outFunc){
  this.pageR=radius;
  this.pageH=x;
  this.pageK=y;
  this.xOffset=Math.round(pageWidth/2);
  this.pageR2=Math.pow(this.pageR,2);
  this.intHnd=null;
  this.currentPageObj;
  this.newPageObj;
  this.activePage;
  this.currentPage=homePage;
  this.homePage=homePage;
  this.pAngle=0;
  this.outFunc=outFunc;
  this.inFunc=inFunc;
}

PageRotater.prototype.rotatePage=function(page){
    if($(page)==null) return false;
    if(page == this.currentPage) return false;
    if(this.intHnd==null){
      this.activePage=page;
      if(this.currentPage!=this.homePage){
        this.currentPageObj=$(this.currentPage);
        this.outFunc(this.currentPage);
      }
      if(page != this.homePage)
        this.newPageObj=$(page);
      var obj = this;
      this.intHnd=setInterval(function(){obj.rotatePage(page)},40);
      this.pAngle=Math.PI/2.4;
    }else if(page != this.activePage) return false;
    this.pAngle-=Math.PI/48;
    if(this.pAngle<0){
      clearInterval(this.intHnd);
      if(page != this.homePage)
        this.inFunc(page);
      this.currentPage=page;
      this.intHnd=null;
      return true;
    }
    if(page != this.homePage){
      var y=this.pageR*Math.cos(this.pAngle);
      var x=Math.floor(this.pageH+Math.sqrt(this.pageR2-(y*y)));
      y+=this.pageK;
      this.newPageObj.style.top=y+"px";
      this.newPageObj.style.left=x-this.xOffset+"px";
    }
    if(this.currentPage!=this.homePage){
      var y2=this.pageR*Math.cos(this.pAngle-Math.PI/2.4);
      var x2=Math.floor(this.pageH-Math.sqrt(this.pageR2-(y2*y2)));
      y2+=this.pageK;
      this.currentPageObj.style.top=y2+"px";
      this.currentPageObj.style.left=x2-this.xOffset+"px";
    }
    return true;
}

