var map = null;
var geocoder = null;
var addressTried = 0;
var zoomLevel = 11;
var caption = "";
var recordCoordinates;
var linePoints = new Array();
var allCurrentPoints = new Array();
var allCaptions = new Array();

function initializePlace() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapsWindow"));
    geocoder = new GClientGeocoder();
  }
}

function initializePerson() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapsPersonWindow"));
    geocoder = new GClientGeocoder();
  }
}

function showPoint(point) {
	if (!point) {
	  if(allAddresses.length > (addressTried + 1)) {
	  	  addressTried++;
	  	  showAddress(address);
	  } else if(allAddresses[addressTried].indexOf('<') > 0) {
	  	  var newAddress = allAddresses[addressTried].replace(/<(.*)>/g,"");
	  	  showAddress(newAddress);
	  } else if(allAddresses[addressTried].indexOf('\/') > 0) {
	  	  var addressArray = allAddresses[addressTried].split('\/');
	  	  showAddress(addressArray[0]);
	  } else {
	  	  alert(allAddresses[addressTried] + " not found");
	  	  return null;
	  }
	} else {
		map.setCenter(point, zoomLevel);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var marker = new GMarker(point);
		var oldMarkerPos = checkForOtherMarkers(marker);
		if(oldMarkerPos < 0) {
			allCurrentPoints.push(marker);
			map.addOverlay(marker);
			if(caption != "") {
				allCaptions.push(caption);
				marker.bindInfoWindowHtml(caption);
			}
		} else {
			var oldMarker = allCurrentPoints[oldMarkerPos];
			var oldCaption = allCaptions[oldMarkerPos];
			
			if(typeOf(oldCaption) == "Array") {
				var newMarkerArray = caption.split("<br>", 2);
				var newInfoTab = new GInfoWindowTab(newMarkerArray[0], newMarkerArray.length==1?"":newMarkerArray[1]);
				oldCaption.push(newInfoTab);
				allCaptions[oldMarkerPos] = oldCaption;
			} else {
				var oldMarkerArray = oldCaption.split("<br>", 2);
				var newMarkerArray = caption.split("<br>", 2);
				var infoTab1 = new GInfoWindowTab(oldMarkerArray[0], oldMarkerArray.length==1?"":oldMarkerArray[1]);
				var infoTab2 = new GInfoWindowTab(newMarkerArray[0], newMarkerArray.length==1?"":newMarkerArray[1]);
				var infoTabsArray = new Array(infoTab1, infoTab2);
				allCaptions[oldMarkerPos] = infoTabsArray;
			}
			oldMarker.bindInfoWindowHtml(null);
			oldMarker.bindInfoWindowTabs(allCaptions[oldMarkerPos]);
		}
		if(recordCoordinates) {
			linePoints.push(point);
		}
		caption = "";
	}
}

function checkForOtherMarkers(_marker) {
	for(var i=0;i<allCurrentPoints.length;i++){
		var oldPoint = allCurrentPoints[i];
		if(_marker.getLatLng().equals(oldPoint.getLatLng())) {
			return i;
		}
	}
	return -1;
}

function addPoint(point) {
	if (!point) {
	  if(allAddresses.length > (addressTried + 1)) {
	  	  addressTried++;
	  	  addAddress(address);
	  } else if(allAddresses[addressTried].indexOf('<') > 0) {
	  	  var newAddress = allAddresses[addressTried].replace(/<(.*)>/g,"");
	  	  addAddress(newAddress);
	  } else if(allAddresses[addressTried].indexOf('\/') > 0) {
	  	  var addressArray = allAddresses[addressTried].split('\/');
	  	  addAddress(addressArray[0]);
	  } else {
	  	  alert(allAddresses[addressTried] + " not found");
	  	  return null;
	  }
	} else {
		var marker = new GMarker(point);
		var oldMarkerPos = checkForOtherMarkers(marker);
		if(oldMarkerPos < 0) {
			allCurrentPoints.push(marker);
			map.addOverlay(marker);
			if(caption != "") {
				allCaptions.push(caption);
				marker.bindInfoWindowHtml(caption);
			}
		} else {
			var oldMarker = allCurrentPoints[oldMarkerPos];
			var oldCaption = allCaptions[oldMarkerPos];
			map.removeOverlay(oldMarker);
			if(typeOf(oldCaption) == "Array") {
				var newMarkerArray = caption.split("<br>", 2);
				var newInfoTab = new GInfoWindowTab(newMarkerArray[0], newMarkerArray.length==1?"":newMarkerArray[1]);
				oldCaption.push(newInfoTab);
				allCaptions[oldMarkerPos] = oldCaption;
			} else {
				var oldMarkerArray = oldCaption.split("<br>", 2);
				var newMarkerArray = caption.split("<br>", 2);
				var infoTab1 = new GInfoWindowTab(oldMarkerArray[0], oldMarkerArray.length==1?"":oldMarkerArray[1]);
				var infoTab2 = new GInfoWindowTab(newMarkerArray[0], newMarkerArray.length==1?"":newMarkerArray[1]);
				var infoTabsArray = new Array(infoTab1, infoTab2);
				allCaptions[oldMarkerPos] = infoTabsArray;
			}
			map.addOverlay(oldMarker);
			oldMarker.bindInfoWindowTabsHtml(allCaptions[oldMarkerPos]);
		}
		if(recordCoordinates) {
			linePoints.push(point);
		}
		caption = "";
	}
}

function addAddress(place) {
	allAddresses = place.split("|");
	addressTried = 0;
	if (geocoder) {
    	geocoder.getLatLng(allAddresses[addressTried],addPoint);
	}
}

function showAddress(address) {
	allAddresses = address.split("|");
	addressTried = 0;
	if (geocoder) {
    	geocoder.getLatLng(allAddresses[addressTried],showPoint);
	}
}

function showAddressPlace(address) {
	if(!map) {
		initializePlace();
	}
	caption = "";
	showAddress(address);
}

function showAddressPerson(place, _caption) {
	if(!map) {
		initializePerson();
	}
	map.clearOverlays();
	allCurrentPoints = new Array();
	allCaptions = new Array();
	linePoints = new Array();
	zoomLevel = 6;
	caption = _caption;
	showAddress(place);
}

function addAddressPerson(place, _caption) {
	zoomLevel = 6;
	caption = _caption;
	addAddress(place);
}

function showList(_paramString) {
	allParams = _paramString.split("%%%");
	if(allParams.length == 0) {
		return;
	}
	map.clearOverlays();
	linePoints = new Array();
	if(allParams.length <= 2) {
		//show only one, no lines needed
		window.setTimeout('addAddressPerson("'+allParams[0]+'", "'+allParams[1]+'")', 800);
	} else {
		recordCoordinates = true;
		for(var i=0;i<=(allParams.length/2)-1;i++) {
			window.setTimeout('addAddressPerson("'+allParams[2*i]+'", "'+allParams[2*i+1]+'")', (i+1)*800);
		}
		//recordCoordinates = false;
		window.setTimeout('addLine()', (i+1)*800);
	}
}

function addLine() {
	var line = new GPolyline(linePoints);
	map.addOverlay(line);
	//line.show();
}