﻿jQuery.noConflict();

(function($) {
$(window).unload(function() { GUnload(); });
	
var map,
	gdir,
	location,
	infoWindow,
	myMap;

myMap = {
	initialize: function() {
		if (GBrowserIsCompatible()) {
			var ourLocation = new GLatLng(48.704702, 9.210918);
			
			map = new GMap2(document.getElementById("gmap"));
			map.setCenter(ourLocation, 13); // unsere koordinaten: 48.704702, 9.210918
			map.setUIToDefault();
			
			map.addOverlay(new GMarker(ourLocation));
			
			map.openInfoWindowHtml(map.getCenter(), "<p class=\"more_space_bottom\">G + P Glanzer + Partner<br>Werbeagentur GmbH</p><p>Paracelsusstraße 26, 70599 Stuttgart</p>");
			
			gdir = new GDirections(map, document.getElementById("directions"));
			//GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
			//GEvent.addListener(gdir, "error", handleErrors);
		}
	},
	setDirections: function(fromAddress, toAddress, locale) {
		gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	}
};

$(document).ready(function() {
	var bg = "#FFF url(images/content/kontakt/bg_input.png) 10% 60% no-repeat"; // BG für das Input Feld

	myMap.initialize();
	
	$("#mapform")
		.submit(function() {
			map.closeInfoWindow();
			var from = $(this).find("#fromAdress").val();
			myMap.setDirections(from, "Paracelsusstraße 26, 70599 Stuttgart", "de_DE");
			
			$(this).data("submitted", true);
			$(this).data("from", from);
			
			return false;
		})
		.find("#fromAdress")
		.val("")
		.css("background", bg)
		.focus(hideBg)
		.blur(showBg);
	
	function hideBg() {
		$(this).css("background", "#FFF");
	}
	function showBg() {
		if($(this).val() == "")
			$(this).css("background", bg);
	}
	
	$("#link-fullsize")
		.one("click", function() { // es soll nur einmal ausführbar sein
			
			$(this).remove();
			
			$("#text_wrapper")
				.hide("normal", function() {              // verstecke den Text
					$(this)
						.find("#mapform")
						.appendTo("#new_mapform")         // versetze ihn in den Inhaltsbereich
						.show("normal", function() {      // zeigen und map vergrößern und neu laden
							
							$("#map_wrapper").addClass("map-fullsize");
							
							myMap.initialize();
							
							var $mapform = $("#mapform");
							if($mapform.data("submitted")) { // wenn vor dem vergrößern schon einmal die route berechnet worden ist
								$("#directions").empty();
							
								map.closeInfoWindow();
								myMap.setDirections($mapform.data("from"), "Paracelsusstraße 26, 70599 Stuttgart", "de_DE");
							}
						
						});
					$("#content h2, #content h3, #content p").show();
				});
				
				return false;
		});
	
});
})(jQuery);
