﻿
ScrollLayout=function(controller,pageView,scrollDiv)
{var me=this;this.fPageController=controller;this.fPageView=pageView;this.fScrollDiv=scrollDiv;this.fPageGap=0.07;this.fShadowDepth=5;this.fScrollBarCorrection=30;this.fLoadingPages=0;var LoadinBarOnLoad=function()
{if(me.fHasSize)
{SetLoadingBarLayout();}}
this.fLoadingBar=document.createElement("img");this.fLoadingBar.onload=LoadinBarOnLoad;this.fLoadingBar.style.position="absolute";this.fLoadingBar.style.visibility="hidden";this.fLoadingBar.style.zIndex=80;this.fPageView.parentNode.appendChild(this.fLoadingBar);var SetPageLayout=function(pageNode)
{var pageTop=((pageNode.fNewPage-1)*(me.fPageController.fPageHeight*(1+me.fPageGap))+me.fPageGap*me.fPageController.fPageHeight);pageNode.fShadow.style.top=(pageTop+me.fShadowDepth)+"px";pageNode.fImagePane.style.top=pageTop+"px";pageNode.fLoadingPane.style.top=pageTop+"px";pageNode.fPageTop=pageTop;pageNode.fShadow.style.left=(me.fPageLeft+me.fShadowDepth)+"px";pageNode.fImagePane.style.left=me.fPageLeft+"px";pageNode.fLoadingPane.style.left=me.fPageLeft+"px";}
var SetImageLayout=function(pageNode)
{if(pageNode.fType==me.fPageController.TEST_PAGE)
{pageNode.fHtmlContainer.style.left="0px";pageNode.fHtmlContainer.style.top="0px";return;}
var imageLeft=0;var imageTop=0;if(pageNode.fType!=me.fPageController.REGULAR_PAGE)
{imageLeft=((me.fPageController.fPageWidth-pageNode.fImage.width)*0.5);var heightK;if(pageNode.fType==me.fPageController.IMAGE_PAGE||pageNode.fType==me.fPageController.ADV_PAGE)
{heightK=0.08;}
else
{heightK=0.5;}
imageTop=((me.fPageController.fPageHeight-pageNode.fImage.height)*heightK);}
pageNode.fImage.style.left=imageLeft+"px";pageNode.fImage.style.top=imageTop+"px";if(pageNode.fType==me.fPageController.MOVIE_PAGE||pageNode.fType==me.fPageController.AUDIO_PAGE)
{pageNode.fFlashTitlePane.style.width=(pageNode.fImage.width-40)+"px";pageNode.fFlash.style.left=imageLeft+"px";pageNode.fFlash.style.top=imageLeft+"px";var flashContentLeft=(pageNode.fImage.width-pageNode.fFlashImage.width)*0.5;var flashContentTop=pageNode.fImage.height*0.2;pageNode.fFlashContentPane.style.left=flashContentLeft+"px";pageNode.fFlashContentPane.style.top=flashContentTop+"px";pageNode.fFlashTitlePane.style.top=(flashContentTop-25)+"px";if(pageNode.fType==me.fPageController.MOVIE_PAGE)
{var movieImageLeft=(pageNode.fFlashImage.width-pageNode.fMoviePlayImage.width)*0.5;var movieImageTop=(pageNode.fFlashImage.height-pageNode.fMoviePlayImage.height)*0.5;pageNode.fMoviePlayImage.style.left=movieImageLeft+"px";pageNode.fMoviePlayImage.style.top=movieImageTop+"px";}}
else if(pageNode.fType==me.fPageController.FLASH_PAGE)
{var flashLeft=(pageNode.fImage.width-pageNode.fFlashImage.width)*0.5;var flashTop=pageNode.fImage.height*0.2;pageNode.fFlashContentPane.style.left=flashLeft+"px";pageNode.fFlashContentPane.style.top=flashTop+"px";pageNode.fFlash.style.left=imageLeft+"px";pageNode.fFlash.style.top=imageTop+"px";}}
var SetLoadingBarLayout=function()
{var pageViewWidth=parseInt(me.fPageView.style.width);var pageViewHeight=parseInt(me.fPageView.style.height);var loadingBarWidth=parseInt(me.fLoadingBar.width);var loadingBarHeight=parseInt(me.fLoadingBar.height);me.fLoadingBar.style.left=((pageViewWidth-loadingBarWidth)*0.5)+"px";me.fLoadingBar.style.top=((pageViewHeight-loadingBarHeight)*0.5)+"px";}
var GetScrolledPage=function()
{var viewCenterHeight=me.fPageView.scrollTop+me.fPageController.fPageHeight*0.2;return Math.floor((viewCenterHeight-me.fPageGap*me.fPageController.fPageHeight)/(me.fPageController.fPageHeight*(1+me.fPageGap)))+1;}
var UpdateScrollPosition=function()
{var currentPageTop=Math.floor((me.fPageController.fPageHeight*(1+me.fPageGap))*(me.fPageController.fCurrentPage-1)+me.fPageGap*me.fPageController.fPageHeight);var scrollPosition=currentPageTop;me.fPageView.scrollTop=scrollPosition;}
var AttachScrollHandler=function()
{me.fScrollTimer=setTimeout(me.DoScroll,500);}
var DetachScrollHandler=function()
{clearInterval(me.fScrollTimer);}
this.DoScroll=function()
{if(me.fPageController.fLayout!=me)
{return;}
var newPageNum=GetScrolledPage();if(!isNaN(newPageNum))
{if(newPageNum!=me.fPageController.fCurrentPage)
{me.fPageController.SetCurrentPage(newPageNum,false,false);}}
AttachScrollHandler();}
this.Init=function()
{this.fLoadingBar.src="/img/viewer/loading.png";}
this.Resize=function()
{var scrollDivHeight=Math.floor((this.fPageController.fPageHeight*(1+this.fPageGap))*this.fPageController.fPagesCount+this.fPageGap*this.fPageController.fPageHeight);if(this.fPageController.fCurrentPage==this.fPageController.fPagesCount)
{scrollDivHeight+=(this.fPageView.clientHeight-this.fPageController.fPageHeight);}
this.fScrollDiv.style.height=scrollDivHeight+"px";var pageViewWidth=parseInt(this.fPageView.clientWidth);if(pageViewWidth<=this.fPageController.fPageWidth)
{this.fPageLeft=0;}
else
{var containerCenter=Math.floor(pageViewWidth/2);this.fPageLeft=containerCenter-Math.floor(this.fPageController.fPageWidth/2);}
SetLoadingBarLayout();this.fHasSize=true;}
this.Dispose=function()
{DetachScrollHandler();this.fScrollDiv.style.height="0px";this.fPageView.scrollTop=0;this.fPageView.removeChild(this.fLoadingBar);}
this.PageUpdateLayout=function(pageNode)
{SetPageLayout(pageNode);}
this.ImageUpdateLayout=function(pageNode)
{SetImageLayout(pageNode);}
this.PagePreLoad=function(pageNode)
{pageNode.fShadow.style.visibility="visible";pageNode.fImagePane.style.visibility="hidden";pageNode.fLoadingPane.style.visibility="visible";pageNode.fIsLoading=true;}
this.PagePostLoad=function(pageNode)
{pageNode.fImagePane.style.visibility="visible";pageNode.fLoadingPane.style.visibility="hidden";if(pageNode.fType==this.fPageController.TEST_PAGE)
{pageNode.fHtmlContainer.style.visibility="visible";}
else
{pageNode.fImage.style.visibility="visible";}
pageNode.fIsLoading=false;if(pageNode.fPage==this.fPageController.fCurrentPage)
{this.fLoadingBar.style.visibility="hidden";}}
this.PagePreChange=function()
{DetachScrollHandler();}
this.PagePostChange=function(refresh)
{if(this.fPageController.fCacheHead.fIsLoading)
{this.fLoadingBar.style.visibility="visible";}
else
{this.fLoadingBar.style.visibility="hidden";}
if(refresh)
{UpdateScrollPosition();}
AttachScrollHandler();}
this.GetDefaultZoom=function(pageWidth,pageHeight,zoomStep)
{var viewWidth=parseInt(this.fPageView.clientWidth)-this.fScrollBarCorrection;var widthZoom=(viewWidth-pageWidth)/zoomStep;return widthZoom;}
this.ZoomIn=function()
{var newZoom=this.fPageController.GetZoom()+1;this.fPageController.SetZoom(newZoom);}
this.ZoomOut=function()
{var newZoom=this.fPageController.GetZoom()-1;this.fPageController.SetZoom(newZoom);}
this.Next=function()
{var newPage=this.fPageController.fCurrentPage+1;this.fPageController.SetCurrentPage(newPage,false,true);}
this.Prev=function()
{var newPage=this.fPageController.fCurrentPage-1;this.fPageController.SetCurrentPage(newPage,false,true);}
this.Page=function(newPage)
{this.fPageController.SetCurrentPage(newPage,false,true);}}