var pdfjsLib = window['pdfjs-dist/build/pdf'];
var pdfcanvas = "the-canvas";
var cnt = 100;


function renderPage() {

    var el_title = document.getElementById('pdfprogress');
  

    el_title.innerHTML = 'Идет подготовка страницы ...';

    pageRendering = true;
    // Using promise to fetch the page
    pdfDoc.getPage(pageNum).then(function(page) {

    var canvas = document.getElementById(pdfcanvas);  
    var context = canvas.getContext('2d');
    //var viewport = page.getViewport({scale: scale});

   /* if (category == 3)
      currscale = 3.5;
    else
      currscale = 2.5;  */

    currscale = 2;  
    
    var viewport = page.getViewport({scale: currscale});
   // var viewport = page.getViewport(canvas.width / page.getViewport(0.5).width)
    
   
      canvas.height = viewport.height;
      canvas.width = viewport.width;
  
      // Render PDF page into canvas context
      var renderContext = {
        canvasContext: context,
        viewport: viewport
      };
      var renderTask = page.render(renderContext);
  
      // Wait for rendering to finish
      renderTask.promise.then(function() {
        pageRendering = false;
        if (pageNumPending !== null) {
          // New page rendering is pending
          renderPage(pageNumPending);
          pageNumPending = null;
         
        }
          el_title.innerHTML = '';
      });
    });
    
    
   
}  


function prevclick(){
    prevurl = prevlink + '/'+ scale;
    location.href = prevurl;
    return;
}

function nextclick(){
    nexturl = nextlink + '/'+ scale;
    location.href = nexturl;
    return;
}


function pdfzoomin(){

    scale += 0.25;
    setzoom(scale);
    renderPage();


}

function pdfzoomout(){

    scale -= 0.25;
   setzoom(scale);
    renderPage();

}


function resize_plus(){
  if (window.screen.width >= 560) {
    cnt += 5;
  }
  else{
    cnt += 20;
  }
  newpdfzoom(cnt);
}


function resize_minus(){  
  if (window.screen.width >= 560) {
    cnt -= 5;
  }
  else{
    cnt -= 20;
  }
  newpdfzoom(cnt);
}

function newpdfzoom(czoom){
  
  var status = czoom+"%";
  
  document.getElementById(pdfcanvas).style.width = status;
  document.getElementById('zoomstatus_up').innerHTML = status;
  //document.getElementById('zoomstatus_down').innerHTML = status;

  setzoom('pdfscale', czoom);

}  

function getzoom( name ){

  
  var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
 
  if ( results ){
    return parseFloat(unescape(results[2]));
  }  
  else {
    return null;
  }  

}

function setzoom( cname, cvalue, exdays = 30 ) {
    
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    
   
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

  }





function startpdfviewer(newcurr){

  pdcurr =  getzoom('pdcurr');
  
  if (pdcurr == newcurr){
    czoom = getzoom('pdfscale');
    if (czoom > 10) {
        cnt = czoom;
        newpdfzoom(czoom);
    }  
  }
  else {
     setzoom('pdcurr', newcurr);
     setzoom('pdfscale', 100);
  }
      
    
    pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
        pdfDoc = pdfDoc_;
        renderPage();
    });
    
}


function changepage(){

  var e = document.getElementById("pages");
  var currpage = e.options[e.selectedIndex].value;
  var newpage =  baseurl + currpage;

  location.href = newpage;

  return true;

}


function changeyear(){

  var e = document.getElementById("year_filter");
  var curryear = e.options[e.selectedIndex].value;

  yearurl = '/mybooks/gazeta/';

  if (curryear !== 'all')
    yearurl = yearurl + curryear + '/1';
  
  location.href = yearurl;

  return true;


}



