var map;
var gdir;
var tenerifeLat = 28.291564;
var tenerifeLng = -16.53913;
var defaultZoom = 9;

function openUrl( url ){
	document.location.href = url;
}
		
function addPoint(point, address, icono) {
	var miIcono = makeIcon(icono);
	var marker = new GMarker(point, {icon:miIcono});
	GEvent.addListener(marker, "click", function() { openUrl( address);  } );
	return marker;
}
		  // funcion para calcular las rutas
function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from:" + fromAddress + "  to:" + toAddress,	{ "locale": locale }); 
}
		  // funcion para la carga de direcciones
function onGDirectionsLoad() 
{ 
} 
function Calcular( form )
{
	setDirections(form.from.value, form.to.value, language); 	
}
function Intro(e)
{
	if( !e ) {
		if( window.event ) {
			e = window.event;
		}
	}
	var form = $("#Form1")[0];
	if (e.keyCode == 13)
	{
		
		setDirections(form.from.value, form.to.value, language); 
		form.submit = false;
		form.onsubmit = null;
		e.preventDefault();
		/* if( e.preventDefault ) { e.preventDefault(); } */
		e.returnValue = false;
		return false;
	}
	
	if( e.stopPropagation ) { e.stopPropagation(); }
	e.cancelBubble = true;
}
// funcion para cargar el mapa en la pagina
function createDestinosMap(){
	var point;
	var address;
	var marker;
	var mapa = document.getElementById("destinosylugaresmap");
    if ((mapa == null) || (mapa == 'undefined')) {
        return false; 
    }				   
	mapadestinos = new google.maps.Map2( mapa );
	mapadestinos.setCenter(new google.maps.LatLng( document.latitud, document.longitud ),  document.zoom);
	mapadestinos.addControl(new GSmallMapControl());
	
	destinos_markers = [];
	for( el in destinosmap) {
		var establecimiento = destinosmap[el];
		var point = new GLatLng(establecimiento.latitud, establecimiento.longitud);
		var message = setDataTag(establecimiento);
		var icono = makeIcon(establecimiento.icono);
		var marker = addTag(mapadestinos, point, message, {icon: icono, title: establecimiento.nombre	} ) ;
		mapadestinos.addOverlay(marker);
		destinos_markers.push(marker);
	};
	return destinos_markers;
}
function createDestinosMap2(){
	mapadestinos = new google.maps.Map2( document.getElementById("destinosylugaresmap"));
	mapadestinos.setCenter(new google.maps.LatLng( document.latitud, document.longitud ),  document.zoom);
	mapadestinos.addControl(new GLargeMapControl());
	//mgr = new MarkerManager(mapahome);

	$('#destinos li a').click(function(e) {
		e.preventDefault();
		var ref = $(this).attr('rel');
		destinos_markers = buildMapPoints(mapahome, mapdata[ref]);
	}); /*  */
	return destinos_markers
}

function makeIcon( icon ){
	var miIconos = new GIcon(G_DEFAULT_ICON, icon);
	miIconos.iconSize = new GSize(25,25);
   	miIconos.image = icon;
	//miIconos.shadow = "";
	//miIconos.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	//miIconos.shadowSize = new GSize(42, 31);
    miIconos.iconAnchor = new GPoint(10, 29);
    miIconos.infoWindowAnchor = new GPoint(10, 14);
    miIconos.printImage = "";
    miIconos.mozPrintImage = "";
    miIconos.printShadow = "";
    miIconos.transparent = "";
	
	return miIconos;
}

function makePointMap(map, latitud, longitud, zoom, icono) { 
	var mapa = document.getElementById(map); 
	if ((mapa == null) || (mapa == 'undefined')) 
		return false; 
	mapz = new google.maps.Map2(mapa);
	
	var point = new GLatLng (latitud, longitud);
	mapz.setCenter(point, zoom);
	
	var miIconos = makeIcon(icono);
	
	var markerd = new GMarker(point, {icon:miIconos});
  	mapz.addOverlay(markerd);
  	return mapz;
}

function setDataTag(el) {
	return "<h4><a href='" +el.url +"'  >"+el.nombre+"</a></h4>";	
}
function setLongDataTag(el) {
 	return "<h4><a href='" +el.url +"'  >"+el.nombre+"</a></h4><a href='" +el.url +"'  ><img style='float:left' alt='' src='"+ el.img + "' width='75' /></a>" + el.address;	
}

function addTag(map, point, address, options) {
	var marker = new GMarker( point, options);
	marker.message = address;
	GEvent.addListener(marker, "click", function() { 
    	map.openInfoWindowHtml(point, marker.message);
	} )
	return marker;
}

function makeBigPointMap( latitud, longitud, zoom, icono){
    Shadowbox.open({
        player:     'html',
        content:    '',
	//	autoDimensions: true,
        height:     450,
        width:      700,  
        options:    {
            onFinish: function(item){
                var body = document.getElementById(Shadowbox.contentId());
                var mapi = new GMap2(body);
				
                var point = new GLatLng (latitud, longitud);
				
				mapi.setCenter(point, zoom);
				var miIcono = makeIcon(icono);
				var markers = new GMarker(point, {icon:miIcono});

      			mapi.addOverlay(markers);

                mapi.addControl(new GLargeMapControl());
                mapi.addControl(new GMapTypeControl());
				mapi.addControl(new GScaleControl());
            }
        }
    });
}

function buildMapPoints(map,points){
	removeMarkers(map);
	map.clearOverlays();
	var ma = [];
	
	for( el in points) {
		var categoryPionts = points[el];
		//console.log(points[el])
		var point = new GLatLng(categoryPionts.latitud, categoryPionts.longitud);
		// detect height and aply message
		var message = ( $("#mapahome" ).height > 250 )? setLongDataTag(categoryPionts): setDataTag(categoryPionts);
		var icon = makeIcon(categoryPionts.icono);
		var marker = addTag(map, point, message, {icon: icon, title: categoryPionts.nombre } );
		ma.push(marker);
		map.addOverlay(marker);
	}
	return ma
}

function removeMarkers(map){
for (m in markers){
		map.removeOverlay(markers[m]);
		//mgr.removeMarker(markers[m])
	}
}
// visualización de la ruta
function Desde_Click(latitud, longitud)	
{
	Form1.txtOrigen.value = latitud+ ',' + longitud;
	if (Form1.txtDestino.value != "")
		{
			
			setDirections(Form1.txtOrigen.value, Form1.txtDestino.value);
		}
}


function Hasta_Click(latitud, longitud, locale)	
{
	Form1.txtDestino.value= latitud+ ',' + longitud;
	if (Form1.txtOrigen.value != "")
		{
			
			setDirections(Form1.txtOrigen.value, Form1.txtDestino.value);
		}
}	
// Infowin class for displaying a miniature info window. Does not
// respond to any events - so you should show and remove the
// overlay yourself as necessary.

function Infowin(latlng, html) {
	this.latlng_ = latlng;
	this.html_ = html;
	this.prototype = new GOverlay();

	// Creates the DIV representing the infowindow
	this.initialize = function(map) {
		var div = $('<div />');
		div.css({
			position : 'absolute',
			width : 234
		}).appendTo(map.getPane(G_MAP_FLOAT_PANE))
	
		this.map_ = map;
		this.div_ = div;

		this.update(html);
	}

	this.update = function(html){
		this.html_ = html;
		
		this.div_.empty();
		
		$('<div />').css({
			'background-image' : 'url(/images/infow-top.png)',
			height : 14,
			padding: '0 0 0 0'
		}).appendTo(this.div_);

		var content = $('<div/>').addClass('infowin-content').css({
			'position' : 'relative',
			'overflow' : 'hidden',
			'max-height' : 100,
			'top' : -5
		}).html(html);
		
		$('<div />').css({
			'background-image' : 'url(/images/infow-bottom.png)',
			'background-position' : 'bottom left',
			'padding' : '0 10px 25px 10px'
		}).append(content).appendTo(this.div_);
		
		this.redraw(true);
	}
	
	// Remove the main DIV from the map pane
	this.remove = function() {
	  this.div_.remove();
	}

	// Copy our data to a new instance
	this.copy = function() {
	  return new Infowin(this.latlng_, this.html_);
	}

	// Redraw based on the current projection and zoom level
	this.redraw = function(force) {
		if (!force) return;

		var point = this.map_.fromLatLngToDivPixel(this.latlng_);

		// Now position our DIV based on the DIV coordinates of our bounds
		this.div_.css({
			left : point.x - 108,
			top : point.y - this.div_.height() - 22
		});
	}
}


 









