	var widthShadow = 40;

	var borderPage = true;
	var borderPageColor = "gray";
	var flipSpeed = 20;
	var flipSpeedDelay = 1000; 
	var flipVerticalTurn = 0;

	var page2go = getQueryString('page2go');

	timeInPage = new Array(numPgs);
	timeZoomInPage = new Array(numPgs);
	
	for (i = 0; i < numPgs ; i++){
			timeInPage[i] = timeZoomInPage[i] = 0;
	}
	

/***************************************************/
	var leftImg, midImg, rightImg, tempImg, Angle = 0, iNavPgImg = 0, flipDirection = 1;
	var midResize, B_Stppd=false; 
	var bufferPreImg = new Array(pageImgSources.length);bufferPreImgHigh=new Array(pageImgSourcesHigh.length);
	var iLastPage = pageImgSources.length-1;
	var iFirstPage = 0;
	var onBack = 0, onFront=1, iRight=0, iLeft=iLastPage , zoomIsActive=0, onZoomIndex=0;
	var ID_AutoFlip, autoFlip = 1, isFliping = 0, loadingSite = false, pageFaultLeft = false, pageFaultRight = false;
	
	var is = new Is();
	
	//Posicao Botao Impressao
	if (BtnFotoHighOffsetL==undefined) {
		var BtnFotoHighOffsetL = 0 , BtnFotoHighOffsetR = 2;
	} else {
		//valores definidos em theme/js/config.js
	}
	
	function rePosition() {
		 /*orelha*/
		 var wWin = document.body.clientWidth;
		 var doublePage = pageImgWidth * 2;
		 
		 if (!(is.ie)) {
			 divEarRight = document.getElementById('divEarRight');
			 divEarLeft = document.getElementById('divEarLeft');
			 navegacao = document.getElementById('navegacao');
			 conteudoBook  = document.getElementById('conteudoBook');
			 divShadowRight = document.getElementById('divShadowRight');
			 divGradCenterRight = document.getElementById('divGradCenterRight');
			 divShadowLeft =  document.getElementById('divShadowLeft');
			 divGradCenterLeft =  document.getElementById('divGradCenterLeft');
			 divBtnFotoHighR = document.getElementById('divBtnFotoHighR');
			 divBtnFotoHighL = document.getElementById('divBtnFotoHighL');
			 EarRight = document.getElementById('EarRight');
			 EarLeft = document.getElementById('EarLeft');
			 dica = document.getElementById('dica');
			 Zoom = document.getElementById('Zoom');
			 ZoomOut = document.getElementById('ZoomOut');
			 medidas = document.getElementById("medidas");
			 btnPgBack = document.getElementById("btnPgBack");
			 btnPgNext = document.getElementById("btnPgNext");
			 btnPrintR = document.getElementById("btnPrintR");
			 btnPrintL = document.getElementById("btnPrintL");
			 divCarregandoZoom = document.getElementById("divCarregandoZoom");
			 btnFotoHighR = document.getElementById('btnFotoHighR');
			 btnFotoHighL = document.getElementById('btnFotoHighL');
			 btnZoomOut = document.getElementById('btnZoomOut');
			 btnSearch = document.getElementById('btnSearch');
			 btnCapa = document.getElementById('btnCapa');
			 btnIndex = document.getElementById('btnIndex');
			 btnAutoFlip = document.getElementById('btnAutoFlip');			 
			 pg2go = document.getElementById('pg2go');			 
			 btnPrintZ = document.getElementById('btnPrintZ');		
		 }
			divEarRight.style.left = ((wWin-doublePage)/2) + (pageImgWidth*2) - 48 +"px";
			EarRight.style.height = divEarRight.style.height = pageImgHeight +"px";
			
			EarLeft.style.height = divEarLeft.style.height = pageImgHeight +"px";
			navegacao.style.width = doublePage +"px";
			conteudoBook.style.width = doublePage +"px";
	  /*Sombras*/
			wWin = (wWin - doublePage)/2 ;
			divShadowRight.style.height=pageImgHeight+"px";
			divShadowRight.style.width=pageImgWidth+"px";
			divShadowRight.style.left= (wWin + pageImgWidth)+"px";
			divGradCenterRight.style.height = pageImgHeight+"px";
			divGradCenterRight.style.width= widthShadow + "px";
			divGradCenterRight.style.left=(wWin + pageImgWidth )+"px";
			divGradCenterRight.style.zIndex=300;
			
			divShadowLeft.style.height = pageImgHeight+"px";
			divGradCenterLeft.style.height = (pageImgHeight)+"px";
			divShadowLeft.style.width = pageImgWidth+"px";
			divGradCenterLeft.style.width = widthShadow + "px";
			divGradCenterLeft.style.left = (wWin + pageImgWidth - widthShadow) + "px";
			divGradCenterLeft.style.zIndex = 300;
			dica.style.left = (wWin + pageImgWidth/2 ) + "px";
			dica.style.top = (pageImgHeight/2  ) + "px";
		 //z = document.getElementById('Zoom');
			//Zoom Div
			if (screen.width > 800) {
				Zoom.style.width = parseInt(pageImgWidthHigh[0]) + parseInt(20)+ "px";
				Zoom.style.height = 668 + "px" ;
			}else {
				Zoom.style.width = doublePage + "px";
			}
			
			winSize = parseInt(document.body.clientWidth);
			zoomSize = parseInt(Zoom.style.width);
			Zoom.style.left = (( winSize-zoomSize )/2) + "px";
			ZoomOut.style.left = (( winSize-zoomSize )/2 +2)  + "px";
			
		 //Buttons HighRes
			divBtnFotoHighR.style.left = (wWin + (pageImgWidth*2) - 50 + BtnFotoHighOffsetR) + "px";
			divBtnFotoHighL.style.left = (wWin  + BtnFotoHighOffsetL) + "px";
						
			/*Botoes Impressao*/
			divBtnFotoHighR.style.top = 32; // ;pageImgHeight + 16;
			divBtnFotoHighL.style.top = 32; // pageImgHeight + 16;
			
			/*Botao Impressora*/
			if ( btnPrintL.style.display != 'none') {
					divBtnFotoHighR.style.height = 50 +"px"; // ;pageImgHeight + 16;
					divBtnFotoHighL.style.height = 50 +"px"; // pageImgHeight + 16;
					ZoomOut.style.height = 50 +"px"; // ;pageImgHeight + 16;
			}
			
	}	
	
	var iCount = 1, contaLoads = 0;
//	var progressBarOffSetACC = 0,progressBarOffSetTam = Math.round(100 / numPgs);
		function timerPreImgs() {
		 if (iCount < pageImgSources.length) {
		 	 bufferPreImg[iCount]=new Image();
					bufferPreImg[iCount].src = pageImgSources[iCount]; 
					bufferPreImg[iCount].onload  =  function(){
								if (!(isFliping)) {
										leftImg.src = bufferPreImg[iLeft].src;
										rightImg.src = bufferPreImg[iRight].src;
								}
						}
 			setTimeout("timerPreImgs()",500);
		}
		iCount ++;
	}
	iCount = 5;
	function timerPreImgsAfterFive() {
		 if (iCount < pageImgSources.length-1) {
		 	 bufferPreImg[iCount]=new Image();
					bufferPreImg[iCount].src = pageImgSources[iCount]; 
					bufferPreImg[iCount].onload  =  function(){
								
								if (pageFaultLeft) { 
									//rightImg.src = bufferPreImg[iRight].src;
									flipPage();
								}
								if (pageFaultRight) { 
									//leftImg.src = bufferPreImg[iLeft].src;

//									rightImg.src = bufferPreImg[iRight].src;
									flipPage();
								}
								
								if (!(isFliping)) {
										leftImg.src = bufferPreImg[iLeft].src;
										rightImg.src = bufferPreImg[iRight].src;
								}
								
								hideLoading();
								pageFault = pageFaultLeft = pageFaultRight = false;
				}
 			setTimeout("timerPreImgsAfterFive()",500);
		}
		iCount ++;
	}
	
	var pagesOnLoading = 0, autoFlipFault=0;
	function loadPageOnDemand (indice, quantPages) {
					if (bufferPreImg[indice-1] == 'undefined' || bufferPreImg[indice-1] == null) {
						bufferPreImg[indice-1]=new Image();
						bufferPreImg[indice-1].src = pageImgSources[indice-1]; 		
					}
					
					for (i=0; ( i <= quantPages)  && ( (i+indice)<=iLastPage ) ;i++){
 										var i2Load = i+indice;
											if (bufferPreImg[i2Load] == 'undefined' || bufferPreImg[i2Load] == null) {
													bufferPreImg[i2Load] = new Image();
													bufferPreImg[i2Load].src = pageImgSources[i2Load]; 	
													pagesOnLoading++;	
											}
					}
					

					/* autoFlipFault = 0 sem falha
								autoFlipFault = -1 falha à esquerda
								autoFlipFault = 1 falha à direita
					*/
					if (!autoFlip) {
							stopAutoFlip();
							if (pageFaultRight) autoFlipFault = 1;
							else autoFlipFault = -1;							
					}
					
					bufferPreImg[indice].onload  =  function() {
										flipPage();
										if (autoFlipFault!=0) {
											 //StartAutoFlip
												btnAutoFlip.value = "Stop-Flip";
												if (zoomIsActive) offZoom(); 
												if (onBack || iRight==iLastPage || autoFlipFault==-1) {
														ID_AutoFlip = setInterval("flipPagePar()", 5000);
												}	else {
													 ID_AutoFlip = setInterval("flipPageImpar()", 5000);
												}
												autoFlip = 0;
												autoFlipFault = 0;
										}
										
										hideLoading();

										if (pageFaultRight) rightImg.src = bufferPreImg[iRight].src;
										if (pageFaultLeft) leftImg.src = bufferPreImg[iLeft].src;

										pageFault = pageFaultLeft = pageFaultRight = false;
										bufferPreImg[indice].onload = '';
										pagesOnLoading--;
				}
				if (bufferPreImg[indice] != 'undefined') {
					bufferPreImg[indice].src = pageImgSources[indice]; 
				}
				//window.status = pagesOnLoading;
	}
	function loadOnePageOnDemand (indice) {
					/*if (bufferPreImg[indice] == 'undefined' || bufferPreImg[indice] == null) {
						bufferPreImg[indice]=new Image();
						bufferPreImg[indice].src = pageImgSources[indice]; 		
						bufferPreImg[indice+1]=new Image();
						bufferPreImg[indice+1].src = pageImgSources[indice+1]; 		
					}*/
	}
	function timerSetTokenLogin(){
			submitTokenLogin();
			setTimeout("timerSetTokenLogin()",47000); 
	}
			
	function CreateBookX() {
		if(document.getElementById) {
			rePosition();
			bufferPreImg[0]=new Image();bufferPreImg[0].src=pageImgSources[0]; 
			//for(i=1;i<pageImgSources.length;i++) { 
			//	bufferPreImg[i]=new Image();
			//	bufferPreImg[i].src=pageImgSources[i]; 
			//}

			var LIMITFIRSTIMAGES = (iLastPage < 5)? iLastPage : 5; // testa se flip é menor q 5 pgs
			for(i=1;i < LIMITFIRSTIMAGES; i++) { 
				bufferPreImg[i]=new Image();
				bufferPreImg[i].src=pageImgSources[i]; 
			}
			bufferPreImg[iLastPage]=new Image();
			bufferPreImg[iLastPage].src=pageImgSources[iLastPage];
			//bufferPreImg[iLastPage-1]=new Image();
			//bufferPreImg[iLastPage-1].src=pageImgSources[iLastPage-1]; 
			
			//bufferPreImg[iPgIndex]=new Image();
			//bufferPreImg[iPgIndex].src=pageImgSources[iPgIndex]; 
			
			//setTimeout("timerPreImgs()",100); 
		//	setTimeout("timerPreImgsAfterFive()",5000); 
			//timerSetTokenLogin();
			
			Zoom_Div=document.getElementById("Zoom");
			Zoom_Div.style.height=(pageImgHeight)+"px";
			Zoom_Div.style.width=(pageImgWidth * 2)+"px";
			
			zoomImg = document.createElement("img");Zoom_Div.appendChild(zoomImg);
			zoomImg.src = "pixel_transp.gif";
			zoomImg.style.top = 0+"px";
			zoomImg.style.left = 0+"px";
			zoomImg.style.height = pageImgHeight+"px";
			zoomImg.style.width = (pageImgWidth-50)+"px";
			zoomImg.style.position = "absolute";
			
			RevistaDiv=document.getElementById("book");
			leftImg=document.createElement("img");RevistaDiv.appendChild(leftImg);	
			rightImg=document.createElement("img");RevistaDiv.appendChild(rightImg);
			midImg=document.createElement("img");RevistaDiv.appendChild(midImg);	
			leftImg.style.position=midImg.style.position=rightImg.style.position="absolute";
			leftImg.style.zIndex=rightImg.style.zIndex=1;midImg.style.zIndex=2;
			RevistaDiv.style.visibility="hidden";
			medidas.style.visibility="hidden"; 
						
			leftImg.style.top=(flipVerticalTurn?pageImgHeight+1:0)+"px";
			leftImg.style.left=0+"px";
			midImg.style.top=0+"px";
			midImg.style.left=(flipVerticalTurn?0:pageImgWidth+1)+"px";
			rightImg.style.top=0+"px";
			rightImg.style.left=(flipVerticalTurn?0:pageImgWidth+1)+"px";
			
			leftImg.style.height=pageImgHeight+"px";
			midImg.style.height=pageImgHeight+"px";
			rightImg.style.height=pageImgHeight+"px";

			medidas.style.height=pageImgHeight+"px";
			medidas.style.width=pageImgWidth+"px";
			
			leftImg.style.width=pageImgWidth+"px";
			midImg.style.width=pageImgWidth+"px";
			rightImg.style.width=pageImgWidth+"px";
			
			midImg.style.visibility = "hidden";
			
			if(borderPage){
				medidas.style.borderStyle=leftImg.style.borderStyle=midImg.style.borderStyle=rightImg.style.borderStyle="solid";
				leftImg.style.borderWidth=1+"px";
				midImg.style.borderWidth=1+"px";
				rightImg.style.borderWidth=1+"px";
				medidas.style.borderWidth=0+"px";
				medidas.style.borderColor=leftImg.style.borderColor=midImg.style.borderColor=rightImg.style.borderColor=zoomImg.style.borderColor=borderPageColor
			}else {
				leftImg.style.borderStyle=midImg.style.borderStyle=rightImg.style.borderStyle="solid";
				leftImg.style.borderWidth=0+"px";
				midImg.style.borderWidth=0+"px";
				rightImg.style.borderWidth=0+"px";
				leftImg.style.borderColor=midImg.style.borderColor=rightImg.style.borderColor=zoomImg.style.borderColor=borderPageColor;
			}
			
			
			//Slider Configurations
			s = new Slider(document.getElementById("slider"), document.getElementById("slider-input-1"));
			s.setMaximum(Math.floor(pageImgSources.length/2));
			s.setMinimum(0);
			s.setValue(0);
			s.onchange = function () {
								if (s.getValue()!=0) gotoPage(s.getValue()*2);
								else gotoPage(1);
								if (initTimeInPage.ok) getTimeInPage();
							}
			
			/*orelhas*/
			if (is.ie) {
				divEarRight.filters[0].enabled=false;
				divEarLeft.filters[0].enabled=false;
				divEarRight.onmouseout = function(){divEarRight.filters[0].enabled=false;}
			 divEarRight.onmouseover = function(){divEarRight.filters[0].enabled=true;}
			 divEarLeft.onmouseout = function(){divEarLeft.filters[0].enabled=false;}
				divEarLeft.onmouseover = function(){divEarLeft.filters[0].enabled=true;}
										
				rightImg.style.filter="blendTrans(duration=1)";
				rightImg.filters.blendTrans.Apply();
				rightImg.filters.blendTrans.Play();
			}
			rightImg.onload = function() {  
					RevistaDiv.style.visibility="visible"; 
					medidas.style.visibility="visible"; 
					hideLoading(); 
					divShadowRight.style.visibility="visible"; 
					divGradCenterRight.style.visibility = "visible";
					//dica.style.visibility = "visible";
					showDica();
					rightImg.onload = "";
					showPage(1);
					if (page2go!='') gotoPage(page2go);
			}
			
			hidePage(0);
			//showPage(1);
			
			var tempIMGLoading = new Image();
			tempIMGLoading.onload = function(){
				window.status = '5 imagens';
				rightImg.src=bufferPreImg[iFirstPage].src;
		 	leftImg.src=bufferPreImg[iLastPage].src;
  		midImg.src=bufferPreImg[iFirstPage].src;
			}
			tempIMGLoading.src = pageImgSources[LIMITFIRSTIMAGES];
			
			
			
			
			/* Zoom Image*/
			tempZoomImg = new Image();
			tempZoomImg.onload = function() { zoomImg.src = tempZoomImg.src; hideLoading(); }
			/*----*/
			
			//Define acoes
			EarLeft.onclick = function() { flipPagePar(); stopAutoFlip(); }
			EarRight.onclick = function() { flipPageImpar(); stopAutoFlip();	}
			
			btnPgBack.onclick = function() { flipPagePar(); stopAutoFlip(); }
			btnPgNext.onclick = function() { flipPageImpar(); stopAutoFlip();	}
			
			urlFotoHigh = "print.php?foto=" + pageImgSourcesHigh[0] + "&ID=" + modelID;
			btnPrintR.onclick = function() {openFotoHigh(urlFotoHigh, pageA4Width, pageA4Height)};
			
			/*Acoes de Zoom*/
			btnFotoHighR.onclick = function() { zoomPageRight();	}
		 btnFotoHighL.onclick = function() {	zoomPageLeft();		}
		 
		 rightImg.onclick = zoomPageRight;
			leftImg.onclick = zoomPageLeft;
			zoomImg.onclick = offZoom;
		 /**/
		 
		 btnZoomOut.onclick = function() {offZoom();}
			
			btnSearch.onclick = function() {openSearch("frmSearch.php?ID="+idEdicao);}
			
			btnCapa.onclick = function() {if (!(autoFlip)) stopAutoFlip(); gotoPage(1);}
		
			btnIndex.onclick = function() {if (!(autoFlip)) stopAutoFlip(); gotoPage(iPgIndex);}
			
			btnAutoFlip.onclick = function() {
					if(autoFlip){
						startAutoFlip();						
						//btnAutoFlip.value = "Stop-Flip";
						//autoFlip = 0;
					}else {
						stopAutoFlip();
						//btnAutoFlip.value = "Auto-Flip";
						//autoFlip = 1;
					}
				}
			
			pg2go.value = pageTextSource[iRight];
			pg2go.onclick = function() {
					s.setValue( pg2go.value / 2);
				}
			
			window.onresize = function () {
				s.recalculate();
				rePosition();
			}
			rePosition();
			
			leftImg.style.cursor  =  "crosshair";
			rightImg.style.cursor = "crosshair";
			zoomImg.style.cursor  = "crosshair";
			midImg.style.cursor  = "crosshair";
			
		 rightImg.useMap = "#MapPg"+(iRight+1)//+"z";
		}
	}

	function flipPage() {
		isFliping = 1;
		if (openPage) {
			midImg.style.visibility = "visible"; // goFlipPageImPar
			midResize=Math.abs(Math.round(Math.cos(Angle)*pageImgWidth));
			MidOffset=flipDirection?pageImgWidth+1:pageImgWidth-midResize;
			midImg.style.left=MidOffset+"px";
			midImg.style.width=midResize+"px";
			Angle+=flipSpeed/720*Math.PI;

			if(Angle>=Math.PI/2&&flipDirection){
				flipDirection=0;
				midImg.src=bufferPreImg[iLeft].src;
			}
			if(Angle>=Math.PI){
				flipDirection=1;
				tempImg=leftImg;
				leftImg=midImg;
				midImg=tempImg;
				if(flipVerticalTurn)midImg.style.top=0+"px";
				else midImg.style.left=pageImgWidth+1+"px";			
				midImg.src=rightImg.src;
				rightImg.src=bufferPreImg[iRight].src;
				
				//rightImg.useMap = "#MapIndex";												
				
				Angle=0;
				midImg.style.zIndex=0;
				midImg.style.visibility = "hidden";
				
				//ativa eventos para virar pagina
				EarLeft.onclick=flipPagePar;
				EarRight.onclick=flipPageImpar;
				
			 rightImg.onclick = zoomPageRight;
			 leftImg.onclick = zoomPageLeft;
			
				//alert( " iLeft:" + iLeft + " iRight:" + iRight + " iPgIndex:" + iPgIndex);
				
				//MAPA
				if (iRight == (iPgIndex-1)){
				  rightImg.useMap = "#MapIndex";
					 leftImg.useMap = "#MapPg"+(iLeft+1)//+"z";
				} else {
					 if (iLeft == (iPgIndex-1)) {
					 		leftImg.useMap = "#MapIndex";
					 	 rightImg.useMap = "#MapPg"+(iRight+1)//+"z";
				  } else {
				  	 rightImg.useMap = "#MapPg"+(iRight+1)//+"z";
				    leftImg.useMap = "#MapPg"+(iLeft+1)//+"z";
					 }
				}

				//window.status='open';
				
				//Ativa links para foto alta resolução
				//urlFotoHigh = "print.php?foto=" + pageImgSourcesHigh[iRight] + "&ID=" + modelID;
				//btnFotoHighR.onclick = function() {openFotoHigh(urlFotoHigh, pageImgWidthHigh[iRight], pageImgHeightHigh[iRight])};
				//urlFotoHigh2 = "print.php?foto=" + pageImgSourcesHigh[iLeft] + "&ID=" + modelID;
				//btnFotoHighL.onclick = function() {openFotoHigh(urlFotoHigh2, pageImgWidthHigh[iLeft], pageImgHeightHigh[iLeft])};
				urlFotoHigh = "print.php?foto=" + pageImgSourcesHigh[iRight] + "&ID=" + modelID;
			 btnPrintR.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoHigh, pageA4Width, pageA4Height)};
			 urlFotoHigh2 = "print.php?foto=" + pageImgSourcesHigh[iLeft] + "&ID=" + modelID;
			 btnPrintL.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoHigh2, pageA4Width, pageA4Height)};
			//	btnFotoHighR.onclick = function() {zoomPageRight();};
		 // btnFotoHighL.onclick = function() {zoomPageLeft();};
										
				//atualiza div com numero da pagina
				/*
				pg = (iRight==0) ? "" : iLeft+1;
				labelPgLeft.innerHTML = "<div class=txtNumPg>" + pg + "</div>";
				pg = (iLeft==iLastPage) ? "" :iRight+1;
				labelPgRight.innerHTML = "<div class=txtNumPg>" + pg + "</div>";
				*/
				
				//if (initTimeInPage.ok) getTimeInPage();
				if ((onBack && !autoFlip) || (iRight==iLastPage && !autoFlip)) {
				  EarLeft.style.visibility="visible";
				  stopAutoFlip();
				}
				onFront=0;
				
				//impressao do composite
				//if (onFront) {
				//	 btnFotoHighL.onclick = function() {openComposite(urlComposite, pageImgWidth * 2, pageImgHeight)};
				//}
				
				writeNumPage();
				showPage(0);
				initTimeInPage();
				isFliping = 0;
				loadOnePageOnDemand((iRight+3));
			} else 
				setTimeout("flipPage()",50);
		} else {
			midImg.style.visibility = "visible"; //goFlipPagePar
			midResize=Math.abs(Math.round(Math.cos(Angle)*pageImgWidth));
			MidOffset=flipDirection?pageImgWidth-midResize:pageImgWidth+1;
			midImg.style.left=MidOffset+"px";
			midImg.style.width=midResize+"px";
			Angle+=flipSpeed/720*Math.PI;
	
			if(Angle>=Math.PI/2&&flipDirection){
				flipDirection=0;
				midImg.src=bufferPreImg[iRight].src;
			}
			if(Angle>=Math.PI){
				flipDirection=1;
				tempImg=rightImg;
				rightImg=midImg;
				midImg=tempImg;
				if(flipVerticalTurn)midImg.style.top=0+"px";
				else midImg.style.left=pageImgWidth+1+"px";			
				midImg.src=rightImg.src;
				leftImg.src=bufferPreImg[iLeft].src;
				
				Angle=0;
				midImg.style.zIndex=0;
				midImg.style.visibility = "hidden";
				
				//ativa eventos para virar pagina
				EarLeft.onclick=flipPagePar;
				EarRight.onclick=flipPageImpar;

			 rightImg.onclick = zoomPageRight;
			 leftImg.onclick = zoomPageLeft;

				//MAPA
				if (iRight == (iPgIndex-1)){
					 rightImg.useMap = "#MapIndex";
					 leftImg.useMap = "#MapPg"+(iLeft+1)//+"z";
				} else {
					 if (iLeft == (iPgIndex-1)) {
					 		leftImg.useMap = "#MapIndex";
					 		rightImg.useMap = "#MapPg"+(iRight+1)//+"z";
				  } else {
					 	 rightImg.useMap = "#MapPg"+(iRight+1)//+"z";
				    leftImg.useMap = "#MapPg"+(iLeft+1)//+"z";
					 }
				}

				//Ativa links para foto alta resolução
				//urlFotoHigh = "print.php?foto=" + pageImgSourcesHigh[iRight] + "&ID=" + modelID;
				//btnFotoHighR.onclick = function() {openFotoHigh(urlFotoHigh, pageImgWidthHigh[iRight], pageImgHeightHigh[iRight])};
				//urlFotoHigh2 = "print.php?foto=" + pageImgSourcesHigh[iLeft] + "&ID=" + modelID;
		  //btnFotoHighL.onclick = function() {openFotoHigh(urlFotoHigh2, pageImgWidthHigh[iLeft], pageImgHeightHigh[iLeft])};
				urlFotoHigh = "print.php?foto=" + pageImgSourcesHigh[iRight] + "&ID=" + modelID;
			 btnPrintR.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoHigh, pageA4Width, pageA4Height)};
			 urlFotoHigh2 = "print.php?foto=" + pageImgSourcesHigh[iLeft] + "&ID=" + modelID;
			 btnPrintL.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoHigh2, pageA4Width, pageA4Height)};
				//btnFotoHighR.onclick = function() {zoomPageRight();};
		  //btnFotoHighL.onclick = function() {zoomPageLeft();};

				
				/*
				pg = (iRight==0) ? "" : iLeft+1;
				labelPgLeft.innerHTML = "<div class=txtNumPg>" + pg + "</div>";
				pg = (iLeft==iLastPage) ? "" :iRight+1;
				labelPgRight.innerHTML = "<div class=txtNumPg>" + pg + "</div>";
				*/
				if (onFront && !autoFlip) {
				  EarRight.style.visibility="visible";
				  stopAutoFlip();
				}
				onBack=0;
				writeNumPage();
				//if (initTimeInPage.ok) getTimeInPage();
				
				showPage(1);
				initTimeInPage();
				isFliping = 0;
				
				//impressao do composite
				//if (onFront) {
				//	 btnFotoHighL.onclick = function() {zoomPageRight()};
				//}
				loadOnePageOnDemand((iRight-2));
			} else 
				setTimeout("flipPage()",50);
		}
	}

	function writeNumPage(){
		try {
			pgL = (iRight==0) ? "" : pageTextSource[iLeft];
			pgR = (iLeft==iLastPage) ? "" : pageTextSource[iRight];
			separator = ((iRight==0)&&(iLeft==iLastPage)) ? "" : "|";
			
			pgL = (onBack) ? pageTextSource[iLastPage]  : pgL;
			pgR = (onFront) ? pageTextSource[0] : pgR;

			pg2go.value = pgL + separator + pgR;
		} catch(error){
			pgL = (iRight==0) ? "" : iLeft+1;
			pgR = (iLeft==iLastPage) ? "" : iRight+1;
			separator = ((iRight==0)&&(iLeft==iLastPage)) ? "" : "|";
			
			pgL = (onBack) ? iLastPage+1  : pgL;
			pgR = (onFront) ? "1" : pgR;
			
			pg2go.value = pgL + separator + pgR;
		}
	}
	
	function flipPageImpar() {EarRight.style.visibility="hidden"; numPage = iNavPgImg + 2; s.setValue(numPage/2);} //gotoPage(numPage); }
	function flipPagePar() { EarLeft.style.visibility="hidden"; numPage = iNavPgImg - 1; s.setValue(numPage/2);} //gotoPage(numPage); }
	
	function hidePage(side){
		//getTimeInPage();
		if (side) { // side == 1, oculta pgDireita
			rightImg.style.visibility = "hidden";
			EarRight.style.visibility = "hidden";
			divEarRight.style.visibility = "hidden";
			divShadowRight.style.visibility = "hidden";
			divGradCenterRight.style.visibility = "hidden";
			divBtnFotoHighR.style.visibility = "hidden";
			//medidas.style.visibility = "hidden";

		} else {
			leftImg.style.visibility = "hidden";
			EarLeft.style.visibility = "hidden";
			divEarLeft.style.visibility = "hidden";
			divShadowLeft.style.visibility = "hidden";
			divGradCenterLeft.style.visibility = "hidden";
			divBtnFotoHighL.style.visibility = "hidden";
			//btnFotoHighL.onclick = function() { zoomPageRight(); };
		}
	}
	
	function showPage(side){
		//initTimeInPage();
		if (side) { // side == 1, exibe pgDireita
			divShadowRight.style.visibility = "visible";
			divShadowRight.style.zIndex = -10;
			divGradCenterRight.style.visibility = "visible";
			rightImg.style.visibility = "visible";
			EarRight.style.visibility = "visible";
			divEarRight.style.visibility = "visible";
			divBtnFotoHighR.style.visibility = "visible";
			if (!(onFront))	EarLeft.style.visibility = "visible";
		} else {
			divShadowLeft.style.visibility = "visible";
			divShadowLeft.style.zIndex = -10;
			divGradCenterLeft.style.visibility = "visible";
			leftImg.style.visibility = "visible";
 		EarLeft.style.visibility = "visible";
 		divEarLeft.style.visibility = "visible";
			divBtnFotoHighL.style.visibility = "visible";
			if (!(onBack)) EarRight.style.visibility = "visible";
		}
	}

	function goFlipPageImpar() {
		if (iRight >= pageImgSources.length) {
			iRight = iFirstPage;
			iLeft = iLastPage;
			iNavPgImg =iLastPage;
			hidePage(1);
			onBack=1; //indica q esta na ContraCapa
			//labelPgRight.innerHTML = "<div></div>";
		} 
		//turn2Right
		midImg.src = rightImg.src;
		midImg.style.zIndex=2;		
		rightImg.style.zIndex=1;
		leftImg.style.zIndex=1;
	//	midImg.style.visibility = "visible"; 
		
		openPage=1; 
		EarLeft.onclick="";
		EarRight.onclick="";
		try {
			rightImg.src = bufferPreImg[iRight].src;
			flipPage();
		} catch (error) {
			showLoading();
			pageFaultRight = true;
			loadPageOnDemand(iRight,4);
		}
	}
	
	function goFlipPagePar() {
		if (iNavPgImg <= 0) {
			iRight = iFirstPage;
			iLeft = iLastPage;
			iNavPgImg = 0;
			
			hidePage(0);
			onFront=1; // indica q esta na Capa
		}		
		//turn2Left		
		midImg.src = leftImg.src;		
		midImg.style.zIndex=2;
		rightImg.style.zIndex=1;
		leftImg.style.zIndex=1;
		//midImg.style.visibility = "visible"; 		
		
		openPage=0; 
		EarLeft.onclick="";
		EarRight.onclick="";
		try {
			leftImg.src = bufferPreImg[iLeft].src;
			flipPage();
		} catch (error) {
			showLoading();
			pageFaultLeft = true;
			loadPageOnDemand(iRight,4);
		}
	}
	
	function gotoPage(numPage){
		if (zoomIsActive) offZoom(); 

		if (numPage < iLastPage+1) s.setValue( numPage/2 ); 
		 
		if ( (numPage>0) && (numPage<=iLastPage+1) ) { //restrige o range de navegação
			var offSet = numPage - iNavPgImg;
			var iAtual = iNavPgImg;
			var stay = 1;

			if (numPage%2==0) 
				if (iAtual!=numPage) iNavPgImg += offSet; // se pagina par e diferente da atualLeft
				else stay = 0;
			else 
				if (iAtual+1!=numPage) iNavPgImg += offSet-1; // se pagina impar e diferente da atualRight
				else stay = 0;
//						alert(iNavPgImg + "-" +iLastPage);
			if (stay==1) {
				iRight = iNavPgImg;
				iLeft = iNavPgImg-1;	
				if (iAtual<iNavPgImg) {
					goFlipPageImpar(); 

				} else goFlipPagePar();
			}
		}
	}
	
	function stopAutoFlip (){
		btnAutoFlip.value = "Auto-Flip";
		clearInterval(ID_AutoFlip);
		autoFlip = 1;
	}
	
	function startAutoFlip (){
		btnAutoFlip.value = "Stop-Flip";
		if (zoomIsActive) offZoom(); 
							
		if (onBack || iRight==iLastPage) {
			 flipPagePar();
				ID_AutoFlip = setInterval("flipPagePar()", 5000);
		}	else {
			 flipPageImpar();
			 ID_AutoFlip = setInterval("flipPageImpar()", 5000);
		}
		autoFlip = 0;
	}
	
	function offZoom(){
				if (onFront) { showPage(1); hidePage(0);}
		else
			if (onBack) {showPage(0); hidePage(1);}
			else
				{showPage(0); showPage(1);}				
		
		zoomIsActive = 0;
		onZoomIndex = 0;
		hideLoading();
		
		getTimeInZoomPage();
		
		medidas.style.visibility = "visible";
		Zoom_Div.style.visibility = "hidden";		
		ZoomOut.style.visibility = "hidden";		
		if (is.ie) {
		zoomImg.filters.blendTrans.Apply();
		zoomImg.style.visibility = "hidden";
		zoomImg.filters.blendTrans.Play();
		}else{
			zoomImg.style.visibility = "hidden";
		}
		zoomImg.src = "pixel_transp.gif";
		//Zoom_Div.style.display = "none";
		
	}
	
	function zoomPageRight() {
		if (!isFliping){
			medidas.style.visibility = "hidden";
			hidePage(0);
			hidePage(1);
			zoomIsActive = 1;
			onZoomIndex = iRight;
			
			urlFotoZoom = "print.php?foto=" + pageImgSourcesHigh[onZoomIndex] + "&ID=" + modelID;
			btnPrintZ.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoZoom, pageA4Width, pageA4Height)};
			
			stopAutoFlip();
			
			initTimeInZoomPage();
			
			zoomImg.src = rightImg.src;
			if (is.ie) {
				zoomImg.style.filter="blendTrans(duration=1)";
				zoomImg.filters.blendTrans.Apply();
				zoomImg.filters.blendTrans.Play();
			}
			Zoom_Div.style.visibility = "visible";
			//Zoom_Div.style.display = "block";
			zoomImg.style.visibility = "visible";
			ZoomOut.style.visibility = "visible";
			RevistaDiv.style.zIndex=0;
			Zoom_Div.style.zIndex=16;
			
			
			//B_Pre_Img_Zoom[iRight]=new Image();B_Pre_Img_Zoom[iRight].src=pageImgSourcesZoom[iRight];
			//zoomImg.src = B_Pre_Img_Zoom[iRight].src;
			loadZoom();
			
			zoomImg.style.width = pageImgWidthHigh[onZoomIndex]+"px";
			zoomImg.style.height = pageImgHeightHigh[onZoomIndex]+"px";
			zoomImg.style.zIndex=17;
		}
	}

	function zoomPageLeft () {
		if (!isFliping){
			medidas.style.visibility = "hidden";
			hidePage(0);
			hidePage(1);
			zoomIsActive = 1;
			onZoomIndex = iLeft;
			
			urlFotoZoom = "print.php?foto=" + pageImgSourcesHigh[onZoomIndex] + "&ID=" + modelID;
			btnPrintZ.onclick = function() {if (!(autoFlip)) stopAutoFlip();openFotoHigh(urlFotoZoom, pageA4Width, pageA4Height)};
			
			stopAutoFlip();
			
			initTimeInZoomPage();
			
			zoomImg.src = leftImg.src;
			if (is.ie) {
				zoomImg.style.filter="blendTrans(duration=1)";
				zoomImg.filters.blendTrans.Apply();
				zoomImg.filters.blendTrans.Play();
			}
			
					
			Zoom_Div.style.visibility = "visible";
			//Zoom_Div.style.display = "block";
			zoomImg.style.visibility = "visible";
			ZoomOut.style.visibility = "visible";
			RevistaDiv.style.zIndex=0;
			Zoom_Div.style.zIndex=16;
			
			
			//B_Pre_Img_Zoom[iRight]=new Image();B_Pre_Img_Zoom[iRight].src=pageImgSourcesZoom[iRight];
			//zoomImg.src = B_Pre_Img_Zoom[iRight].src;
		//tempZoomImg = pageImgSourcesHigh[onZoomIndex];
			
		 loadZoom();
			
			zoomImg.style.width = pageImgWidthHigh[onZoomIndex]+"px";
			zoomImg.style.height = pageImgHeightHigh[onZoomIndex]+"px";
			zoomImg.style.zIndex=17;
		}
	}
	
	function loadZoom() {
		showLoading();
		tempZoomImg.src = pageImgSourcesHigh[onZoomIndex];  		
		
		//MAPA
		if (onZoomIndex == (iPgIndex-1)){
			 zoomImg.useMap = "#MapIndexZoom";
		}else {
			 //zoomImg.useMap = "";
			 zoomImg.useMap = "#MapPg"+(onZoomIndex+1)+"z";
		}
	}
	
	function disableZoomHandle(){
		rightImg.onclick="";
		leftImg.onclick="";

		leftImg.style.cursor = "hand";
		rightImg.style.cursor = "hand";
		zoomImg.style.cursor = "hand";
		//zoomImg.onclick="";
	}
	
	function enableZoomHandle(){
		rightImg.onclick=clickOnPageRight;
		leftImg.onclick=clickOnPageLeft;
		zoomImg.onclick=offZoom;

		leftImg.style.cursor = "crosshair";
		rightImg.style.cursor = "crosshair";// "url('includes/maiszoom.cur')";
		zoomImg.style.cursor = "crosshair";// "url('includes/menoszoom.cur')";
	}
	
	function showLoading(){
		divCarregandoZoom.style.visibility="visible"; 
	}
	function hideLoading(){
		if (!(loadingSite)) divCarregandoZoom.style.visibility="hidden"; 
	}
	
	function clickOnPageRight () {
		zoomPageRight();
	}
	function clickOnPageLeft () {
		zoomPageLeft();
	}
	function clickOnPageZoom () {
		offZoom();
	}
	
	function showDica(){
		dica.style.visibility = "visible";
		setTimeout("hideDica()", 4000);
	}
	function hideDica(){
		dica.style.visibility = "hidden";
	}
	
	function getQueryString( sProp ) {
		var re = new RegExp( sProp + "=([^\\&]*)", "i" );
		var a = re.exec( document.location.search );
		if ( a == null ) return "";
		return a[1];
	}
	function GetKey(evt){
	  if(navigator.appName=="Netscape"){theKey=evt.which}
	  if(navigator.appName.indexOf("Microsoft")!=-1){theKey=window.event.keyCode}
	  //window.status=theKey;
	  return theKey;
 }