var lang = 'de';

var error_title = {
	de: 'Fehler entstanden!'
}

var network_error = {
	de: 'Wegen schlechter Netzwerkverbindung konnte die Seite nicht angezeigt werden. Versuchen Sie es bitte erneut.'
}

var not_found_error = {
	de: 'Die gewünschte Seite konnte nicht gefunden werden.'
}

var internal_server_error = {
	de: 'Die Seite kann wegen eines Serverfehlers nicht angezeigt werden.'
}

var other_error = {
	de: 'Fehler entstanden:'
}

var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP"); 
var imgCache = new Image();
var content, contOpacity = 1.0, replaceContent;

var imgOpen = new Image(), imgOpeneing = new Image(), imgMiddle = new Image(), imgClosing = new Image(), imgClosed = new Image();
imgOpen.src = 'gfx/ArrowOpen.gif';
imgOpeneing.src = 'gfx/ArrowOpenening.gif';
imgMiddle.src = 'gfx/ArrowMiddle.gif';
imgClosing.src = 'gfx/ArrowClosing.gif';
imgClosed.src = 'gfx/ArrowClosed.gif';

var catrefs = Array(), catdims = Array(), catttls = Array();
var opencat = null, closecat = null;
var movingup, movingdown, replacing, loading;
var steps = 20;

var speed = 10;

var debug = false;

function init_ajax() {
	if(debug) debug = document.getElementById('infoBoxError');
	content = document.getElementById('store');
	var el = document.getElementById('brandbox');
	if(el.firstChild.firstChild.firstChild) {
		el = el.firstChild.firstChild.firstChild;
		var cats = 0;
		var h;
		var o = document.getElementById('open');
		var n = 0;
		while(el.nextSibling) {
			el = el.nextSibling;
			if(el.nodeName=='DIV') {
				cats = catrefs.push(el);
				el.firstChild.nextSibling.style.display = 'block';
				h = el.firstChild.nextSibling.clientHeight;
				if(o && o==el) {
					opencat = n;
				} else {
					el.firstChild.nextSibling.style.display = 'none';
				}
				var sub = el.firstChild.nextSibling.firstChild.childNodes
				for(var i=0; i<sub.length; i++) {
					sub[i].firstChild.onclick = function() {return false;}
				}
				if(h>0) {
					el.firstChild.firstChild.onclick = shiftMenu;
					el.firstChild.firstChild.firstChild.onclick = function() {return false;}
					catdims.push({height: h, step: h/steps, at: 0});
					catttls.push(el.firstChild.firstChild.firstChild.firstChild.nodeValue.replace(/ /g,'_'));
				}
				n++;
			}
		}
	}

	el = document.getElementById('basket');
	el.putInFocus = function() {
		var p = (document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop) + 0;
		d = document.getElementById('brandbox').offsetHeight;
		el.optimalPlacing = 250 + d - 10;
		if(!el.moveInterval){
			if(p>el.optimalPlacing) {
				el.scrollDiff = p - el.optimalPlacing;
				el.scrollCounter = 0;
				el.moveInterval = setInterval("document.getElementById('basket').scrollUp()",speed);
			} else if(p<el.optimalPlacing) {
				el.scrollDiff = el.optimalPlacing - p;
				el.scrollCounter = 0;
				el.moveInterval = setInterval("document.getElementById('basket').scrollDown()",speed);
			}
		}
	}
	el.scrollSteps = (Math.PI/2)/10;
	el.scrollCounter = 0;
	el.scrollUp = function () {
		var p = (document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop) + 0;
		if(el.optimalPlacing<p) {
			el.scrollCounter += el.scrollSteps; 
			var c = Math.sin(1-el.scrollCounter) <= 0 ? 0.0001 : Math.sin(1-el.scrollCounter);
			if(document.documentElement && document.documentElement.scrollTop) {
				document.documentElement.scrollTop = parseInt(c*el.scrollDiff+el.optimalPlacing);
			} else if(document.body.parentNode.scrollTop) {
				document.body.parentNode.scrollTop = parseInt(c*el.scrollDiff+el.optimalPlacing);
			} else if(document.body.scrollTop) {
				document.body.scrollTop = parseInt(c*el.scrollDiff+el.optimalPlacing);
			} 
		} else {
			clearInterval(el.moveInterval);
			el.moveInterval = null;
		}
	}
	el.sparrmax = 25;
	el.scrollDown = function () {
		var p = (document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop) + 0;
		if(el.optimalPlacing>p && el.sparrmax--) {
			el.scrollCounter += el.scrollSteps; 
			var c = Math.sin(1-el.scrollCounter) <= 0 ? 0.0001 : Math.sin(1-el.scrollCounter);
			if(!p) {
				document.documentElement.scrollTop += 1;
				document.body.parentNode.scrollTop += 1;
				document.body.scrollTop += 1;
			}
			var v = parseInt(el.optimalPlacing-c*el.scrollDiff+5);
			var max = document.documentElement.scrollHeight-document.documentElement.clientHeight;
			if(document.documentElement && document.documentElement.scrollTop) {
				document.documentElement.scrollTop = v;
			} else if(document.body.parentNode.scrollTop) {
				document.body.parentNode.scrollTop = v;
			} else if(document.body.scrollTop) {
				document.body.scrollTop = v;
			} 
			if(v>max) {
				clearInterval(el.moveInterval);
				el.moveInterval = null;
			}
		} else {
			clearInterval(el.moveInterval);
			el.moveInterval = null;
		}
	}

	imgCache.src = 'gfx/LoadingGreen.gif';
}

// Category Navigation Dynamic

var shifting;

function shiftMenu(e) {
	if(!(movingup||movingdown||replacing||busy||shifting)) {
		shifting = true;
		if(!e) {
			e = event;
			var el = e.srcElement;
		} else {
			var el = e.target;
		}
		if(el.nodeName=='A') el = el.parentNode;
		el = el.parentNode;
		if(opencat!=null) closecat = opencat;
		for(var i=0; i<catrefs.length; i++) {
			if(el==catrefs[i].firstChild) opencat=i;
		}
		if(closecat!=null && closecat==opencat) {
			opencat = null;
			catdims[closecat].at = catdims[closecat].height;
			movingdown = setInterval('closeDown('+closecat+')',speed);
			catrefs[closecat].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowOpenening.gif)';
			shifting = false;
		} else if(opencat!=null && closecat!=null) {
			catdims[closecat].at = catdims[closecat].height;
			movingdown = setInterval('closeDown('+closecat+')',speed);
			catrefs[closecat].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowOpenening.gif)';
			movingup = setInterval('openUp('+opencat+')',speed);
			catrefs[opencat].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowClosing.gif)';
		} else if(opencat!=null) {
			movingup = setInterval('openUp('+opencat+')',speed);
			catrefs[opencat].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowClosing.gif)';
		}
//	alert('Click!'+el.nodeName+' and '+catrefs[0].nodeName+' is '+(el.nodeName==catrefs[0].nodeName)+' Num: '+opencat+' ('+closecat+')');
	}
}

function openUp(i) {
	catrefs[i].firstChild.nextSibling.style.display = "block";
//	alert('Height: '+catdims[i].height+' Step: '+catdims[i].step);
	if(catdims[i].at>catdims[i].height*2/3) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowOpenening.gif)';
	} else if(catdims[i].at>catdims[i].height*1/3) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowMiddle.gif)';
	}
	catdims[i].at += catdims[i].step;
	if(catdims[i].at>=catdims[i].height) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowOpen.gif)';
		catrefs[i].firstChild.nextSibling.style.height = catdims[i].height+'px';
		clearInterval(movingup);
		movingup = null;
		opencat = i;
		removeIntervals();
		content.innerHTML = '<div id="wait"></div>';
		content.style.opacity = 1.0;
		content.style.filter = 'alpha(opacity=100)';
		getContent('mar='+encodeURIComponent(catttls[i]));
	} else {
		catrefs[i].firstChild.nextSibling.style.height = parseInt(catdims[i].at)+'px';
		contOpacity -= 1.0/steps;
		content.style.opacity = contOpacity.toFixed(2);
		content.style.filter = 'alpha(opacity='+parseInt(contOpacity*100)+')';
	}
}

function closeDown(i) {
	catdims[i].at -= catdims[i].step;
	if(catdims[i].at<catdims[i].height*1/3) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowClosing.gif)';
	} else if(catdims[i].at<catdims[i].height*2/3) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowMiddle.gif)';
	}
	if(catdims[i].at<0) {
		catrefs[i].firstChild.firstChild.style.backgroundImage = 'url(gfx/ArrowClosed.gif)';
		catrefs[i].firstChild.nextSibling.style.display = "none";
		clearInterval(movingdown);
		movingdown = null;
		closecat = null;
	} else {
		catrefs[i].firstChild.nextSibling.style.height = parseInt(catdims[i].at)+'px';
	}
}

// Page Loading Functionality

function getContent(q) {
	if((req.readyState==0 || req.readyState==4) && q && !replacing) {
//		req.setRequestHeader('Content-Type','text/xml; charset=UTF-8');
		req.open("GET",'page2Content.php5?'+q,true);
		req.onreadystatechange = function() {
			if(req.readyState==4) {
				switch(req.status) {
					case 200:				
						var result;
						if(req.responseXML && req.responseXML.documentElement) result = req.responseXML.documentElement;
						if(result && result.firstChild && result.firstChild.nodeName=='data') {
							replaceContent = result.firstChild.firstChild.nodeValue;
							if(debug) debug.innerHTML += 'Recieved data, publishing<br/>';
						} else if(result && result.firstChild && result.firstChild.nodeName=='error') {
							replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+result.firstChild.firstChild.nodeValue+'</h2>';
							if(debug) debug.innerHTML += result.firstChild.firstChild.nodeValue+'<br/>';
						} else if(result && !result.firstChild) {
							if(debug) debug.innerHTML += 'Unexpected Empty Result Returned<br/>';
						} else {
							if(debug) debug.innerHTML += 'Corrupt Response Document: '+req.responseText+'<br/>';
						}
						break;
					case 404:
						if(debug) debug.innerHTML += '404 - Document Not Found!<br/>';
						replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+not_found_error[lang]+'</h2>';
						break;
					case 500:
						replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+internal_server_error[lang]+'</h2>';
						if(debug) debug.innerHTML += '500 - Internal Server Error!<br/>';
						break;
					case 0:
						replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+network_error[lang]+'</h2>';
						if(debug) debug.innerHTML += '0 - Suspected Failure in Firefox/Safari (Network Failure)<br/>';
						break;
					default:
						if(req.status>=1000) {
							replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+network_error[lang]+'</h2>';
							if(debug) debug.innerHTML += req.status+' - Suspected Failure in Internet Explorer (Network Failure)<br/>';
						} else {
							replaceContent = '<h1>'+error_title[lang]+'</h1><h2>'+other_error[lang]+' '+req.status+'</h2>';
							if(debug) debug.innerHTML += req.status+' - Unexpected Error!<br/>';
						}
				}
				replacing = setInterval('showNewContent()',speed);
			} else if(req.readyState==1||req.readyState==2||req.readyState==3) {
				if(debug) debug.innerHTML += 'Processing request: '+req.readyState+'<br/>';
				// processing
			} else {
				if(debug) debug.innerHTML += 'Unexpected error: '+req.readyState+' (HTTP status '+req.status+')<br/>';
//				alert("Failed with HTTP Status code: "+req.status);
			}
		}
		if(debug) debug.innerHTML += 'Sending request<br/>';
		req.send(null);
	} else {
//		alert("Bitte, Warten Sie, eine andere Seite ladet schon!");
		if(debug) debug.innerHTML += 'Duplicate request! Page still loading.<br/>';
	}
}

var retrieve, busy;
function replaceContentWith(q) {
	if(!(movingup||movingdown||replacing||loading||busy||shifting)) {
		busy = true;
		retrieve = q;
		loading = setInterval("removeOldContent()",speed);
		contOpacity = 1.0;
	} else {
		if(debug) debug.innerHTML += 'Already busy handling request. Stage: '+(movingup||movingdown?'moving':'')+(replacing?'replacing':'')+(loading?'loading':'')+'<br/>';
		return false;
	}
}

function removeOldContent() {
	if(contOpacity>0) {
		contOpacity -= 0.05;
		content.style.opacity = contOpacity.toFixed(2);
		content.style.filter = 'alpha(opacity='+parseInt(contOpacity*100)+')';
		if(debug) debug.innerHTML += 'Decreasing opacity: '+contOpacity.toFixed(2)+'<br/>';
	} else {
		clearInterval(loading);
		loading = null;
		removeIntervals();
		content.innerHTML = '<div id="wait"></div>';
		content.style.opacity = 1.0;
		content.style.filter = 'alpha(opacity=100)';
		if(debug) debug.innerHTML += 'Fetching new content: <br/>';
		getContent(retrieve);
	}
}

function showNewContent() {
	if(!(movingup||movingdown)) {
		if(replaceContent) {
			content.innerHTML = replaceContent;
			replaceContent = null;
			contOpacity = 0.0;
		}
		if(contOpacity<1) {
			contOpacity += 0.05;
			content.style.opacity = contOpacity.toFixed(2);
			content.style.filter = 'alpha(opacity='+parseInt(contOpacity*100)+')';
			if(debug) debug.innerHTML += 'Increasing opacity: '+contOpacity.toFixed(2)+'<br/>';
		} else {
			clearInterval(replacing);
			replacing = null;
			content.style.opacity = 1.0;
			content.style.filter = 'alpha(opacity=100)';
			busy = false;
			shifting = false;
			if(debug) debug.innerHTML += 'Content fully replaced.<br/>';
		}
	}
}

// Scroll into View

var scrollTarget = null, scrollCounter = 0, scrollDiff = null, scrollSteps = (Math.PI/2)/10;
var scrollInterval = null;

function scrollToTop() {
	var p = (document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop) + 0;
//alert('Value is '+p+'  body.scrollTop: '+(document.body.scrollTop)+'body.parentNode.scrollTop: '+(document.body.parentNode.scrollTop)+'document.documentElement.scrollTop: '+(document.documentElement.scrollTop));
	if(p>250 && !scrollInterval) {
		scrollCounter = 0;
		scrollTarget = 250;
		scrollDiff = p-250;
		scrollInterval = setInterval("softScroll()",speed);
	}	
}

function softScroll() {
	var p = (document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop) + 0;
	if(scrollTarget<p) {
		scrollCounter += scrollSteps; 
		if(document.documentElement && document.documentElement.scrollTop) {
			document.documentElement.scrollTop = parseInt(Math.sin(1-scrollCounter)*scrollDiff+250);
		} else if(document.body.parentNode.scrollTop) {
			document.body.parentNode.scrollTop = parseInt(Math.sin(1-scrollCounter)*scrollDiff+250);
		} else if(document.body.scrollTop) {
			document.body.scrollTop = parseInt(Math.sin(1-scrollCounter)*scrollDiff+250);
		} 
	} else {
		clearInterval(scrollInterval);
		scrollInterval = null;
	}
}

// Image magnification

function magnify(img,w,h) {
	var steps = 15;
	var con = img.parentNode;
	if(!con.inProgress) {
		con.inProgress = true;
		if(!con.naturalWidth) {
			con.naturalWidth = con.offsetWidth;
			con.naturalHeight = con.offsetHeight;
			con.naturalImageWidth = 200;
//			con.naturalImageWidth = img.offsetWidth;
			con.naturalImageHeight = img.offsetHeight;
		}
		con.currentWidth = con.offsetWidth;
		con.currentImageWidth = 200;
//		con.currentImageWidth = img.offsetWidth;
		con.currentImageHeight = img.offsetHeight;
		con.currentHeight = con.offsetHeight;
		con.heightSteps = h-con.currentHeight>0 ? (h-con.currentHeight)/steps : 0;
		con.imageHeightSteps = (h-con.currentImageHeight)/steps;
		con.horizontalSteps = (con.naturalWidth/2-w/2)/steps;
		con.verticalSteps = con.naturalHeight/2-h/2>0 ? (con.naturalHeight/2-h/2)/steps : 0;
		con.widthSteps = (w-con.currentImageWidth)/steps;
//		if(con.widthSteps<0) con.widthSteps = 0;
		con.currentRight = 0;
		con.currentTop = 0;
		img.fullWidth = w;
		img.fullHeight = h;
		con.currentOpacity = 1.0;
		con.opacitySteps = (1.0-0.0)/steps;
		img.style.cursor = "auto";
//alert(' con.naturalWidth: '+con.naturalWidth+' con.naturalHeight: '+con.naturalHeight+' con.naturalImageWidth: '+con.naturalImageWidth+' con.currentWidth: '+con.currentWidth+' con.currentImageWidth: '+con.currentImageWidth+' con.currentHeight: '+con.currentHeight+' img.fullWidth: '+img.fullWidth+' img.fullHeight: '+img.fullHeight+' con.heightSteps: '+con.heightSteps+' con.horizontalSteps: '+con.horizontalSteps+' con.widthSteps: '+con.widthSteps);
		con.enlarge = function() {
			con.currentHeight += con.heightSteps;
			con.currentImageHeight += con.imageHeightSteps;
			con.currentRight += con.horizontalSteps;
			con.currentTop += con.verticalSteps;
			con.currentImageWidth += con.widthSteps;
			con.currentOpacity -= con.opacitySteps;
			if(con.currentImageWidth<img.fullWidth) {
	//		if(con.currentHeight<img.fullHeight) {
				con.style.height = parseInt(con.currentHeight)+'px';
				img.style.height = parseInt(con.currentImageHeight)+'px';
				img.style.width = parseInt(con.currentImageWidth)+'px';
				img.style.right = parseInt(con.currentRight)+'px';
				img.style.top = parseInt(con.currentTop)+'px';
				img.nextSibling.style.opacity = con.currentOpacity.toFixed(2);
				img.nextSibling.style.filter = 'alpha(opacity='+parseInt(con.currentOpacity*100)+')';
			} else {
		//		con.style.height = h+'px';
				img.style.height = h+'px';
				img.style.width = w+'px';
				img.nextSibling.style.opacity = 0.0;
				clearInterval(con.movement);
				con.movement = null;
				con.onmouseout = minimise;
			}
		}
		con.reduce = function() {
			con.currentHeight -= con.heightSteps;
			con.currentImageHeight -= con.imageHeightSteps;
			con.currentRight -= con.horizontalSteps;
			con.currentTop -= con.verticalSteps;
			con.currentImageWidth -= con.widthSteps;
			con.currentOpacity += con.opacitySteps;
			if(con.currentImageWidth>200) {
				con.style.height = parseInt(con.currentHeight)+'px';
				img.style.height = parseInt(con.currentImageHeight)+'px';
				img.style.width = parseInt(con.currentImageWidth)+'px';
				img.style.right = parseInt(con.currentRight)+'px';
				img.style.top = parseInt(con.currentTop)+'px';
				img.nextSibling.style.opacity = con.currentOpacity.toFixed(2);
				img.nextSibling.style.filter = 'alpha(opacity='+parseInt(con.currentOpacity*100)+')';
			} else {
				con.style.height = con.naturalHeight+'px';
				img.style.height = con.naturalImageHeight+'px';
				img.style.width = con.naturalImageWidth+'px';
				img.style.right = '0px';
				img.style.top = '0px';
				img.nextSibling.style.opacity = 1.0;
				img.nextSibling.style.filter = 'alpha(opacity=100)';
				clearInterval(con.movement);
				con.movement = null;
				img.style.cursor = "pointer";
				con.inProgress = false;
			}
		}
		con.movement = setInterval("document.getElementById('"+con.id+"').enlarge()",10);
	}
}

function minimise(e) {
	if(!e) {
		e = event;
		var dest = e.toElement;
	} else {
		var dest = e.relatedTarget;
	}
	if(dest && dest!=this && dest.parentNode) {
		while(dest && dest.parentNode && dest.nodeName!="BODY" && dest.parentNode!=this) {
			dest = dest.parentNode;
		}
		if(dest.nodeName=='BODY') {
			if(this.movement) clearInterval(this.movement);
			this.movement = setInterval("document.getElementById('"+this.id+"').reduce()",10);
		}
	}
}

function removeIntervals() {
	el = document.getElementById('store').getElementsByTagName('OL');
	for(var n=0; n<el.length; n++) {
		var t = el[n].firstChild.firstChild.nextSibling;
		if(t.movement) {
			clearInterval(t.movement);
			t.movement = null;
			t.inProgress = false;
		}
	}
}

// Martin Ekblom 2009
