var clickHandler;
var map;
var lat;
var lng;
var locations;
var bounds;

$(document).ready(function() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		
        map.addControl(new GSmallMapControl());
        //map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(0, 0), 13);
		//map.setMapType(G_SATELLITE_MAP);
		map.disableDragging();
        $.get('loc.php?continent_id='+document.valid.continent_id.value+'&pays_id='+document.valid.pays_id.value, processLocations );
		//$.get('loc.php', processLocations );
    }
    
    $('a#new_location').click(function() {
        $('a#new_location').hide();
        $('div#info').show('slow');
        clickHandler = GEvent.addListener(map, "click", function(marker, point) {
            setNewMarker(point);
        });
    });
    
    $('a#save').click(function() {
        $('div#formular').hide('slow');
        $.post('loc.php',
               { type: 'upload',
                 name: $('form').find('input').get(0).value,
                 latitude: lat,
                 longitude: lng
               }, 
               processLocations );        
    });
    
    $('a#cancel').click(function() {
        $('div#formular').hide('slow');
    });
    
    $('a#zoom_show_all').click(function() {
        zoomShowAll();
    });
});

function setNewMarker(point) {
    $('div#formular').show('slow');
    $('a#new_location').show();
    $('div#info').hide();
    lat = point.lat();
    lng = point.lng();
    $('div#formular').find('p:nth-of-type(0) ').html('Latitude=<b>'+lat+'</b>, Longitude=<b>'+lng+'</b>');
    $('div#formular').show();
    GEvent.removeListener(clickHandler);
}

var locations = new Array();

function processLocations(content) {
	
	var element;
	var marker = new Array();
	eval("locations = "+content);
	//var element = new Array();
	for (var i = 0; i < locations.length; i++) {
		element = locations[i];
		//alert(element.name);
		marker[i] = new GMarker(new GLatLng(element.latitude, element.longitude), {title: element.name+' ('+element.nb+')'});
		map.addOverlay(marker[i]);
		marker[i].latitude = element.latitude;
		marker[i].longitude = element.longitude;
		marker[i].name = element.name;
		marker[i].nb = element.nb;
		marker[i].codepostal = element.codepostal;
		marker[i].region_id = element.region_id;
		marker[i].pays_id = element.pays_id;
		marker[i].continent_id = element.continent_id;
		marker[i].detail = element.detail;
		GEvent.addListener(marker[i], 'click', function() {
			//alert(this.name);
			if (this.codepostal) {
				document.location.href='result.php?select_europe='+this.pays_id+'&codepostal='+this.codepostal;
			} else if (element.region_id) {
				document.location.href='result.php?select_europe='+this.pays_id+'&select_region='+this.region_id;
			} else if (this.pays_id && this.pays_id > 0) {
				if (this.detail == 'nok') {
					document.location.href='result.php?select_europe='+this.pays_id;
				} else {
					this.openInfoWindowHtml(''+this.name+' ('+this.nb+')<br/><a href="result.php?select_europe='+this.pays_id+'">Global search</a><br/><a href="map.php?pays_id='+this.pays_id+'">View regions</a>'); 
				}
			} else {
				document.location.href='map.php?continent_id='+this.continent_id;
			}
		});
	};
    //locations.forEach(function(element, index, array) {
	/*for (var i = 0; i < locations.length; i++) {
	//for each (element in locations) {
		//var element = locations[i];
        var marker = new GMarker(new GLatLng(locations[i].latitude, locations[i].longitude), {title: locations[i].name+' ('+locations[i].nb+')'});
        map.addOverlay(marker);
		//alert (element[i].continent_id);
        GEvent.addListener(marker, 'click', function() {
			alert(locations[0].name);*/
			/*if (element.codepostal) {
				document.location.href='result.php?select_europe='+element.pays_id+'&codepostal='+element.codepostal;
			} else if (element.region_id) {
				document.location.href='result.php?select_europe='+element.pays_id+'&select_region='+element.region_id;
			} else if (element.pays_id && element.pays_id > 0) {
				if (element.detail == 'nok') {
					document.location.href='result.php?select_europe='+element.pays_id;
				} else {
					marker.openInfoWindowHtml(''+element.name+' ('+element.nb+')<br/><a href="result.php?select_europe='+element.pays_id+'">Global search</a><br/><a href="map.php?pays_id='+element.pays_id+'">View regions</a>'); 
				}
			} else {
				document.location.href='map.php?continent_id='+element.continent_id;
			}*/
        /*});
	}
    //});*/
    zoomShowAll();
}

/*function processLocations(content) {
	var locations;
	eval("locations = "+content);
    locations.forEach(function(element, index, array) {
        var marker = new GMarker(new GLatLng(element.latitude, element.longitude), {title: element.name+' ('+element.nb+')'});
        map.addOverlay(marker);
        GEvent.addListener(marker, 'click', function() {
            if (element.codepostal) {
				document.location.href='result.php?select_europe='+element.pays_id+'&codepostal='+element.codepostal;
			} else if (element.region_id) {
				document.location.href='result.php?select_europe='+element.pays_id+'&select_region='+element.region_id;
			} else if (element.pays_id && element.pays_id > 0) {
				if (element.detail == 'nok') {
					document.location.href='result.php?select_europe='+element.pays_id;
				} else {
					marker.openInfoWindowHtml(''+element.name+' ('+element.nb+')<br/><a href="result.php?select_europe='+element.pays_id+'">Global search</a><br/><a href="map.php?pays_id='+element.pays_id+'">View regions</a>');
				}
			} else {
				document.location.href='map.php?continent_id='+element.continent_id+'&pays_id='+element.pays_id;
			}
        });
    });
    zoomShowAll();
}*/

function moveMapTo(index) {
    map.panTo(new GLatLng(locations[index].latitude, locations[index].longitude));
}

function zoomShowAll() {
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(0,0),0);
    
    /*locations.forEach(function(elemet, id, array) {
        bounds.extend(new GLatLng(locations[id].latitude, locations[id].longitude));
    });*/
	for (var i = 0; i < locations.length; i++) {
		bounds.extend(new GLatLng(locations[i].latitude, locations[i].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));
}
