﻿/***************************************
 ****** GOOGLE MAP  
 ****************************************/

// Initialize Google Map : params(latitude, longitude)
function initialize_map(lat, ln)
{
	var latlng = new google.maps.LatLng(lat,ln);
	var opt = { center:latlng, zoom:10, mapTypeId: google.maps.MapTypeId.ROADMAP };
	
	var map = new google.maps.Map(document.getElementById("map_canvas"),opt);
	var marker= new google.maps.Marker({ position: latlng, title: "", map: map });
}

//Initialize Big Google Map
var infowindow = new google.maps.InfoWindow();
function initialize_bigmap(parametres, lang)
{
	// Position : au milieu, un poil à droite pour afficher la NZ
	var latlng = new google.maps.LatLng(-0.432857,17.662353);
	var opt = { center:latlng, zoom:2, mapTypeId: google.maps.MapTypeId.ROADMAP };
	var bigmap = new google.maps.Map(document.getElementById("big_map_canvas"),opt);

	// Récupération des paramètres (id ; geoloc ; titre ; thumbnail)
	var myParams = parametres.split(';');
	
	// Message à afficher dans l'infoWindow selon la langue
	var message;
	if (lang == 'fr')
		message = 'Cliquez sur l\'image...';
	else if (lang == 'en')
		message = 'Click on picture...';
	
	for(var i=0; i<=myParams.length-4; i+=4)
	{
		// Récup des coordonnées
		var loc = myParams[i+1].split(",");
		if (parseFloat(loc[0]) == 0 && parseFloat(loc[1]) == 0)
			continue; // Si on a (0,0), on n'affiche pas de marker
		
		// Affectation des coord
		latlng = new google.maps.LatLng(parseFloat(loc[0]),parseFloat(loc[1]));
		
		// Création du marker et de l'infoWindow
		var marker = createMarker(bigmap, message, latlng, myParams[i], myParams[i+2], myParams[i+3]);
	}
	
	google.maps.event.addListener(bigmap, 'click', function() { infowindow.close(); });
}

function createMarker(map, msg, latlng, id, titre, thumbnail)
{
	var contentString = '<div class="title">'+titre+'</div><div class="small_text">'+msg+'</div><a href="index.php?showimage='+id+'"><img src="'+thumbnail+'" class="thumbnails"></a>';
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: titre,
        clickable: true,
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        });
}


/***************************************
 ****** POPUP
 ****************************************/

// Popup Status (0 disabled; 1 enabled)
var popupStatus = 0;

// Enable Popup
function enablePopup()
{
	if(popupStatus == 0) // only if popup disable
	{
		$("#backgroundPopup").css({	"opacity": "0.7" });
		$("#backgroundPopup").fadeIn("slow");
		$("#popupMap").fadeIn("slow");
		popupStatus = 1;
	}
}

// Disable Popup
function disablePopup()
{
	if(popupStatus == 1) // only if popup enabled
	{
		$("#backgroundPopup").fadeOut("slow");
		$("#popupMap").fadeOut("slow");
		popupStatus = 0;
	}
}

// Center Popup in the window
function centerPopup()
{
	// Get window data
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupMap").height();
	var popupWidth = $("#popupMap").width();
	// Center the popup
	$("#popupMap").css({ "position": "absolute", "top": windowHeight/2-popupHeight/2, "left": windowWidth/2-popupWidth/2 });
	$("#backgroundPopup").css({ "height": windowHeight }); // needed force for IE6
}


// Get events from HTML window
$(document).ready(function(){
	
	// Show popup
	$("#link_geoloc").click(function(){
		centerPopup();
		enablePopup();
	});
				
	// Close popup
	// click on X
	$("#popupClose").click(function(){
		disablePopup();
	});
	// click on background
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	// escape key pressed
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
