 /* Npaper - Powered by Wehaa*/

 function npaper(o){
	 MODE={NORMAL:0x1,SELECT:0x3};

	var call=this,zoom=N,pubMode=MODE.NORMAL,focus=N,rgOp={},gPub=false,aZm=false,aMv=false,mM,uE,bkS,hAp,hLd,hMv={},W=N,focusRg=N,pgA=0;
	var b=function(g,p,n){var u=gBase(n); u+='run/npaper?';  u+='paper='+call.paper+'&'; if(g)u+='get='+g+'&'; if(p)u+=p; return u; },
	
	   chgMrgI=function(o,mrg){o.style.top=mrg+'px'; o.style.left=mrg+'px';},
	   tm=function(){return new Date().getTime();},
	   isN=function(){return pubMode==MODE.NORMAL;},
	   geMarg=function(sz){
		if(sz.w<50 || sz.h<50)return 1;
		else if(sz.w<100 || sz.h<100)return 2;
		else if(sz.w<200 || sz.h<200)return 3;
		else if(sz.w<300 || sz.h<300)return 4;
		else return 5; 
	  };

	this.paper=o.paper;
	this.pages=[];
	this.onloadedPub=function(p){};
	this.onDrawPages=function(){};
	this.onSetZoom=function(zoom,args){};
	this.onSetPage=function(num,pg){};
	this.onLoadPage=function(){};
	this.endLoadPages=function(){};
	
	this.gBSz=function(){return (!this.box)?N:{w:this.box.offsetWidth,h:this.box.offsetHeight};}

	this.req=function(u,f,pD,wHdl){
		var o={url:u,func:f}; if(pD){o['method']='POST';o['param']=pD;}
		if(!wHdl){return toAjax(o);
	    }else{this.stopRq(); sO(function(){call.rHdl=toAjax(o,call.rHdl?call.rHdl.o:N);},1);}
	  }	
	  
    this.stopRq=function(){
		if(this.rHdl){this.rHdl.abort();}
	 }
	 
	 this.getPub=function(pid){
	 	return this.get({pub:pid});
	 }
	 
	 this.gC=function(szO,prp){
		if(!zoom)return;
		var o,sz={w:szO.w,h:szO.h},prp=prp || (sz.h/sz.w);
		sz.w=zoom*this.pub.minw/100;   W=sz.w;
		sz.h=sz.w*prp;
		return sz;
	 }
	 
	 this.getZbyPx = function(v){ 
	   if(!this.pub)return;
	    var o,zoom, sz={mxw:this.pub.maxw,mxh:this.pub.maxh,mnw:this.pub.minw,mnh:this.pub.minh}, prp=sz.mxh/sz.mxw;
		
		zoom=toScale(v-10,[(o && o.min)?o.min+20:20,sz.mnw+20], [0,100]);
		return zoom<0?0:(zoom>100?100:Math.round(zoom));
	 }
	 
		
	 this.eachPg=function(f,d,m){var k,l=this.pages.length,F=function(o,k){if(!(d&&!o.drawed)){return f.apply(call,[o,k]);}};
	    if(!m)for(k=0;k<l;k++){if(-1==F(this.pages[k],k))return -1;} 
	    else for(k=l-1;k>=0;k--){if(-1==F(this.pages[k],k))return -1;}
		return 1;
	 }

    this.cPgs=function(){return this.box.firstChild;}
	
	this.loadedPub=function(){
		return this.pub && this.pub.loaded;
	}

	this.drawedPub=function(){
		return this.loadedPub() && this.pub.drawed;
 	}
	
	 this.Free=function(){ 
	   var b=this.box; dE(b);
	   this.eachPg(function(o){if(o.im&&o.im.parentNode){o.im.parentNode.removeChild(o.im);} o.im=N; o=N;});  
	   this.pages=N; focus=N; this.pages=[]; rgOp={}; fRg=N;  W=N;
	 }
	 
	 var hCur=function(h){var c=h?'default':'',c2=h?'pointer':'';
	   call.eachPg(function(o){o.pObj.firstChild.style.cursor=c;if(rg=o.reg){for(k=0;k<rg.length;k++)rg[k].o.style.cursor=c2;}},true);
	}
	
	this.sCur=function(m,o){var m=m || 0,sB=o||call.box;
      if(m==0){if(isIE)sB.style.cursor='url(img/cur/hndo.cur)';else if(isFF)sB.style.cursor='-moz-grab';else sB.style.cursor='grab';}
	  else{if(isIE)sB.style.cursor='url(img/cur/handd.cur)';else if(isFF)sB.style.cursor='-moz-grabbing';else sB.style.cursor='grabbing';}
	}
	
	 this.drgS=function(e){if(pubMode!=MODE.NORMAL)return;
	     var sB=this.box,e=MkEv(e),tCur=e.cursor,tmv=0,rg,k;
	     call.sCur(1);  SDW=true; cls();  if(mM){mM.stop(); mM=N;} if(uE){uE.stop(); uE=N;}

		 mM=aEv('onmousemove',function(e){if(tmv==1){hCur();}
		   var cur=MkEv(e).cursor,cur_={x:tCur.x-cur.x,y:tCur.y-cur.y};
		   sB.scrollTop=sB.scrollTop+cur_.y; sB.scrollLeft=sB.scrollLeft+cur_.x;
		   tCur=cur; tmv++;
		 });
		
		 
	     uE=aEv('onmouseup',function(e){SDW=false;
		  if(tmv>=1){hCur(1);}
		  mM.stop(); uE.stop(); call.sCur(); if(tmv>2)call.mv=tm();
		 });
	 }
	  
	 var tm=function(){return new Date().getTime();},
	 oSc=function(){
		var s=call.box.scrollTop; call.mvScroll=true; //call.clDrwC(); 
		if(!aZm){if(call.hL){cI(call.hL);}call.hL=sO(function(){call.hL=N; call.load_pages(); call.slPg(1); bkS=N; call.mvScroll=false;},250); if(call.hM){cI(call.hM);} call.hM=sO(function(){call.hM=N;call.mk_pg();},10);} bkS=s;
	 },oNd=function(e){
		 if(pubMode==MODE.NORMAL){call.drgS(e);}else if(pubMode==MODE.SELECT){call.sSel(e);}
	 };
	
	 this.setBox=function(box){
		this.box=typeof box=='string'?ge(box):box;
		this.box.onscroll=oSc;
		with(this.box.style){cursor='default';overflow='auto';position='relative';}
	  }
	  
	 this.getPgsV=function(sT,oks){
		 var pgs=[],sz=this.gBSz(),sB=this.box,sT=sT||sB.scrollTop,oT,ok=0,t;
		 this.eachPg(function(o){t=Math.abs(sT-o['T']); if((sT-o['T'])<o['H']&&(sz.h-t>=1 || (o['H']-t>=1 && sT>o['T']))){ok=1;pgs.push(o);}else if(ok)return -1;});
		 return pgs;
	 }
	this.cTm=function(){sO(function(){call.mv=0;},100);}
	
	this.load_pages=function(F,refresh,all){
		 refresh=refresh || false; all=all || false;
		 var k,pgvw=all?this.pages:this.getPgsV(),l=pgvw.length,pgL=0,ldPgs=0;
		  for(k=0; k<l; k++){p=pgvw[k]; 
		   if(!p.LdImg && (refresh || !p.iSz || p.iSz.w!=p.sZ.w || p.iSz.h!=p.sZ.h)){pgL++; 
				this.ld_pg_(p,function(o){ldPgs++; sO(function(){if(ldPgs>=pgL){if(F)F();call.endLoadPages(); call.ldPgs=false;}},10);});
		     }
		  }
		if(pgL>0){this.ldPgs=true; this.onLoadPage();}
		return pgL;
	}
	
	var chgRgSz=function(o){
		if(!o.dLded)return;
		var t,l,w,h,k,o_,r=o.dDet.r,rg=o.reg,w_=o.iSz_.w/o.w,h_=o.iSz_.h/o.h,imH,hL=o.dDet.h;
		if(r.drawed && (!r.sz  || r.sz.w!=o.iSz_.w || r.sz.h!=o.iSz_.h)){
		 for(k=0;k<rg.length;k++){o_=rg[k];
		   t=Math.round(o_.pY*h_); l=Math.round(o_.pX*w_); w=Math.round(o_.sW*w_); h=Math.round(o_.sH*h_);
		   with(o_.o.style){top=t+'px'; left=l+'px'; width=w+'px'; height=h+'px';}
		   if(o_.o.wHl==2){with(hL.im.style){width=(o.iSz_.w)+'px'; height=(o.iSz_.h)+'px'; top=(t*-1)+'px'; left=(l*-1)+'px';} hL.iSz=o.iSz_;}
		   if(o_.o.sHl==2){with(hL.imSt.style){width=(o.iSz_.w)+'px'; height=(o.iSz_.h)+'px'; top=(t*-1)+'px'; left=(l*-1)+'px';} hL.iSzSt=o.iSz_;}}
		 r.sz={w:o.iSz_.w,h:o.iSz_.h};
		}
	}
	
	 var rmkHighLight=function(o,r,st){
		var st=st||false,hM=!st?'wHl':'sHl',h,im;
		if(r.o[hM]){ 
		 if(r.o[hM]==2){h=o.dDet.h,im=!st?h.im:h.imSt;if(im && im.parentNode){im.parentNode.removeChild(im);}}
		 r.o[hM]=0;
		}
	 }
	 
    var copyHighLight=function(o,r,st){
		var st=st||false,hM=!st?'wHl':'sHl',im,sz; r.o[hM]=r.o[hM]||1; 
		if(o.lHL){var h; 
			if(r.o[hM]!=2 && o.dDet && (h=o.dDet.h) && h.im){
		    if(st){if(!h.imSt){h.imSt=h.im.cloneNode(false); nDrg(h.imSt);} im=h.imSt; sz=h.iSzSt||{};}else{im=h.im; sz=h.iSz; if(h.im.parentNode){h.im.parentNode.wHl=0;}}
			
			if(!sz || sz.w!=o.iSz_.w || sz.h!=o.iSz_.h){with(im.style){width=(o.iSz_.w)+'px'; height=(o.iSz_.h)+'px';} sz.w=o.iSz_.w; sz.h=o.iSz_.h;}
			 with(im.style){top=(parseInt(r.o.style.top)*-1)+'px'; left=(parseInt(r.o.style.left)*-1)+'px';}
			 r.o.appendChild(im);
			 r.o[hM]=2;
			}
		}else{var wH=sI(function(){if(!r.o[hM]){cI(wH); return;}else if(o.lHL){cI(wH);copyHighLight(o,r,st); }},30);}
     }
	 function print_r(obj){ 
	var h='';  
	for(var x in obj){
		h+=' '+x+' => ';
		h+=obj[x]+',<br>\r\n';
	}
	return h;
}
	
	var MkRg=function(r,o){
		 var o_=ce('div'),fc=fRg,a=r.action,i='<img src="img/blk.gif" ondrag="return false;" onmousedown="return false" width="100%" height="100%">'; r.o=o_;
		 with(o_.style){position='absolute';zIndex=r.zI;overflow='hidden';cursor='pointer';}
		 o_.innerHTML=a==3?i:'<div style="width:100%;height:100%;position:absolute;background-color:'+r.color+';visibility:hidden;top:0px;left:0px;"></div>'+i; //if(a!=3)setAlpha(o_.firstChild,call.alpha);
 		 
		 o_.onmouseover =function(){if(!isN())return; fcRg=[r,o]; cI(r.hHt); o.vRg=1; if(!r.o.sHl && r.action==3){copyHighLight(o,r);}}
		 o_.onmouseout  =function(){if(!isN())return; fcRg=N; r.hHt=sO(function(){if(r.o && r.o.wHl){o.vRg=0; rmkHighLight(o,r);}},10);}
		 o_.onclick=function(){if(isN()){if(call.mv&&(tm()-call.mv)<100){call.cTm();return;} window.parent.location=gBase()+call.pub.pUrl+';see-'+call.pub.pub+';c-'+r.id;}};
	    o.dDet.r.box.appendChild(o_);
		
		if(call.pub.rid==r.id){focusRg=r; sO(function(){rmkHighLight(o,r); copyHighLight(o,r,true);},1);}
	}
	
	this.toRg=function(r){
		var bSz=this.gBSz(),o=r.pg,tPg=o.T+o.iMrg,W=o.iSz_.w/o.w,H=o.iSz_.h/o.h, x=r.pX*W,y=r.pY*H,w=r.sW*W,h=r.sH*H ,C=(h-bSz.h)/2;
		this.sS(tPg+y+Math.max(0,C), false);
	}
	
	 this.drwDt=function(o,t){
		if(!o.dLded)return; var k,d=o.dDet,r=d.r,h=d.h,dv,l_=0,R;
		
		r.box=ce('div'); o.reg=[]; for(var k=0;k<r.d.length;k++){if((R=r.d[k])&&R.action>=3){R.pg=o; MkRg(R,o); o.reg.push(R);}} o.dObj.appendChild(r.box);
	    r.d=N; r.drawed=1; chgRgSz(o);  if(focusRg && focusRg.pg.id==o.id){this.toRg(focusRg);}
	 }
	 
	 this.ld_pg_=function(o,f,sz,nRz){if(!o.drawed)return;
	 	var ldbx=o.cObj.firstChild,sz=sz||o.sZ,mrg=geMarg(o.sZ),m=(mrg*2)+(isIE?2:0),szi={w:sz.w-m,h:sz.h-m},iEr=[0,0];
		o.iLded=false; o.iSz=sz; o.iSz_=szi; ldbx.innerHTML='Loading...';
		
		if(!o.im){o.dLded=false; o.iMrg=mrg;
		o.im=new Image(); nDrg(o.im);
		with(o.dObj.style){top=mrg+'px'; left=mrg+'px';  width=szi.w+'px'; height=szi.h+'px'; zIndex='2';}
		with(o.im.style){position='absolute'; top=mrg+'px'; left=mrg+'px'; width=szi.w+'px'; height=szi.h+'px'; zIndex='1';}
		o.im.onerror=function(){if(iEr[1]<3){sO(function(){o.im.src=o.im.src;},500);} ldbx.innerHTML='Unknown error'; o.iLded=false; iEr[1]++;};
		o.im.onload=function(){if(o.iLded||!o.im)return; iEr[0]=0; if(!o.im.parentNode)o.cObj.appendChild(o.im); o.iLded=true; if(o.dLded)ldbx.innerHTML=''; o.im.onload=N;};
		o.im.src=b('geIm','pgid='+o.id+'&w='+szi.w+'&h='+szi.h+'&pid='+this.pub.data.pid,o.num);
		
		call.req(b('gePgDetails','pgid='+o.id+'&pid='+this.pub.data.pid),function(b){
			  if(!o.dDet){o.dDet={h:{},r:{}};}else if(!o.dDet.h){o.dDet.h={};}
			   var hL,b; eval('b='+b+';');
			   if(b && !isu(b.r) && !isu(b.h)){hL=sI(function(){if(!o.iLded)return;cI(hL); o.dDet.h.d=b.h; o.dDet.r.d=b.r; o.dLded=true; if(pubMode==MODE.NORMAL)call.drwDt(o,1);else call.drwDt(o,2);  if(f)f.apply(call,[o]);  ldbx.innerHTML=''; b=N;},1); }else alert('Can\'t be load regions');
			 });
		
		}else{
		if(o.iMrg!=mrg){chgMrgI(o.im,mrg); chgMrgI(o.dObj,mrg); o.iMrg=mrg;}
		if(!nRz){with(o.im.style){width=szi.w+'px'; height=szi.h+'px';} with(o.dObj.style){width=szi.w+'px'; height=szi.h+'px';}}
		o.im.onload=function(){if(!o.im.parentNode)o.cObj.appendChild(o.im); if(f)f.apply(call,[o]); if(o.dLded)ldbx.innerHTML=''; o.iLded=true; this.onload=N;}
		o.im.src=b('geIm','pgid='+o.id+'&w='+szi.w+'&h='+szi.h+'&pid='+this.pub.data.pid,o.num);
		}
		if(!o.lHL){
		var hL=new Image();
		hL.onload=function(){if(o.lHL)return; o.lHL=1;  iEr[1]=0; if(!o.dDet){o.dDet={h:{},r:{}};}else if(!o.dDet.h){o.dDet.h={};}
		var h=o.dDet.h; h.iSz=szi;
		if(isIE){h.im=ce('DIV'); stAlp(h.im,hL.src);}else{h.im=hL; nDrg(h.im); h.im.onload=N;}
			with(h.im.style){top=mrg+'px'; left=mrg+'px';overflow='hidden'; width=szi.w+'px'; height=szi.h+'px'; position='absolute';} 
		}
		hL.onerror=function(){if(iEr[1]<3){sO(function(){hL.src=o.path+'highlight.png';},500);} iEr[1]++; o.lHL=0;}
		hL.src=o.path+'highlight.png';
		}
	 }
	 
	 this.mk_pg=function(all){
		var k,p,pg=all?this.pages:this.getPgsV(),l=pg.length;
	    for(k=0; k<l; k++){p=pg[k]; if(p.drawed)continue; MkPg(p);}
	 }
	
	this.changeSzPg=function(o,sz){
		sz.w=Math.round(sz.w); sz.h=Math.round(sz.h); if(isNaN(sz.w) || isNaN(sz.h))return;
		 var fc=fRg,c=o.pObj.firstChild,sz_=(isIE)?{w:sz.w+2,h:sz.h+2}:sz; 
		
		 c.style.width=(sz_.w)+'px'; c.style.height=(sz_.h)+'px';
		 o.cObj.style.width=(sz_.w)+'px'; o.cObj.style.height=(sz_.h)+'px';
		 o['W']=sz.w+10; o['H']=sz.h+10; 
		 with(o.pObj.style){width='100%';height=o['H']+'px';}
		 o.shd.style.width=sz.w+'px'; o.shd.style.height=sz.h+'px';
		
		 if(o.im){
		   var mrg=geMarg(sz),m=(mrg*2)+(isIE?2:0),szi={w:Math.max(sz.w-m,0),h:Math.max(sz.h-m,0)}; o.iSz_=szi; 
			with(o.dObj.style){width=szi.w+'px'; height=szi.h+'px';} if(o.iMrg!=mrg){chgMrgI(o.im,mrg); chgMrgI(o.dObj,mrg); o.iMrg=mrg;}
			with(o.im.style){width=szi.w+'px';height=szi.h+'px';}
	      }
		  
		 o.pObj.style.height=o['H']+'px';
		 
		 if(isN()){if(focusRg && focusRg.pg.id==o.id){chgRgSz(o);}}
		 else this.clSl();
		 o.sZ=sz;
	}
	
	this.sBoxAtt=function(){
		this.cPgs().onmousedown=oNd; 
	}
	
	var MkPg=function(o,sz){ 
		 if(o.drawed)return;
		 var bx=ce('div'),sz=sz || call.gC({w:o.w,h:o.h},o.prp),C=call.cPgs(),sz_=(isIE)?{w:sz.w+2,h:sz.h+2}:sz; o.pObj=bx; 
		 o.mk=1; o.sZ=sz;
		 with(bx.style){width='100%'; height=o['H']+'px'; top=o['T']+'px'; left=o['L']+'px'; zIndex='1'; position='absolute';}
		 bx.innerHTML='<div style="position:relative; margin:2px auto; width:'+sz_.w+'px; height:'+sz_.h+'px;"><div class="pgbx" style="background:white;overflow:hidden;position:absolute;width:'+sz_.w+'px;height:'+sz_.h+'px;z-index:1;top:0px;left:0px;text-align:left;"><div style="position:absolute;top:5px;z-index:3;left:5px;" class=ldpg></div><div style="position:absolute; overflow:hidden; top:0px; left:0px;"></div></div> <div style="position:absolute; background:#D4D8DD; top:+4px; left:+4px; width:'+o.sZ.w+'px; height:'+o.sZ.h+';"></div></div>';

		 o.cObj=bx.firstChild.firstChild; //page container
		 o.dObj=o.cObj.childNodes[1]; //page
		 o.shd =bx.firstChild.lastChild; //shade
		 o.cObj.onmouseover =function(){if(isN())chgRgSz(o);};
		 o.cObj.ondblclick=function(){nHdl.setPage(o.num,1);};
		 C.appendChild(bx); o.drawed=true;
	}
	
	 var pg={
	    chgSz:function(sz,m){return call.changeSzPg(this,sz,m);},
		P:function(){return this.num%2==0},
		chgPos:function(t,l){var o=this; o['T']=t; o['L']=l; if(o.drawed){o.pObj.style.top=o['T']+'px'; o.pObj.style.left=o['L']+'px';}},
		isVisible:function(m){var bSz=call.gBSz(),sT=call.sBox.scrollTop,m=m||1,fc=focus; return (m==1 || bSz.h<this['H'])?(sT-this['T']<this['H'] && bSz.h-Math.abs(sT-this['T'])>=1):(sT-this['T']>=1)?1:((this['T']+this['H'])-sT>bSz.h?2:0); },
	    hidden:function(b){if(this.hdd===b)return; var sBx; if(this.drawed){this.pObj.style.display=b?'none':''; if(sBx=this.secBox){sBx.box.style.visibility=b || !call.displaySec?'hidden':'';}} this.hdd=b;}
		};
	
	 this.draw_pages=function(nMk){
		if(!this.loadedPub())return false; this.Free();
		this.box.innerHTML='<div style="position:relative;float:left;width:100%;overflow:hidden;height:1px;visibility:hidden;"></div>';
		this.cBx=this.box.firstChild;
		var path=base+this.pub.data['path'],SZ,k,o,T=0,sZ,c=this.cPgs(); this.sBoxAtt();

		 for(k=0; k<this.pub.pgs.length; k++){
		  o=this.pub.pgs[k];
		  if(!o.w || !o.h)continue;
		  o.name=o.name||'Page '+o.num; 
		  o.prp=o.h/o.w; o.im=N; o.reg=[]; o.path=(!o.path)?path+'page_'+o.num+'/':path+o.path+'/';
		  for(var at in pg)o[at]=pg[at];
		  SZ=call.gC({w:o.w,h:o.h},o.prp); 
		  o.W=SZ.w+20; o.H=SZ.h+20; o.T=T; o.L=0; T+=o.H;
		  this.pages.push(o);
		 }
		this.pub.drawed=true; 
		
		if (sZ=this.pub.setZ) {
			if(!sZ[1])sZ[1]={animate:false,iLd:false,rSz:true,nmv:1}; 
			sZ[1]['iLd']=false; sZ[1]['animate']=false; sZ[1]['nmv']=1; 
			sZ[1]['nMk']=nMk || this.pub.spg;
			this.setZoom.apply(this,[sZ[0],sZ[1],true]);
			this.pub.setZ=N;
		} else {
			this.rZoom({animate:false,iLd:false,nmv:1});
		}
		
		with(c.style){overflow='visible';visibility='';}
		if(this.pub.spg)this.setPage(this.pub.spg,1,1);
		else if(!nMk){this.load_pages();}
	
		this.onDrawPages();	
		return true;
	 };
	 
	 this.get=function(o) {
		gPub=true; delete this['pub'];
		this.pub={loaded:false,drawed:false}; var p=this.pub,args=arguments;
		if(o.box)this.setBox(o.box);
		if(!this.box)return;
		
		p.pub=o.pub;
		if(o.pg) p.spg = o.pg;
		 
		this.req(b('pub-embed','pub='+o.pub+'&r=1'+(o.c?'&c='+o.c:'')),function(b){
		 if(b==-1){call.onerror('npaper::get Paper not found'); return;}
		 else if(b==-2){call.onerror('npaper::get Publication not found'); return;}
		 else if(b==0){call.onerror('npaper::get Invalid Request'); return;}
		 else if(b=='NLOGGED'){call.SignIn(function(){call.get.apply(call,args);}); return;}
		
         d=eval('('+b+')');
		 if(!d){call.onerror('npaper::get unknown'); return;}
		 
		
		 for(var e in d){if(!p[e])p[e]=d[e];} if(!p.ett){var CH=document.styleSheets[0],R=CH?CH.cssRules:N,n; if(R){for(n=0;n<R.length;n++){if(R[n].selectorText=='.tooltips'){CH.deleteRule(n); break;}}}}
		 pubId=d.data['pid'];
		 pId=d.data['id_paper'];
         p.loaded=true;
		 call.onloadedPub(p);
		 
		 d=N; b=N;
		});
	 }
	 this.gcSz=function(){if(!this.pub || !this.pub.pgs)return N;
	 	var lastPg=this.pub.pgs[this.pub.pgs.length-1];
		return !lastPg? N: lastPg['T']+lastPg['H'];
	 }

	 this.rSzSbx=function(){if(!this.box)return;
		 this.cPgs().style.height=(this.gcSz())+'px';
	 }
	 
	 this.getPage=function(pg){
		var p = N; 
		this.eachPg(function(o,k){if(o.num==pg ){ p=[o,k]; return -1;}});
		return p;
	 }
	 
	 this.getFocusPg=function(){
		return focus||N; 
	 }
	
	 this.sS=function(v, a, t, f) {
		var sB=this.box,t=t||1,sB,s=(t==1)?'scrollTop':'scrollLeft'; if(!sB)return;
		if(!a){sB[s]=v;}else{var sT=sB[s],stop=function(){if(hMv[t]){hMv[t].stop();hMv[t]=N;sO(function(){aMv=false; for(var e in hMv){if(hMv[e]&&hMv[e].stopped&&!hMv[e].stopped()){aMv=true; break;}} },100);}}; 
		 stop(); aMv=true; 
		hMv[t]=new an({round:1,delay:gDelay,fps:30,duration:300,from:[sT],to:[v],f:function(v){if(sT!=sB[s]){stop();return;} sB[s]=v[0]; sT=sB[s];},endf:function(v,ok){if(ok){ if(f)f(); stop();}}});
		}
	 }
	 
	 this.setPage = function(pg,toM,nA){ 
		 if(!(pg=this.getPage(pg)))return; 
		 pg=pg.shift();
		 MkPg(pg);
		 if(toM){var sT=this.box.scrollTop,tS=pg['T'];  if(nA||(sT-tS)==0||Math.abs(sT-tS)>1000|| pubMode!=MODE.NORMAL){this.sS(tS); this.mk_pg(); this.load_pages();}else{this.sS(tS,true); sO(function(){call.load_pages();},250);}} 
		 if(!focus || focus.num!=pg.num)this.onSetPage(pg.num,pg);
		
		focus=pg;
	 }
	 
	 this.slPg=function(st){
	 	var pg=this.getPgsV(),p,k,sT=this.box.scrollTop,h=this.gBSz().h/2;
		for(k=0; k<pg.length; k++){p=pg[k]; 
			if((p.T-sT)<=h && ((p.T-sT)+p.H)-h >=1){
				if(st)this.setPage(p.num);
				return p.num;
			 }
		}
	 }
	 this.rZoom=function(arg){return this.setZoom.apply(this,[zoom,arg?arg:N,true]); }
	 
	 this.gZoom=function(){ 
	    return zoom; 
	  }
	  
	 this.setZoom=function(z, args, zI) {
		if((z==zoom && !zI) || !(z=parseInt(z)) || isNaN(z) || z<0 || z>100)return; 
        var args=args||{animate:true,iLd:true},plen=this.pages.length; zoom=z;

	 if(!this.drawedPub()){
		   if(this.pub)this.pub.setZ=arguments;
	   }else{
	  	 this.onSetZoom(zoom,args);
		 T=0; aZm=args.animate; aZm=false;  if(hLd){cI(hLd);hLd=N;}
		 var sT=this.box.scrollTop,sL=this.box.scrollLeft,szW=call.pub.pgs[0]['W'],szH=this.gcSz(),sT_=sT,bSz=this.gBSz(),T=0,st,
		 rZpg=function(){var t,isV; call.eachPg(function(o){if(!(t=o['tmp']))return; o.hidden(0);  if(!t.isV){if(o.drawed){call.changeSzPg(o,{w:t.tSz.w,h:t.tSz.h}); o.pObj.style.top=o['T']+'px'; o.pObj.style.left=o['L']+'px';}} o['tmp']=N; delete o['tmp'];}); },
		 endF=function(){pgA--; if(pgA<0){pgA=0;} if(pgA==0)endA();},
		 endA=function(){if(st)return; st=1; aZm=false; call.mvScroll=false; rZpg(); hLd=sO(ldpg,100);},
		 ldpg=function(){ if(!args.nmv){call.sS(Math.max(call.gcSz()*sT_/szH,0),true,1,function(){if(args.iLd)call.load_pages();}); call.sS(Math.max((call.pub.pgs[0]['W']-bSz.w)*sL/(szW-bSz.w),0),true,2);}else{ if(args.iLd)call.load_pages(); }};

		 if(!aZm)hLd=sO(function(){rZpg();ldpg();},200);
		 
		  this.eachPg(function(o,k){t=o['tmp']; if(!t){t={}; o['tmp']=t;}
			  t.tSz=call.gC({w:o.w,h:o.h},o.prp); t.rSz={w:t.tSz.w+10,h:t.tSz.h+10};
			  t.t=T; t.l=0;
			  T+=t.rSz.h;
		});
		  
			 if(sT>T-bSz.h){sT=Math.max(T*sT_/this.gcSz(),0); this.sS(sT,0);}
			
		     if(aZm&&!hAp)hAp=sI(function(){if(!aZm){cI(hAp);hAp=N;} call.rSzSbx();},10);
			  
		  this.eachPg(function(o){var t=o['tmp'],isV,T=Math.abs(sT-o['T']),T2=Math.abs(sT-t.t);   
			t.isV=!args.nMk && (((sT-o['T'])<o['H']&& (bSz.h-T>=1 || (o['H']-T>=1 && sT>o['T']))) || ((sT-t.t)<t.rSz.h&&(bSz.h-T2>=1 || (t.rSz.h-T2>=1 && sT>t.t))));
			
			o.hidden(!t.isV);
			 if(aZm){
				 if(t.isV){
					if(!o.drawed){MkPg(o,{w:o['W']-10,h:o['H']-10});}
				    
					if(t.a  && !t.a.stopped()){ 
				   		t.a.change([t.tSz.w,t.tSz.h,t.t],1);
			     	}else{
				  	 t.a=new an({round:1,delay:gDelay,fps:30,duration:200,from:[o.sZ.w,o.sZ.h,o['T']],to:[t.tSz.w,t.tSz.h,t.t],f:function(v){
				     call.changeSzPg(o,{w:v[0],h:v[1]}); o['T']=v[2]; o.pObj.style.top=o['T']+'px';
				 	 },endf:endF});  pgA++;
				   }
				 }else{if(t.a){t.a.stop(1); t.a=N;} o['T']=t.t; o['W']=t.rSz.w; o['H']=t.rSz.h;}
			 }else{
				if(t.isV){MkPg(o,{w:t.tSz.w,h:t.tSz.h}); o['T']=t.t; call.changeSzPg(o,{w:t.tSz.w,h:t.tSz.h}); o.pObj.style.top=o['T']+'px'; o.pObj.style.left=o['L']+'px'; }
				else{o['T']=t.t; o['W']=t.rSz.w; o['H']=t.rSz.h;}
			 }
			
		  });
		  if(!aZm)this.rSzSbx();
	   }
	} 
	this.setZoom(100);
 }
 
 ///
  function gDelay(c){
	return (2-(Math.cos(Math.PI*c)+1))/2;
 }

 function an(o){
		var call=this,f=o.from,t=o.to,d=(typeof o.delay=='function')?o.delay:new Function('return '+o.delay),stop=false,iHdl,l=f.length;
		if(l!=t.length)return false;
		
		call.stopped=function(){return stop;};
		call.stop=function(ok){stop=true; cI(iHdl); if(o.endf)o.endf(call.value,ok); iHdl=null;};
		call.change=function(v,rTm){if(stop || v.length!=l)return false; for(k=0; k<l; k++){f[k]=call.value[k]; t[k]=v[k];} if(rTm)call.time=0;}
		
		call.time=0;
		call.value=[];
		for(k=0; k<l; k++){call.value.push(f[k]);}
		
		call.run=function(){
			if(stop)return;
			call.time+=o.fps;
			if(call.time>o.duration)call.time=o.duration;
			
			for(k=0; k<l; k++){
				call.value[k]=(t[k]-f[k])*d(call.time/o.duration)+f[k];
				if(o.round)call.value[k]=Math.round(call.value[k]);
			}
			o.f.apply(call,[call.value]);
			if(call.time==o.duration){call.stop(1);}
			else iHdl=sO(arguments.callee,o.fps);
		}
		iHdl=sO(call.run,o.fps);
}
 
function toScale(vInScaleA, sA, sB){
	return (vInScaleA-sA[0])*(sB[1]-sB[0])/(sA[1]-sA[0])+sB[0];
}

function isL(){
	return location.href.indexOf('local')!=-1;
}

function gBase(n){
	return (n) ? 'http://n' + (n%2==0?2:1) + '.' + base.replace(/http\:\/\/(www\.)?/,'') : base;
}
 
function pgFc(){
	var pg=nHdl.getFocusPg(); 
	return (pg)?pg.num:1;
}

tbtn=N;
function onBtn(k){
	if(k==0)tbtn.btn[k].on=!tbtn.btn[k].on;
	else if(k==1 || k==2){
	var z=Math.floor(nHdl.gZoom()/10);
	 if(k==1){z-=1; if(z<1)z=1;}else{z+=1; if(z>10)z=10;}
	 nHdl.setZoom(z*10);
	 tbtn.btn[1].disable(z==1);
	 tbtn.btn[2].disable(z==10);
	}else if(k==3 || k==4){
		var pg=pgFc();
		if(k==3){nHdl.setPage(pg-1,1);}
		else{nHdl.setPage(pg+1,1);}
	}
 }
 function addBtn(x,x2,k){
  var o=ce('div'); with(o.style){position='absolute'; top='0px'; left=x2+'px';}
  o.title=['Select text','Decrease zoom','Increase zoom','Previous page','Next page'][k];
  iS({i:iBtn,o:o,l:x,t:-42,w:23,h:21});
  o.onmouseover=function(){if(o.on || o.d)return;o.firstChild.style.top='-21px';};
  o.onmouseout=function(){if(o.on || o.d)return;o.firstChild.style.top='-42px';};
  o.onmousedown=function(){if(o.on || o.d)return;o.firstChild.style.top='0px';};
  o.onmouseup=function(){sO(function(){if(o.on || o.d)return;o.firstChild.style.top='-42px';},1);};
  o.onclick=function(){onBtn(k);}
  o.disable=function(b){if(b){o.firstChild.style.top='-63px';}else{o.firstChild.style.top='-42px';}o.d=b;}
  tbtn.obj.appendChild(o);
  tbtn.btn[k]=o;
 }
 
 ldb=N;
function ldc(b){
  if(!ldb){ if(!b)return;
  ldb=new dv; ldb.autoHideClick=false; ldb.obj.style.zIndex='10'; iS({i:iBtn,o:ldb.obj,l:0,t:-84,w:11,h:11});
  ldb.f=function(){var idx=ldb.idx*-11;  ldb.obj.firstChild.style.left=idx+'px'; ldb.idx=(ldb.idx<7)?ldb.idx+1:0;}; ldb.idx=0;
  } cI(ldb.iHdl);
  if(b){ ldb.idx=0; ldb.f(); ldb.iHdl=sI(ldb.f,100); ldb.display({r:33,y:30});}
  else ldb.hidden();
 }
 
 function drwBtn(){
	if(tbtn)return;
	var k,w=0;
	tbtn=new dv; tbtn.btn={}; tbtn.disable=function(b){var k; for(k in this.btn){if(!Object.prototype[k]){this.btn[k].disable(b);}}}
	tbtn.autoHideClick=false; 
	for(k=0; k<5; k++){
		addBtn(k*-23,w,k);
		w+=23; if(k==0 || k==2){w+=5;}
	}
	with(tbtn.obj.style){zIndex='10';height='21px'; width=w+'px';} tbtn.disable(1);
	tbtn.display({r:30,y:5}); 
 }
 
 function getPub(pid, c, pg){
	nHdl.get({box:'vwbox', c:c, pg:pg, pub:pid, callback:function(){}});
 }
