var map;
var mgr;
var geocoder;
var points = new Array();
var markers;
var baseIcon;

function initBaseIcon() {
	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';
	var ajax = YAHOO.util.Connect.asyncRequest('GET', '/ajax/country/get/code/'+code,{
			success:function(jsonObj) {
				showAddress(name, jsonObj);
			}
	});
}
function resizeMap(center) {
	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));
		if (center==undefined) {
			center = rect.getCenter();
		}
		map.setCenter(center, Math.min(14,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
	        	  if (cid) {
	        		  var ajax = YAHOO.util.Connect.asyncRequest('GET', '/ajax/coordinates/save/cid/'+cid+'/lat/'+point.lat()+'/long/'+point.lng(),{success:doNothing});
	        	  }
	        	  addPointMarker(point,index,rm,infoContent,type);
	          }
	        }
	      );
}

function addPointMarker(pt,index,rm,infoContent,type) {
	//alert(pt);
	initBaseIcon();
    var letteredIcon = new GIcon(baseIcon);
    var markerIcon;
    if (type=="home") {
    	letteredIcon.image = "/images/markers/marker.png";
    	markerIcon = "/images/markers/JPEG/marker.jpg";
    }
    else 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);
    var markerElem = document.getElementById('marker'+index);
    if (markerElem) {
    	markerElem.innerHTML = '<img class="marker" src="'+markerIcon+'"/>';
    	markerElem.style.display = 'block';
    }
    if (infoContent) {
    	GEvent.addListener(marker, "mouseover", function() {
    	    marker.openInfoWindowHtml(infoContent,{maxWidth: 280});
    	 });
    }
    if (rm==true) {
    	resizeMap();
    }
}
