// JavaScript Document

var map;
var centerLatitude = 47.389285;
var centerLongitude = 0.689049; 
var startZoom = 9;
var xmlhttp;
var markers;

var iconeVert = new GIcon();
iconeVert.image = "vert.png";
iconeVert.shadow = "ombre.png";
iconeVert.iconSize = new GSize(12, 20);
iconeVert.shadowSize = new GSize(22, 20);
iconeVert.iconAnchor = new GPoint(6, 20);
iconeVert.infoWindowAnchor = new GPoint(5, 1);

var deselectCurrent = function() {};

function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var marker = new GMarker(point,{icon: iconeVert, title: pointData.nom});
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.onmouseover = function() { marker.setImage("rouge.png");}
	listItemLink.onmouseout = function() { marker.setImage("vert.png");}
	listItemLink.innerHTML = '<font face="Arial, Helvetica, sans-serif"><strong>' + pointData.nom + ' </strong></font><span>' + pointData.codepostal + ', ' + pointData.ville + ' (' + pointData.count + ' CV)</span>';
	listItemLink.bubule = '<font face="Arial, Helvetica, sans-serif"><strong>' + pointData.nom + ' </strong></font><br /><font size="-2" face="Arial, Helvetica, sans-serif">Type : <em>' + pointData.type + '</em></font><hr /><center><table><tr><td align="left"><a style:"border-color:#FF0000" href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '#cv" title="' + pointData.nom + ' : ' + pointData.count + ' CV" target="_blank"><img src="http://map.esc-cv.com/img/cv_diplome.png" alt="' + pointData.nom + ' : ' + pointData.count + ' CV" height="15"  width="15" /></a> : <a href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '#cv" title="' + pointData.nom + ' : ' + pointData.count + ' CV" target="_blank"><font size="-2" face="Arial, Helvetica, sans-serif">' + pointData.count + ' CV</font></a><br /> <a style:"border-color:#FF0000" href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '#plus" title="' + pointData.nom + ' : En savoir +" target="_blank"><img src="http://map.esc-cv.com/img/add.png" alt="' + pointData.nom + ' : En savoir +" height="15"  width="15" /></a> : <a href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '" title="' + pointData.nom + ' : En savoir +" target="_blank"><font size="-2" face="Arial, Helvetica, sans-serif">' + pointData.nom + '</font></a><br />  <a style:"border-color:#FF0000" href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '#salaires" title="' + pointData.nom + ' : Les Salaires" target="_blank"><img src="http://map.esc-cv.com/img/moneybag.png" alt="' + pointData.nom + ' : Les Salaires" height="15"  width="15" /></a> : <a href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '#salaires" title="' + pointData.nom + ' : Les Salaires" target="_blank"><font size="-2" face="Arial, Helvetica, sans-serif">Les Salaires</font></a><center></td></tr></table><hr /></center><center><table><tr><td><a style:"border-color:#FF0000" href="http://cv.esc-cv.com/esc-details.php?ecole=' + pointData.rec_id + '" title="' + pointData.nom + ' " target="_blank"><img src="http://open.thumbshots.org/image.pxf?url=http://' + pointData.lien + '" alt="' + pointData.nom + '" height="90"  width="120" /></a></td><td><font size="-2" face="Arial, Helvetica, sans-serif">' + pointData.adresse + '</font><br /><font size="-2" face="Arial, Helvetica, sans-serif">' + pointData.codepostal + ', ' + pointData.ville + '</font></td></tr></table></center><center><font size="-2" face="Arial, Helvetica, sans-serif">&copy;</font> <a href="http://www.esc-cv.com" title="ESC-CV" target="_blank"><font size="-2" face="Arial, Helvetica, sans-serif">www.esc-cv.com</font></a></center>';
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		marker.setImage("rouge.png");
		deselectCurrent = function() { listItem.className = ''; marker.setImage("vert.png");}
		marker.openInfoWindowHtml(listItemLink.bubule);
		map.panTo(point);
		return false;
	}
	listItemLink.onclick = focusPoint;
	GEvent.addListener(marker, 'click', focusPoint);	
	GEvent.addListener(marker, 'mouseover', function() { marker.setImage("rouge.png"); });
	GEvent.addListener(marker, 'mouseout', function() { marker.setImage("vert.png"); });
	GEvent.addListener(marker, 'infowindowclose', function(){marker.setImage("vert.png");});	
	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_PHYSICAL_MAP,G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP]});
	map.addControl(new GMapTypeControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GOverviewMapControl(new GSize(200,180)));
	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(), map.getBoundsZoomLevel(bounds));
	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 = "";
	var linkt = divt.appendChild(document.createElement('a'));
	linkt.href = "http://www.esc-cv.com";
	linkt.innerHTML = "CV des Etudiants / Diplom&eacute;s d'Ecoles Sup&eacute;rieures de Commerce";
	linkt.title = "ESC-CV.com";
	linkt.style.textDecoration = "none";
	linkt.style.font = "0.8em Helvetica, sans-serif";
	linkt.style.color = "black";
	document.getElementById('sidebar').appendChild(divt);
}

function init() {
	handleResize();
	xmlhttp = GXmlHttp.create();
	xmlhttp.open('GET', 'json.php', 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.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;