	
	//document ready           
	$(function () {
	
	
		/////////////////////////////////////////////////////////
		// INIT
		//
		
		
		//globals
		galleryOpen = false;
		galleryLoaded = false;
		indexActive = true;
		prevPage = "";
		prevWidth = 0;
		loadingTarget = "";
		windowRef = $(window);

		//IE detection
		ieDetect = 0;
		
		if ($.browser.msie && $.browser.version == 6) {
			ieDetect = 6;
		}
		
		if ($.browser.msie && $.browser.version == 7) {
			ieDetect = 7;
		}
		
		if ($.browser.msie && $.browser.version == 8) {
			ieDetect = 8;
			//to prevent some strange CSS margin behaviour / bug
			window.location = "#"
		}
		
		//Webkit detection
	 	if ($.browser.webkit) {
    		webkitDetect = 1;
 		} else {
 			webkitDetect = 0;
 		}


		
		//transparent PNG's
		//$(document).pngie();
		//$(document).ifixpng(); 
	
		
		//random backgrounds
		
		var backgroundImages = 4;
		function getRandom( min, max ) {
			if( min > max ) {
				return( -1 );
			}
			if( min == max ) {
				return( min );
			}
				return( min + parseInt( Math.random() * ( max-min+1 ) ) );
		}
		
		var background = getRandom(1, backgroundImages);
		
		//  Initialize Backgound Stretcher	
		if(ieDetect != 6) {   
			$(document).bgStretcher({
				images: ['images/bg'+background+'.jpg'],
				imageWidth: 2500, imageHeight: 1319, slideShow: false
			});
		} else {
			//load static 1024 bg for ie6
			$("body").css("background", "url(images/bg"+background+".jpg)");
			$("body").css("background-repeat", "no-repeat");
			$("body").css("background-attachment", "fixed");
		}
		
		
		//load news content
		if(newsPath != "nofile") {
			$('#news').load(newsPath, '#loadContainer');
		} else {
			var customNews = "<div class='newsText'>"+newsText+"</div>";
			$('#news').html(customNews);
			
		}
		
		
		//toggle news panel
		$('#sidebar .header').click(function() {
			var newsVisible = $('#news').is(':visible');
			if (newsVisible) {
				$("#news #loadContainer").hide();
				$("#news").fadeOut(1);
			} else {
				$("#news").fadeIn(1, function() {
					if(ieDetect < 8) {  
						$("#news").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
					}
					$("#news #loadContainer").show();
      			});
			}
				
			if($(this).attr('class') == "header open") {
				$(this).removeClass('open');
				$(this).addClass('closed');
				$(this).contents().removeClass('open');
				$(this).contents().addClass('closed');	
			} else if($(this).attr('class') == "header closed") {
				$(this).removeClass('closed');
				$(this).addClass('open');
				$(this).contents().removeClass('closed');
				$(this).contents().addClass('open');
			}
		});
		
	
		$('#sidebar .header .arrow').hover(
		  function () {
			$(this).css("text-decoration", "none");
		  }, 
		  function () {
			$(this).css("text-decoration", "underline");
		  }
		);
		
		
		//fix header links in ie6
		

		
	
		//main sections to show/hide
		var sections = $('#content .panel');
		sections.hide();
		var hash = "undefined";
		
		
		//scrollbar detections
			
		function checkScrollbars() {
			var barWidth = 0;
			if(typeof( window.innerWidth ) == 'number') {
				//Non-IE
				var outerWidth = window.innerWidth;
				var innerWidth = $(window).width();
					
				if(outerWidth > innerWidth) {
					barWidth = outerWidth-innerWidth;
				} else {
					barWidth = 0;
				}	
			
			} else if( document.documentElement && document.documentElement.clientWidth ) {
				//IE 6+ in 'standards compliant mode'
				if(ieDetect == 6) {
					var border = 3;	
				} else {
					var border = 4;
				}
						
				var innerWidth = $(window).width();
				var outerWidth = $(document).width();
						
				if(outerWidth > innerWidth+border) {
					barWidth = outerWidth-innerWidth;
				} else {
					barWidth = 0;
				}
					

					
			} else if( document.body && document.body.clientWidth ) {
					//IE 4 compatible
					var outerWidth = document.body.clientWidth;
			}
			
			return barWidth;
		};
					
				
				
		
		//center content
		
		function centerContent() {
			if($('#galleryContainer').is(':visible')) {
				galleryVisible = true;
			} else {
				galleryVisible = false;
			}
			
			
			
			scrollbarWidth = checkScrollbars();
			if( typeof( window.innerWidth ) == 'number') {
				//Non-IE
				var outerWidth = window.innerWidth;
				if(webkitDetect == 1) {
					//chrome, safari...
					var targetWidth = 772; //HARDCODED!!!!
				} else {
					var targetWidth = $("#container").outerWidth();
				}
				var openSpace = outerWidth - targetWidth;
				//var leftMargin = openSpace / 2 - scrollbarWidth / 2;
				var leftMargin = openSpace / 2 - scrollbarWidth / 2;
				//alert("outerWidth: "+outerWidth+" | targetWidth: "+targetWidth+" | openSpace:"+openSpace+" | leftMargin: "+leftMargin);
				if(leftMargin < 0) {
					leftMargin = 0;
				}
				$("#container").css("margin-left", leftMargin);
			 } else if( document.documentElement && document.documentElement.clientWidth && !galleryVisible) {
				var outerWidth = $(document).width();
				
				var targetWidth = $("#container").outerWidth();
				var openSpace = outerWidth - targetWidth;
				if(prevWidth != outerWidth && !galleryOpen) {
					var leftMargin = openSpace / 2 - scrollbarWidth / 2;
					prevWidth = outerWidth;
				}
				
				//var leftMargin = openSpace / 2;
				//alert("outerWidth: "+outerWidth+" | targetWidth: "+targetWidth+" | openSpace:"+openSpace+" | leftMargin: "+leftMargin);
				if(leftMargin < 0) {
					leftMargin = 0;
				}
				$("#container").css("margin-left", leftMargin);
				
			}
			//alert("centered");
		}
		
		centerContent();
		
		//center on resize
		$(window).bind('resize', function() {
			centerContent();
			//if (centerTimer) clearTimeout(centerTimer);
			//centerTimer = setTimeout(centerContent, 10);
		});
		
		
		
		//
		// END INIT
		////////////////////////////////////////////////////////
		
		
		
		/////////////////////////////////////////////////////////
		// WINDOW CHANGE (click, back btn, etc.)
		//
		
		$(window).bind('hashchange', function () {
			//centerContent();
			var hash = window.location.hash;
			var tempClean = hash.split("#");
			var cleanHash = tempClean[1];
			
			if(hash == "undefined") {
				//alert("hash undefined");
				prevPage = "";
			} else if(hash == "#news") {
				//$(".panel").hide();
				prevPage = hash;
			} else if(hash == "#") {
			
				
				//this stuff is ie only!!
				//galleryOpen = false;
				prevPage = hash;
				$(".panel").hide();
				
				var galleryVisible = $('#galleryContainer').is(':visible');
				if (galleryVisible && galleryOpen) {
					$("#galleryContainer").css("display", "none");
					$("#galleryContainer").hide(1, function() {
						//centerContent();
						galleryOpen = false;
					});
				}
				
				if(ieDetect != 6  && ieDetect != 7) {
					$("#container").css("display", "inherit");
				}
				
				//show index container & hide eventually opened galleries
				var containerVisible = $('#container').is(':visible');
				if (!containerVisible && !ieDetect) {
					$("#container").show(1, function() {
					centerContent();
				  });
				} else {
					$("#container").show();
				}	
							
				$(".open").each(function() {
					
						$(this).removeClass('open');
						$(this).addClass('closed');
						$(this).attr("href", "#"+$(this).attr("name"));
						
						
					
					
				});
				
				
				
			//**************
			//** gallery
			//**************
			} else if(hash.indexOf("#gallery") != -1) {
				
				$("#container").hide(0, function() {
					$("#statusContainer").text("wird geladen...");
					$("#statusContainer").css("display", "block");
					galleryOpen = true;
					if(ieDetect < 8) {  
						$(".panel").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
					}
					//hide index content & show gallery
					
					//$("#galleryContainer").css("opacity", 0);

				});
				
				
				//load html
				var tempPath = hash.split("=");
				var htmlPath = tempPath[0].split("#");
				var loadingUrl = htmlPath[1] + ".html";
				var galleryTarget = "#"+tempPath[1];
				$("#galleryContainer").html("");
				$("#galleryContainer").load(loadingUrl, function() {
					//alert("html loaded");
					//********* on success
					//set back link
					if(prevPage == "") {
						$(".backlink").attr("href", "#");
					} else {
						$(".backlink").attr("href", prevPage);
					}
					
					$("#backHeader").hover(
							function() {
								$(this).css("text-decoration",  "none");
							},
							function() {
								$(this).css("text-decoration",  "underline");
							}
						);

					//hide panels
					$(".fotoPanel").hide();
					loadImages();
				});
							
					
					
					
					function loadImages() {
						//**************	
						//** image load
						//**************
						
						pics = new Array();
						$("#galleryContent .pic").each(function() {
							pics.push($("img", this).attr("src"));
						});
	
						picsToload = 0;
						picsToload = pics.length;
						//alert("to load: "+picsToload);
						picsLoaded = 0;
						
						if(ieDetect == 8) {
							//alert("now load "+picsToload+" images IN IE 8");
							for (var jj=1;jj<=picsToload;jj++) {
								loadingTarget = "#pic"+jj+" img";
								//alert("checking: "+loadingTarget);
								var loadingPath = "images/"+pics[jj-1];
								
								$(loadingTarget).attr("src", loadingPath + "?random=" + (new Date()).getTime()).load(function (){
									picsLoaded++;
									$("#statusContainer").text(picsLoaded+" von "+picsToload+" Objekten geladen...");
									//alert(picsLoaded+" of "+picsToload+" loaded");	
									if(picsLoaded == picsToload) {
										//alert("all loaded")
										setupGallery();
										
									}
								});
							}
						} else if(ieDetect == 7) {
							//alert("now load "+picsToload+" images IN IE"+ieDetect);
							
							for (var jj=1;jj<=picsToload;jj++) {
								loadingTarget = "#pic"+jj+" img";
								var loadingPath = "images/"+pics[jj-1];
								var ie7Path = "images"+ loadingPath.slice(loadingPath.indexOf(".ch")+3);
								$(loadingTarget).attr("src", ie7Path + "?random=" + (new Date()).getTime()).load(function (){
									picsLoaded++;
									$("#statusContainer").text(picsLoaded+" von "+picsToload+" Objekten geladen...");
									//alert(picsLoaded+" of "+picsToload+" loaded");	
									if(picsLoaded == picsToload) {
										//alert("all loaded")
										setupGallery();
										
									}
								});
	
							}
						
						} else if(ieDetect == 6) {
							//alert("now load "+picsToload+" images IN IE"+ieDetect);
							
							for (var jj=1;jj<=picsToload;jj++) {
								loadingTarget = "#pic"+jj+" img";
								//allways load a jpg for ie6 -> transparency not possible...
								var ie6Img = pics[jj-1].replace(/.png/g, ".jpg");
								//alert("replaced: "+ie6Img.replace(/.png/g, ".jpg"));

								var loadingPath = "images/"+ie6Img;
								var ie6Path = "images"+ loadingPath.slice(loadingPath.indexOf(".ch")+3);
								$(loadingTarget).attr("src", ie6Path + "?random=" + (new Date()).getTime()).load(function (){
									picsLoaded++;
									$("#statusContainer").text(picsLoaded+" von "+picsToload+" Objekten geladen...");
									//alert(picsLoaded+" of "+picsToload+" loaded");	
									if(picsLoaded == picsToload) {
										//alert("all loaded")
										setupGallery();
										
									}
								});
	
							}
								
							 
						} else {
							//alert("now load "+picsToload+" images");
							picCounter = 0;
							$("#galleryContent .pic img").each(function() {
								currentImg = picCounter+1;
								loadingTarget = "#pic"+currentImg+" img";
								$(loadingTarget).attr("src", "");
								var loadingPath = "images/"+pics[picCounter];
								//alert("loading: "+loadingTarget);
								$(loadingTarget).attr("src", loadingPath).load(function() {
									picsLoaded++;
									$("#statusContainer").text(picsLoaded+" von "+picsToload+" Objekten geladen...");
									//alert("loaded: "+picsLoaded);	
									if(picsLoaded == picsToload) {
										//alert("ALL loaded");	
										setupGallery();
										
									}
								});
								picCounter++;
							});
							
						}
					};
					
		
							
					function setupGallery() {
					
					//check if gallery is still open:
					var hash = window.location.hash;
					var tempClean = hash.split("#");
					var cleanHash = tempClean[1];
					
					if(cleanHash.indexOf("gallery") != -1) {
					
							//$("#galleryContainer").css("opacity", 1);
							//alert("setting up gallery");
							$("#statusContainer").css("display", "none");
							$("#galleryContainer").css("display", "block");
							
							//set width of gallery, by adding all the pics width
							galleryWidth = 0;
							$("#galleryContent .pic").each(function() {
									//alert("id: "+$(this).attr("id"));
									if(ieDetect != 6) {
										galleryWidth += $(this).outerWidth(true);
									}
									//alert("galleryWidth: "+galleryWidth);
											
									//set width of panel text
									if(ieDetect != 6) {
										var panelPadding = $(".fotoPanel").innerWidth() - $(".fotoPanel").width();
										var panelWidth = $("img", this).width() - panelPadding;
										var titleWidth = panelWidth - 10;
									} else {
										var panelPadding = $(".fotoPanel").innerWidth() - $(".fotoPanel").width();
										var panelWidth = $("img", this).width() - panelPadding;
										var titleWidth = panelWidth - 10;	
										$(this).css("width", "auto");
										galleryWidth += panelWidth+30;
									}
									
									
									$(".fotoHeader", this).width(titleWidth);
									$(".fotoHeader .title", this).width(titleWidth);
									$(".fotoHeader", this).height("auto");
									$(".fotoPanel", this).width(panelWidth);
									
									if(ieDetect == 7) {
										var picWidth = $("img", this).innerWidth();
										var picHeight = $(".fotoHeader", this).innerHeight() + $("img", this).innerHeight() ;
										$(this).width(picWidth);
										$(this).height(picHeight);
									}
									
									
									//alert("panelWidth: "+panelWidth);
										
							});
										
							//make foto panel toggleable
							$('#galleryContent .fotoHeader.closed, #galleryContent .fotoHeader.open').click(function() {
							
									
									$(this).next().toggle();
									
									if(ieDetect != 6) {
										if($(this).attr('class') == "fotoHeader open") {
											$(this).removeClass('open');
											$(this).addClass('closed');
											$(this).contents().removeClass('open');
											$(this).contents().addClass('closed');	
										} else if($(this).attr('class') == "fotoHeader closed") {
											$(this).removeClass('closed');
											$(this).addClass('open');
											$(this).contents().removeClass('closed');
											$(this).contents().addClass('open');
										}
									} else {
										if($(this).attr('class') == "fotoHeader open") {
											$(this).removeClass('open');
											$(this).addClass('closed');
										} else if($(this).attr('class') == "fotoHeader closed") {
											$(this).removeClass('closed');
											$(this).addClass('open');
										}
										$("#backContainer").css("background", "transparent url(images/arrow_up.gif) no-repeat scroll right center;");
									}
									
							});
							
							
							//toggle also on image click
							$('#galleryContent .pic img').click(function() {
									
									
									$toggleHeader = $(this).prev().prev();
									
									if($toggleHeader.attr('class') == "fotoHeader closed" || $toggleHeader.attr('class') == "fotoHeader open") {
										$(this).prev().toggle();
									
						     			 if(ieDetect != 6) {
						     			 	if($toggleHeader.attr('class') == "fotoHeader open") {
						     			 		$(this).prev().prev().removeClass('open');
						     			 		$(this).prev().prev().addClass('closed');
						     			 		$(this).prev().prev().contents().removeClass('open');
						     			 		$(this).prev().prev().contents().addClass('closed');	
						     			 	} else if($toggleHeader.attr('class') == "fotoHeader closed") {
						     			 		$(this).prev().prev().removeClass('closed');
						     			 		$(this).prev().prev().addClass('open');
						     			 		$(this).prev().prev().contents().removeClass('closed');
						     			 		$(this).prev().prev().contents().addClass('open');
						     			 	}
						     			 } else {
						     			 	if($(this).prev().prev().attr('class') == "fotoHeader open") {
						     			 		$(this).prev().prev().removeClass('open');
						     			 		$(this).prev().prev().addClass('closed');
						     			 	} else if($(this).prev().prev().attr('class') == "fotoHeader closed") {
						     			 		$(this).prev().prev().removeClass('closed');
						     			 		$(this).prev().prev().addClass('open');
						     			 	}
						     			 	$("#backContainer").css("background", "transparent url(images/arrow_up.gif) no-repeat scroll right center;");
						     			 }
									}
									
									
									
							});
							
											
							$(".fotoHeader.closed, .fotoHeader.open").hover(
								function() {
										$(this).css("text-decoration",  "none");
								},
								function() {
										$(this).css("text-decoration",  "underline");
								}
							);
											
							
							if(ieDetect != 6) {
								$('#galleryContent').width(galleryWidth);
							} else {
								$('#galleryContent').width(galleryWidth);
								$("#backHeader").width(galleryWidth);
							}
							//alert("galleryWidth set to final: "+galleryWidth);
							//$(window).scrollLeft($(galleryTarget).position().left);
							if($(galleryTarget).position().left > 0) {
								windowRef.scrollLeft($(galleryTarget).position().left-40);
							} else {
								windowRef.scrollLeft($(galleryTarget).position().left+40);
							}
							windowRef.scrollTo($(galleryTarget).position(), 800);
							
					} else {
					
						//the gallery was closed (back button etc.), so hide everything
						$("#statusContainer").css("display", "none");
						$("#galleryContainer").css("display", "none");
					}
										
				};
					
					
				
			
			//**************
			//** main content
			//**************
			} else {

				prevPage = hash;
				
				
				var galleryVisible = $('#galleryContainer').is(':visible');
				if (galleryVisible && galleryOpen) {
					$("#galleryContainer").css("display", "none");
					$("#statusContainer").css("display", "none");
					$("#galleryContainer").hide(1, function() {
						if(ieDetect < 8) {  
							$(".panel").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
						}
					});
				}
				
				if(ieDetect != 6  && ieDetect != 7) {
					$("#container").css("display", "inherit");
				}
				
				//show index container & hide eventually opened galleries
				var containerVisible = $('#container').is(':visible');
				if (!containerVisible && !ieDetect) {
					$("#container").show(1, function() {
						centerContent();
				  	});
				  
				} else {
					$("#container").show();
				}

				
					
				//hide all
				sections.hide(0, function() {
					if(ieDetect < 8) {  
						$(".panel").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
					}
				 });

				//show only hashed
				
				if(ieDetect == 6 || ieDetect == 7 || ieDetect == 8) {
					sections.filter(hash).show(0, function() {
						if(ieDetect < 8) {  
							$(".panel").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
						}
      				});
					
				} else {
					sections.filter(hash).fadeIn("normal", function() {
							//centerContent();
      				});

				}
				
				
				//change clicked link, so the panel can be closed again
				//alert("galleryOpen: "+galleryOpen);
				$("a.closed, a.open").each(function() {	
					if($(this).attr('href') == hash && !galleryOpen) {
						//alert("switch to open");
						$(this).attr("href", "#");
						$(this).removeClass('closed');
						$(this).addClass('open');
						$(this).parent().removeClass('closed');
						$(this).parent().addClass('open');
						$(this).contents().removeClass('closed');
						$(this).contents().addClass('open');
						
						
						//$("#news").css("background", "red");
					//change clicked closing link, so the panel can be opened again
					} else if($(this).attr('href') == "#" && !galleryOpen) {
						//alert("switch to close");
						$(this).attr("href", "#"+$(this).attr("name"));
						$(this).removeClass('open');
						$(this).addClass('closed');
						$(this).parent().removeClass('open');
						$(this).parent().addClass('closed');
						$(this).contents().removeClass('open');
						$(this).contents().addClass('closed');
					} else if($(this).attr('href') == "#" && galleryOpen && $(this).attr('name') == cleanHash) {
						//alert("nothing 2");
					} else if($(this).attr('href') == hash && galleryOpen) {
						$(this).attr("href", "#");
						$(this).removeClass('closed');
						$(this).addClass('open');
						$(this).parent().removeClass('closed');
						$(this).parent().addClass('open');
						$(this).contents().removeClass('closed');
						$(this).contents().addClass('open');
					} else if($(this).attr('href') == "#" && galleryOpen && $(this).attr('name') != cleanHash) {
						//alert("switch to close 2");
						$(this).attr("href", "#"+$(this).attr("name"));
						$(this).removeClass('open');
						$(this).addClass('closed');
						$(this).parent().removeClass('open');
						$(this).parent().addClass('closed');
						$(this).contents().removeClass('open');
						$(this).contents().addClass('closed');
					} else {
						//alert("nothing");
					}
					
					
				});
				
				galleryOpen = false;
				
				//load html
				
				tempPath = hash.split("#");
				var contentPath = tempPath[1] + ".html";  
				sections.filter(hash).load(contentPath, '#loadContainer', function() {
					//centerContent();
					if(ieDetect < 8) {
						$(".panel").css("filter", "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#E5ffffff', EndColorStr='#E5ffffff'");
					}
				});				
						
			}
			
			//check for scrollbars
			
			
		  });
		  
		//
		// END WINDOW CHANGE
		////////////////////////////////////////////////////////
	  	$(window).trigger( "hashchange" );
		
		
	
		
		
		
		
		
		
		
		/////////////////////////////////////////////////////////
		// CSS STUFF
		//
		
		$(".header").hover(
			function() {
				$(this).css("text-decoration",  "none");
			},
			function() {
				$(this).css("text-decoration",  "underline");
			}
		);
		
		//
		// END CSS STUFF
		////////////////////////////////////////////////////////
	
	
	
	});

