﻿var map;
var deselectCurrent = function() {};
var gmarkers = [];
var center = new GLatLng(41.705728515237524,1.7631689453125); //by default, catalunya
var zoom = 8;
var lastmarker;
var clickedpoint;

function addLoadEvent(func) {
var oldonload = window.onload;
 if (typeof window.onload != 'function') {
 window.onload = func;
 } else {
 window.onload = function () {
 oldonload();
 func();
 }
 }
}

var MapaVeure = function () {
 Mapa.init();
 Mapa.llegeixXML();

}

var iconBlue = new GIcon();
 iconBlue.image = 'http://www.moblesdecuina.com/img/marcador.png';
 iconBlue.shadow = 'http://www.moblesdecuina.com/img/shadow50.png';
 iconBlue.iconSize = new GSize(12, 20);
 iconBlue.shadowSize = new GSize(22, 20);
 iconBlue.iconAnchor = new GPoint(6, 20);
 iconBlue.infoWindowAnchor = new GPoint(5, 1);


function CatControl() {}

CatControl.prototype = new GControl();

CatControl.prototype.initialize = function(map) {
 var container = document.createElement("div");
 container.setAttribute("class","caixeszoom");

 var mapbtn = document.createElement("div");
 mapbtn.appendChild(document.createTextNode("Mapa"));
 mapbtn.id="mpbtn";
 this.setButtonStyle2_(mapbtn);
 GEvent.addDomListener(mapbtn, "click", function() {
 		CatControl.prototype.setButtonStyle3_(document.getElementById("stlbtn"));
			CatControl.prototype.setButtonStyle2_(mapbtn);
 map.setMapType(G_NORMAL_MAP);
 });

 var satelitbtn = document.createElement("div");
 satelitbtn.id="stlbtn";
 satelitbtn.appendChild(document.createTextNode("Satèl·lit"));
 this.setButtonStyle3_(satelitbtn);
 GEvent.addDomListener(satelitbtn, "click", function() {
 		CatControl.prototype.setButtonStyle_(document.getElementById("mpbtn"));
 	CatControl.prototype.setButtonStyle4_(satelitbtn);
 map.setMapType(G_HYBRID_MAP);
 });

//-----------------------------------------------------------------

 var mapcat = document.createElement("div");
  mapcat.innerHTML = "Catalunya";
  mapcat.id="cat";
 this.setButtonStyle_(mapcat);
 GEvent.addDomListener(mapcat, "click", function() {
 	CatControl.prototype.setButtonStyle_(document.getElementById("cat"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("bar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("gir"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("tar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("llei"));
	CatControl.prototype.setButtonStyle2_(mapcat);
	Mapa.zoomFull();
 });
 
 var mapbar = document.createElement("div");
  mapbar.innerHTML = "Barcelona";
  mapbar.id="bar";
 this.setButtonStyle_(mapbar);
 GEvent.addDomListener(mapbar, "click", function() {
 	CatControl.prototype.setButtonStyle_(document.getElementById("cat"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("bar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("gir"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("tar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("llei"));
	CatControl.prototype.setButtonStyle2_(mapbar);
 map.setCenter(new GLatLng(41.7567006344, 1.91152626352), 9);
 });

 var mapgir = document.createElement("div");
  mapgir.innerHTML = "Girona";
  mapgir.id="gir";
 this.setButtonStyle_(mapgir);
 GEvent.addDomListener(mapgir, "click", function() {
 	CatControl.prototype.setButtonStyle_(document.getElementById("cat"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("bar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("gir"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("tar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("llei"));
	CatControl.prototype.setButtonStyle2_(mapgir);
 map.setCenter(new GLatLng(42.1, 2.5), 9);
 });

 var mapllei = document.createElement("div");
  mapllei.innerHTML = "Lleida";
  mapllei.id="llei";
 this.setButtonStyle_(mapllei);
 GEvent.addDomListener(mapllei, "click", function() {
 	CatControl.prototype.setButtonStyle_(document.getElementById("cat"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("bar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("gir"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("tar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("llei"));
	CatControl.prototype.setButtonStyle2_(mapllei);
 map.setCenter(new GLatLng(42, 1), 9);
 });

 var maptar = document.createElement("div");
  maptar.innerHTML = "Tarragona";
  maptar.id="tar";
 this.setButtonStyle_(maptar);
 GEvent.addDomListener(maptar, "click", function() {
 	CatControl.prototype.setButtonStyle_(document.getElementById("cat"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("bar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("gir"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("tar"));
 	CatControl.prototype.setButtonStyle_(document.getElementById("llei"));
	CatControl.prototype.setButtonStyle2_(maptar);
 map.setCenter(new GLatLng(41.1, 1.1), 9);
 });

 container.appendChild(mapbtn);
 container.appendChild(satelitbtn);
 container.appendChild(mapcat);
 container.appendChild(mapbar);
 container.appendChild(mapgir);
 container.appendChild(mapllei);
 container.appendChild(maptar);

 map.getContainer().appendChild(container);
 return container;
}
CatControl.prototype.getDefaultPosition = function() {
 return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(68, 7));
}

CatControl.prototype.setButtonStyle_ = function(button) {
 // button.style.textDecoration = "underline";
 button.style.color = "#000";
 button.style.backgroundColor = "white";
 button.style.font = "small Arial";
 button.style.border = "1px solid black";
 button.style.padding = "2px";
 button.style.marginBottom = "1px";
 button.style.textAlign = "center";
 button.style.width = "5em";
 button.style.cursor = "pointer";
}

CatControl.prototype.setButtonStyle2_ = function(button) {
 // button.style.textDecoration = "underline";
 button.style.color = "#000";
 button.style.backgroundColor = "#CCCFBF";
 button.style.font = "small Arial";
 button.style.border = "1px solid black";
 button.style.padding = "2px";
 button.style.marginBottom = "1px";
 button.style.textAlign = "center";
 button.style.width = "5em";
button.style.cursor = "pointer";
}

CatControl.prototype.setButtonStyle3_ = function(button) {
 // button.style.textDecoration = "underline";
 button.style.color = "#000";
 button.style.backgroundColor = "white";
 button.style.font = "small Arial";
 button.style.border = "1px solid black";
 button.style.padding = "2px";
 button.style.marginBottom = "20px";
 button.style.textAlign = "center";
 button.style.width = "5em";
 button.style.cursor = "pointer";
}

CatControl.prototype.setButtonStyle4_ = function(button) {
 // button.style.textDecoration = "underline";
 button.style.color = "#000";
 button.style.backgroundColor = "#CCCFBF";
 button.style.font = "small Arial";
 button.style.border = "1px solid black";
 button.style.padding = "2px";
 button.style.marginBottom = "20px";
 button.style.textAlign = "center";
 button.style.width = "5em";
button.style.cursor = "pointer";
}

var Mapa={
 init:function() {

 map = new GMap2(document.getElementById("mapa"));
 map.setMapType(G_NORMAL_MAP);
 
 map.addControl(new GLargeMapControl());
 map.addControl(new CatControl());
 map.setCenter(center, zoom);
 map.enableScrollWheelZoom();

 },
llegeixXML:function() {
 GDownloadUrl("xml/negocis.xml", function(data) {
 var xml = GXml.parse(data);
 var markers = xml.documentElement.getElementsByTagName("negoci");
 for (var i = 0; i < markers.length; i++) {
 var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lon")));
 
 //Recollim atributs del XML
 var id = markers[i].getAttribute("id");
 var nom = markers[i].getAttribute("nom");
 var direccio = markers[i].getAttribute("direccio");
 var localitat = markers[i].getAttribute("localitat");
 var cp = markers[i].getAttribute("cp");
 var tel = markers[i].getAttribute("tel");
 var fax = markers[i].getAttribute("fax");
 var mbl = markers[i].getAttribute("mbl");
 var web = markers[i].getAttribute("web");

 var marker = Mapa.createMarker(point, id, nom, direccio, localitat, cp, tel, fax, mbl, web);
 map.addOverlay(marker);
 }
 });
 },

 //Funció que crea un marcador
 createMarker:function(point, id, nom, direccio, localitat, cp, tel, fax, mbl, web) {
 var title=nom;
 var marker = new GMarker(point,{icon:iconBlue,title:title});

 // Si es fa clic sobre un marker
 GEvent.addListener(marker, 'click', function() {
 clickedpoint=marker.getLatLng();
 marker.openInfoWindowHtml(makeWindow2(nom,direccio,localitat,cp,tel,fax,mbl,web));

 });
 return marker;
 },

 
 //Zoom a resolució correcta
 zoomOn: function() {
 map.closeInfoWindow();
 map.setMapType(G_NORMAL_MAP);
 map.setCenter(clickedpoint, 16);
 //map.panTo(clickedpoint);
},

 
 zoomFull: function() {
 map.closeInfoWindow();
 map.setCenter(center, zoom);
 map.setMapType(G_NORMAL_MAP);
 }

} 

function makeWindow2(nom,direccio,localitat,cp,tel,fax,mbl,web) {

 var html ="<div id='info'><br/>" + "<span class='empresamapa'>" + nom + "</span>" + "<br/>" + "<span class='empresaloc'>" + direccio + "<br/>" + cp + "-" + localitat + "<br/>" + tel + "<br/>" + mbl + "</span>" + "<br/>" + "<span class='empresaweb'>" + "<a href='" + web +"'>" + web + "</a>" + "</span><br/><a href=javascript:Mapa.zoomOn()>Amplia la zona</a></div>";

 return html;
} 

