/*
 *  JavaScript for paged-PDF books
 */

function init() {
  // search box
  var searchEl = document.getElementById('search');
  var searchBtn = document.getElementById('searchButton');
  searchEl.style.width = 180 - searchBtn.offsetWidth - 19 + 'px';
}

function initInc() {
  // incremental search box
  var searchElInc = document.getElementById('searchInc');
  var str = location.search.substring(1);
  if (str.length > 0) {
    if (window.decodeURI) {
      str = decodeURI(str);
    } else {
      str = unescape(str);
    }
    searchElInc.value = str;
  }
  // pujde zaradit kurzor na konec vepsaneho retezce?
/*
  try {
    var rng = searchElInc.createTextRange();
    //alert(rng.text);
    searchElInc.insertAdjacentText('beforeEnd', 'TEST');
    //rng.moveToElementText(searchElInc);
  }
  catch (ex) {
    alert('ZADRHEL!');
  }
*/
  // focus input box
  searchElInc.focus();
}

function searchFor(what) {
  if (window.decodeURI) {
    what = encodeURI(what);
  } else {
    what = escape(what);
  }
  document.location = 'search.html?'+what;
}

function openPage() {
  var elValue = document.getElementById('openPageBox').value;
  elValue = Math.floor(elValue);
  if ( (elValue >= 1) && (elValue <= maxPage) ) {
    elValue = formatValue(elValue);
    document.location = elValue + '.html';
  } else {
    var message = '';
    switch (lang) {
      case 'cze':
        message = 'Prosím, zadejte číslo mezi 1 a ' + maxPage + '.';
        break;
      case 'eng':
        message = 'Please enter the number between 1 and ' + maxPage + '.';
        break;
    }
    alert(message);
  }
}

function formatValue(value) {
  if (value < 10) {
    value = '00' + value;
  } else if (value < 100) {
    value = '0' + value;
  }
  return value;
}

