
// COMMON: Load page in a new window; use class="new_window" on <a> tags
$(document).ready(function(){
	$("a.new_window").click(function(){
		w = window.open(this.href, 'new_window');
		return false;
	});
});


// LINKS: Toggle the menu on click
$(document).ready(function(){
	// Click menu items
	$("#links .toggle").click(function(){
		$("#links ul ul").hide();
		$(this).next().fadeIn(400);
	});
	// Click outside the menus
	$(document).click(function(e){
		if ($(e.target).closest('#links').get(0) == null){
			$("#links ul ul").hide();
		}
	});
});


// NAV: Toggle the menu on click
$(document).ready(function(){
	// Open section of current page on page load
	var current = $("#nav .current_page").parents('ul');
	current.show();
	// Click menu items
	$("#nav .toggle").click(function(){
		$("#nav ul ul").not(current).slideUp(400);
		$(this).next().slideDown(400);
	});
	// Click outside the menus
	$(document).click(function(e){
		if ($(e.target).closest('#nav').get(0) == null){
			$("#nav ul ul").not(current).slideUp(400);
		}
	});
});


// PARTAGER: "AddThis" popup window offset
var v = parseInt(jQuery.browser.version);
if (jQuery.browser.webkit) {
	var addthis_offset_top = 1;
}
else {
	var addthis_offset_top = -15;
}


// RÉPERTOIRE VIRTUEL: Place markers on the map
var map;
var infoWindow;

$(document).ready(function(){
	/* Check if map element exists */
	if ($('#map').length > 0){
		init_map()
	};
});

function init_map(){
	/* Load XML */
	$.get(url, function(xml){
	
		/* Get center latitude/longitude; initialize map */
		$(xml).find('center').each(function(){
			var lat = $(this).find('latitude').text();
			var lng = $(this).find('longitude').text();
			map = new google.maps.Map(document.getElementById("map"), {
				center: new google.maps.LatLng(lat, lng),
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				zoom: 11,
				zoomControl: true,
				zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL }
			});
		});
		
		/* Initialize info window */
		infoWindow = new google.maps.InfoWindow(); 
		
		/* Display markers */
		$(xml).find('marker').each(function(){
			var title = $(this).find('title').text();
			var description = $(this).find('description').text();
			var url = $(this).find('url').text();
			var latitude = $(this).find('latitude').text();
			var longitude = $(this).find('longitude').text();
			create_marker(title, description, url, latitude, longitude);
		});

	});
}

function create_marker(title, description, url, latitude, longitude){
	var latlng = new google.maps.LatLng(latitude, longitude);
	var marker = new google.maps.Marker({
		position: latlng,
		map: map,
		title: title
	});
	var html = '<div style="width:300px; height:150px;">';
	html += '<h1><a href="' + url + '">' + title + '</a></h1>';
	html += '<p>' + description + '</p>';
	html += '</div>';
	google.maps.event.addListener(marker, 'click', function(){
		infoWindow.setContent(html);
		infoWindow.open(map, marker);  
	});
}

