function getStyle(el, prop) {
  if (document.defaultView && document.defaultView.getComputedStyle) {
    return document.defaultView.getComputedStyle(el, null)[prop];
  } else if (el.currentStyle) {
    return el.currentStyle[prop];
  } else {
    return el.style[prop];
  }
}

function fadeIn(toggle) {
	var nav = document.getElementById('navtoggle');
	var categories = document.getElementById('categories');
	if (toggle && getStyle(nav, 'opacity') == '1') {
		nav.className = 'hideable faded';
		categories.className = 'hideable fadeOut';
	} else {
		nav.className = 'hideable fadeIn';
		categories.style.display = '';
		categories.className = 'hideable fadeIn';
	}
}

function popup() {
	if (window.location.href.match('p=on')) {
		self.close();
		return;
	}
	params  = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0'
	params += ', fullscreen=yes';
	var url = window.location.pathname + '?p=on' + window.location.hash;
	newwin=window.open(url, document.title, params);
	if (window.focus) {newwin.focus()}
}

function hotKeys(kk) {
	if (kk == 'J') {
		Shows.current.showNext();
	} else if (kk == 'K') {
		Shows.current.showPrev();
	} else if (kk == 'L') {
		fadeIn(true);
	} else if (kk == 'H') {
		window.location = 'index.html';
	} else if (kk == 'S') {
		Shows.current.playpause();
	} else if (kk == 'F') {
		popup();
	} else if (kk == '¿') { // ?
		var help = document.getElementById('help');
		if (help) {
			toggleHelp();
		}
	}
}

function toggleHelp() {
	var help = document.getElementById('help');
	if (help.style.display == 'none') help.style.display = ''
	else help.style.display = 'none';
}

document.onkeydown = function(event) {
	// if (!SlideShow.listen_for_keys) {
	// 	return;
	// }
	return hotKeys(String.fromCharCode(event.keyCode));
}
document.getElementById('categories').addEventListener('webkitTransitionEnd', function(evt) {
	if (getStyle(this, 'opacity') == '0') {
		this.style.display = 'none';
	} else {
		this.style.display = '';
	}
}, false);

if (window.location.href.match('p=on')) {
	var sw = screen.availWidth;
	var sh = screen.availHeight;
	var scalew = sw / parseInt(getStyle(document.body, 'width'));
	var scaleh = sh / 720;
	var zoom = scalew > scaleh ? scaleh : scalew;
	document.body.style.zoom = zoom;
	// $('#categories a').each(function() {
	// 	var url = $(this).attr('href');
	// 	$(this).attr('href', url + '?p=on');
	// });
}
if (window.innerHeight <= 650 && self.s) {
	var sn = document.getElementById('screennotice');
	sn.style.display = '';
	sn.className = 'hideable fadeIn';
	setTimeout(function(){
		sn.className = 'hideable fadeOut';
		sn.addEventListener('webkitTransitionEnd', function(evt) {
			sn.style.display = 'none';
		}, false);
	}, 3000);
}