var map = null;
var mgr = null;
var markers = [];
var xhr = null;

/*************************************** MAP ************************************************/

function initialize(latitude, longitude, zoom) {
	map = new GMap2(document.getElementById("map_canvas"));
	map.enableScrollWheelZoom();
	map.addControl(new GLargeMapControl3D());
	map.setCenter(new GLatLng(latitude, longitude), zoom);
	mgr = new MarkerManager(map, {maxZoom: 15, trackMarkers: true });
	
	mgr.addMarkers(markers,0);
	mgr.refresh();
}

function initSizeScreen(){
	document.getElementById("td_map_canvas").style.width = Math.round(getWindowWidth()*0.75)+"px";
	document.getElementById("td_map_canvas").style.height = Math.round(getWindowHeight())+"px";
	document.getElementById("divCrecheInfo").style.width = Math.round(getWindowWidth()*0.15)+"px";
	document.getElementById("divCrecheInfo").style.height = Math.round(getWindowHeight())+"px";
}

function createMarker(mLat, mLong, title, htmlInfo, crecheID, color) {
	
	if(color == 1){ // creche Arbalange
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.arbalange.com/images/map/AVATAR-CRECHE-ARBALANGE.png";
		blueIcon.shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
		blueIcon.iconSize = new GSize(18, 28);
		blueIcon.shadowSize = new GSize(28, 28);
		markerOptions = {title: title, icon:blueIcon};
	}
	else if(color == 3){ // local Arbalange
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.arbalange.com/images/map/AVATAR-LOCAL-ARBALANGE.png";
		blueIcon.shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
		blueIcon.iconSize = new GSize(18, 28);
		blueIcon.shadowSize = new GSize(28, 28);
		markerOptions = {title: title, icon:blueIcon};
	}
	else if(color == 4){ // maternité Arbalange
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.arbalange.com/images/map/AVATAR-MAT-ARBALANGE.png";
		blueIcon.shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
		blueIcon.iconSize = new GSize(18, 28);
		blueIcon.shadowSize = new GSize(28, 28);
		markerOptions = {title: title, icon:blueIcon};
	}
	else if(color == 5){ // assistante maternelle Arbalange
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.arbalange.com/images/map/AVATAR-ASS-MAT-ARBALANGE.png";
		blueIcon.shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
		blueIcon.iconSize = new GSize(18, 28);
		blueIcon.shadowSize = new GSize(28, 28);
		markerOptions = {title: title, icon:blueIcon};
	}
	else{ // creche non-Arbalange
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = "http://www.arbalange.com/images/map/AVATAR-CRECHE.png";
		blueIcon.shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
		blueIcon.iconSize = new GSize(18, 28);
		blueIcon.shadowSize = new GSize(28, 28);
		markerOptions = {title: title, icon:blueIcon};
	}
	
	var marker = new GMarker(new GLatLng(mLat,mLong), markerOptions);
	GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(htmlInfo);request(readData, crecheID); } );
	markers.push(marker);
}

function refreshMarker(){
	mgr.addMarkers(markers,0);
	mgr.refresh();
}

/*************************************** AJAX ************************************************/



function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) { // IE
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else { // autres navigateurs
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

function request(callback, crecheID) {
	if (xhr && xhr.readyState != 0) { // verifie si la requete n'est pas en cours
		xhr.abort();
	}
	
	xhr = getXMLHttpRequest(); // instancie le xhr
	
	xhr.onreadystatechange = function() { // fonction pour verifier l'etat
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);
		}
		else{
			document.getElementById("divCrecheInfo").style.display = "none";			
		}
	};
	
	xhr.open("GET", "../../module/map/getInformationCreche.php?crecheID="+crecheID, true);
	xhr.send(null);
}

function readData(sData) { // traitement des donnŽes
	oDiv = document.getElementById("divCrecheInfo");
	if(sData != "FAIL"){
		oDiv.innerHTML = sData;
		oDiv.style.display = "block";
	}
	else{
		oDiv.innerHTML = "FAIL";		
	}
}

function getWindowHeight() {
    var h = 0;
    if (typeof(window.innerHeight) == 'number') { // Netscape
        h = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        h = document.documentElement.clientHeight;
    } else if (document.body && document.body.offsetHeight) { //client
        h = document.body.offsetHeight;
    }
    return h;
}
function getWindowWidth() {
    var w = 0;
    if (typeof(window.innerWidth) == 'number') { // Netscape
        w = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        w = document.documentElement.clientWidth;
    } else if (document.body && document.body.offsetWidth) { //client
        w = document.body.offsetWidth;
    }
    return w;
}
//document.getElementById("td_map_canvas").style.height = Math.round(getWindowHeight())+"px";
