function MapPics() {
	var this1 = this;
	this1.mymap = null;
	this1.markerInfo = null;
	
	this1.mouseoverCB = function() {
	}
	this1.mouseoutCB  = function() {
	}

	/*========================================================================================
	  
	   Function : init
	   
	 ========================================================================================*/
	this1.init = function() {
		logit("init");
	}
	/*========================================================================================
	  
	   Function : parseXmlData
	   
	   creates markerInfo structure
	  
	 ========================================================================================*/
	var parseXmlData = function(data) {
		var imgsrc,imgpopup,mapCoord,coord,zoom,name,link,img1,placemark,city,country,gpslat,gpslng;
		
		logit("parseXmlData");
		//
		// get xml info 
		//
		var tempInfo = {};
		
		tempInfo.name 					= $(data).find("document").attr("name");
		tempInfo.lastCountryIdx	= -1; // init
		tempInfo.lastRegionIdx 	= -1; // init
		tempInfo.nr_countries			= parseInt($(data).find("nr_countries").text());

		if ($(data).find("all_maxlat")) {
			tempInfo.maxlat 		= parseFloat($(data).find("all_maxlat").text());
			tempInfo.maxlng 		= parseFloat($(data).find("all_maxlng").text());
			tempInfo.minlat 		= parseFloat($(data).find("all_minlat").text());
			tempInfo.minlng 		= parseFloat($(data).find("all_minlng").text());    
			
			setCenterBounds(tempInfo);
		}	
		var countryAll = [];
		$(data).find("country").each(function() {
			var country = {};
			country.name 			= $(this).attr("name");
			if (!country.name) {
				country.name = '';
			}
	  	logit("found country:"+country.name);
			country.nr_regions 	= parseInt($(this).find("nr_regions").text());
			country.defThumb = rand(0,country.nr_regions - 1);
			country.maxlat 		= parseFloat($(this).find("cty_maxlat").text());
			country.maxlng 		= parseFloat($(this).find("cty_maxlng").text());
			country.minlat 		= parseFloat($(this).find("cty_minlat").text());
			country.minlng 		= parseFloat($(this).find("cty_minlng").text());
			logit("----------------------country max:"+country.maxlat+'/'+country.maxlng+'/'+country.minlat+'/'+country.minlng);
	
			var regionAll = [];
			$(this).find("region").each(function() {
				var region = {};
				region.name 			= $(this).attr("name");
				if (!region.name) region.name = '';  // Region can be blank
		  	logit("found region:"+region.name);
	
				region.nr_cities 	= parseInt($(this).find("nr_cities").text());
				region.defThumb = rand(0,region.nr_cities - 1);
				region.maxlat 		= parseFloat($(this).find("reg_maxlat").text());
				region.maxlng 		= parseFloat($(this).find("reg_maxlng").text());
				region.minlat 		= parseFloat($(this).find("reg_minlat").text());
				region.minlng 		= parseFloat($(this).find("reg_minlng").text());
				
				var cityAll = [];
		  	$(this).find("city").each(function() {
		  		var city = {};
		  		city.name 		= $(this).attr("name");
		  		city.nr_img  	= parseInt($(this).find("nr_img").text());
					city.defThumb = rand(0,city.nr_img - 1);
			  	city.gpslat 	= parseFloat($(this).find("gpslat").text());
			  	city.gpslng 	= parseFloat($(this).find("gpslng").text());
			  	
			  	city.trip 		= $(this).find("trip").text();
			  	city.link 	= $(this).find("link").text();
			  	city.description 	= $(this).find("description").text();
			  	city.nrpic 	= $(this).find("nrpic").text();
			  	//logit("found city:"+city.name);
			  	//logit("found city gps:"+city.gpslat+'/'+city.gpslng);
		  		var imgAll = []; 
			  	$(this).find("image").each(function() {
			  		var img = {};
			  		img.album = $(this).attr("album");
			  		img.imagesource = $(this).find("imagesource").attr("src");;
			  		img.file_type = $(this).find("file_type").text();
			  		img.startdate = $(this).find("date").text();
	  				img.imagethumb = $(this).find("imagethumb").attr("src");
						if (!img.imagethumb) {
		  				img.imagethumb = imagephp+img.imagesource;
		  				//logit("thumb="+img.imagethumb);
		  			} 
		  			var video_id = $(this).find("video_id");
		  			if (video_id) {
		  				img.video_id = $(video_id).text();
		  			}
						var img_title = $(this).find("img_title");
						if (img_title) {
							img.title = $(img_title).text();
						}
			  		imgAll.push(img);
					});
					city.img = imgAll;
					cityAll.push(city);
				});
				region.city = cityAll;
				regionAll.push(region);
				regionAll.markersAdded = false;
			});
			country.region = regionAll;
			countryAll.push(country);
			countryAll.markersAdded = false;
		});
		tempInfo.country = countryAll;
		tempInfo.markersAdded = false;
		
		logit("first album="+tempInfo.country[0].region[0].city[0].img[0].album);
		
		return tempInfo;
	}
	/*========================================================================================
	  
	   Function : showMarkers
	  
	 ========================================================================================*/
	this1.showMarkers = function(countryIdx,regionIdx) {
		logit("showMarkers:"+countryIdx+'/'+regionIdx+",last:"+this1.markerInfo.lastCountryIdx+'/'+this1.markerInfo.lastRegionIdx);
		if (!this1.markerInfo || this1.markerInfo.name == 'trips') {
			logit("showMarkers NO markerInfo or Trips");
			return;
		}
		//
		// remove previous shown city markers 
		//
		if (this1.markerInfo.lastCountryIdx >= 0) {
			var con1 = this1.markerInfo.country[this1.markerInfo.lastCountryIdx];
			if (this1.markerInfo.lastRegionIdx >= 0) {
				//
				// Show last hidden Region Marker
				//
				if (con1.region[this1.markerInfo.lastRegionIdx].marker) {
					con1.region[this1.markerInfo.lastRegionIdx].marker.show(); // show last region marker again
				}
			} else {
				//
				// Show last hidden Country Marker
				//
				con1.marker.show(); // show last country marker again
			} 
			//
			// Remove Region Markers
			//
			logit("nr regions="+con1.region.length);
			if (con1.region.length != 1) {
				for (var i = 0; i < con1.region.length; i += 1) {
					logit("Remove Region Marker:"+con1.region[i].name);
					if (con1.region[i].marker) con1.region[i].marker.hide();
				}
			}
			if (this1.markerInfo.lastRegionIdx >= 0 || con1.region.length == 1) {
				//
				// Remove City Markers 
				// 
				if (con1.region.length == 1) {
					//
					// When there is only 1 Region then remove all City Markers
					// 
					var reg1 = con1.region[0];
				} else {
					var reg1 = con1.region[this1.markerInfo.lastRegionIdx];
				}
				removeCityMarkers(reg1.city);
			}
			if (this1.markerInfo.lastRegionIdx >= 0) {
				logit("Reset lastRegion");
				this1.markerInfo.lastRegionIdx = -1;
			} else {
				logit("Reset lastCountry");
				this1.markerInfo.lastCountryIdx = -1;
			}
		}
		if (countryIdx < 0) { // when countryIdx is -1 set then show country-markers
			showCountryMarkers();
		} else {
			var con1 = this1.markerInfo.country[countryIdx];
			if (regionIdx < 0) {
				this1.setZoomCenter(con1);
				showRegionCityMarkers(countryIdx,regionIdx,con1,con1.region);
			} else {
				var reg1 = con1.region[regionIdx];
				this1.setZoomCenter(reg1);
				showRegionCityMarkers(countryIdx,regionIdx,reg1,reg1.city);
				//showCityMarkers(countryIdx,regionIdx);
			}
		}
	}
	
	this1.removeCountryMarkers = function(conA) {
		for (var i = 0; i < conA.length; i += 1) {
			logit("Remove Country Marker:"+conA[i].name);
			if (conA[i].marker) conA[i].marker.hide();
			removeRegionMarkers(conA[i].region); // and all region markers
		}
	}
	var removeRegionMarkers = function(regA) {
		for (var i = 0; i < regA.length; i += 1) {
			logit("Remove Region Marker:"+regA[i].name);
			if (regA[i].marker) regA[i].marker.hide();
			removeCityMarkers(regA[i].city); // and all city markers
		}
	}
	
	var removeCityMarkers = function(cityA) {
		for (var i = 0; i < cityA.length; i += 1) {
			logit("Remove City Marker:"+cityA[i].name);
			if (cityA[i].marker) cityA[i].marker.hide();
		}
	}
	
	/*========================================================================================
	  
	   Function : showCountryMarkers
	  
	 ========================================================================================*/
	var showCountryMarkers = function() {
		this1.mymap.setCenter(new GLatLng(0,0),2);
		/*
		if (this1.markerInfo.region_bounds) {  
			this1.setZoomCenter(this1.markerInfo);
		} else {
			this1.mymap.setCenter(new GLatLng(0,0),2);
		}
		*/
		var conA = this1.markerInfo.country;
		for (var i = 0; i < conA.length; i += 1) {
			if (conA[i].name == '') { 
				//
				// No Country name so display all region markers of that blank country
				//
				showRegionCityMarkers(i,-1,conA[i],conA[i].region);
			} else {
				if (this1.markerInfo.markersAdded) {
					// 
					// Markers have already been added : show it
					//
					conA[i].marker.show();
				} else {
					//
					// Create marker and add to map
					//
					reg1 = conA[i];
					setCenterBounds(reg1);  // add Center and Bounds to markerInfo 
					createMarker(reg1,i,-1,-1,"link");
				}	
			}
		} 				
		this1.markerInfo.markersAdded = true;
	}
	/*========================================================================================
	  
	   Function : showRegionCityMarkers
	  
		 this is a recursive function : when region name is blank than all city markers will be shown
		
		 when regionIdx == -1 then display cities only.
	 ========================================================================================*/
	var showRegionCityMarkers = function(countryIdx,regionIdx,reg1,reg1Array) {
		logit("showRegionCityMarkers:"+countryIdx+'/'+regionIdx);
		
		var showCity = false; 
		if (regionIdx >= 0) showCity = true;
		
		showText(reg1.name);
		//
		// Hide the country/region-marker if there is any
		//
		if (reg1.marker) reg1.marker.hide();
		//
		// Save regionIdx in markerInfo, so that we know later which region markers to remove
		//
		this1.markerInfo.lastCountryIdx = countryIdx;
		this1.markerInfo.lastRegionIdx = regionIdx;
		//
		// Now add all region markers
		//
		for (var i = 0; i < reg1Array.length; i += 1) {
			logit("reg1Array="+i);
			if (reg1Array[i].name == '' && !showCity) { 
				//
				// No Region name so display all city markers of that blank region
				//
				showRegionCityMarkers(countryIdx,i,reg1Array[i],reg1Array[i].city);
			} else {
				if (reg1.markersAdded) {
					// 
					// Markers have already been added : show it
					//
					logit("Reshow region:"+reg1Array[i].name);
					if (reg1Array[i].marker) {
						reg1Array[i].marker.show();
					}
				} else {
					//
					// Create markers for first time
					//
					logit("Create new markers for:"+i);
					setCenterBounds(reg1Array[i]);  // add Center and Bounds to markerInfo 
					if (showCity) {
						createMarker(reg1Array[i],countryIdx,regionIdx,i,"city");
					} else {
						createMarker(reg1Array[i],countryIdx,i,-1,"link");
					}
				}
			}
		} 
		reg1.markersAdded = true;
	}
	/*========================================================================================
	  
	   Function : createMarker
	  
	 ========================================================================================*/
	var createMarker = function(reg1,countryIdx,regionIdx,cityIdx,type) {
		logit("Create:"+reg1.name+",idx:"+countryIdx+'/'+regionIdx+'/'+cityIdx+'/'+type);
	  
	  if (type == 'link') {
			icon 			= kmlLinkIcon;
			iconOver 	= kmlLinkIconOver.image;
			point 		= reg1.center;
	  } else {
			icon 			= picIcon;
			iconOver 	= picIconOver.image;
			point 		= new GLatLng(reg1.gpslat,reg1.gpslng);
		}
		//markerOptions = { icon:icon,title:reg1.name };
		markerOptions = { icon:icon};
	  reg1.marker = new GMarker(point,markerOptions);
	  //reg1.marker = new BdccBlock(reg1.center,"#666666",reg1.name); 
	  //reg1.marker.title = null;
	  reg1.marker.title = reg1.name;  // tooltip
	  
		reg1.marker.iconImage = reg1.marker.getIcon().image;
		reg1.marker.iconImageOver = iconOver;
		// indici to markerInfo - structure
		reg1.marker.countryIdx = countryIdx;
		reg1.marker.regionIdx  = regionIdx;
		reg1.marker.cityIdx    = cityIdx;
		//
		// Events for marker for showing popup
		//
	  GEvent.addListener(reg1.marker, "mouseover", function(mapPoint){
	  	logit("mouseover:"+mapPoint+"/icon="+this.iconImageOver+'/'+this.title);
			logit("Idx:"+this.countryIdx+'/'+this.regionIdx+'/'+this.cityIdx);
			var countryIdx = this.countryIdx;
			var regionIdx  = this.regionIdx; 
			var cityIdx    = this.cityIdx;
			if (countryIdx < 0) countryIdx = 0;
			if (regionIdx < 0)  regionIdx = this1.markerInfo.country[countryIdx].defThumb;
			logit("regionIdx="+regionIdx);
			var city_show = true;
			if (cityIdx < 0) {
				cityIdx = this1.markerInfo.country[countryIdx].region[regionIdx].defThumb;
				city_show = false;
			}
			logit("cityIdx="+cityIdx);
		  var imgNr = this1.markerInfo.country[countryIdx].region[regionIdx].city[cityIdx].defThumb;
			logit("imgNr="+imgNr);
			var img = this1.markerInfo.country[countryIdx].region[regionIdx].city[cityIdx].img[imgNr];
			this.setImage(this.iconImageOver);  	
			//
			// if there are more then 1 thumbs then take a random one
			//
			//var len = imgA.length - 1;
			//var randnum = rand(0,len);
			//logit("number thumbs="+len+'/'+randnum);
			if (img.title && city_show) {
				var img_title = img.title;
			} else {
				var img_title = this.title;
			}
			this1.mouseoverCB(mapPoint,img.imagethumb,img_title);
	  });
	  GEvent.addListener(reg1.marker, "mouseout", function(mapPoint){
			this.setImage(this.iconImage);  	
			this1.mouseoutCB();
	  });
	  logit("created a marker:"+reg1.marker.title);
	  this1.mymap.addOverlay(reg1.marker);
	}
	/*========================================================================================
	  
	   Function : loadMap
	  
	 ========================================================================================*/
	this1.loadMap = function(map,p_xmlfile,readyCB) {
		this1.mymap = map;
		//statusm("Please wait,loading data...");
	  // Because IE had a problem (parse error) when using jQuery, I am using Google Maps Ajax 
	 	GDownloadUrl(kmlDir+p_xmlfile+"?dummy=7", function(data, responseCode) {
			if(responseCode == 200) {
				xml = GXml.parse(data);
				// I do not quite understand the following line, but found it on the web 
				$('node', xml.documentElement);
 			  logit("******************** ajax received:"+$(xml).find("document").attr("name"));
	
				this1.markerInfo = parseXmlData(xml);
				this1.showMarkers(-1,-1); // show region markers
				logit("ready receiving");
				readyCB();
			} else if(responseCode == -1) {
				alert("Data request timed out. Please try later.");
				return;
			} else {
				alert("Request resulted in error. Check XML file is retrievable.");
				return;
			}
			logit("ende GDownloadUrl");		
		});
	}
	/*========================================================================================
  
	   Function : setZoomCenter
	
		 Zoom in and set the center  
	 ========================================================================================*/
	this1.setZoomCenter = function(reg1) {
		if (reg1.region_bounds) {
			var zoom = this1.mymap.getBoundsZoomLevel(reg1.region_bounds);
		} else {
			var zoom = 8;
		}
		logit("zoom level for displaying region="+zoom+',at:'+reg1.center.lat()+'/'+reg1.center.lng());
		this1.mymap.setCenter(reg1.center);
		this1.mymap.setZoom(zoom);
	}
	function logit(text) {
		logitAll("MapPics-"+text);
	}

}
/*
  Structure :
  country array (+markersAdded)
    defThumb
    marker
    maxlat
    maxlng
    minlat
    minlng
    name
    nr_regions
    region array (+markersAdded)
      city array
				defThumb
				decsription
				gpslat
				gpslng
				img array
				  album
				  file_type
				  imagesource
				  imagethumb (Image)
				  startdate
				link
				name
				nr_img
				nrpic
				trip
      defThumb
	    maxlat
	    maxlng
	    minlat
	    minlng
			name
			nr_cities
    +ne (GpsLatLng)
    +sw (GpsLatLng)
    +region_bounds
    +center
  lastCountryIdx
  lastRegionIdx
  markersAdded
  name
  nr_countries
*/
