// JavaScript Document
var agent = navigator.userAgent.toLowerCase();
// browserfenster dimensionen
if (window.innerHeight != null) {
	winH = window.innerHeight;
	winW = window.innerWidth;
} else if (document.documentElement.clientHeight != null) {
	winH = document.documentElement.clientHeight;
	winW = document.documentElement.clientWidth;
} else if (document.body.clientHeight != null) {
	var winH = document.body.clientHeight;
	var winW = document.body.clientWidth;
}

// shortcut
function $(id) {
	obj = document.getElementById(id);	
	return obj;
}

// tooltipps links zeigen
function switchTipp(alt) {
	$('tt'+alt).style.display = 'block';
}

function hideTipp() {
	for(i=1;i<=30;i++) {
		$('tt'+i).style.display = 'none';	
	}
}
// editionsplan fuer lt ie 7
function switchDetail(obj) {
		coverObj = 'p' + obj.id.substr(1,obj.id.length);
		if ($(coverObj).style.display != 'block') {
			obj.style.background = $('bgcolor').currentStyle.backgroundColor;
			$(coverObj).style.display = 'block';
		} else {
			obj.style.background = '#fff';
			$(coverObj).style.display = 'none';
		}
		
}
function hideDetail(obj) {
		coverObj = 'p' + obj.id.substr(1,obj.id.length);
		obj.style.background = '#fff';
		$(coverObj).style.display = 'none';
		
}
// editionsplan link auf detail
function showDetail(obj) {
	detailUrl = '/bibliothek/band-' + obj.id.substr(1,obj.id.length) + '.shtml';
	window.location.href = detailUrl;
}

// coveranzeige auf detailseiten
function showCover(obj,h) {
	var coverWidth = 364;
	var coverHeight = (h == null) ? 649 : h;
	var winLeft = (screen.width - coverWidth) / 2;
	var winTop = (screen.height - coverHeight) / 2;
	var winFeatures = 'width=' + coverWidth + ',height=' + coverHeight + ',left=' + winLeft + ',top=' + winTop;
	htmlUrl = '/bibliothek/coveransicht.shtml?'+obj.href;
	window.open(htmlUrl,obj.target,winFeatures);
}

// leseproben auf detailseiten
function showProbe(url) {
	var popWidth = 464;
	var popHeight = (screen.height <= 600) ? 500 : 649;
	var winLeft = (screen.width - popWidth) / 2;
	var winTop = (screen.height - popHeight) / 2;
	var winFeatures = 'width=' + popWidth + ',menubar=yes,height=' + popHeight + ',left=' + winLeft + ',top=' + winTop;
	window.open(url,obj.target,winFeatures);
}

function attachEvents() {
	//tooltipps events attachen/detachen
	var areas = document.getElementsByTagName('area');
	var count = areas.length
	for(i=count; i >= 1; i--){
		obj = areas[i-1];
		if (obj.onmouseover == null) {
			obj.onmouseover = function () {
				switchTipp(this.alt);
			}
			obj.onmouseout = function () {
				hideTipp();
			}
		} else {
			obj.onmouseover = null;
			obj.onmouseout = null;
		}
	}
}

// events fuer den editionsplan
function attachEditionsEvents() {
	var divs = document.getElementsByTagName('div');
	var count = divs.length
	for(i=0; i < count; i++){
		if (divs[i].className == 'plan-box') {
			//only for lt ie 7 and no opera (alias ie)
			if (document.all && agent.indexOf('msie 7') == -1 && agent.indexOf('opera') == -1 ) {
				divs[i].onmouseover = function () {
					switchDetail(this);
				}
				divs[i].onmouseout = function () {
					hideDetail(this);
				}
			}
			divs[i].onclick = function() {
				showDetail(this);
			}			
		}
	}
}

function replacePng() {
	//logooben - kick it
	obj = $('logolink');
	obj.style.background = $('n').currentStyle.backgroundColor;
	obj.innerHTML = '<span>Die Bibliothek von Babel</span>';
	var images = document.getElementsByTagName('img');
	//logos editionspngs
	for (i=0;i<document.getElementsByTagName('img').length;i++) {
		images(i).src = (images(i).src.indexOf('edition-buechergilde.png') != -1) ? '/inc/img/edition-buechergilde.gif' : images(i).src ;	
	}
}

//popups focusieren
function focusPopup() {
	window.focus();
}

window.onload = function () {
	//800x600 catch
	(screen.width <= 800) ? $('n').style.position = "absolute" : null;
	//tooltipps attachen
	attachEvents();
	//falls editionsplan weitere eventes attachen
	(window.location.href.indexOf('autoren-und-editionsplan') != -1) ? attachEditionsEvents() : null ;
	//falls ie 5.0 - png kicken
	(document.all && agent.indexOf('msie 5.0') != -1 ) ? replacePng() : null ;
	//popups in den vordergrund
	(window.location.href.indexOf('_band') != -1) ? focusPopup() : null ;
}

window.onunload = function() {
	//tooltipps detachen
	attachEvents();
}
