var map;
var mgr;
var geocoder;
var points = new Array();
var markers;

var baseIcon = new GIcon();
baseIcon.shadow = "/images/markers/Pin_shadow.png";
//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(1, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function showAddress(address, zoom) {
	geocoder.getLatLng(address, function(point) {
		if (!point) {
			//alert(address + " not found");
		} else {
			map.setCenter(point, zoom);
		}
	});
}

function showCountry(code, name) {
	var url = '/_ajax/countries.php';
	new Json.Remote(url, {
		method :'get',
		autoCancel :'true',
		onComplete : function(jsonObj) {
			showAddress(name, jsonObj);
		}
	}).send( {
		'code' :code,
		'mode' :'json'
	});
}
function resizeMap() {
	var rects = {
		'minLng' :999,
		'minLat' :999,
		'maxLng' :-999,
		'maxLat' :-999
	}
	//alert(points.length);
	if (points.length > 0) {
		for ( var i = 0; i < points.length; i++) {

			rects = {
				'minLng' :Math.min(rects.minLng, points[i].lng()),
				'minLat' :Math.min(rects.minLat, points[i].lat()),
				'maxLng' :Math.max(rects.maxLng, points[i].lng()),
				'maxLat' :Math.max(rects.maxLat, points[i].lat())
			}

		}

		var rect = new GLatLngBounds(new GLatLng(rects.minLat, rects.minLng),
				new GLatLng(rects.maxLat, rects.maxLng));
		
		map.setCenter(rect.getCenter(), map.getBoundsZoomLevel(rect));
	}
}
function addMarker(address,index,rm) {
	geocoder.getLatLng(
	        address,
	        function(point) {
	          if (!point) {
	            // alert(address + " not found");
	        	  if (rm==true) {
	        	    	resizeMap();
	        	   }
	          } else {
		        addPointMarker(point,index,rm);
	          }
	        }
	      );
}
function addCacheMarker(cid,address,index,rm,infoContent,type) {
	geocoder.getLatLng(
	        address,
	        function(point) {
	          if (!point) {
	        	  if (rm==true) {
	        	    	resizeMap();
	        	   }
	          } else {
	        	  // Ergebnis cachen
	        	  var url = '/_ajax/coordinates.php';
	        		new Json.Remote(url, {
	        			method: 'get',
	        			autoCancel: 'true'
	        		}).send({
	        			'mode': 'json',
	        			'cid': cid,
	        			'lat': point.lat(),
	        			'long': point.lng()
	        		});
	        		addPointMarker(point,index,rm,infoContent,type);
	          }
	        }
	      );
}

function addPointMarker(pt,index,rm,infoContent,type) {
	//alert(pt);
    var letteredIcon = new GIcon(baseIcon);
    var markerIcon;
    if (type=="premium") {
    	letteredIcon.image = "/images/markers/Pin_blue"+index+".png";
    	markerIcon = "/images/markers/JPEG/Pin_blue"+index+".jpg";
    }
    else {
    	letteredIcon.image = "/images/markers/Pin_blank"+index+".png";
    	markerIcon = "/images/markers/JPEG/Pin_blank"+index+".jpg";
    	//letteredIcon.image = "/images/markers/marker" + index + ".png";
    }
    var markerOptions = { icon:letteredIcon };
    var marker = new GMarker(pt, markerOptions);
    
    map.addOverlay(marker);
    points.push(pt);
    if ($('marker'+index)) {
    	$('marker'+index).innerHTML = '<img class="marker" src="'+markerIcon+'"/>';
    	$('marker'+index).style.display = 'block';
    }
    if (infoContent) {
    	GEvent.addListener(marker, "mouseover", function() {
    	    marker.openInfoWindowHtml(infoContent,{maxWidth: 280});
    	 });
    }
    if (rm==true) {
    	resizeMap();
    }
}
