
var gMarker=null;
var icon="";
var gTxtGlobus = "";

function loadMapCityGuide(address, zoom, lat, lon, txtGlobus)
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("mapaCityGuideDiv"));

		if(zoom == -1) zoom = 13;

		// Creem la icona. Es desarà a la variable global "icon"
		if(typeof nivell_html == "undefined") nivell_html = "./";
		getIcon(nivell_html+"img/"+HOST+"/icon_places.png", nivell_html+"img/shadow.png", nivell_html+"img/"+HOST+"/apartments-trans.png");

		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);
			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 loadMapHome(address, zoom)
{
  if (GBrowserIsCompatible())
  {
	map = new GMap2(document.getElementById("mapaHomeDiv"));
//    	point=new GPoint(lat,lon);
//	map.setCenter(point,zoom);
  	geocoder = new GClientGeocoder();
	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"));
//    	point=new GPoint(lat,lon);
//	map.setCenter(point,zoom);
  	geocoder = new GClientGeocoder();

	setMapCenter(address, zoom);
  }
}
function loadMap(address,gGlobus)
{
  if (GBrowserIsCompatible())
  {
	map = new GMap2(document.getElementById("mapaDiv"));
  	geocoder = new GClientGeocoder();
	map.addControl(new GSmallMapControl());			// Control d'escala i posicio (GSmallMapControl, GLargeMapControl)

  	map.enableContinuousZoom();
    	map.enableDoubleClickZoom();
		map.addControl(new GHierarchicalMapTypeControl());
		//map.enableScrollWheelZoom();

    //map.addControl(new GMapTypeControl()); 			// Tipus de mapa (mapa, satelit, hibrid)

		showAddress(address,gGlobus);

  }
}

var ge=null;
function loadMapContact(address,gGlobus, tx, ty)
{
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("mapaDiv"));

  	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){
	return getIcon("./img/"+host+"/apartments.png", "./img/shadow.png", "./img/"+host+"/apartments-trans.png");
}
function getIcon(img, shad, trans)
{
	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);
			}
		}
	);
}
