/*
    EXSMap v0.09
*/


function isArray(obj) {
    return obj.constructor == Array;
}

var geocoder;
var map;
var houseIicon3 ;

function initializeGoogleApi() {
    geocoder = new GClientGeocoder();
    geocoder.setBaseCountryCode("Germany");
    map = new GMap2(document.getElementById("map_canvas"));
    showLink = document.getElementById("google_showLink").value;
    //map.addControl(new GSmallMapControl()); // Adds zoom, cross etc.
    center = null;

    // loop through result set
    if (isArray(searchResults)) {
		
        for (var i = 0; i < searchResults.length; i++) {
            if (searchResults[i]["latitude"] != null && searchResults[i]["latitude"] != "" && searchResults[i]["longitude"] != null && searchResults[i]["longitude"] != "") {
            		var point = new GLatLng(searchResults[i]["latitude"], searchResults[i]["longitude"]);
            		var link = "";
            		if (showLink == 1)
            		{
            				link = "href='"+searchResults[i]["href"]+"'";
            		}
				add = searchResults[i]["address"].split(",",3);
    			    var address = '<h3><a '+link+'>' + searchResults[i]["title"] +'</a></h3><p>'+add[0]+', '+add[1]+add[2]+'</p>';

					//var marker = createMarker(point, address);
					var marker = createMarkerNumber(point, address, i);					
                    map.addOverlay(marker);
           }
            else {
                // use geocoding to fetch the address
                //alert('before');	
                //var address = searchResults[i]["address"];
                //var title = searchResults[i]["title"];
                //var link = searchResults[i]["href"];
                //showAddress(address, title, link);
            }
        }
                //alert ('There should be ' + searchResults.length );

    }
    center = new GLatLng(50.962579, 10.257568);
	map.setCenter(center, 6);
    map.setUIToDefault();

}

function createMarker(point,html) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

function createMarkerNumber(point, html, i) {
	
	
	i = parseInt(i)+1;
	
	houseIicon3 = new GIcon(G_DEFAULT_ICON);
	
	//houseIicon3.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/green/marker"+i+".png";
	
	//houseIicon3.image = "http://dev.dalason.com/outdoor.de/images/map-icons/"+i+".png";

	houseIicon3.image = "http://www.outdoor.de/images/map-icons/"+i+".png";	

	houseIicon3.shadow = false;
	markerOptions = { icon:houseIicon3 };
		
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
 	
	});
 
 	return marker;	
}


function getInfoText(address, title, link) {
    var txt = '<h3><a href="' + link + '">' + title + '</a></h3><p>' + address + '</p>';
    return txt;
}

function createMarkerOptions() {
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    // Set up our GMarkerOptions object
    markerOptions = { icon: blueIcon };
    return markerOptions;

}
function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}


function showaddress_act(i) {

	/*var point = new GLatLng(lan, lat);
	var marker = createMarkerNumber(point, address, i);			
	map.addOverlay(marker);
	marker.openInfoWindowHtml(address);	
	*/
	i = parseInt(i);

			var point = new GLatLng(searchResults[i]["latitude"], searchResults[i]["longitude"]);
			var link = "";

			address = document.getElementById("hidden_"+i).innerHTML;
			//var marker = createMarker(point, address);
			var marker = createMarkerNumber(point, address, i);					
			map.addOverlay(marker);
			marker.openInfoWindowHtml(address);
/*
	if (searchResults[i]["latitude"] != null && searchResults[i]["latitude"] != "" && searchResults[i]["longitude"] != null && searchResults[i]["longitude"] != "") {
			var point = new GLatLng(searchResults[i]["latitude"], searchResults[i]["longitude"]);
			var link = "";
			
			var address = '<h3>' + searchResults[i]["title"] +'</h3><p>'+searchResults[i]["address"]+'</p>';

			
			var marker = createMarker(point, address);
			var marker = createMarkerNumber(point, address, i);					
			map.addOverlay(marker);
			marker.openInfoWindowHtml(address);	
   }
*/
	
}

function showAddress(address, title, link) {
   // alert('inshowaddress');
    geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {
            alert(address + " not found");
        } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);

/*

            map.setCenter(point, 13);
            map.addOverlay(marker);
		        marker.openInfoWindowHtml(address);
            markerOptions = createMarkerOptions();
            var marker = new GMarker(point, markerOptions);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(getInfoText(address, title, link));
            */
        }
    }
  );
}

