// JavaScript Document

var SebDesc = '<div class="bub-titol"><h4 class="titol-lloc">Santa Eug&egrave;nia de Berga</h4></div><div class="bub-content">Lloc: <a href="locals.php?lloc=1">Pavell&oacute; Municipal</a><br />Festes celebrades: 4<br />&rsaquo; <a href="rutes.php">Com arribo?</a></div>';
var TonaDesc = '<div class="bub-titol"><h4 class="titol-lloc">Tona</h4></div><div class="bub-content">Lloc: <a href="locals.php?lloc=2">&Iacute;taca</a><br />Festes celebrades: 3</div>';
var RipollDesc = '<div class="bub-titol"><h4 class="titol-lloc">Ripoll</h4></div><div class="bub-content">Lloc: <a href="locals.php?lloc=3">Il Diavolo</a><br />Festes celebrades: 7</div>';
var ManlleuDesc = '<div class="bub-titol"><h4 class="titol-lloc">Manlleu</h4></div><div class="bub-content">Lloc: <a href="locals.php?lloc=4">Festes de Vilamirosa</a><br />Festes celebrades: 1</div>';


var map;
var escala;
var gdir;
var geocoder = null;
var addressMarker;

function init(page) {
	
	if (arguments.length == 0) {
		page = 0;	
	}
	
	switch(page) {
		case 0:
			inittq();
			// initInici();
			break;
		case 1:
			initInici();
			break;
		case 2:
			initSituacio();
			break;
		case 3:
			initMusica();
			break;
		case 4:
			initViuho();
			break;
		case 5:
			initBlog();
			break;
		case 6:
			initRutes();
			break;
	}

}


/**** Situacio    ***/
function initSituacio() {
	iniMap();
}

/* Rutes  */
function initRutes() {
	var botoc = document.getElementById("botoCalcula");
	var formulari = document.postform;
	botoc.onclick = function() {
		calculaRuta();
		return false;
	}
	formulari.onsubmit = function() {
		calculaRuta();
		return false;
	}
}

function calculaRuta() {
	origen = document.getElementById("origen").value;
	if (origen == "") {
		alert("Origen no definit");
		return false;
	}
	
	oSelect = document.getElementById("desti");
	desti = oSelect.options[oSelect.selectedIndex].value;
	
	if (desti == 1) {
		desti = "41.899945, 2.2787"; // PuntSeb
	} else if (desti == 2) {
		desti = "41.853684, 2.235214"; // PuntTona
	} else if (desti == 3) {
		desti = "42.213668, 2.203059"; // PuntDiabolo
	} else {
		desti = "42.002916, 2.279427";	// PuntManlleu
	}
	
	oCapa = document.getElementById("dirmapa");
	oCapa.style.display = "block";
	oCapa.className = "esperant";
	
	iniDirMap(origen, desti);
}

/** Google Maps */
function iniMap() {
	if (GBrowserIsCompatible()) {
		escala = 9;
    	map = new GMap2(document.getElementById("mapagran"));
		
		/* var icona = new GIcon();
		icona.image = "http://maps.google.com/mapfiles/ms/micons/green.png";
		// icona.image = "../favicon.png";
		icona.shadow = "http://maps.google.com/mapfiles/ms/micons/msmarker.shadow.png";
		icona.iconSize = new GSize(32, 32);
		icona.shadowSize = new GSize(59, 32);
		icona.iconAnchor = new GPoint(6, 20);
		icona.infoWindowAnchor = new GPoint(5, 1); */
		
		// iconOptions.;
		var tqspotopt = {};
		tqspotopt.width = 52;
		tqspotopt.height = 52;
		tqspotopt.primaryColor = "#00CC00";
		tqspotopt.label = "talqual";
		tqspotopt.labelSize = 0;
		tqspotopt.labelColor = "#000000";
		tqspotopt.shape = "circle";
		var icona = MapIconMaker.createFlatIcon(tqspotopt);
		
		var icosimple = MapIconMaker.createFlatIcon({width: 16, height: 16, primaryColor: "#CC7300"});
		// var icona = MapIconMaker.createLabeledMarkerIcon({label: "talqual", primaryColor: "#00CC00"});
		var iconaOptions = { icon:icona };
		var icosimpleOptions = { icon:icosimple };
		
		var puntSeb = new GLatLng(41.899945, 2.2787);
		var puntTona = new GLatLng(41.853684, 2.235214);
		var puntRipoll = new GLatLng(42.213668, 2.203059);
		var puntManlleu = new GLatLng(42.002916, 2.279427);
	
		// Centrem el mapa a SEB
    	map.setCenter(new GLatLng(41.98, 2.2787), escala);
		// Afegim les icones
		Seb = new GMarker(puntSeb, iconaOptions);
		Tona = new GMarker(puntTona, icosimpleOptions);
		Ripoll = new GMarker(puntRipoll, icosimpleOptions);
		Manlleu = new GMarker(puntManlleu, icosimpleOptions);
		map.addOverlay(Seb);
		map.addOverlay(Tona);
		map.addOverlay(Ripoll);
		map.addOverlay(Manlleu);
		
		// Events
		GEvent.addListener(Seb, "click", function() {
			Seb.openInfoWindowHtml(SebDesc);
		});
		GEvent.addListener(Tona, "click", function() {
			Tona.openInfoWindowHtml(TonaDesc);
		});
		GEvent.addListener(Ripoll, "click", function() {
			Ripoll.openInfoWindowHtml(RipollDesc);
		});
		GEvent.addListener(Manlleu, "click", function() {
			Manlleu.openInfoWindowHtml(ManlleuDesc);
		});
		
		// Afegim els controls basics
		map.enableDoubleClickZoom();
		//map.addControl(new GSmallMapControl());
		map.addControl(new GLargeMapControl());
     	map.addControl(new GMapTypeControl());
		
		// Obrim per defecte la finestra de la festa
		Seb.openInfoWindowHtml(SebDesc);
	}
}
  
/** Ruta */
function iniDirMap(origen, desti) {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("mapa"));
		gdir = new GDirections(map, document.getElementById("direccions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		map.addControl(new GSmallMapControl());
		
		setDirections(origen, desti, "ca_ES");
	}
}

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

// Pel tractament d'errors
function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No es pot localitzar una adreça amb aquest nom, prova un lloc pròxim o un nom alternatiu.");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("No s'ha pogut processar la petició, torna-ho a intentar més tard.");
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("No has introduit cap adreça origen. La ruta no s'ha pogut processar.");

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	else 
		alert("S'ha produit un error al servidor. El sistema no està operatiu en aquests moments, intenta-ho de nou més tard.");
	
	   /* 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.");
	   alert("error"); */
	}

// Quan la càrrega es satisfactoria...
function onGDirectionsLoad(){
	// Eliminem la classe 'esperant'
	oCapa = document.getElementById("dirmapa").className = "";
}


