var xMap = null;
var xMapMarkers = [];
var xMapPW = null;
var xMapPE = null;
var xMapPS = null;
var xMapPN = null;

function jumpToLatLong(lat,lng) {

		if (!document.getElementById("map")) {
			return;
		}

		if (!xMap) {
			xMapInit();
		}

		var point = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));

		google.maps.event.addListener(xMap, "bounds_changed", function() {
			var center = xMap.getCenter();
			var bounds = xMap.getBounds();
			var sw = bounds.getSouthWest();
			var ne = bounds.getNorthEast();
			var w = Math.round(sw.lng()-0.5);
			var s = Math.round(sw.lat()-0.5);
			var e = Math.round(ne.lng()+0.5);
			var n = Math.round(ne.lat()+0.5);
			if (w<xMapPW || xMapPW == null || e>xMapPE || xMapPE==null || s<xMapPS || xMapPS==null || n>xMapPN || xMapPN==null) {
				xMapPW = w; xMapPE = e; xMapPS = s; xMapPN = n;
				for (i in xMapMarkers) {
					xMapMarkers[i].setMap(null);
				}
				xMapMarkers.length=0;
		for (var fi = 0; fi < xMapFeeds.length; fi++ ) {
				// --------------------------------------------------
				// alert(xMapFeeds[fi]+"?e="+e+"&w="+w+"&n="+n+"&s="+s); 
				// -------------------------------------------------- 
				jQuery.get(xMapFeeds[fi]+"?e="+e+"&w="+w+"&n="+n+"&s="+s, {}, function(data) {
					var icon = null;
					jQuery(data).find("marker").each(function() {
						var markerspec = jQuery(this);
						icon = new google.maps.MarkerImage();
						icon.url = markerspec.attr("image");
						// --------------------------------------------------
						// icon.size = new google.maps.Size(parseInt(markerspec.attr("sizex")), parseInt(markerspec.attr("sizey")));
						// icon.anchor = new google.maps.Size(parseInt(markerspec.attr("anchorx")), parseInt(markerspec.attr("anchory")));
						// -------------------------------------------------- 
					});
					// --------------------------------------------------
					// markerspecs = xml.documentElement.getElementsByTagName("marker");
					// var icon=null;
					// if (markerspecs.length>0) {
					// 	icon = new GIcon();
					// 	icon.image = markerspecs[0].getAttribute('image');
					// 	if (markerspecs[0].getAttribute('shadow')) {
					// 		icon.shadow = markerspecs[0].getAttribute('shadow');
					// 	};
					// 	icon.iconSize = new GSize(
					// 		parseInt(markerspecs[0].getAttribute('sizex'))
					// 		, parseInt(markerspecs[0].getAttribute('sizey'))
					// 	);
					// 	icon.iconAnchor = new GPoint(
					// 		parseInt(markerspecs[0].getAttribute('anchorx'))
					// 		, parseInt(markerspecs[0].getAttribute('anchory'))
					// 	);
					// 	icon.infoWindowAnchor = new GPoint(
					// 		parseInt(markerspecs[0].getAttribute('infox'))
					// 		, parseInt(markerspecs[0].getAttribute('infoy'))
					// 	);
					// }
					// -------------------------------------------------- 

					jQuery(data).find("item").each(function() {
						var item = jQuery(this);
						var info =
							'<strong><a href="'
							+ item.attr('item_url')
							+ '">'
							+ item.attr('item_name')
							+ '</a></strong>'
							+ '<br />'
							+ ''
							+ item.attr('item_teaser')
						;
						var point = new google.maps.LatLng(parseFloat(item.attr("lat")), parseFloat(item.attr("long")));
						var marker = createMarker(point, info, icon);
						// --------------------------------------------------
						// var marker = createMarker(point, info, icon);
						// -------------------------------------------------- 

						//map.addOverlay(marker)
						
						if ( 
							( xMapURL == item.attr('item_url') )
							|| ( xMapURL.replace(/http:\/\/draft/,'http://www') == item.attr('item_url') )
						) {
							// --------------------------------------------------
							// marker.openInfoWindowHtml(info);
							// -------------------------------------------------- 
							google.maps.event.trigger(marker, 'click');
						}

					});

				});
			}
			}
		});
		if (xMapN && xMapS && xMapW && xMapE ) {
			xMap.fitBounds(
				new google.maps.LatLngBounds(
					new google.maps.LatLng(xMapS,xMapW)
					, new google.maps.LatLng(xMapN,xMapE)
				)
			);
		} else {
			xMap.setCenter(point);
		}
}

function createMarker(point,info,icon) {
	var point, info, icon;
	var marker;

	if (icon) {
		marker = new google.maps.Marker({
			position: point
			, icon: icon
			, map: xMap
		});
	} else {
		marker = new google.maps.Marker({
			position: point
			, map: xMap
		});
	}
	var infowindow = new google.maps.InfoWindow({
		content: info
	});
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(xMap,marker);
	});
	xMapMarkers.push(marker);
	return marker; 
}

function xMapInit() {
	var zoom = xMapZoom;
	if (!zoom) {
		zoom = xMapDomainZoom;
	}
	var mapOptions = {
		zoom: zoom
		, center: new google.maps.LatLng(0,0)
		, mapTypeId: google.maps.MapTypeId.ROADMAP

		// , panControl: false
		, zoomControl: true
		, zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL
		}
		, mapTypeControl: true
		, mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		}
		, scaleControl: false
		, streetViewControl: false
		, overviewMapControl: false
	};
	xMap = new google.maps.Map(document.getElementById("map"), mapOptions);
}

