(function($){
	var Map = {
		curPostal: null,
		directions: null,
		icon: null,
		instance: null,
		showAddress: function(){
			var map = Map.instance, pub = new GLatLng(51.9030011, 4.4586422); // Schiehavenkade
			map.clearOverlays();
			jQuery('#directions').empty();
			map.setCenter(pub, 15);
			map.setMapType(G_NORMAL_MAP);
			m = new GMarker(pub, Map.getIcon());
			map.addOverlay(m);
			GEvent.addListener(m, 'click', function(){
				m.openInfoWindow('Schiehavenkade 102<br/>3024 EZ Rotterdam<br/>T +31 (0)800-CallPubler<br/>');
			});
			if(Map.directions){
				Map.directions = null;
			}
		},
		catchError: function(el){/*
			if (el.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + el.getStatus().code);
			else if (el.getStatus().code == G_GEO_SERVER_ERROR)
				alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + el.getStatus().code);
			else if (el.getStatus().code == G_GEO_MISSING_QUERY)
				alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + el.getStatus().code);	   
			else if (el.getStatus().code == G_GEO_BAD_KEY)
				alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + el.getStatus().code);
			else if (el.getStatus().code == G_GEO_BAD_REQUEST)
				alert("A directions request could not be successfully parsed.\n Error code: " + el.getStatus().code);
			else alert("An unknown error occurred.");*/
		},
		calculateDirections: function(){
			var p = jQuery('#sPostal').val(), r = new RegExp(/^[0-9]{4}[\s]?[a-zA-Z]{0,2}$/);
			if(r.test(p)){
				Map.curPostal = p;
				if(Map.directions){
					Map.directions.clear();
				}
				var div = jQuery('#directions').empty().get(0);
				if(!div){
					div = jQuery('#map').after('<div id="directions"/><br class="clear" />').parent().find('#directions').get(0)
				}
				var d = new GDirections(Map.instance, div);
				GEvent.addListener(d, 'load', Map.directionsLoaded);
				GEvent.addListener(d, 'error', Map.catchError);
				Map.directions = d;
				Map.directions.load("from: "+p+", Nederland to: Schiehavenkade 102, 3024EZ, Rotterdam", {"setGetPolyline": true, "setGetSteps": true, "locale": "nl"});
			}
			return false;
		},
		copyPolyline: function(p){
			var arr = [], i;
			for(i = 0 ; i < p.getVertexCount() ; i++){
				arr.push(p.getVertex(i));
			}
			return new GPolyline(arr, '#1F68FF');
		},
		directionsLoaded: function(){
			setTimeout(function(){
				var map = Map.instance;
				map.clearOverlays();
				map.setMapType(G_NORMAL_MAP);
				var div = jQuery('#directions').get(0), ih = div.innerHTML, img = '/images/yes2web/icons/yes2you2.png', img2 = '/images/yes2web/icons/yes2maps.png';
				ih = ih.replace('http://maps.google.com/intl/en_ALL/mapfiles/icon_greenA.png', img).replace('http://maps.google.com/intl/en_ALL/mapfiles/icon_greenB.png', img2);
				ih = ih.replace('http://maps.google.com/intl/nl_ALL/mapfiles/icon_greenA.png', img).replace('http://maps.google.com/intl/nl_ALL/mapfiles/icon_greenB.png', img2);
				ih = ih.replace('http://maps.google.com/intl/en_ALL/mapfiles/icon_greenA_graybg.png', img).replace('http://maps.google.com/intl/en_ALL/mapfiles/icon_greenB_graybg.png', img2);
				ih = ih.replace('http://maps.google.com/intl/nl_ALL/mapfiles/icon_greenA_graybg.png', img).replace('http://maps.google.com/intl/nl_ALL/mapfiles/icon_greenB_graybg.png', img2);
				div.innerHTML = ih;
				var d = Map.directions, p = d.getPolyline(), m;
				if(div){
					jQuery('body').addClass(' directions');
				}
				
				if(p){
					var newP = Map.copyPolyline(p);
					map.addOverlay(newP);
				}
				m = d.getMarker(1);
				if(m){
					m.hide();
					map.addOverlay(new GMarker(m.getPoint(), Map.getIcon()));
				}
				m = d.getMarker(0);
				if(m){
					m.hide();
					var icon = new GIcon();
					icon.image = '/images/publer_new/icons/map-icon.png';
					icon.shadow = '/images/publer_new/icons/map-shadow.png';
					icon.iconSize = new GSize(80, 52);
					icon.shadowSize = new GSize(95, 52);
					icon.iconAnchor = new GPoint(0, 52);
					map.addOverlay(new GMarker(m.getPoint(), icon));
				}
				/*
				jQuery('#directions').find('div[style],table[style],tr[style],td[style],a[style],b[style]').each(function(){
					jQuery(this).removeAttr('style');
				}).end().find('div[jsdisplay=walking]').hide().end().find('td[jscontent=addres]').addClass('addres');
				*/
			}, 0);
		},
		getIcon: function(){
			if(!Map.icon){
				var icon = new GIcon();
					icon.image = '/images/publer_new/icons/map-icon.png';
					icon.shadow = '/images/publer_new/icons/map-shadow.png';
					icon.iconSize = new GSize(80, 52);
					icon.shadowSize = new GSize(95, 52);
					icon.iconAnchor = new GPoint(0, 52);
				icon.infoWindowAnchor = new GPoint(35, 2);
				Map.icon = icon;
			}
			return Map.icon;
		},
		initMap: function(){
			var m = jQuery('#map').get(0);
			if (m && GBrowserIsCompatible()){
				var map = new GMap2(m);
				map.addMapType(G_PHYSICAL_MAP);
				map.addControl(new GMapTypeControl());
				map.addControl(new GSmallMapControl());
				Map.instance = map;
				Map.showAddress();
				if(jQuery('form#route').get(0)){
					jQuery('form#route').submit(Map.calculateDirections);
					/*jQuery('#right-content-column .block-b:nth-child(1)').append('<p><a href="#" id="resetMap">Toon op kaart</a></p>').find('#resetMap').click(function(){Map.showAddress(); jQuery('body').removeClass(' directions'); return false;});*/
				}
				jQuery(window).unload(GUnload);
			}
		}
	}
	
	Map.initMap();
	$('#sPostal').focus(function(){
		if(this.value == 'Postcode' || this.value == 'Postal'){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = $('body').hasClass('nl')?'Postcode':'Postal';
		}
	});	

})(jQuery);