﻿// JavaScript Document

var map;
var centerLatitude = 50.41667;
var centerLongitude = 2.93333; 
var startZoom = 9;
var xmlhttp;
var markers;

// Icônes //
var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(12,20);
baseIcon.shadowSize=new GSize(22,20);
baseIcon.iconAnchor=new GPoint(6,20);
baseIcon.infoWindowAnchor=new GPoint(5,1);
var veto = new GIcon(baseIcon, 'http://www.nord-dressage.fr/autres/fichiers/images/veto.png', null, 'http://www.nord-dressage.fr/autres/fichiers/images/ombre.png');
var refuge = new GIcon(baseIcon, 'http://www.nord-dressage.fr/autres/fichiers/images/refuge.png', null, 'http://www.nord-dressage.fr/autres/fichiers/images/ombre.png');
var chambre = new GIcon(baseIcon, 'http://www.nord-dressage.fr/autres/fichiers/images/chambre.png', null, 'http://www.nord-dressage.fr/autres/fichiers/images/ombre.png');

// Boutons DragZoom
var OptionsStyleRectangle = {
							  opacity: .3, border: "1px solid #ff7b00"
							};
var OptionsBouton 		  = {
							  buttonHTML: "<img src='http://www.nord-dressage.fr/autres/fichiers/images/zoom-button.png' title='Sélectionner une partie de la carte' />",
							  buttonZoomingHTML: "<img src='http://www.nord-dressage.fr/autres/fichiers/images/zoom-button-activated.gif' title='Annuler' />",
							  buttonStartingStyle: {width: '24px', height: '24px'},
							  backButtonEnabled: true,
							  backButtonHTML: "<img src='http://www.nord-dressage.fr/autres/fichiers/images/zoom-back.png' title='Revenir au zoom précedent' />",
							  backButtonStyle: {display: 'none', marginTop: '5px', width: '25px', height: '25px'}
							};

var deselectCurrent = function() {};

function initializePoint(pointData) {

	var type = pointData.type;
	if(type == 'veto'){var typeIcone = veto;} 
	if(type == 'refuge'){var typeIcone = refuge;} 
	if(type == 'chambre'){var typeIcone = chambre;}

	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var marker = new GMarker(point,{icon: typeIcone, title: pointData.nom});
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.onmouseover = function() { marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/rouge.png");}
	listItemLink.onmouseout = function() { marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/" + type + ".png");}
	listItemLink.innerHTML = '<strong>' + pointData.nom + ' </strong><span>' + pointData.codepostal + ', ' + pointData.ville + '</span>';
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/rouge.png");
		deselectCurrent = function() { listItem.className = ''; marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/" + type + ".png");}
		
		if(type == 'chambre')
		{
		  marker.openInfoWindowHtml('<div style="text-align: center; font-family: arial"><strong>' + pointData.nom + ' </strong><br /><span>' + pointData.adresse + '</span><br /><span>' + pointData.codepostal + ' ' + pointData.ville + '</span><br /><span>Téléphone : ' + pointData.tel + '</span><br /><span>Site Internet : <a href="' + pointData.site + '" target="blanck">' + pointData.site + '</a></span><br /><br /><span><i>Découvrez notre offre <a href="http://www.nord-dressage.fr/autres/index.php?page=chambres-mont-blanc">Week-end <b>Détente &amp; Dressage</b></a></i></span></div>');
		}
		else  marker.openInfoWindowHtml('<div style="text-align: center; font-family: arial"><strong>' + pointData.nom + ' </strong><br /><span>' + pointData.adresse + '</span><br /><span>' + pointData.codepostal + ' ' + pointData.ville + '</span><br /><span>Téléphone : ' + pointData.tel + '</span><br /><span>Site Internet : <a href="' + pointData.site + '" target="blanck">' + pointData.site + '</a></span></div>');
		map.panTo(point);
		return false;
	}
	listItemLink.onclick = focusPoint;
	GEvent.addListener(marker, 'click', focusPoint);	
	GEvent.addListener(marker, 'mouseover', function() { marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/rouge.png"); });
	GEvent.addListener(marker, 'mouseout', function() { marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/" + type + ".png"); });
	GEvent.addListener(marker, 'infowindowclose', function(){marker.setImage("http://www.nord-dressage.fr/autres/fichiers/images/" + type + ".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 GMap2(document.getElementById("map"),{mapTypes:[G_PHYSICAL_MAP,G_NORMAL_MAP]});
	map.addControl(new ExtMapTypeControl());
	map.addControl(new DragZoomControl(OptionsStyleRectangle, OptionsBouton),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(10,10)));
	map.addControl(new CustomZoomControl());
	map.enableScrollWheelZoom();
	map.enableContinuousZoom();
	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.style.borderBottom = "1px solid #979797";
	divt.style.borderLeft = "1px solid #979797";
	divt.style.borderRight = "1px solid #979797";
	divt.innerHTML = "";
	var linkt = divt.appendChild(document.createElement('a'));
	linkt.href = "http://www.touraineverte.com";
	linkt.innerHTML = "Aide documentation exemples tutoriels en Français sur l'API Google Maps : http://www.touraineverte.com";
	linkt.title = "Exemples et Tutoriels sur l'API Google Maps";
	linkt.target = "blanck";
	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', 'http://www.nord-dressage.fr/autres/fichiers/inc/json.php', true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
       		if (xmlhttp.status != 200) 
       			setAlertText('Impossible d\'accé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;
