﻿// JavaScript Document

var map;
var centerLatitude = 47.28588888205659;
var centerLongitude = -70.59602339522218; 
var startZoom = 6;
var xmlhttp;
var markers;
var ski2;
var ski1;
var film;

var iconeVert = new GIcon();
iconeVert.image = "ski.png";
iconeVert.shadow = "ombre.png";
iconeVert.iconSize = new GSize(25, 25);
iconeVert.shadowSize = new GSize(25, 25);
iconeVert.iconAnchor = new GPoint(6, 20);
iconeVert.infoWindowAnchor = new GPoint(5, 1);
ski2 = "ski2.png";
ski1 = "ski.png";
film = "<img src='puce2.gif' width='18' height='17' border='0'>";

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'));
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	listItemLink.href = "#";
	listItemLink.onmouseover = function() { 
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	marker.setImage(ski2);}
	listItemLink.onmouseout = function() { 
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	marker.setImage(iconeVert.image);}
	listItemLink.innerHTML = '<strong>' + pointData.nom + film + ' </strong><span>' + pointData.codepostal + '</span>';
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		marker.setImage(ski2);
		deselectCurrent = function() { 
		if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
		listItem.className = ''; marker.setImage(iconeVert.image);}
		marker.openInfoWindowHtml('<font size="2" face="Arial, Helvetica, sans-serif">' + pointData.adresse + '<br/>' + pointData.ville + ' </font>');
		map.panTo(point);
		return false;
	}
	listItemLink.onclick = focusPoint;
	GEvent.addListener(marker, 'click', focusPoint);	
	GEvent.addListener(marker, 'mouseover', function() { 
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	marker.setImage(ski2); });
	GEvent.addListener(marker, 'mouseout', function() { 
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	marker.setImage(iconeVert.image); });
	GEvent.addListener(marker, 'infowindowclose', function(){
	if (pointData.type == "skiv")
	{
	ski2 = "skiv2.png";
	ski1 = "skiv.png";
	iconeVert.image = "skiv.png";
	film = "<img src='puce2.gif' width='18' height='17' border='0'>";
	}
		if (pointData.type == "ski")
	{
	ski2 = "ski2.png";
	ski1 = "ski.png";
	iconeVert.image = "ski.png";
	film = "";
	}
	marker.setImage(iconeVert.image);});	
	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() {
	// Remplacement de GMap pour GMap2 -- Dr Frankenstein 2009-08-16
	map = new GMap2(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 = "";
	linkt.innerHTML = "";
	linkt.title = "";
	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;