﻿ // JavaScript Document
var map;

var startZoom = 9;
var xmlhttp;
var markers;
var highlightCircle;
var currentMarker;
var iconeVert = new GIcon();



var iconeVert = new GIcon();
iconeVert.image = "http://www.zones-activites.net/img/rectbrd.gif";
iconeVert.shadow = "http://www.zones-activites.net/img/rectbrd.gif";
iconeVert.iconSize = new GSize(10, 10);
iconeVert.shadowSize = new GSize(10, 10);
iconeVert.iconAnchor = new GPoint(10, 10);
iconeVert.infoWindowAnchor = new GPoint(5, 1);

var deselectCurrent = function() {};

function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var lien = pointData.nom + ' - ' + pointData.ville;
	var marker = new GMarker(point,{icon: iconeVert, title: lien });
	var listItem = document.createElement("li");
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.onmouseover = function() { marker.setImage("http://www.zones-activites.net/img/rectbrdover.gif");}
	listItemLink.onmouseout = function() { marker.setImage("http://www.zones-activites.net/img/rectbrd.gif");}
	listItemLink.innerHTML = '<span>' + pointData.ville + '<strong>' + pointData.nom + '</strong></span><br>';
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		marker.setImage("http://www.zones-activites.net/img/rectbrdover.gif");
		deselectCurrent = function() { listItem.className = ''; marker.setImage("http://www.zones-activites.net/img/rectbrd.gif");}
		marker.openInfoWindowHtml('<strong>' + pointData.nom + ' </strong><br><span> ' + pointData.ville + '</span><br><a href=plan-' +pointData.num+ '-plan.html>plan</a> - <a href=photo-aerienne-' +pointData.num+ '-photo.html>photo aérienne</a>');
		map.panTo(point);
		return false;
	}
	listItemLink.onclick = focusPoint;
	GEvent.addListener(marker, 'click', focusPoint);	
	GEvent.addListener(marker, 'mouseover', function() { marker.setImage("http://www.zones-activites.net/img/rectbrdover.gif"); });
	GEvent.addListener(marker, 'mouseout', function() { marker.setImage("http://www.zones-activites.net/img/rectbrd.gif"); });
	GEvent.addListener(marker, 'infowindowclose', function(){marker.setImage("http://www.zones-activites.net/img/rectbrd.gif");});	
	document.getElementById('sidebar-list').appendChild(listItem);
	map.addOverlay(marker);
}

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case.
	return 0;
}

function handleResize() {
	var height = windowHeight()-20;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
}

function changeBodyClass(from, to) {
	document.body.className = document.body.className.replace(from, to);
	return false;
}

function setAlertText(str) {
	document.getElementById('alert').innerHTML = '<p>' + str + '</p>';
}

function initData() {
	map = new GMap(document.getElementById("map"),{mapTypes:[G_NORMAL_MAP]});
	map.addControl(new GMapTypeControl());
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	var bounds = new GLatLngBounds();
	for(id in markers) {
		initializePoint(markers[id]);
		bounds.extend(new GLatLng(markers[id].latitude, markers[id].longitude));
	}
	map.setCenter(bounds.getCenter(), 13);
	changeBodyClass('loading', 'standby');
	var divt = document.createElement('div');
	divt.style.margin = "0";
	divt.style.padding = "0";
	divt.style.textAlign = "center";
	divt.style.backgroundColor = "#FFF9DD";
	divt.style.borderTop = "1px solid #dddddd";
	divt.innerHTML = "";
	
}

function init() {
	
	xmlhttp = GXmlHttp.create();
	xmlhttp.open('GET', 'bor.php?id='+pla, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
       		if (xmlhttp.status != 200) 
       			setAlertText('Impossible d\'acéder aux données de la carte.');
       		else
       		{
				var responseText = xmlhttp.responseText;
				markers = eval(responseText);
				if (!markers)
					setAlertText('Erreur de données.');
				else
					initData();
			}
       }
    }
    xmlhttp.send(null);
}


window.onload = init;
window.onunload = GUnload;
