/* * * * * * * * * * * * *
 * BEGIN touchtap-event  *
 * * * * * * * * * * * * */

/**
 * @license
 * touchtap-event <http://github.com/Tyriar/touchtap-event>
 * Copyright 2014 Daniel Imms <http://www.growingwiththeweb.com>
 * Released under the MIT license <http://github.com/Tyriar/touchtap-event/blob/master/LICENSE>
 */
(function () {
  'use strict';

  var touchTapEvent;
  var isTapLength;
  var tapLengthTimeout;
  var startPosition = { x: -1, y: -1 };
  var currentPosition = { x: -1, y: -1 };

  /**
   * Gets the touch object from a touch* event.
   * @param {Event} e The event.
   * @returns {Touch} The (first) touch object from the event.
   */
  function getTouchObject(e) {
    if (e.originalEvent && e.originalEvent.targetTouches) {
      return e.originalEvent.targetTouches[0];
    }
    if (e.targetTouches) {
      return e.targetTouches[0];
    }
    return e;
  }

  /**
   * Gets whether two numbers are approximately equal to each other.
   * @param {number} a The first number.
   * @param {number} b The second number.
   * @returns {Boolean}
   */
  function approximatelyEqual(a, b) {
    return Math.abs(a - b) < 2;
  }

  /**
   * Handler for the touchstart event.
   * @param {Event} e The touchstart event.
   */
  function touchstart(e) {
    var touchObject = getTouchObject(e);
    startPosition.x = touchObject.pageX;
    startPosition.y = touchObject.pageY;
    currentPosition.x = touchObject.pageX;
    currentPosition.y = touchObject.pageY;
    isTapLength = true;
    if (tapLengthTimeout) {
      clearTimeout(tapLengthTimeout);
    }
    tapLengthTimeout = setTimeout(function () {
      isTapLength = false;
    }, 200);
  }

  /**
   * Handler for the touchend event.
   * @param {Event} e The touchend event.
   */
  function touchend(e) {
    if (isTapLength &&
        approximatelyEqual(startPosition.x, currentPosition.x) &&
        approximatelyEqual(startPosition.y, currentPosition.y)) {
      touchTapEvent.customData = {
        touchX: currentPosition.x,
        touchY: currentPosition.y
      };
      e.target.dispatchEvent(touchTapEvent);
    }
  }

  /**
   * Handler for the touchmove event.
   * @param {Event} e The touchmove event.
   */
  function touchmove(e) {
    var touchObject = getTouchObject(e);
    currentPosition.x = touchObject.pageX;
    currentPosition.y = touchObject.pageY;
  }

  /**
   * Initialises the library.
   */
  function init() {
    try {
      // The basic events module is supported by most browsers, including IE9 and newer.
      // https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent#Example
      touchTapEvent = document.createEvent('Event');
      touchTapEvent.initEvent('touchtap', true, true);

      // EventTarget.addEventListener() is supported by most browsers, including IE9 and newer.
      // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Browser_compatibility
      document.addEventListener('touchstart', touchstart, false);
      document.addEventListener('touchend', touchend, false);
      document.addEventListener('touchcancel', touchend, false);
      document.addEventListener('touchmove', touchmove, false);
    }
    catch (err) {
      // Ignore "Object doesn't support this property or method" in IE8 and earlier.
    }
  }

  init();
})();

/* * * * * * * * * * * * *
 * END touchtap-event    *
 * * * * * * * * * * * * */
 
/* * * * * * * * * * * * *
 * BEGIN abbr-touch      *
 * * * * * * * * * * * * */

 /**
 * @license
 * abbr-touch <http://github.com/Tyriar/abbr-touch>
 * Copyright 2014 Daniel Imms <http://www.growingwiththeweb.com>
 * Released under the MIT license <http://github.com/Tyriar/abbr-touch/blob/master/LICENSE>
 */
var abbrTouch = (function () { // eslint-disable-line no-unused-vars
  'use strict';

  /**
   * Generates a touchtap event handler that calls the tap handler provided.
   * @param {function} handler The tap handler to call.
   * @returns {function}
   */
  function generateTouchtapHandler(handler) {
    return function (e) {
      handler(e.currentTarget, e.currentTarget.title, e.customData.touchX, e.customData.touchY);
    };
  }

  /**
   * The default lightweight tap handler.
   */
  function defaultOnTapHandler(target, title, touchX, touchY) { // eslint-disable-line no-unused-vars
    alert(title); // eslint-disable-line no-alert
  }

  /**
   * Attaches abbrTouch events on all abbr[title] elements within an element
   * @param {HTMLElement} elementScope The element containing abbr[title]
   * elements.
   * @param {function} customTapHandler (Optional) A custom touchtap handler to
   * be used when abbr[title] elements are touched.
   */
  function init(elementScope, customTapHandler) {
    try {
      if (!elementScope) {
        elementScope = document;
      }

      var tapHandler = customTapHandler || defaultOnTapHandler;

      var elements = elementScope.querySelectorAll('abbr[title]');
      var touchtapHandler = generateTouchtapHandler(tapHandler);
      for (var i = 0; i < elements.length; i++) {
        // EventTarget.addEventListener() is supported by most browsers, including IE9 and newer.
        if (elements[i].addEventListener) {
          elements[i].addEventListener('touchtap', touchtapHandler, false);
        }
        else {
          // EventTarget.addEventListener() is not implemented in this browser.
          // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Browser_compatibility
          break;
        }
      }
    }
    catch (err) {
      // Some old browsers do not support Document.querySelectorAll() - e.g. prior to IE9.
      // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll#Browser_compatibility
    }
  }
  return init;
})();

/* * * * * * * * * * * * *
 * END abbr-touch        *
 * * * * * * * * * * * * */

var ie 			= (document.all) ? true : false;
var cookiePath	= '/';

function changeElems(t,c,f){
	var i,node,elems;
	if(t=='*')
		elems = (ie) ? document.all : document.getElementsByTagName('*'); // '*' not supported by IE/Win <=5.5
	else
		elems = document.getElementsByTagName(t);
	if(c!='*')
		c = c.split(' ').sort().join(' ');
	for(i=0; i<elems.length; i++){
		node = elems.item(i);
		if(c=='*'){
			f(node,j);
			break;
		}
		for(var j=0; j<node.attributes.length; j++){
			if(node.attributes.item(j).nodeName=='class'&&node.attributes.item(j).nodeValue.split(' ').sort().join(' ')==c)
				f(node,j);
		}
	}
}

function changeClass(t,c,d){
	changeElems(t,c, function(node,j){ node.attributes.item(j).nodeValue = d } );
}

/** cookies **/
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for(i=0;i<ARRcookies.length;i++){
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf('='));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf('=')+1);
	  x=x.replace(/^\s+|\s+$/g,'');
	  if(x==c_name)
	  	return unescape(y);
	}
}
function setCookie(c_name,c_value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	c_value = escape(c_value)+((exdays==null) ? '' : ';expires='+exdate.toUTCString())+';path='+cookiePath+';';
	document.cookie = c_name+"="+c_value;
}



/** language switching **/
function switchClassLangEnabled(cls,lang,isOn){
	var clslang	= cls+' '+lang;
	if(isOn)
		changeClass('*',clslang+' disabled',clslang);
	else
		changeClass('*',clslang,clslang+' disabled');
}

function updateLang(disableLangInput){
	var lang = disableLangInput.name;
	var isOn = ! disableLangInput.checked;
	setCookie('no'+lang,(isOn)?'0':'1',366);
	switchClassLangEnabled('article',lang,isOn);
	switchClassLangEnabled('excerpt',lang,isOn);
	switchClassLangEnabled('datelabel',lang,isOn);
	switchClassLangEnabled('langdisabler',lang,isOn);
	if(!(document.getElementById('ldenglish').checked || document.getElementById('ldczech').checked ))
		changeClass('*','langform onelang','langform twolang');
	else
		changeClass('*','langform twolang','langform onelang');
}


/** height adjustments for text areas **/
function adjustHeightForElementsOfClass(t,c,d){
	changeElems(t,c, function(node,j){
		node.style.height=(node.scrollHeight+d)+'px';
		if(t=='textarea' && window.opera)
			node.style.overflow='hidden'; // hide scroll bar in Opera
	} );
}

/** map links **/
function addMapLinks(){
	changeElems('span','address', function(node,j){
		node.innerHTML+=' <a href="http://maps.google.com/maps?q='+encodeURIComponent(node.innerHTML)+'">&#x261B;</a>';
	} );
}


function myOnLoad(){
	// add handlers
	(function () {
		var tooltipTimeout;

		function getTooltipElement() {
		  var tooltip = document.querySelector('#abbr-tooltip');
		  if (!tooltip) {
			tooltip = document.createElement('div');
			tooltip.id = 'abbr-tooltip';
			// Technically this is duplicate content, just exposing it on mobile
			tooltip.setAttribute('aria-hidden', 'true');
			document.body.appendChild(tooltip);
		  }
		  return tooltip;
		}

		function updateTooltip(tooltip, term, expandedTerm) {
		  var text = /* term + ': ' + */ expandedTerm.replace(/\*([^*]*)\*/g,'<em>$1</em>');	// replace 
		  tooltip.innerHTML = text;
		  tooltip.classList.add('visible');

		  if (tooltipTimeout) {
			window.clearTimeout(tooltipTimeout);
		  }

		  var timeoutLength = text.length * 120;
		  tooltipTimeout = window.setTimeout(function () {
			tooltip.classList.remove('visible');
		  }, timeoutLength);
		}

		abbrTouch(document, function (target, title) {
		  var tooltip = getTooltipElement();
		  // Ensure the tooltip is ready so that the initial transition works
		  window.setTimeout(function () {
			updateTooltip(tooltip, target.innerHTML, title);
		  }, 0);
		});
	})();


	adjustHeightForElementsOfClass('textarea','code',(window.opera)?0:-2);
	addMapLinks();
	var lde = document.getElementById('ldenglish');
	var ldc = document.getElementById('ldczech');
	if(getCookie('noenglish')=='1')
		lde.checked = true;
	else if(getCookie('noczech')=='1')
		ldc.checked = true;
	updateLang(lde);
	updateLang(ldc);	
	lde.onchange=function(){updateLang(lde)}
	ldc.onchange=function(){updateLang(ldc)}
}

if(document.addEventListener)
	document.addEventListener("DOMContentLoaded", function(){didMyOnLoad=1; myOnLoad()}, false)
else if(document.all && !window.opera){	// MSIE
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
	document.getElementById("contentloadtag").onreadystatechange=function(){ if(this.readyState=="complete"){ didMyOnLoad=1; myOnLoad(); } }
}
window.onload=function(){ setTimeout("if(!didMyOnLoad) myOnLoad()", 0) } // fallback