var map;
var deselectCurrent = function() {};

	baseIcon = new GIcon();
	baseIcon.iconSize=new GSize(19,32);
	baseIcon.shadowSize=new GSize(37,34);
	baseIcon.iconAnchor=new GPoint(12,32);
	baseIcon.infoWindowAnchor=new GPoint(5,1);

	miniIcon = new GIcon();
	miniIcon.iconSize=new GSize(12,20);
	miniIcon.shadowSize=new GSize(20,22);
	miniIcon.iconAnchor=new GPoint(12,20);
	miniIcon.infoWindowAnchor=new GPoint(5,1);

	var chateau = new GIcon(baseIcon, '../images/logoChatoBleu.png', null, '../images/logoChatoBleushadow.png');
	var minichateau = new GIcon(miniIcon, '../images/minilogoChatoBleu.png', null, '../images/minilogoChatoBleushadow.png');
	var pole = new GIcon(baseIcon, 'http://labs.google.com/ridefinder/images/mm_20_green.png', null, 'http://labs.google.com/ridefinder/images/mm_20_shadow.png');
	var tower = new GIcon(baseIcon, 'http://labs.google.com/ridefinder/images/mm_20_blue.png', null, 'http://labs.google.com/ridefinder/images/mm_20_shadow.png');


/* [listing 6-20] */
function initializePoint(pointData,compteur) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	
	if (compteur <= 40) var icone = chateau;
	else var icone = minichateau;
	
	var marker = new GMarker(point,icone);
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;

	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.propriete + '</strong>';
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		marker.openInfoWindowHtml('<a href="' + pointData.url + '"> ' + pointData.propriete + '</a> <br />' + pointData.titre + ' ' + pointData.prenom + ' ' + pointData.nom + '<br />' + pointData.telfixe + ' <br /><a href="' + pointData.url + '">' + pointData.suite + '...</a>' );
		map.panTo(point);
		return false;
	}

	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;

	pointData.show = function() {
		if (!visible) {
//			document.getElementById('sidebar-list').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hide = function() {
		if (visible) {
//			document.getElementById('sidebar-list').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}

	pointData.show();
}
/* [listing 6-20 end] */

/* [listing 6-22] */
function initializeSortTab(type) {
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));

	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.onclick = function() {
		changeBodyClass('standby', 'loading');

		for(id in markers) {
			if (markers[id].type == type || 'All' == type)
				markers[id].show();
			else
				markers[id].hide();	
		}

		changeBodyClass('loading', 'standby');

		return false;
	}

}
/* [listing 6-22 end] */

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() - document.getElementById('toolbar').offsetHeight - 30;
//	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 init() {
	var type;
	var allTypes = { 'All':[] };
	
	map = new GMap(document.getElementById("map"));
	map.setUIToDefault();
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
	/* [listing 6-21] */
	for(id in markers) {
		initializePoint(markers[id],compteur);
		allTypes[markers[id].type] = true;
	}

	for(type in allTypes) {
		initializeSortTab(type);
	}
	/* [listing 6-21 end] */

	changeBodyClass('loading', 'standby');
}

// window.onresize = handleResize;
window.onload = init;
