Event.observe(window,"load", initGMaps);

	CMIcon = Class.create( new GIcon(), {
			initialize: function () {
				this.image = "/ObraSocial/images/gmap/cmicon.png";
				this.shadow = "/ObraSocial/images/gmap/cmiconshadow.png";
				this.iconSize = new GSize(32, 23);
				this.shadowSize = new GSize(55, 23);
				this.iconAnchor = new GPoint(15, 23);
				this.infoWindowAnchor = new GPoint(12, 15);													
			}
	});

	CMLocator = Class.create({
			initialize: function() {
			this.geo = new GClientGeocoder();},
			geo:null,
			onSuccess:null,
			onFailure:null,
			findLocation: function (marker) {
				this.geo.getLatLng(marker.getAddress(),this.onLoadGeo.bind(this,marker));},
			onLoadGeo : function (marker,point) {
					if (!point) {
						if (this.onFailure) this.onFailure(marker);
					} else {
						marker.setPoint(point);
						if (this.onSuccess) this.onSuccess(marker);
					}}					
	});	

	CMMarker = Class.create({
			initialize: function() {},
			marker:null,
			icon:null,
			address:null,
			center:false,
			zoom:6,
			onClick: function (marker,point) {},
			setMarker: function (marker) {
					this.marker=marker;
					GEvent.addListener(this.marker,'click', this.onClick.bindAsEventListener(this));},

			setPoint: function (point) {
						if (point) {
							var markerOptions = (this.getIcon()) ? {icon:this.getIcon()} : null;
							this.point = point;
							this.setMarker(new GMarker(point,markerOptions));
						}},
			getMarker: function () {
					return this.marker;},
			getPoint: function () {
					return this.marker.getPoint();},
			setIcon: function (icon) {
					this.icon = icon;},
			getIcon: function () {
					return this.icon;},
			getAddress: function () {},
			isCenter: function () {
					return this.center;},
			setCenter: function (center) {
					this.center = center;},
			getZoom: function () {
					return this.zoom;},
			setZoom: function (zoom) {
					this.zoom = zoom;}								
	});

	
	CMMarkerProvince = Class.create(new CMMarker(), {
			initialize: function (id,name,country) {
				this.id = id;
				this.name=name;
				this.country=country;},		
			id:null,
			name:null,
			country:null,	
			getAddress: function () {
						return this.name+","+this.country;},
			onClick : function (marker,point) {
				location.href='/ObraSocial/os_cruce/0,0,70210_0_0_0,00.html?provincia='+this.id;
			}		
	});

	CMMarkerCentre = Class.create(new CMMarker(), {
			initialize: function (id,name,address,zipCode,locality,province,country) {
				this.id = id;
				this.name=name;
				this.address=address;
				this.zipCode=zipCode;
				this.locality=locality;
				this.province=province;
				this.country=country;},		
			id:null,
			name:null,
			address:null,
			zipCode:null,
			locality:null,
			province:null,
			country:null,
			getAddress: function () {
						return this.address+" "+this.zipCode+" "+this.locality+" "+this.province+" "+this.country;
			}
	});


	CMMap = Class.create({
			initialize: function (container,position,zoom) {
				this.container = container;
				this.init(position,zoom);	
			}, 
			container: null,
			map: null,
			mgr: null,
			markers: new Array(),			
			init: function (position,zoom) {
				if (this.container) {
					 if (GBrowserIsCompatible()) {
					        this.map = new GMap2(this.container);
					        this.map.setCenter(position,zoom);
					        this.map.setMapType(G_NORMAL_MAP);
					        this.mgr = new MarkerManager(this.map);
					 }
				}
			},
			setMapType: function (maptype) {
				this.map.setMapType(maptype);
			},
			addControl: function (control) {
		    	this.map.addControl(control);
			},
			addCMMarker : function (marker) {
				this.markers.push(marker);
				this.paint(marker);
			},
			paint: function (marker) {			
				this.mgr.addMarker(marker.getMarker(),marker.getZoom());		
				if (marker.isCenter()) {
					this.map.setCenter(marker.getPoint(),marker.getZoom());
				}		
			}
			
	});





function initGMaps() {
	if ($('mapa_google_centros')) {
		new Ajax.Request("/ObraSocial/centros/ajax/os_provincias_centros",
			{method:'get',
			 onSuccess: setProvinciasConCentros
			});
	}
	if ($('mapa_google_centro') && $('mapa_google_centro_id')) {
		new Ajax.Request("/ObraSocial/centros/ajax/os_centro", {
			method:'post',
			postBody:"idCentro="+$('mapa_google_centro_id').firstChild.nodeValue,
			onSuccess: setCentro
		});
	}
}


function setProvinciasConCentros(transport) {
	if (transport.status == 200) {		
		var xmlDoc = transport.responseXML;
		var provincias = xmlDoc.getElementsByTagName("provincia");
		
		if (provincias.length != 0) {
			
			mapa = new CMMap($('mapa_google_centros'),new GLatLng(39.856778,-4.024476),4);
			locator = new CMLocator();
			
			mapa.addControl(new GSmallMapControl());
			
			locator.onSuccess = function (marker) {
				mapa.addCMMarker(marker);
			}
			
			locator.onFailure = function(marker) {
			}
			
			
			
			for (var i=0; i<provincias.length;i++) {
				var idProvincia = provincias[i].getAttribute("id");
				var provincia = provincias[i].firstChild.firstChild.nodeValue;
				var pais = provincias[i].childNodes[1].firstChild.firstChild.nodeValue;
				var marker = new CMMarkerProvince(idProvincia,provincia,pais);
				marker.setIcon(new CMIcon());
				marker.setZoom(4);
				locator.findLocation(marker);
			}
		}
	}
}


function  setCentro(transport) {
	if (transport.status == 200) {		
		var xmlDoc = transport.responseXML;
		var centro = xmlDoc.getElementsByTagName('centro');
		
		if (centro.length != 0) {
			var localidad = centro[0].childNodes[1];
			var provincia = centro[0].childNodes[2];
			var pais = centro[0].childNodes[3];
			
			mapa = new CMMap($('mapa_google_centro'),new GLatLng(39.856778,-4.024476),4);
			locator = new CMLocator();
			
			mapa.addControl(new GSmallMapControl());
			
			locator.onSuccess = function (marker) {
				mapa.addCMMarker(marker);
			}
			
									
			var id = centro[0].getAttribute("id");
			var name = centro[0].firstChild.firstChild.nodeValue;
			var locality = localidad.childNodes[0].firstChild.nodeValue;
			var direccion = localidad.childNodes[1].firstChild.nodeValue;
			var codigoPostal = localidad.childNodes[2].firstChild.nodeValue;
			var provincia = provincia.firstChild.firstChild.nodeValue;
			var pais = pais.firstChild.firstChild.nodeValue;
			var marker = new CMMarkerCentre(id,name,direccion,codigoPostal,locality,provincia,pais);
			marker.setIcon(new CMIcon());
			marker.setCenter(true);
			marker.setZoom(15);
			locator.findLocation(marker);
		}
		
	}
}