// JavaScript Document
var admin 		= false;
var icon_marker = [];
var icon_dot    = [];
var all_marker  = [];
var last_marker = null;
var dot_info = false;
var bound_spot1 = 5;
var bound_spot2 = 10;
var pratique_global = 1;
var dot_mgr 	= null;
var marker_mgr 	= null;
var timer_dot   = null;
var add_spot_marker = null;
var add_spot = false;
// Creates a marker at the given point with the given number label
function createMarker(lat, lng, id, itineraire, pratique) {
	var point = new GLatLng(lat,lng);
	var marker = new GMarker(point, icon_marker[pratique]);
	if ( itineraire.length > 4 )
	{
		encoded_point = itineraire.substr(0, itineraire.lastIndexOf(" "));
		encoded_level = itineraire.substr(itineraire.lastIndexOf(" ")+1);
		var polyline = new GPolyline.fromEncoded({
			color: "#0084FF",
			opacity: 0.8,
			weight: 4,
			points: encoded_point,
			levels: encoded_level,
		    zoomFactor: 2,
		    numLevels: 18
		});
		map.addOverlay(polyline);
	}
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<div id=\"spot_"+id+"\" style=\"width:400px; overflow:auto;\">Loading ...</div>");
		/*GEvent.addListener(marker.infowindow, 'domready', function()
		{
		    alert("pouet");
		});*/
		last_marker = marker;
		$.post("spot.php", {id_spot: id}, function(html){
			reloadWindow(html);
		});
		setTimeout("test("+id+")",1000);
	});
	
	
	all_marker[id] = marker;
	return marker;
}

function test(id)
{
	id_foreign_commentaire=id;
	id_spot=id;
	chercheCommentaires("spotcommentaires.php", true);
	$.ajax({
		type: "POST",
	    url: "spotnote.php?id_spot="+id,
		async: false,
	    success: function(data) {
			$("#spot_note").append(data);
		} 
	});
	$('a.colorbox').colorbox();
}

// Creates a marker at the given point with the given number label
function createDot(lat, lng, id, spot_count, name) {
	var point = new GLatLng(lat,lng);
	var spot_count_index = 1;
	if ( spot_count > bound_spot1 ) spot_count_index = 2;
	if ( spot_count > bound_spot2 ) spot_count_index = 3;
	var dot_marker = new GMarker(point, icon_dot[spot_count_index]);
	
    GEvent.addListener(dot_marker, "click", function() {
		clearTimeout(timer_dot);
		map.removeOverlay(dot_marker);
		PutMarker('../map/map_data_'+pratique_global+'_'+id+'.xml');
		listerDepartement(id, pratique_global);
	});
	
	GEvent.addListener(dot_marker, "mouseover", function() {
		clearTimeout(timer_dot);
		last_marker = dot_marker;
		timer_dot = setTimeout("displayInfoDot(\""+name+"<hr color='#DDDDDD' size='1' width='250' noshade>"+spot_count+" spot"+(spot_count>1? "s":"")+"\")",1000);
	});
	GEvent.addListener(dot_marker, "mouseout", function() {
		clearTimeout(timer_dot);
		hideInfoDot();
	});
 
	return dot_marker;
}

function displayInfoDot(txt) {
	if ( last_marker != null ) {
		last_marker.openInfoWindowHtml(txt);
		dot_info = true;
	}
}

function hideInfoDot() {
	if ( dot_info ) {
		map.closeInfoWindow();
		dot_info = false;
	}
}

function reloadWindow(html) {
	if ( last_marker != null ) {
		last_marker.openInfoWindowHtml("<div style=\"width:400px;height:400px;overflow:auto;margin-top:5px;\">"+html+"</div>", {maxWidth:400});
	}
}

var map = null;
var loading_icon = null;
var loading_marker = null;
function load(pratique, default_dep, default_spot) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
	map.controlInit("map", "map_parent", "full");
	var default_zoom = (default_dep==0)? 6 : 12;
    map.setCenter(new GLatLng(46.63,2.66), default_zoom);
	pratique_global = pratique;
		
	// Set the loading layer
	loading_icon = new GIcon();
	loading_icon.image = "../images/map_loading.png";
	loading_icon.shadow = "../images/map_loading_s.png";
	loading_icon.iconSize = new GSize(281, 101);
	loading_icon.shadowSize = new GSize(337, 101);
	loading_icon.iconAnchor = new GPoint(140, 50);
	loading_icon.infoWindowAnchor = new GPoint(0, 0);
	
	loading_marker = new GMarker(map.getCenter(), loading_icon);
	map.addOverlay(loading_marker);
	
	for ( i=1 ; i<14 ; i++ ) {
		icon_marker[i] = new GIcon();
		icon_marker[i].image = "../images/marker_"+i+".png";
		icon_marker[i].shadow = "../images/marker_s.png";
		icon_marker[i].iconSize = new GSize(18, 26);
		icon_marker[i].shadowSize = new GSize(29, 26);
		icon_marker[i].iconAnchor = new GPoint(9, 23);
		icon_marker[i].infoWindowAnchor = new GPoint(10, 7);
	}
	
	/*if ( pratique == 1 ) {
		for ( i=1 ; i<14 ; i++ ) {
			icon_marker[i] = new GIcon();
			icon_marker[i].image = "../images/marker_"+i+".png";
			icon_marker[i].shadow = "../images/marker_s.png";
			icon_marker[i].iconSize = new GSize(18, 26);
			icon_marker[i].shadowSize = new GSize(29, 26);
			icon_marker[i].iconAnchor = new GPoint(9, 23);
			icon_marker[i].infoWindowAnchor = new GPoint(10, 7);
		}	
	} else {
		icon_marker[pratique] = new GIcon();
		icon_marker[pratique].image = "../images/marker_"+pratique+".png";
		icon_marker[pratique].shadow = "../images/marker_s.png";
		icon_marker[pratique].iconSize = new GSize(18, 26);
		icon_marker[pratique].shadowSize = new GSize(29, 26);
		icon_marker[pratique].iconAnchor = new GPoint(9, 23);
		icon_marker[pratique].infoWindowAnchor = new GPoint(10, 7);
	}*/

	icon_dot[1] = new GIcon();
	icon_dot[1].image = "../images/dot_1_"+pratique+".png";
	icon_dot[1].shadow = "../images/dot_1_s.png";
	icon_dot[1].iconSize = new GSize(20, 20);
	icon_dot[1].shadowSize = new GSize(28, 20);
	icon_dot[1].iconAnchor = new GPoint(10, 10);
	icon_dot[1].infoWindowAnchor = new GPoint(12, 4);
	
	icon_dot[2] = new GIcon();
	icon_dot[2].image = "../images/dot_2_"+pratique+".png";
	icon_dot[2].shadow = "../images/dot_2_s.png";
	icon_dot[2].iconSize = new GSize(24, 24);
	icon_dot[2].shadowSize = new GSize(34, 24);
	icon_dot[2].iconAnchor = new GPoint(12, 12);
	icon_dot[2].infoWindowAnchor = new GPoint(16, 5);
	
	icon_dot[3] = new GIcon();
	icon_dot[3].image = "../images/dot_3_"+pratique+".png";
	icon_dot[3].shadow = "../images/dot_3_s.png";
	icon_dot[3].iconSize = new GSize(30, 30);
	icon_dot[3].shadowSize = new GSize(44, 30);
	icon_dot[3].iconAnchor = new GPoint(15, 15);
	icon_dot[3].infoWindowAnchor = new GPoint(20, 7);

	PutDot('../map/map_dot_'+pratique+'.xml', default_dep, default_spot);
  }
}

function PutDot(file, default_dep, default_spot) {
	if ( map != null )
	{	
		GDownloadUrl(file, function(data, responseCode) {
			var xml = GXml.parse(data);
			if ( xml.documentElement != null ) {
			  var markers = xml.documentElement.getElementsByTagName("m");
				/*var maxs    = xml.documentElement.getElementsByTagName("b");
				bound_spot1 = maxs[0].getAttribute("s1");
				bound_spot2 = maxs[0].getAttribute("s2");*/
				
				for (var i = 0; i < markers.length; i++) {
					id_dep = parseInt(markers[i].getAttribute("id"));
					if ( default_dep != id_dep )
						map.addOverlay(createDot(parseFloat(markers[i].getAttribute("la")), parseFloat(markers[i].getAttribute("ln")), id_dep, parseInt(markers[i].getAttribute("s")),markers[i].getAttribute("n")));
					else
					{
						clearTimeout(timer_dot);
						PutMarker('../map/map_data_'+pratique_global+'_'+id_dep+'.xml', default_spot);
						listerDepartement(id_dep, pratique_global);
					}	
				}
			}
			map.removeOverlay(loading_marker);
		});
	}
}

function PutMarker(file, default_spot) {
	if ( map != null )
	{	
		GDownloadUrl(file, function(data, responseCode) {
			var xml = GXml.parse(data);
		  	var markers = xml.documentElement.getElementsByTagName("m");
			var batch = new Array(15);
			for ( var j=0 ; j<15 ; ++j )
				batch[j] = [];
		  	for (var i = 0; i < markers.length; i++) {
		  		id_spot = parseInt(markers[i].getAttribute("id"));
		  		if ( id_spot != default_spot )
			    	batch[parseInt(markers[i].getAttribute("l"))].push(createMarker(parseFloat(markers[i].getAttribute("la")), parseFloat(markers[i].getAttribute("ln")), id_spot, markers[i].getAttribute("i"), parseInt(markers[i].getAttribute("p"))));
				else {
					var defautl_marker = createMarker(parseFloat(markers[i].getAttribute("la")), parseFloat(markers[i].getAttribute("ln")), id_spot, markers[i].getAttribute("i"), parseInt(markers[i].getAttribute("p")));
					map.addOverlay(defautl_marker);
					GEvent.trigger(defautl_marker,"click");
				}
		  	}
			marker_mgr = new GMarkerManager(map);
			for ( var k=0 ; k<15 ; ++k ) {
				if ( batch[k].length > 0 )
					marker_mgr.addMarkers(batch[k], k);
			}
			marker_mgr.refresh();
			map.removeOverlay(loading_marker);
		});
	}
}

function OpenMarkerInfo(id) {
	alert(location.hash);
	GEvent.trigger(all_marker[id],"click");
}
