/*
Cogumelo v0.1 - INNOTO SL
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@innoto.es>

modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/	

var map;
var gdir;
var geocoder = null;
var addressMarker;
var address2;
var point;
var point2;
var locale;
var ruta;	
	
function setIdioma(){
	if (lang === "lang_gl"){
		locale="gl";
	}
    else if (lang == "lang_en"){
    	locale="en";
    }
    else {
    	locale="es";
    }
}

function setLocation(address1){
	point = new GLatLng(43.359683,-8.405448);
    map.setCenter(point, 14);
    //Personalizamos o icono
	var icon = new GIcon();
	icon.image = "Templates/images/marker.png";
	icon.shadow = "Templates/images/icon_shadow.png";
	icon.iconAnchor = new GPoint(16.0, 16.0);
	icon.infoWindowAnchor = new GPoint(16.0, 16.0);
    marker = new GMarker(point, icon);
    map.addOverlay(marker);
}

function setDirections(fromAddress, toAddres, locale){
    $('direction').show();
    gdir = new GDirections(map, $('direction'));
    gdir.load("from: " + point2 + " to: " + point, { "locale": locale});
    marker.hide();
    marker2.hide();
}

function showAddress(response){
	map.clearOverlays();
    if (!response || response.Status.code !== 200){
    	alert("Status Code:" + response.Status.code);
    } 
    else{
    	place = response.Placemark[0];
    	point2 = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    	//Personalizamos o icono2 
    	var icon2 = new GIcon();
		icon2.image = "Templates/images/blue-dot.png";
		icon2.shadow = "icon_shadow.png";
		icon2.iconSize = new GSize(32.0, 32.0);
		icon2.shadowSize = new GSize(49.0, 32.0);
		icon2.iconAnchor = new GPoint(16.0, 16.0);
		icon2.infoWindowAnchor = new GPoint(16.0, 16.0);
    	marker2 = new GMarker(point2, icon2);
    	map.addOverlay(marker2);
    	address2 = place.add;
    	setLocation("Juan Florez 129 A Coruña");  
    	setIdioma();
    	setDirections(point2,point,locale);
    }
}

function getAddress(overlay, latlng){
	if (latlng !== null){
		add = latlng;
        $('mainBody').style.height="780px";
        $('direction').innerHTML="";       	
    	geocoder.getLocations(latlng, showAddress);
    }
}

function initialize(){

	if (GBrowserIsCompatible()){
		map = new GMap2(document.getElementById("mapita"));       
    	//para q funcione o zoom coa rodiña do rato
    	map.enableScrollWheelZoom();	
    	setLocation("Juan Florez 129 A Coruña");  
    	//Reverse Geocoding
    	GEvent.addListener(map, "click", getAddress);  	
    	geocoder = new GClientGeocoder();
    }
}    
    	
function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
    	alert("Dirección no disponible.\nError code: " + gdir.getStatus().code);
    }	
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
    	alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + 	gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
    	alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For 	directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_KEY){
    	alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
    	alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    }
    else{
    	alert("An unknown error occurred.");
    }
}
    	
function onGDirectionsLoad(){
}	
    	
function ZoomIn(){
    map.zoomIn();
}
    	
function ZoomOut(){
    map.zoomOut();
}

function ShowMap(){
    map.setMapType(G_NORMAL_MAP );
}
    	
function ShowMapSatellite(){
    map.setMapType(G_SATELLITE_MAP);
}
    	
function changeText(){
    if (lang == "lang_gl"){
    	$('ruta').value="Sinala no mapa a orixe";
    }
    else if (lang == "lang_en"){
    	$('ruta').value="Place on the map the starting point";
    }
    else{
    	$('ruta').value="Señala en el mapa el origen";
    }
    $('ruta').addClassName('resalted');
}
