// JavaScript Document
var geocoder;
var map;
var markers_ville;
var markers_pv;
var icon_ville;
var icon_pv;

var zoom_ville = 13;
var niveau_actuel;

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();
var http2 = createRequestObject();

function initialize(num_zoom)
{
	var selectpays = document.getElementById('select_pays').options[document.getElementById('select_pays').selectedIndex].value;
	if(selectpays != 'FRA')
	{
		jQuery('.choix_dpt').hide();
	}
	else
	{
		jQuery('.choix_dpt').show();
	}	
	
	// Initialisation
	geocoder = new google.maps.Geocoder();
		
	//var latlng = new google.maps.LatLng(46.75,2.35);
	var latlng = new google.maps.LatLng(47.00,2.35);

	var myOptions = {
	  zoom: num_zoom,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	map = new google.maps.Map(document.getElementById("points_vente"), myOptions);
	
	// Evenement sur dezoom
    google.maps.event.addListener(map, 'zoom_changed', function()
	{
    	// Si on est inférieur au zoom ville, on affiche les villes, sinon on affiche les points de vente
		if (map.zoom < zoom_ville)
		{
			display_villes();
		} else {
			display_pv_ville();
		}
    });
	
	markers_ville = new Array();
	markers_pv = new Array();
	
	icon_ville = new google.maps.MarkerImage('/front/modules/marketing/points_vente/images/marker_ville.png',
		  new google.maps.Size(32, 37),
		  new google.maps.Point(0,0),
		  new google.maps.Point(16, 37));
	
	icon_pv = new google.maps.MarkerImage('/front/modules/marketing/points_vente/images/marker_pv.png',
		  new google.maps.Size(32, 37),
		  new google.maps.Point(0,0),
		  new google.maps.Point(16, 37));
	
	// On initialise les villes
	display_villes();
	//go_pays(selectpays);
}

function display_villes()
{
	if (niveau_actuel != 1)
	{
		niveau_actuel = 1;
		
		// Suppression des points de vente
		remove_pv();
		
		markers_ville = new Array();
		
		// On va chercher les villes en ajax
		http.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_get_villes");
		http.onreadystatechange = ajax_display_villes;
		http.send(null);
	}
}

function ajax_display_villes()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		
		var villes = response.split("#");
		
		for (var i=0 ; i<villes.length ; i++)
		{
			coord = villes[i].split(";");
			marker_ville(coord[1], coord[0], coord[2], coord[3]);
		}
	}
}

function display_pv_ville()
{
	if (niveau_actuel != 2)
	{
		niveau_actuel = 2;
		
		// Suppression des villes
		remove_villes();
		
		markers_pv = new Array();
		
		// On va chercher les pv en ajax
		http.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_get_pv");
		http.onreadystatechange = ajax_get_pv;
		http.send(null);
	}
}

function ajax_get_pv()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		
		var pvs = response.split("##");
		
		for (var i=0 ; i<pvs.length ; i++)
		{
			pv = pvs[i].split(";;");
			marker_pv(pv);
		}
	}
}

function remove_villes()
{
	for (var i=0 ; i<markers_ville.length ; i++)
	{
		markers_ville[i].setMap(null);
	}
}

function remove_pv()
{
	for (var i=0 ; i<markers_pv.length ; i++)
	{
		markers_pv[i].setMap(null);
	}
}

function marker_ville(nom_ville, cp, lat, lng)
{
	if (!isNaN(parseFloat(lat)) && !isNaN(parseFloat(lng)))
	{
		var location = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
		
		var marker = new google.maps.Marker({
			position: location, 
			map: map, 
			icon: icon_ville,
			title:nom_ville
		});
	
		markers_ville.push(marker);
		
		var contentString = "<p class='pv_nom_ville'>"+nom_ville+"</p>";
		
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		google.maps.event.addListener(marker, 'click', function()
	
		{
			map.setZoom(zoom_ville);
			map.setCenter(location);
			infowindow.close();
		});
		
		google.maps.event.addListener(marker, 'mouseover', function()
		{
			infowindow.open(map,marker);
		});
		
		google.maps.event.addListener(marker, 'mouseout', function()
		{
			infowindow.close();
		});
	}
}

function marker_pv(pv)
{
	if (!isNaN(parseFloat(pv[7])) && !isNaN(parseFloat(pv[8])))
	{
		var location = new google.maps.LatLng(parseFloat(pv[7]),parseFloat(pv[8]));
	
		var marker = new google.maps.Marker({
			position: location, 
			map: map, 
			icon: icon_pv,
			title:pv[5]
		});	
				
		texte = "";
		
		if (pv[3] != "")
		{
			texte += "<p class='center'><img src='/fichiers/logos_pv/"+pv[3]+"' height=\""+pv[10]+"\" width=\""+pv[9]+"\" /></p>";
		}
		
		texte += "<strong>"+pv[5]+"</strong><br/>"+pv[0]+"<br/>"+pv[1]+" "+pv[2];
	
		if (pv[6] != "")
		{
			texte += "<br/>"+pv[6];
		}
	
		if (pv[4] != "")
		{
			texte += "<br/><a href='"+pv[4]+"'>"+pv[4]+"</a>";
		}
		
		var contentString = texte;
		
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		google.maps.event.addListener(marker, 'click', function()
		{
			infowindow.open(map,marker);
		});
		
		markers_pv.push(marker);
		
		google.maps.event.addListener(marker, 'click', function() {
		  map.setCenter(location);
		});
	}
}


function go_departement(num_dep)
{		
	if (num_dep != "")
	{
		http2.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_change_dpt&num_dep="+num_dep);
		http2.send(null);
	
		var hidden = document.getElementById('location_dep_'+num_dep).value;
		var th = hidden.split(";");
		
		var location = new google.maps.LatLng(parseFloat(th[0]),parseFloat(th[1]));
	
		map.setZoom(9);
		map.setCenter(location);
		
		document.getElementById('phrase_dep').style.display = "inline";
		
		// Tableau ajax
		http.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_get_tableau&num_dep="+num_dep);
		http.onreadystatechange = ajax_get_tableau;
		http.send(null);
	
	} else {
		
		http2.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_change_dpt&num_dep="+num_dep);
		http2.send(null);
		
		//var location = new google.maps.LatLng(46.75,2.35);
		var location = new google.maps.LatLng(47.00,2.35);
		
		map.setZoom(6);
		map.setCenter(location);
		
		document.getElementById('phrase_dep').style.display = "none";
		
		document.getElementById('tableau_points').innerHTML = "";
	}
}

function go_pays(codeiso3)
{
	if (codeiso3 != "")
	{
		if(codeiso3 != 'FRA')
		{
			jQuery('#choix_dpt').hide();
		}
		else
		{
			jQuery('#choix_dpt').show();
		}
		
		http2.open('GET', "index.php?categorie=marketing&module=points_vente&page=points_vente&action=ajax_change_pays&pays="+codeiso3);
		http2.send(null);
		
		var hidden = document.getElementById('location_pays_'+codeiso3).value;
		var th = hidden.split(";");
		
		var location = new google.maps.LatLng(parseFloat(th[0]),parseFloat(th[1]));
	
		map.setZoom(6);
		map.setCenter(location);
		
		document.getElementById('phrase_dep').style.display = "inline";
	
	} else {
		var location = new google.maps.LatLng(46.75,2.35);
		
		map.setZoom(6);
		map.setCenter(location);
		
		document.getElementById('phrase_dep').style.display = "none";
		
		document.getElementById('tableau_points').innerHTML = "";
	}
	
	if(codeiso3 == 'FRA')
	{
		var selectdep = document.getElementById('select_dep').options[document.getElementById('select_dep').selectedIndex].value;
		this.setTimeout(function(){go_departement(selectdep)},500);
	}
}

function ajax_get_tableau()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		
		document.getElementById('tableau_points').innerHTML = response;
	}
}
