/*
 ***********************************************
 *                                             *
 *  This is not the Source you`re looking for  *
 *                                             *
 *          Move along. Move along!            *
 *       (Well, yeah, Star Wars rocks)         *
 *                                             *
 ***********************************************
 */


// scrolls the browserwindow to the given ID
function scrollToID(id) {
	var obj = getObj(id);
	if(!obj)
		return;
	
	var offset = obj.offsetTop ? obj.offsetTop : 0;
	window.scrollTo(0, offset - 100);
}


function toggleSubMenu(parent_id) {
	var parent = getObj("main_" + parent_id);
	var obj = getObj("child_" + parent_id);
	
	if(!obj)
		return;
	
	if(parent) {
		parent.setAttribute("class", (obj.style.display == "none") ? "sub_expanded" : "sub_hidden");
		try { parent.blur(); } catch(e) {}
	}
	obj.style.display = (obj.style.display == "none") ? "block" : "none";
}


// calls toggle() only if not yet shown
function show(linkid, id) {
	var link = getObj(linkid);
	var elem = getObj(id);
	if(null == elem)
		return;
	
	var hidden = (link && link.className.indexOf("link_hidden") > -1) || (elem && elem.className.indexOf("block_hidden") > -1);
	
	if(hidden) {
		toggle(linkid, id);
	}
}

// toggles the css-class of the given element. first argument is the link-id, second argument the id of the element to show.
function toggle(linkid, id) {
	var link = getObj(linkid);
	var elem = getObj(id);
	if(null == elem)
		return;
	
	var collapse = (link && link.className.indexOf("link_expanded") > -1) || (elem && elem.className.indexOf("block_expanded") > -1);
	
	if(link) {
		link.blur();
		var linkclass = link.className ? link.className : '';
		link.className = collapse ?
							linkclass.replace(/ *link_expanded */g, '') + " link_hidden" :
							linkclass.replace(/ *link_hidden */g, '') + " link_expanded";
	}
	
	if(elem) {
		var elemclass = elem.className ? elem.className : '';
		elem.className = collapse ?
							elemclass.replace(/ *block_expanded */g, '') + " block_hidden" :
							elemclass.replace(/ *block_hidden */g, '') + " block_expanded";
	}
}

// toggles the class of the next found div (view from the current link)
function toggleNext(elem) {
	alert('toggleNext: ' + elem);
	var found = getNextDiv(elem);
	if(null == found)
		return 0;
	
	found.cssClass = (found.cssClass.indexOf("block_expanded") == -1) ? "block_expanded" : "block_hidden";
	return 1;
}


// finds next non-text element
function getNextElem(elem) {
	var obj = getObj(elem);
	
	var next = obj.nextSibling;
	while(null != next) {
		if('#text' != next.nodeName.toLowerCase()) {
			return next;
		}
		next = next.nextSibling;
	}
	return null;
}


// finds next div-element
function getNextDiv(elem) {
	
	// search next div within same parent
	while(next = elem.nextSibling) {
		if('div' == next.nodeName.toLowerCase()) {
			return next;
		}
	}
	
	// search one level higer
	if(elem.parentNode && elem.parentNode.parentNode && elem.parentNode.parentNode.childNodes) {
		var brothers = elem.parentNode.parentNode.childNodes.firstChild;
		while(next = elem.nextSibling) {
			if('div' == next.nodeName.toLowerCase()) {
				return next;
			}
		}
	}
	
	return null;
}


function popPic(entry) {
	popup("/showpic.php?path=" + entry, 600, 600);
}

function showContactform() {
	popup("/contact.php", 500, 500);
}

function popup(url, width, height) {
	if(!url)
		return false;
	
	if(!width)
		var width = 500;
	if(!height)
		var height = 500;
	var screenwidth = screen.innerWidth ? screen.innerWidth : (screen.width ? screen.width : 1024);
	
	var addition = "width=" + width + ", height=" + height + ", top=60, left=" + ((screenwidth - width) / 2) + ", status=yes, menubar=no, scrollbars=auto";
	var pup = window.open(url, 'popup', addition);
	if(pup)
		pup.focus();
}


