var clickHandler;
var map;
var lat;
var lng;
var locations = Array();
var bounds;
var markers = Array();
var names = Array();

$(document).ready(function() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("gmap"));
        map.addControl(new GLargeMapControl());
 	    map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(43.111, 5.776), 5);


		$.getJSON('/comun/locations.php?lng=' + lngLoc + '&htSel=' + hotelSel, {lng: lngLoc, htSel: hotelSel}, processLocations);
      //  $.get('/comun/locations.php?lng=' + lngLoc + '&htSel=' + hotelSel, processLocations );
    }
});

function processLocations(content) {

//    eval("locations = "+content);
//	locations = jQuery.makeArray(content);

	locations = content

	// iconos
	var icono = new GIcon();
	icono.image = "/images/marker.png";
	icono.shadow = "/images/marker_shadow.png";
	icono.iconSize = new GSize(26, 30);
	icono.shadowSize = new GSize(39, 30);
	icono.iconAnchor = new GPoint(6, 20);
	icono.infoWindowAnchor = new GPoint(5, 1);

    jQuery.each(locations, function() {

		markers.push(new GMarker(new GLatLng(this.latitude, this.longitude), icono));

		objMark = markers[markers.length-1];
        map.addOverlay(objMark);

		addInfoHTML(objMark, this.nombreHotel);
    });

    zoomShowAll();
}


function addInfoHTML(marker, html) {

		GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(html);
        });
}


function zoomShowAll() {
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(0,0),0);

    jQuery.each(locations, function() {
   // locations.forEach(function(elemet, id, array) {
        bounds.extend(new GLatLng(this.latitude, this.longitude));
    });
    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
}

window.onunload = GUnload;