//
// 2006 by bitart; Author: Alexander Graf
//
// Copyright by bitart - Furch & Graf GbR, Gartenstr. 5 56220 St. Sebastian,
// Germany. All rights reserved.
// Copyright in this software is owned by bitart, unless otherwise indicated.
//
// You are not allowed to distribute this software in any form or use it as
// part of another project without asking the owner for permission to use.
//

var hua    = navigator.userAgent.toLowerCase();
var moz    = (hua.indexOf('gecko')>0) && (hua.indexOf('khtml')==0);
var ieh    = navigator.userAgent.indexOf('MSIE')>0;
var fid    = null;
var sid    = null;
var onroot = true;

/* Misc Functions */

function parseId(id)
{
	var idc = id.split('_');
	return Array(idc[0], parseInt(idc[1]), parseInt(idc[2]));
}

function either(/*args...*/)
{
	for (var cnt=0; cnt<arguments.length; cnt++)
		if (arguments[cnt])
			return arguments[cnt]
	return null;
}

/* Tree/Content Functions */

function subtree(id)
{
	frames.data.location.replace('/app/subtree.php?'+id);
}

function update(id, addon)
{
	var nid  = parseId(id);
	var turl = null;
	var curl = null;
	// prepare id addon
	addon = addon?('_'+addon):'';
	// is tree update necessary?
	var tid = frames.tree.fid;
	var thi = frames.tree.entry.high.id;
	if (!tid || nid[0]!=tid[0] || nid[1]!=tid[1] || thi!=id){
		if (!nid[1] && !nid[2])
			frames.tree.highNode(id);
		else if (!frames.tree.highNode(id))
			turl = '/app/tree.php?'+id+addon;
	}
	// is content update necessary?
	var cid = frames.content.fid;
	var chi = frames.tree.entry.high.id;
	if (!cid || nid[0]!=cid[0] || nid[1]!=cid[1] || nid[2]!=cid[2] || chi!=id){
		onroot = false;
		if (!nid[1] && !nid[2]){
			curl = '/fix/start.html';
			onroot = true;
		}else if (nid[2]){
			curl = '/app/show.php?'+id+addon;
		}else{
			curl = '/app/list.php?'+id+addon;
		}
	}
	// do the update if urls differ
	if (turl && turl != frames.tree.location.pathname+frames.tree.location.search){
		try{
			with (frames.tree){
				var pos = either(document.documentElement.scrollTop, document.body.scrollTop);
				pos = pos?-pos:0;
				var node = document.getElementById(id);
				while (node && node.tagName.toLowerCase() != 'body'){
					pos += node.offsetTop;
					node = node.offsetParent;
				}
			}
			pos = pos?('#'+pos):'';
		}catch(e){
			pos = '';
		}
		frames.tree.location.replace(turl+pos);
	}
	if (curl && curl != frames.content.location.pathname+frames.content.location.search){
		frames.content.location.href = curl;
	}
	return false;
}

function entry(e,node,noupdate,addon)
{
	if (!node) node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (!node) return false;
	while (node && node.nodeName.toLowerCase()!='u') node=node.parentNode;
	if (node){
		if (entry.high) entry.high.className='';
		entry.high=node;
		entry.high.className='high';
		if (!noupdate) top.update(node.id,addon);
	}
	return false;
}

/* Tree Functions */

function highNode(id)
{
	var hnode = document.getElementById(id);
	if (!hnode){
		var ids=parseId(id);
		var hnode = document.getElementById(ids[0]+'_'+ids[1]+'_0');
	}
	if (!hnode) return false;
	entry(null,hnode,true);
	var htype = hnode.parentNode.firstChild.className.charAt(0);
	if (fid){
		var snode = document.getElementById(fid[0]+'_'+fid[1]+'_'+fid[2])
		if (snode){
			var pnode = snode.parentNode.parentNode.previousSibling.firstChild.nextSibling;
			var stype = snode.parentNode.firstChild.className.charAt(0);
			return (htype=='f' || (htype=='m' && stype=='f' && hnode.id == pnode.id));
		}
	}
	return (htype=='f');
}

function prevNode(node)
{
	node = (node?node:entry.high).parentNode.previousSibling;
	while (node && node.nodeName.toLowerCase()!='p') node=node.previousSibling;
	if (node) entry(null,node.firstChild.nextSibling);
	return false;
}

function nextNode(node)
{
	node = (node?node:entry.high).parentNode.nextSibling;
	while (node && node.nodeName.toLowerCase()!='p') node=node.nextSibling;
	if (node) entry(null,node.firstChild.nextSibling);
	return false;
}

function upNode(node, addon)
{
	node = (node?node:entry.high).parentNode.parentNode.previousSibling;
	if (node) entry(null,node.firstChild.nextSibling,false,addon);
	return false;
}

function downNode(node)
{
	node = (node?node:entry.high).parentNode.nextSibling;
	if (node && node.nodeName.toLowerCase()=='div'){
		node = node.firstChild;
		while (node && node.nodeName.toLowerCase()!='p') node=node.nextSibling;
		if (node) entry(null,node.firstChild.nextSibling);
	}
	return false;
}

function folder(e)
{
	var node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (node.className.charAt(0)=='m')
		upNode(node);
	else
		entry(null,node.nextSibling);
	return false;
}

function setstatus(e)
{
	var node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (node.nodeName.toLowerCase()!='img'){
		while (node && node.nodeName.toLowerCase()!='p') node=node.parentNode;
		if (node) window.status = node.title;
	}
	if (ieh && this.className.indexOf('high')<0) this.className += ' iehover';
	return true;
}

function clearstatus()
{
	window.status = '';
	if (ieh) this.className = this.className.replace(/ iehover/,'');
	return true;
}

function initTree(doscroll)
{
	// get tree root
	var treeroot = document.getElementById('Tree');
	if (!treeroot){
		treeroot = document.getElementById('FakeTree');
	}
	if (!treeroot) return false;

	// create title tags
	var es = treeroot.getElementsByTagName('p');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++){
		var ei = es[ec];
		if (ei.lastChild.tagName.toLowerCase() != 'u')
			ei.firstChild.style.cursor = 'default';
		else if (ei.lastChild.firstChild == ei.lastChild.lastChild)
			ei.title = ei.lastChild.firstChild.innerHTML;
		else
			ei.title = ei.lastChild.firstChild.innerHTML + ' ' + ei.lastChild.lastChild.innerHTML;
	}
	// add hover/click handler
	var es = treeroot.getElementsByTagName('u');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++){
		var ei = es[ec];
		ei.onclick = entry;
		ei.onmouseover = setstatus;
		ei.onmouseout = clearstatus;
		if (ei.className=='high')
			entry.high=ei;
	}
	// add imageclick handlers and set image class
	var es = treeroot.getElementsByTagName('img');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++){
		var ei = es[ec];
		ei.onclick = folder;
		ei.onmouseover = setstatus;
		ei.onmouseout = clearstatus;
		var cl = ei.src.substring(ei.src.lastIndexOf('/')+1);
		ei.className = cl.substring(0,cl.indexOf('.'));
	}
	// scroll selected node to scrollpos or to middle of viewport
	if (entry.high){
		var hei = 0;
		var pos = 0;
		try{
			var node = entry.high;
			while (node && node.tagName.toLowerCase() != 'body'){
				pos += node.offsetTop;
				node = node.offsetParent;
			}
			if (pos){
				var srl = parseInt(window.location.hash.substr(1));
				var hei = either(document.documentElement.clientHeight, window.innerHeight, document.body.clientHeight);
				if (srl){
					window.scrollTo(0,Math.max(0,pos-srl));
				}else if (pos>hei/2){
					window.scrollTo(0,Math.max(0,pos-hei/2));
				}
			}
		}catch(e){}
	}
	// set tree-id and sync tree & content frame
	if (window.location.search){
		var end = window.location.search.indexOf('&');
		var rid = (end==-1)?window.location.search.substring(1):window.location.search.substring(1,end);
		fid=parseId(rid);
		try{top.update(rid)}catch(e){} // cheat timing problem
	}
}

/* List Functions */

function initList()
{
	// get list root
	var listroot = document.getElementById('List');
	if (!listroot) return false;

	// add click handler
	var es = listroot.getElementsByTagName('tr');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++){
		var ei=es[ec];
		ei.onclick = show;
		if (ieh){
			ei.onmouseover = iehover;
			ei.onmouseout = iedehover;
		}
	}

	// add mark handler
	var es = listroot.getElementsByTagName('input');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++)
		es[ec].onclick = mark;
}

function show(e)
{
	var node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (!node) return false;
	var force=false;
	while (node && node.nodeName.toLowerCase()!='tr'){
		if (node.className.indexOf('ignore')!=-1) force=true;
		if (node.nodeName.toLowerCase()=='input') return true;
		if (node.className=='r' && !force) return false;
		node=node.parentNode
	}
	if (node) window.location.href = 'show.php?'+node.id;
	return false;
}

function mark(e)
{
	var node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (!node) return false;
	var line = node;
	while (line && line.nodeName.toLowerCase()!='tr') line=line.parentNode;
	if (line) (node.checked?top.remember:top.unremember)(line.id);
	return true;
}

/* Mark Functions */

function markButton(id)
{
	var main = opener?opener.top:top;
	if (toggleButton(document.getElementById(id))){
		main.remember(id);
	}else{
		main.unremember(id);
	}
	if (opener){
		var found = opener.document.getElementById(id);
		if (found) toggleButton(found);
	}else if (pop_extwin!==null){
		var found = pop_extwin.document.getElementById(id);
		if (found) toggleButton(found);
	}
}

function toggleButton(but)
{
	if (but.innerHTML=='merken'){
		but.title = but.title.substring(0,but.title.indexOf(' '))+' vom Merkzettel entfernen';
		but.innerHTML = 'löschen';
		return true;
	}else{
		but.title = but.title.substring(0,but.title.indexOf(' '))+' auf den Merkzettel setzen';
		but.innerHTML = 'merken';
		return false;
	}
}

/* Bookmark Functions */

function favorite(url,title)
{
	return pop_ext('/app/book.php?url='+url+'&title='+title,600,130,2);
}

/* Result Functions */

var resid = null;
function initResult(id)
{
	// remember resid
	resid = id;

	// get list listroot
	var listroot = document.getElementById('Result');
	if (!listroot) return false;

	// add click handler & iehover
	var es = listroot.getElementsByTagName('tr');
	var ee = es.length;
	for (var ec=0; ec<ee; ec++){
		var ei=es[ec];
		if (ei.id){
			ei.onclick = resultAction;
			if (ieh){
				ei.onmouseover=iehover;
				ei.onmouseout=iedehover;
			}
		}
	}
}

function resultAction(e)
{
	var node = (typeof e=='undefined')?window.event.srcElement:e.target;
	if (!node) return false;
	var markentry = false;
	while (node && (node.nodeName.toLowerCase()!='tr' || !node.id)){
		if (node.nodeName.toLowerCase()=='div' && node.className.substr(0,6)=='Button'){
			if (node.className.indexOf('next')!=-1){
				markentry = true;
				break;
			}
		}
		node=node.parentNode;
	}
	top.sid = resid;
	if (!markentry && node) top.update(node.id,resid);
	return true;
}

function resultButton()
{
	if (top.sid){
		var but = document.getElementById('ResultButton');
		if (but){
			but.style.display = 'block';
		}
	}
}

function showResult()
{
	top.frames.content.location.href = '/app/result.php?'+sid;
	sid = null;
}

function navResult(id, batch)
{
	var sid_page = top.sid.split('_');
	top.sid = sid_page[0]+'_'+batch;
	return top.update(id, top.sid);
}

/* External Functions */

function root(name)
{
	if (roots[name]){
		return update(roots[name]);
	}else{
		alert('Unbekanntest Archiv "'+name+'".');
		return false;
	}
}

function unknown()
{
	alert('Dieser Eintrag ist leider nicht vorhanden.');
	return false;
}

