$(window).load(function() {
	var lang = (window.location.href.indexOf("/de/") > -1) ? "de" : "en";

	$("#mainNavi h1, #mainNavi h2, #mainNavi h3").hover(function() {
		$(this).addClass("over");
	}, function() {
		$(this).removeClass("over");
	});

	var $searchLink = $("#metaNavi .suche a");
	$searchLink.click(function() {
		$("#searchWrapper").toggle();
		return false;
	});
	$("#searchWrapper .close").click(function() {
		$searchLink.click();
	});

	var $printButton = $(".pageTools .print a");
	if (window.name && (window.name == "printWin" || window.name == "printWinHaendler")) {
		$("body").removeClass("dark").addClass("light");
		$("link[rel='stylesheet']:first").attr("href", "stylesheet_print.css");
		$("link[rel='stylesheet']").each(function(idx, elem) {
			//alert("test="+elem.href);
			if (elem.media == "print" && elem.href.match(/(de|en)\/.*print/)) {
				$(elem).attr("media", "all");
				window.setTimeout('$(".stageactive li").css("display", "inline")', 500);
			}
		});
		$("head").append("<style type=\"text/css\" media=\"screen, projection\">.pageTools { float: left; display: block; }</style>");
		$printButton.click(function() {
			window.print();
			return false;
		});
	} else if ($("body").attr("id") == "geschichte") {
		$printButton.click(function() {
			var printWin = window.open("geschichte-druck.pdf", "printWin", "width=740, height=555, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no");
			return true;
		});
	} else if ($("body").attr("id") == "haendler") {
		$printButton.click(function() {
		var land = $("#land").val();
		var data = $("#data").val();
		var plz = $("#plz").val();
		if(data != "wohnen" && data != "office")
		{
			data = "wohnen";
		}
		
		var path = window.location.href +"?land=" + land + "&data=" + data;
		
		if (plz != "")	
		{
			path = path + "&plz=" + plz;
		}
		path = path + "&printMode=true";
		
		var printWin = window.open(path, "printWinHaendler", "width=740, height=555, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no");
			return false;
		});
	}
	else {
			$printButton.click(function() {
			var printWin = window.open(window.location.href, "printWin", "width=740, height=555, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no");
			return false;
		});
	}

	if (!window.name || (window.name && window.name != "printWin")) {
			$("#subNavi li[class^='p']").each(function() {
			var $naviItem = $(this);
			var $stageItem = $("#stage li." + this.className);
			var idx = Math.ceil(this.className.substr(1, 2) / 9);

			$naviItem.hover(function() {
				$stageItem.mouseover();
				$(".paging .g" + idx + " a").mouseover()
			}, function() {
				$stageItem.mouseout();
				$(".paging .g" + idx + " a").mouseout()
			});

			var $img = $("img", $stageItem);
			if (!$img.length) return;

			var outSrc = $img.attr("src");

			$stageItem.mouseover(function() {
				$img.attr("src", outSrc.replace(/-na\.jpg/, ".jpg"));
				$("a", $naviItem).addClass("over");
			});
			$stageItem.mouseout(function() {
				$img.attr("src", outSrc);
				$("a", $naviItem).removeClass("over");
			});
		});

		$(".paging li[class^='g'] a").each(function(idx) {
			var $img = $("img", this);
			if (!$img.length) return;

			var $this = $(this);
			var outSrc = $img.attr("src");

			$this.mouseover(function() {
				$img.attr("src", outSrc.replace(/\.gif/, "-over.gif"));
			});
			$this.mouseout(function() {
				$img.attr("src", outSrc);
			});
		});
	} else {
		$("#stage li[class^='p']").each(function() {
			var $img = $("img", this);
			if (!$img.length) return;

			$img.attr("src", $img.attr("src").replace(/-na\.jpg/, ".jpg"));
		});
	}

	if (window.imgs) {
		if (window.opener && window.opener.bigImg) {
			$("#stage img").attr("src", window.opener.bigImg);
		} else {
			var $body = $("body");
			var $stage = $("#stage");
			var $tn = $("#tn");
			var $list = $("#tns ol");
			
			var $content = $("#content p:first");
			imgs[0].text = $content.html();

			$.each(window.imgs, function(i) {
				var idx = i + 1;
				var img = this;
				var $item = $("<li></li>").appendTo($list);
				$item.css("background-image", "url(http://www.walterknoll.de/data/images/nr/" + idx + ".gif)");

				$item.hover(function() {
					if (idx != $stage.idx) {
						$(this).css("background-image", $item.css("background-image").replace(/([0-9])+\./, "$1-a."));
						$(this).addClass("over");
						$tn.css("background-image", "url(" + img.tn + ")");
					}
				}, function() {
					if (idx != $stage.idx) {
						$(this).css("background-image", $item.css("background-image").replace(/([0-9])+-a\./, "$1."));
					}
					$(this).removeClass("over");
					$tn.css("background-image", "none");
				});

				$item.click(function() {
					$body.removeClass();
					$body.addClass(img.body);

					$stage.idx = idx;
					window.bigImg = img.lg;
					$stage.children("img").attr("src", bigImg);

					$content.html(img.text || imgs[0].text);

					var $head = $("#content h1");
					var hdText = $head.text();
					$head.empty().removeClass();
					$head.text(hdText);
					if (typeof sIFR_replaceHead == "function") {
						sIFR_replaceHead();
					}

					$(this).css("background-image", $item.css("background-image").replace(/([0-9])+\./, "$1-a."));
					$(this).addClass("active");
					$(this).siblings().each(function() {
						$(this).css("background-image", $(this).css("background-image").replace(/([0-9])+-a\./, "$1."));
						$(this).removeClass("active");
					});

					$tn.css("background-image", "none");
				});

				if (!i) {
					$item.click();
				}
			});
		}
	}

	if (document.getElementById("map")) {
		var wkLat = 48.593569,
			wkLng = 8.862982,
			wkAddress = "Bahnhofstr. 25, 71083 Herrenberg",
			map,
			gdir,
			geocoder = null,
			addressMarker,
			locale = "de_DE",
			dirPanel = document.getElementById("directionsPanel");

		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));

			map.setCenter(new GLatLng(wkLat, wkLng), 15);
			map.addControl(new GLargeMapControl());
			//map.addControl(new GMapTypeControl());
			map.openInfoWindowHtml(map.getCenter(), "Walter Knoll AG &amp; Co. KG,<br />Bahnhofstr. 25,<br />71083 Herrenberg");

			gdir = new GDirections(map, dirPanel);
			GEvent.addListener(gdir, "load", function() {
				//alert($("table", dirPanel).length);
			});
		}
		
		function calcRoute() {
			var fromAddress = $("#from").val();
			if (fromAddress) {
				gdir.load("from: " + fromAddress + " to: " + wkAddress, {"locale": locale});
				map.closeInfoWindow();
			}
		}
		calcRoute();

		/*$("#route").submit(function() {
			var fromAddress = $("#from").val();
			if (fromAddress) {
				gdir.load("from: " + fromAddress + " to: " + wkAddress, {"locale": locale});
				map.closeInfoWindow();
			}
			return false;
		});*/

		window.onunload = GUnload;
	}
}).resize(function() {
	var dummy = $("#page").height();
});
