/*
 **************************************************
 *    Scripts (c) by Pascal Pfiffner 2005/2006    *
 **************************************************
 */

function myCenterWindow(url, width, height, scrollbars) {
	width = width || 500;
	height = height || 400;
	scrollbars = scrollbars ? scrollbars : 'auto';
	var top = screen.availHeight ? (screen.availHeight - height) / 3 : (screen.height - height) / 3;
	var left = screen.availWidth ? (screen.availWidth - width) / 2 : (screen.width - width) / 2;
	var addition = 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', scrollbars=' + scrollbars + ', status=yes, resizable=yes';
	window.open(url, 'window', addition);
}

function showContactform() {
	myCenterWindow("engine/contact.php", 500, 500, false);
}


function showPic(path) {
	myCenterWindow("engine/showpic.php?path=" + path, 300, 300, false);
}

function showImage(add) {				// -1 gets prev image, 1 the next
	if(images && images.length > 0) {
		var pr_img = getObj('prev');
		var nex_img = getObj('next');
		pr_img.style.display = 'inline';
		nex_img.style.display = 'inline';
		
		var show = 1* currentImage + add;
		if(show <= 0) {
			pr_img.style.display = 'none';
			show = 0;
		}
		else if((show + 1) >= images.length) {
			nex_img.style.display = 'none';
		}
		else if(show >= images.length) {
			show = images.length;
		}
		
		currentImage = show;
		getObj("main_image").src = images[show].src;
	}
	else {
		alert("no images defined");
	}
}





/*
 *******************************
 *     Animation Functions     *
 *******************************
 */

var _hideOthers = false;

function toggleBox(box) {
	box = getObj(box);
	box.blur();
	var hght = parseInt(box.offsetHeight);
	
	if(isNaN(hght) || (hght > 0)) {
		hideBox(box);
	}
	else {
		showBox(box);
	}
}

function toggledBox(box) {
	box = getObj(box);
	box.blur();
	var hght = parseInt(box.offsetHeight);
	
	if(isNaN(hght) || (hght > 0)) {
		hiddenBox(box);
	}
	else {
		shownBox(box);
	}
}


function showBox(id) {		// starts the open-box -animation
	var box = getObj(id);
	var child = getFirstElement(box);
	
	if(!box || !child)
		return 0;
	
	var cur = box.offsetHeight;
	var should = child.offsetHeight;
	should = should ? Math.round(should) : 48;
	
	// close all other boxes
	if(_hideOthers) {
		var nodearr = getAllDivs(box.parentNode);
		for(i = 0; i < nodearr.length; i++) {
			if(nodearr[i].id && (nodearr[i].id != box.id))
				hideBox(nodearr[i].id);
		}
	}
	
	// do it (if the box is being resized at the moment, that`s no problem)
	boxAnim.box[box.id] = box;
	boxAnim.from[box.id] = cur;
	boxAnim.to[box.id] = should;
	boxAnim.starttime[box.id] = (new Date()).getTime();
	animateBoxHeight(box.id);
	
	// focus a corresponding form, if one is present
	if(document.forms[box.id + '_form']) {
		document.forms[box.id + '_form'].elements[0].focus();
	}
}


function shownBox(id) {		// works like "showBox()", but without animation
	var box = getObj(id);
	var child = getFirstElement(box);
	
	if(!box || !child)
		return 0;
	
	
	// close all others
	if(_hideOthers) {
		var nodearr = getAllDivs(box.parentNode);
		for(i = 0; i < nodearr.length; i++) {
			if(nodearr[i].id && (nodearr[i].id != box.id))
				nodearr[i].style.height = '0px';
		}
	}
	
	var should = child.offsetHeight;
	should = should ? should : 48;
	
	box.style.height = should + 'px';
}


function hideBox(id) {		// induces the close-the-box-animation
	var box = getObj(id);
	var child = getFirstElement(box);
	
	if(!box || !child)
		return 0;
	
	var cur = box.offsetHeight;
	cur = cur ? cur : 0;
	
	// do it (if the box is being resized at the moment, that`s no problem)
	boxAnim.box[box.id] = box;
	boxAnim.from[box.id] = cur;
	boxAnim.to[box.id] = 0;
	boxAnim.starttime[box.id] = (new Date()).getTime();
	animateBoxHeight(box.id);
}

function hiddenBox(id) {		// works like "hideBox()", but without animation
	var box = getObj(id);
	
	if(!box)
		return 0;
	
	// do it
	box.style.height = '0px';
}


// the box-animation
var boxAnim = { box:new Array(), starttime:new Array(), from:new Array(), to:new Array(), duration:250 }

function animateBoxHeight(id) {
	if(!boxAnim.box[id])
		return false;
	
	var now = (new Date()).getTime();
	
	// last move
	if(now >= (boxAnim.starttime[id] + boxAnim.duration)) {
		(boxAnim.box[id]).style.height = boxAnim.to[id] + 'px';
		if(!boxAnim.box[id].style)
			alert("no style: " + id);
		
		// clean up
		boxAnim.box[id] = null;
		boxAnim.starttime[id] = null;
		boxAnim.from[id] = null;
		boxAnim.to[id] = null;
	}
	
	// animate
	else {
		var diff = boxAnim.to[id] - boxAnim.from[id];		// the distance we need to bridge
		var delta = (now - boxAnim.starttime[id]) / boxAnim.duration;		// where in the timeline are we
		var factor = ((delta * delta) * 3.0) - ((delta * delta * delta) * 2.0);
		var next = getDeltaByFactor(diff, factor);
		next = (boxAnim.to[id] > boxAnim.from[id]) ? (1 * boxAnim.from[id] + next) : (1 * boxAnim.to[id] + next);		// grow or shrink?
		
		(boxAnim.box[id]).style.height = next + 'px';
		setTimeout("animateBoxHeight('" + id + "')", 10);
	}
}

// returns a value from 0 to max corresponding to factor (which must be between 0 and 1)
function getDeltaByFactor(max, factor) {
	if(max < 0) {
		factor = 1 - factor;
		max = Math.abs(max);
	}
	if(factor <= 0)
		return 0;
	if(factor >= 1)
		return max;
	
	return (max * factor);
}



 
 
 
 
/*
 ***************************
 *     Basic Functions     *
 ***************************
 */

// returns the object, whether you supply an id (string) or an object itself
function getObj(id) {
	var obj;
	if(typeof(id) == "string") {
		obj = getById(id) ? getById(id) : null;
	}
	else if(typeof(id) == "object") {
		obj = id;
	}
	
	return obj;
}

// returns the object with the correspondent ID
function getById(id) {
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
		return document.all[id];
	else
		return false;
}

// returns the first ELEMENT_NODE of an object
function getFirstElement(parent) {
	if(!parent)
		return null;
	
	var nodearr = parent.childNodes ? parent.childNodes : new Array();
	var node = null;
	for(var i = 0; i < nodearr.length; i++) {
		if(1 == nodearr[i].nodeType)
			return nodearr[i];
	}
	
	return node;
}

// returns the next child-ELEMENT_NODE of a sibling
function getPrevElement(sibling) {
	var obj = getObj(sibling)
	if(!obj)
		return null;
	
	var nxt = obj.previousSibling;
	while(null != nxt) {
		if(1 == nxt.nodeType)
			return nxt;
		
		nxt = nxt.previousSibling;
	}
	
	return obj;
}

// returns the next child-ELEMENT_NODE of a sibling
function getNextElement(sibling) {
	var obj = getObj(sibling)
	if(!obj)
		return null;
	
	var nxt = obj.nextSibling;
	while(null != nxt) {
		if(1 == nxt.nodeType)
			return nxt;
		
		nxt = nxt.nextSibling;
	}
	
	return obj;
}


// returns all DIVs of an object
function getAllDivs(parent) {
	var obj = getObj(parent);
	var divs = new Array();
	var nodearr = obj.childNodes ? obj.childNodes : new Array();
	
	for(var i = 0; i < nodearr.length; i++) {
		if(nodearr[i].nodeName.toLowerCase() == "div")
			divs.push(nodearr[i]);
	}
	
	return divs;
}

// returns the first DIV of an object
function getFirstDiv(parent) {
	var divs = getAllDivs(parent);
	return divs.shift();
}

// returns the property the browser calculated, if that`s not supported, from the style, if not it guesses
function getComputedValue(prop, obj) {
	var myObj = getObj(obj);
	var getit;
	if(document.defaultView.getComputedStyle)
		getit = document.defaultView.getComputedStyle(myObj, null).getPropertyValue(prop);
	else
		getit = myObj.style[prop];
	
	return getit;
}

// cleans a node from its childs
function cleanNode(node) {
	if(typeof(node) != "object") {
		node = getObj(node);
		if(typeof(node) != "object")
			return false;
	}
	
	while(null != node.firstChild) {
		node.removeChild(node.firstChild);
	}
}



/********************
 *  Shop functions  *
 ********************/

function checkCookie() {
	if(!navigator.cookieEnabled) {
		alert("Cookies müssen aktiviert sein, um den Shop benutzen zu können");
		return false;
	}
	return true;
}


// (de)selects any preceding INPUT type field
function selectVal(clicked) {
	var elem = clicked;
	elem.blur();
	
	while(null != elem.previousSibling) {
		if('input' == elem.previousSibling.nodeName.toLowerCase()) {
			elem.previousSibling.checked = ! elem.previousSibling.checked;
			return;
		}
		elem = elem.previousSibling;
	}
}


// disable by ID
function disableThis(id) {
	setTimeout("getObj('" + id + "').disabled = true;", 100);
}

