
var gMarker=null;
var icon="";
var gTxtGlobus = "";

function loadMapCityGuide(address, zoom, lat, lon, txtGlobus)
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("mapaCityGuideDiv"));

		if(typeof mapType != "undefined")  map.setMapType(mapType);

		if(zoom == -1) zoom = 13;

		if(lat != -1 && lon != -1 && (lat != 0 || lon != 0)){
		    	var point = new GLatLng(lat,lon);

		    	// Centrem el mapa
			map.setCenter(point,zoom);

			// Posem la icona
			gMarker = new GMarker(point, {icon:getIconGuia(),zIndexProcess:zIndexIcon});
			gMarker.zIndex = 1000;
			map.addOverlay(gMarker);
			if(txtGlobus != ""){
				gTxtGlobus = txtGlobus;
				GEvent.addListener(gMarker, "click", function() {gMarker.openInfoWindowHtml(gTxtGlobus);});
			}
		}
		else{
		  	geocoder = new GClientGeocoder();

		  	// Centrem el mapa i posem la icona
			showAddress(address, txtGlobus, icon, zoom)
		}

		//map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());			// Control d'escala i posicio (GSmallMapControl, GLargeMapControl)
	}
}
function zIndexIcon(marker,b)
{
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.zIndex*100000000;
}

function setIconApart(lat, lon, txt)
{
	var point = new GLatLng(lat,lon);

	var marker = new GMarker(point, getDefaultIcon(HOST));
	map.addOverlay(marker);

	if(txt != ""){
		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(txt);});
	}
}
function setIconGuia(lat, lon, txt)
{
	var point = new GLatLng(lat,lon);

	gMarker = new GMarker(point, getIconGuia());
	map.addOverlay(gMarker);

	if(txt != ""){
		gTxtGlobus = txt;
		GEvent.addListener(gMarker, "click", function() {gMarker.openInfoWindowHtml(gTxtGlobus);});
	}
}


function getIconGuia()
{
	var icon = new GIcon();
	if(typeof nivell_html == "undefined") nivell_html = "./";
	icon.image = nivell_html+"img/ico-position.png";
	icon.shadow = nivell_html+"img/ico-position-shadow.png";
	icon.iconSize = new GSize(40,34);
	icon.shadowSize = new GSize(40,34);
	icon.iconAnchor = new GPoint(20,34);
	icon.infoWindowAnchor = new GPoint(20,0);
	icon.transparent = nivell_html+"img/ico-position-transparent.png";
	return icon;
}


function loadMapHome(address, zoom, lat, lon)
{
  if (GBrowserIsCompatible())
  {
	map = new GMap2(document.getElementById("mapaHomeDiv"));

	if(typeof mapType != "undefined")  map.setMapType(mapType);

  	geocoder = new GClientGeocoder();

	if(typeof lat != "undefined" && typeof lon != "undefined"){
		var point = new GLatLng(lat,lon);
		map.setCenter(point, zoom);
	}
	else{
		setMapCenter(address, zoom);
	}

	map.addControl(new GSmallMapControl());			// Control d'escala i posicio (GSmallMapControl, GLargeMapControl)

  }

}
function loadMapNeighborhood(address, zoom)
{
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("mapaNeighborhoodDiv"));

    if(typeof mapType != "undefined")  map.setMapType(mapType);

  	geocoder = new GClientGeocoder();

	setMapCenter(address, zoom);
  }
}
function loadMap(address,gGlobus)
{
	// API v3
	var tMapType = google.maps.MapTypeId.ROADMAP;
	if(typeof mapType != "undefined")  tMapType = mapType;
	var mapOptions = {
		zoom: 15,
		mapTypeId: tMapType,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN
		},
		streetViewControl: true
	};
	map = new google.maps.Map(document.getElementById("mapaDiv"), mapOptions);
	geocoder = new google.maps.Geocoder();

	showAddress_v3(address,gGlobus);
}

var ge=null;
function loadMapContact(address,gGlobus, tx, ty)
{
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("mapaDiv"));

    if(typeof mapType != "undefined")  map.setMapType(mapType);

  	geocoder = new GClientGeocoder();

  	map.addControl(new GLargeMapControl());			// Control d'escala i posicio (GSmallMapControl, GLargeMapControl)
  	map.enableContinuousZoom();
 	map.enableDoubleClickZoom();

    	if(!es.ns&&plugin())
	{
		map.getEarthInstance(getEarthInstanceCB);
		map.addMapType(G_SATELLITE_3D_MAP);

	}
	map.enableScrollWheelZoom();
	map.addControl(new GHierarchicalMapTypeControl());
	if(typeof tx != "undefined" && typeof ty != "undefined")	showAddressByPosition(tx, ty, gGlobus)
	else								showAddress(address,gGlobus);

  }
}
function plugin()
{
	var plugin=false;
	for (i = 0; i < navigator.plugins.length; ++i)
	  	if(navigator.plugins[i].name=="Google Earth Plugin")
	  		plugin=true;


	return plugin;
}
function getEarthInstanceCB(object) {
    	ge = object;
	if(ge)
	{
	      	var navControl = ge.getNavigationControl();
		navControl.setVisibility(ge.VISIBILITY_SHOW);
	    	ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS,true);
		ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS,true);
		ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS,true);
	}
}

function myEventListener()
{

}
function showAddress(address,gGlobus, icon, zoom) {

	if(typeof zoom == "undefined") {
		zoom = 15;
	}

	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert('La direccion "' + address + '" es incorrecta');
			} else {
				map.setCenter(point, zoom);

				if(typeof icon == "undefined") 	gMarker = new GMarker(point, getDefaultIcon(HOST));
				else							gMarker = new GMarker(point, icon);

				map.addOverlay(gMarker);
				//map.addOverlay(new GMarker(point));

				if(typeof gGlobus != "undefined" && gGlobus != "")
					GEvent.addListener(gMarker, "click", function() {gMarker.openInfoWindowHtml(gGlobus);});
			}
		}
	);
}




function showAddressByPosition(tx, ty, gGlobus) {
	var point = new GLatLng(tx, ty);
	map.setCenter(point, 15);
	var gMarker = new GMarker(point, getDefaultIcon(HOST));
	map.addOverlay(gMarker);
	if(typeof gGlobus != "undefined"){
		GEvent.addListener(gMarker, "click", function() {gMarker.openInfoWindowHtml(gGlobus);});
	}
}


function getDefaultIcon(host){
	if(typeof host=="undefined")
		return newGIcon();
	if(typeof nivell_html == "undefined") nivell_html = "./";
	return getIcon(nivell_html+"img/"+host+"/apartments.png", nivell_html+"img/shadow.png", nivell_html+"img/"+host+"/apartments-trans.png");
}
function getIcon(img, shad, trans)
{
	//alert(img);
	icon = new GIcon();
	icon.image =  img;
	icon.shadow = shad;
	icon.iconSize = new GSize(34,38);
	icon.shadowSize = new GSize(34,38);
	icon.iconAnchor = new GPoint(17,34);
	icon.infoWindowAnchor = new GPoint(17,0);
	//  icon.printImage = 'http://google.webassist.com/google/markers/flag/pacifica.gif';
	//  icon.mozPrintImage = 'http://google.webassist.com/google/markers/flag/pacifica_mozprint.png';
	//  icon.printShadow = 'http://google.webassist.com/google/markers/flag/shadow.gif';
	icon.transparent = trans;
	return icon;
}

function showAddressHome(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert('La direccion "' + address + '" es incorrecta');
      } else {
        var marker = new GMarker(point,icon);
        map.addOverlay(marker);

        gMarker = new GMarker(point,icon);
        map.addOverlay(gMarker);
	}
}
  );
}




var globalZoomMapCenter = "";
function setMapCenter(address, zoom) {
	globalZoomMapCenter = zoom;
	var pnt = "";
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert('La direccion "' + address + '" es incorrecta');
			} else {
				map.setCenter(point, zoom);
			}
		}
	);
}




function showAddress_v3(address,gGlobus)
{
	geocoder.geocode( { 'address': address},
		function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
								map: map,
								position: results[0].geometry.location
				});
				marker.setIcon(new google.maps.MarkerImage( "./img/"+HOST+"/apartments.png", null, null, new google.maps.Point(15,30), new google.maps.Size(34,38)));
				marker.setShadow(new google.maps.MarkerImage( "./img/shadow.png", null, null, new google.maps.Point(20,30), new google.maps.Size(34,38)));
				marker.setClickable(true);
				//marker.setTitle("");

				var infowindow = new google.maps.InfoWindow({
					content: gGlobus
				});

				google.maps.event.addListener(marker, 'click', function() {
															infowindow.open(map,marker);
														});

			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		}
	);
}

