// following functions are all copyrighted by Pascal Pfiffner 2003+

function popupper(Jfile, Jwidth, Jheight) {
	Jheight = (Jheight < 100) ? 100 : Jheight;
	if(screen.availHeight)
		Jheight = (Jheight > screen.availHeight) ? (screen.availHeight - 80) : Jheight;
	Jwidth = (Jwidth < 100) ? 100 : Jwidth;
	if(screen.availWidth)
		Jwidth = (Jwidth > screen.availWidth) ? (screen.availWidth - 20) : Jwidth;
	var addition = "status=yes, height=" + Jheight + ", width=" + Jwidth + ", left=" + ((screen.availWidth - Jwidth) / 2) + ", top=50, menubar=no, scrollbars=yes";
	var hoth = window.open(Jfile, 'hillrippers16', addition);
	hoth.focus();
}
// aufruf:
// popupper('link/to/file.jpg', 'höhe', 'breite');


function viewImage(path) {
	var screenheight = screen.availHeight ? screen.availHeight : screen.height;
	var screenwidth = screen.availWidth ? screen.availWidth : screen.width;
	
	width = 600;
	height = 400;
	var addition = 'width=' + width + ', height=' + height + ', top=100, left=' + ((screenwidth - width) / 2) + ', status=yes, menubar=no, scrollbars=no';
	
	var cornholio = window.open('../essentials/viewimage.php?path=' + path, 'titlebar', addition);
	cornholio.focus();
}



function newImgSrc(arg) {
	if(document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
	else return false;
}

function preloadImages() {
	if(document.images) {
		home = newImgSrc("../home/layout/homeO.gif")
		riders = newImgSrc("../riders/layout/ridersO.gif")
		gallery = newImgSrc("../gallery/layout/galleryO.gif")
		stuff = newImgSrc("../stuff/layout/stuffO.gif")
		guestbook = newImgSrc("../guestbook/layout/guestbookO.gif")
		homeOVER = newImgSrc("../essentials/pics/home.jpg")
		ridersOVER = newImgSrc("../essentials/pics/riders.jpg")
		galleryOVER = newImgSrc("../essentials/pics/gallery.jpg")
		stuffOVER = newImgSrc("../essentials/pics/stuff.jpg")
		guestbookOVER = newImgSrc("../essentials/pics/guestbook.jpg")
		return true;
	}
	else return false;
}

function changeImage() {
	if(document.images && preloaded == true) {
		for(var c = 0; c < changeImage.arguments.length; c += 2) {
			if (document[changeImage.arguments[c]]) {
				document[changeImage.arguments[c]].src = changeImage.arguments[c+1];
			}
		}
	}
	else if(document.images)
		preloadImages();
}



function rate(id, rating) {
	ratewindow = window.open("../essentials/ratenews.php?id=" + id + "&rating=" + rating, "Bewertung", "width=300, height=200, left=" + ((screen.availWidth-300) / 2) + ", top=200");
}

function showSecImageHint() {
	alert("Tipp ins Feld unter dem Bild mit den Buchstaben einfach jene Zeichen, die du im Bild erkennen kannst (GROSSBUCHSTABEN).\n\nLeider muessen wir zu solchen Mitteln greifen, um all den Spam zu stoppen, der in letzter Zeit auch hier bei uns gelandet ist.");
}

function check_string() {
	if(document.forms["searchform"].string.value.length < 2) {
		alert("such doch nach mehr als nur einem Buchstaben, ok?");
		document.forms["searchform"].string.focus();
		return false;
	}
	else
		return true;
}

function addValue(obj, text) {
	obj = getObj(obj);
	obj.value = obj.value + text;
}

function check_fastsearch() {
	if(document.forms["fastsearch"].string.value.length < 2) {
		document.location.href = "../search/index.php?f=" + document.forms["fastsearch"].string.value;
		return false;
	}
	else
		return true;
}

function showDrawer(elem) {
	var thediv, theleft;
	
	if(document.getElementById)
		thediv = document.getElementById(elem);
	else if(document.all)
		thediv = document.all(elem);
	else return;
	
	theleft = thediv.style.left.replace(/\D/g, '');
	theleft = Math.floor(theleft * 0.75);
	
	if(theleft < 1)
		return;			// bricht ab, falls schon sichtbar
	thediv.style.left = (theleft * -1) + 'px';
	setTimeout("showDrawer('" + elem + "')", 30);
}

function hideDrawer(elem) {
	var thediv, theleft, thewidth;
	
	if(document.getElementById)
		thediv = document.getElementById(elem);
	else if(document.all)
		thediv = document.all(elem);
	else return;
	
	thewidth = thediv.style.width.replace(/\D/g, '') - 1 + 21;		// breite + 20 (sicher sein, dass ganz ausgeblendet wird)
	theleft = thediv.style.left.replace(/\D/g, '') - 1 + 21;		// in 20er schritten rausfahren
	thediv.style.left = (theleft * -1) + 'px';
	if(theleft < thewidth)
		setTimeout("hideDrawer('" + elem + "')", 30);
}

function scrollToTop() {
	if(window.pageYOffset)
		var y = window.pageYOffset;
	else if(document.body.scrollTop)
		var y = document.body.scrollTop;
	else if(window.scrollTo()) {
		window.scrollTo(0, 0);
		return;
	}
	else
		return;
	
	if(y > 400)
		y = 400;
	
	while(y > 0) {
		y = Math.floor(y * 0.75);
		setTimeout("window.scrollTo(0, '" + y + "')", 30);
	}
}



/*
 ****************
 *	Toggling	*
 ****************
 */

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 expanded = (link && link.className.indexOf("link_expanded") > -1) || (elem && elem.className.indexOf("block_expanded") > -1);
	
	if(!expanded) {
		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) {
	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) {
	elem = getObj(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;
}


