
/*

G_NORMAL_MAP- the default view
G_SATELLITE_MAP - showing Google Earth satellite images
G_HYBRID_MAP - showing a mixture of normal and satellite views
G_DEFAULT_MAP_TYPES - an array of these three types, useful for iterative processing

GLargeMapControl - a large pan/zoom control used on Google Maps. Appears in the top left corner of the map by default.
GSmallMapControl - a smaller pan/zoom control used on Google Maps. Appears in the top left corner of the map by default.
GSmallZoomControl - a small zoom control (no panning controls) used in the small map blowup windows used to display driving directions steps on Google Maps.
GScaleControl - a map scale
GMapTypeControl - buttons that let the user toggle between map types (such as Map and Satellite)
GHierarchicalMapTypeControl - a selection of nested buttons and menu items for placing many map type selectors.
GOverviewMapControl - a collapsible overview map in the corner of the screen

*/

var map = null;
var geocoder = null;
var oldonload=window.onload;

function initialize(address, name, website) {
	
	// Facebook init onload
	initFB();
	
	// making nifty corners work //
	oldonload();
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		showAddress(address, name, website);
		
		//document.getElementById("map").firstChild.nextSibling.style.fontSize = "xx-small";
		
		map.getContainer().style.overflow="hidden"; 
		
		// Map type
		map.setMapType(G_HYBRID_MAP);
		
		// Controls
		var typepos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(28,2));
		var zoompos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2,2));
		
		map.addControl(new GSmallZoomControl(), zoompos);
		map.addControl(new GMapTypeControl(), typepos);
		map.addControl(new GOverviewMapControl());
	}
}


/* --- Details Map --- */

function showAddress(address, name, website) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,
	  function(point) {
	    if (!point) {
	      alert(address + " not found");
	    } else {
	      map.setCenter(point, 16);
	      var marker = new GMarker(point);
	      map.addOverlay(marker);
	      marker.openInfoWindowHtml(name + "<br /><a href='" + website + "'>" + website + "</a>");
	    }
	  }
	);
}

function showContact(name, latitude, longitude) {

	var myLatlng = new google.maps.LatLng(latitude, longitude);
	var myOptions = {
  		zoom: 8,
  		center: myLatlng,
  		mapTypeId: google.maps.MapTypeId.HYBRID
	};
	
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	
	var contentString = name;
	
	var infowindow = new google.maps.InfoWindow({
    	content: contentString
	});
	
	var marker = new google.maps.Marker({
    	position: myLatlng, 
    	map: map, 
   		title: 'Marker'
  	});
  	
  	infowindow.open(map,marker);
  
}
   
    
/* --- Global Map ---------------------------- */

var iconBlue = new GIcon(); 
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon(); 
iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconRed.iconSize = new GSize(12, 20);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["restaurant"] = iconBlue;
customIcons["bar"] = iconRed;


function showContacts(category, city, country, state, rating, search) {

  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
   	
	// Map type
	map.setMapType(G_HYBRID_MAP);
	
	// Controls
	var typepos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(28,2));
	var zoompos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2,2));
	
	map.addControl(new GSmallZoomControl(), zoompos);
	map.addControl(new GMapTypeControl(), typepos);
	//map.addControl(new GOverviewMapControl());
	
	//map.setCenter(new GLatLng(47.296386, 9.369954), 5);
	loadCenter(map);
	
    GDownloadUrl("../../ajax/contactsXml.php?category=" + category + "&city=" + city + "&country=" + country + "&state=" + state + "&rating=" + rating + "&search=" + search, function(data) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
      
      	var id = markers[i].getAttribute("id");
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var zipCode = markers[i].getAttribute("zipCode");
        var city = markers[i].getAttribute("city");
        var country = markers[i].getAttribute("country");
        var state = markers[i].getAttribute("state");
        var categoryName = markers[i].getAttribute("categoryName");
        var url = markers[i].getAttribute("url");
        
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
                                
        var marker = createMarker(point, name, id, address, zipCode + " " + city + " " + country, categoryName, url);
        map.addOverlay(marker);
      }
      
      $('#total-results').html(markers.length);
      
    });
    
    GEvent.addListener(map, 'moveend', function() {
		saveCenter(map);
	})
    
  }
}

function createMarker(point, name, id, address, address2, categoryName, url) {
  var marker = new GMarker(point, customIcons[categoryName]);
  var link = "<a href='/addressbook/contacts/?id=" + id + "'>" + name + "</a>";
  var website = "<a href='" + url + "'>" + url + "</a>";
  
  var html = "<b>" + link + "</b><br />";
  html += categoryName + "<br/>";
  html += address + "<br />";
  html += address2 + "<br />";
  html += website;
  
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}


function saveCenter(map) {
	
	var mapzoom=map.getZoom();
	
	var mapcenter=map.getCenter();
	var maplat=mapcenter.lat();
	var maplng=mapcenter.lng();
	
	var cookiestring = maplat+"_"+maplng+"_"+mapzoom;
	
	var exp = new Date();     //set new date object
	exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));     //set it 30 days ahead
	
	Set_Cookie("mapcenter", cookiestring, "30", "/", "", "");
}

function loadCenter(map) {
	var loadedstring = Get_Cookie("mapcenter");
	
	if(loadedstring != null) {
		var splitstr = loadedstring.split("_");
		map.setCenter(new GLatLng(parseFloat(splitstr[0]), parseFloat(splitstr[1])), parseFloat(splitstr[2]));
	} else {
		map.setCenter(new GLatLng(47.296386, 9.369954), 5);
	}
}