YAHOO.util.Event.onDOMReady(Shadowbox.init);

window.addEvent('domready',function() {
	new SmoothScroll({ duration:600 }, window); //700 milliseconds to get there
});
window.addEvent('domready', function(){
	new FormCheck('formular', {
		display : {
			showErrors : 0,
			closeTipsButton : 0,
			flashTips : 1,
			scrollToFirst: false
		}
	})
});
window.addEvent('domready', function(){
	fadeContent();
	if($('map1')){
		showGoogleMaps('map1', 9.0024491 , 49.6816366, 14, 'http://www.google.de/maps?f=q&source=s_q&hl=de&geocode=&q=Bahnhofstra%C3%9Fe+34,+64720+Michelstadt&sll=49.680514,9.004626&sspn=0.008858,0.027552&ie=UTF8&ll=49.681028,9.003768&spn=0.008858,0.027552&z=16');
	}
	if($('map2')){
		showGoogleMaps('map2', 7.7744349, 48.6216065, 14, 'http://www.google.de/maps?f=q&source=s_q&hl=de&geocode=&q=3,+rue+du+Martin-P%C3%AAcheur++67300+Schiltigheim%2FStrasbourg&sll=49.681639,9.002438&sspn=0.011218,0.027552&ie=UTF8&z=16');
	}
});

function getURLParam(strParamName){
	var strReturn = '';
	strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

function fadeContent(){
	var speed = 1000;
	var fade = getURLParam('fade');
	if(fade.length > 1){
		if(navigator.appName.indexOf("Explorer") < 0){
			contentTween = new Fx.Tween('animcontentbottom',
					{
						duration: speed,
						onStart: function(){
							contentTween2 = new Fx.Tween('animcontenttop',
									{
										duration: speed
									}
								);
							contentTween2.start('opacity','0','1');
						}
					}
				);
			contentTween.start('opacity','0','1');
		}else{
			contentTween = new Fx.Tween('animcontentbottom',
					{
						duration: speed,
						onStart: function(){
							contentTween2 = new Fx.Tween('animcontenttop',
									{
										duration: speed
									}
								);
							contentTween2.start('opacity','1','1');
						}
					}
				);
			contentTween.start('opacity','1','1');	
		}
	}else{
		if(!(navigator.appName.indexOf("Explorer") != -1)){
			$('animcontenttop').style.opacity = 1;
			$('animcontentbottom').style.opacity = 1;
		//	$('animcontenttop').filters.alpha.opacity = 100;
		//	$('animcontentbottom').filters.alpha.opacity = 100;
		}else{
			contentTween = new Fx.Tween('animcontentbottom',
					{
						duration: speed,
						onStart: function(){
							contentTween2 = new Fx.Tween('animcontenttop',
									{
										duration: speed
									}
								);
							contentTween2.start('opacity','1','1');
						}
					}
				);
			contentTween.start('opacity','1','1');
		}
	}
}

function changeContentCloseNavi(id, new_height, url, navId, numChildren, curNavId){
	// navi ausblenden
	var navMorphId2 = 'navigation_' + curNavId;
	if(document.getElementById(navMorphId2)){
		naviMorph2 = new Fx.Morph(navMorphId2,
			{
				onComplete: function(){
					$(navMorphId2).style.display = 'none';
					if(navigator.appName.indexOf("Explorer") < 0){
						contentTween = new Fx.Tween('animcontentbottom',
								{
									onStart: function(){
										contentTween2 = new Fx.Tween('animcontenttop',
												{
													onComplete: function(){
														return true;
													}
												}
											);
										contentTween2.start('opacity','1','0');
									}
								}
							);
						contentTween.start('opacity','1','0');
					}
				}
			}
		);
		naviMorph2.start({'height': 0});
	}
	// ende navi ausblenden
}

function changeContent(id, new_height, url, navId, numChildren, curNavId) {
	// set vars and defaults
	speed = 1200;
	transitionType = 'back:out';
	if(new_height > 300){
		animType = 'animcontenttop';
	}else{
		animType = 'animcontentbottom';
	}
	
	naviMorph = new Fx.Morph(id,
			{
				duration: speed,
				transition: transitionType, // quad:in:out
				onStart: function(){
					var navMorphId = 'navigation_' + navId;
					// to move small absolute positioned image in top-content
					morphTopContent = new Fx.Morph ('animcontenttop',
							{
								duration: speed,
								transition: transitionType						
							}
						);
					morphTopContent.start({'height': (new_height-100)});
					if(document.getElementById(navMorphId)){
						img_height = 15;
						subNavHeight = img_height * numChildren;
						$(navMorphId).style.height = 0;
						naviMorph2 = new Fx.Morph(navMorphId,
							{
								onStart: function(){
									$(navMorphId).style.display = 'block';
									changeContentCloseNavi(id, new_height, url, navId, numChildren, curNavId);
								}
							}
						);
						naviMorph2.start({'height': subNavHeight});
					}else{
						changeContentCloseNavi(id, new_height, url, navId, numChildren, curNavId);
					}
				},
				onComplete: function(){
					if(url.length > 6){
						if(url.indexOf("?") != -1) {
							window.location.href = url + '&fade=' + animType;
						}else{
							window.location.href = url + '?fade=' + animType;
						}
					}else{
						url.submit();
					}
				}
			}
		);
	if(navigator.appName.indexOf("Explorer") != -1){
		cur_y_pos = 490 - new_height;
		new_y_pos = new_height - 250;
		cur_bg_position = '0 '+cur_y_pos;
		new_bg_position = '0 '+new_y_pos;
		naviMorph.start({'height': new_height, 'background-position': [cur_bg_position,new_bg_position]});
	}else{
		naviMorph.start({'height': new_height});
	}
	return false;
}

function highlightNavigation(img, file, id){
	img.src = file;
	if(id > 0){
		listItem = document.getElementById('list_'+id);
	//	listItem.style.backgroundImage = 'url(../pics/list_bullet.gif)';
	}
}

function resetNavigation(img, file, id){
	img.src = file;
	if(id > 0){
		listItem = document.getElementById('list_'+id);
		listItem.style.backgroundImage = 'none';
	}
}

var opened_product_id = 0;

function showProductPopup(id) {
	if(opened_product_id > 0 && opened_product_id != id){
		closeProductPopup(opened_product_id);
	}
	opened_product_id = id;
	speed = 600;
	height = 283;
	width = 652;
	product_id = 'article'+id;
	closeButton_id = 'closeBtn'+id;

	var product = document.getElementById(product_id);
	var closeButton = document.getElementById(closeButton_id);
	popup = new Fx.Morph(product_id,
		{
			duration: speed,
			transition: 'quad:in:out',
			onStart: function(){
				product.style.display = 'block';
			},
			onComplete: function(){
				closeButton.style.display = 'block';
			}
		}
	);
	popup.start({'height': height, 'width': width});
	return false;
}

function closeProductPopup(id){
	speed = 600;
	height = 0;
	width = 0;
	product_id = 'article'+id;
	closeButton_id = 'closeBtn'+id;
	
	var product = document.getElementById(product_id);
	var closeButton = document.getElementById(closeButton_id);
	popup = new Fx.Morph(product_id,
			{
				duration: speed,
				transition: 'quad:in:out',
				onStart: function(){
					closeButton.style.display = 'none';
					opened_product_id = 0;
				},
				onComplete: function(){
					product.style.display = 'none';
				}
			}
		);
	popup.start({'height': height, 'width': width});
	return false;
}

function moveListItem(url, navId, numChildren, curNavId, moveItem, redirect){
	var new_padding = 0;
	var navItemId = 'list_' + navId;
	var navItemId2 = 'list_' + curNavId;
	if(moveItem == 1){
		morph = new Fx.Morph(navItemId,
				{
					onStart: function(){
						if($(navItemId2)){
							morph2 = new Fx.Morph(navItemId2,
									{
										onStart: function(){
											$(navItemId2).style.backgroundImage = 'none';
										}
									}
								);
							morph2.start({'padding-left': 0});
						}
					},
					onComplete: function(){
						if(redirect){
							window.location.href = url;
						}else{
							return true;
						}
					}
				}
			);
		morph.start({'padding-left': new_padding});
	}
	return false;
}

function openSubNav(url, navId, numChildren, curNavId, moveItem, heightCorrection){
	var navMorphId = 'navigation_' + navId; // subNav to be opened
	var navMorphId2 = 'navigation_' + curNavId; // subNav to be closed
	var img_height = 15;
	var subNavHeight = img_height * numChildren + heightCorrection;
	var naviMorph2 = new Fx.Morph(navMorphId2,
			{
				onStart: function(){
					$(navMorphId2).style.overflow = 'hidden';
					moveListItem(url, navId, numChildren, curNavId, moveItem);
				},
				onComplete: function(){
					if(redirect){
						window.location.href = url;
					}
				}
			}
		);
	var naviMorph = new Fx.Morph(navMorphId,
		{
			onStart: function(){
				$(navMorphId).style.display = 'block';
				if(document.getElementById(navMorphId2)){
					naviMorph2.start({'height': 0});
				}else{
					moveListItem(url, navId, numChildren, curNavId, moveItem);
				}
			},
			onComplete: function(){
				window.location.href = url;
			}
		}
	);
	if(curNavId != navId){
		if(numChildren > 0){
			$(navMorphId).style.height = 0;
			$(navMorphId).style.overflow = 'hidden';
			naviMorph.start({'height': subNavHeight});
		}else{
			if(document.getElementById(navMorphId2)){
				var redirect = true;
				naviMorph2.start({'height': 0});
			}else{
				if(moveItem == 1){
					moveListItem(url, navId, numChildren, curNavId, moveItem, true);
				}else{
					window.location.href = url;	
				}
			}
		}
	}
	return false;
}

function closeSubNav(url, navId, numChildren, curNavId){
	var navMorphId = 'navigation_' + curNavId; // subNav to be closed
	var naviMorph = new Fx.Morph(navMorphId,
		{
			onStart: function(){
				$(navMorphId).style.overflow = 'hidden';
			},
			onComplete: function(){
				window.location.href = url;
			}
		}
	);
	if($(navMorphId)){
		naviMorph.start({'height': 0});
	}else{
		window.location.href = url;
	}
	return false;
}

function showGoogleMaps(id, lng, lat, zoom, url){
	if (GBrowserIsCompatible()) {
		$(id).onclick=function(){window.location.href = url;};
		var map = new GMap2(document.getElementById(id));
		var center = new GLatLng(lat, lng);
		map.setCenter(center, zoom);
		var marker = new GMarker(center);
		map.addOverlay(marker);
    }
}

function preloadImages(images){
	var image;
	for (var i=0;i < images.length;i++) {
		image = new Image();
		image.src = images[i];
	}
}

Cufon.replace('div.headline', { fontFamily: 'DINPro-Medium' });
Cufon.replace('div.subheadline', { fontFamily: 'DINPro-Medium' });
Cufon.replace('.text2Image', { fontFamily: 'DINPro-Medium' });
Cufon.replace('.mainnavi li a', { hover: true, hoverables: { a: true }, fontFamily: 'DINPro-Medium' });