/**
 * @author Chris
 */

var VOYAGE_MARGE_CARTE = 32; // Marge entre la carte et le bord du masque
var VOYAGE_KML_URL = "http://voyages.pixinn.net/transsiberien/ressources/javascript/voyage/carte_GoogleMap.kml"

var g_voyage_GCarte;
var g_voyage_geoXml;
var g_voyage_listeCoordsCarte = { 	// Coordonnées et zoom du centre de la carte en fct du chapitre ; rayon = rayon du cercle rouge sur la carte générale (km)
									// nom : pour google analytics
									0:{ lat:43.325178,lng:78.925781,zoom:3,type:G_PHYSICAL_MAP , rayon:0.01 , nom:"Overview"},	//Overview -> Rappelée à chaque fois
									1:{ lat:55.7525,lng:37.623056,zoom:13,type:G_SATELLITE_MAP , rayon:350  , nom:"Moscou"},
									2:{ lat:43.325178,lng:78.925781,zoom:3,type:G_PHYSICAL_MAP , rayon:0.01 , nom:"Transsiberien"},
									3:{ lat:51.823896,lng:104.93042,zoom:8,type:G_HYBRID_MAP   , rayon:500  , nom:"Baikal"},
									4:{ lat:50.141706,lng:105.622559,zoom:7,type:G_NORMAL_MAP  , rayon:500  , nom:"Frontiere_Mongolie"},
									5:{ lat:47.309034,lng:107.424316,zoom:6,type:G_HYBRID_MAP  , rayon:750  , nom:"Steppe_Mongole"},
									6:{ lat:44.504341,lng:103.71093,zoom:8,type:G_HYBRID_MAP   , rayon:350  , nom:"Désert_Gobi"},
									7:{ lat:45.897655,lng:105.000283,zoom:7,type:G_HYBRID_MAP  , rayon:550  , nom:"Retour_Steppe"},
									8:{ lat:42.875964,lng:111.181641,zoom:5,type:G_NORMAL_MAP  , rayon:750  , nom:"Chine"},
									9:{ lat:39.915658,lng:116.402206,zoom:13,type:G_NORMAL_MAP , rayon:350  , nom:"Pekin"},
									10:{ lat:32.042132,lng:118.815765,zoom:12,type:G_NORMAL_MAP, rayon:350  , nom:"Nankin"},
									11:{ lat:36.879621,lng:122.255859,zoom:5,type:G_HYBRID_MAP , rayon:1000 , nom:"Suzhou_Shanghai"}
								}; 
var g_voyage_circle;
 
 /********* BOUTON **********/
 function onToggleMap( )
 {
 	//Cacher carte
 	if( $(VOYAGE_MASQUECARTE).is(":visible") ){
 			//Enabler les boutons
			g_displayManager.enableControls( );
			$(VOYAGE_BUTTON_VIGNETTE).removeAttr("disabled");
			$(VOYAGE_BUTTON_CARTE+" span").empty( )
								  		  .append("Afficher Carte");
			$(VOYAGE_BUTTON_CARTE+ " img").attr("src",VOYAGE_BUTTON_CARTE_ICONOPEN);	
			$(VOYAGE_BUTTON_CARTE).css("background-color","#FFF1C8"); // <- Doit correspondre au CSS
			$(VOYAGE_WRAPPERCARTE).hide("normal", function(){
			$(VOYAGE_MASQUECARTE).fadeOut("normal", function(){
				//Remettre la carte sur la vue par défaut
				gCarte_resetMap( );
			});
		} );	
	}
	//Afficher carte
	else{
		//Taille du masque
		$(VOYAGE_MASQUECARTE).css( "top", $(VOYAGE_TOOLBAR).outerHeight( ) )
							 .css( "width", $(window).width( ) - $(VOYAGE_ZONE_RECIT).outerWidth( ) );
		//Désactivation des boutons
		$(VOYAGE_BUTTON_CARTE).attr("disabled","disabled");	
		$(VOYAGE_NEXTCHAPTER).unbind( "click", voyage_onNextChapter );
		$(VOYAGE_PREVIOUSCHAPTER).unbind( "click", voyage_onPreviousChapter );	
		//Affichage du masque
		$(VOYAGE_MASQUECARTE).fadeIn("normal", function()
		{	
			var numeroChapitre = $(VOYAGE_SELECTION_ETAPE).attr("value");
			//Taille puis affichage de la carte
			var posTop = $(VOYAGE_MASQUECARTE).css("top");
			var posLeft =  $(VOYAGE_MASQUECARTE).css("left");
			$(VOYAGE_WRAPPERCARTE)	.width($(VOYAGE_MASQUECARTE).width( ) - 2*VOYAGE_MARGE_CARTE)
							  		.height( $(window).height() - parseInt(posTop.substr(0,posTop.indexOf("px"))) - 2*VOYAGE_MARGE_CARTE)
							  		.css( "top",  parseInt(posTop.substr(0,posTop.indexOf("px"))) + VOYAGE_MARGE_CARTE + "px"  )
  							  		.css( "left", parseInt(posLeft.substr(0,posTop.indexOf("px"))) + VOYAGE_MARGE_CARTE + "px"  )
							 		.show("normal",
										  //Affichage de la carte générale
										  function(){
													g_voyage_GCarte.checkResize();
													//Affichage du cercle bleu
													g_voyage_circle = new CircleOverlay(	new GLatLng( g_voyage_listeCoordsCarte[numeroChapitre].lat, g_voyage_listeCoordsCarte[numeroChapitre].lng ),
																							g_voyage_listeCoordsCarte[numeroChapitre].rayon,
																							"#BB6633", 1, 1, '#BB6633', 0.5);
            										g_voyage_GCarte.addOverlay( g_voyage_circle );
													//Timer de 2s avant d'afficher la carte locale
													$(this).oneTime("2s", function(){
														gCarte_setMap( numeroChapitre );
													});
									});
			
		}); //FadeIn

		g_displayManager.disableControls( );
		$("#selectionEtape").attr("disabled","disabled");
		$(VOYAGE_BUTTON_VIGNETTE).attr("disabled","disabled");
			
	}
 }
 
 /*********** TAILLE CARTE ***********/
 function majTaillesCarte( )
 {
 		//Taille du masque
		$(VOYAGE_MASQUECARTE).css( "top", $(VOYAGE_TOOLBAR).outerHeight( ) )
							 .css( "width", $(window).width( ) - $(VOYAGE_ZONE_RECIT).outerWidth( ) );
		//Taille de la carte
		var posTop = $(VOYAGE_MASQUECARTE).css("top");
		var posLeft =  $(VOYAGE_MASQUECARTE).css("left");
		$(VOYAGE_WRAPPERCARTE)	.width($(VOYAGE_MASQUECARTE).width( ) - 2*VOYAGE_MARGE_CARTE)
							  	.height( $(window).height() - parseInt(posTop.substr(0,posTop.indexOf("px"))) - 2*VOYAGE_MARGE_CARTE)
							  	.css( "top",  parseInt(posTop.substr(0,posTop.indexOf("px"))) + VOYAGE_MARGE_CARTE + "px"  )
  							  	.css( "left", parseInt(posLeft.substr(0,posTop.indexOf("px"))) + VOYAGE_MARGE_CARTE + "px"  );
		g_voyage_GCarte.checkResize( );
							 				
 }	



/***************** GOOGLE API *****************/
function gCarte_init( )
{
	 if (GBrowserIsCompatible()) {
			var w = $(window).width( ) - $(VOYAGE_ZONE_RECIT).outerWidth( ) - 2*VOYAGE_MARGE_CARTE;
			var h = g_$screenIndex.height( ) - 2*VOYAGE_MARGE_CARTE;
        	g_voyage_GCarte = new GMap2( document.getElementById("wrapperCarte"), {size:new GSize( w, h )} );
		
			gCarte_resetMap( );
			
			g_voyage_geoXml = new GGeoXml( VOYAGE_KML_URL );
			g_voyage_GCarte.addOverlay(g_voyage_geoXml);
			
			//g_voyage_GCarte.setUIToDefault();
			g_voyage_GCarte.addControl( new GMapTypeControl() );
			g_voyage_GCarte.addControl( new GScaleControl () );
			g_voyage_GCarte.addControl( new GLargeMapControl3D() );
      }
}


function gCarte_setMap( numChapitre )
{
		//Réactivation des boutons
		$(VOYAGE_NEXTCHAPTER).click( voyage_onNextChapter );
		$(VOYAGE_PREVIOUSCHAPTER).click( voyage_onPreviousChapter );
		$(VOYAGE_SELECTION_ETAPE).removeAttr("disabled");
		$(VOYAGE_BUTTON_CARTE).removeAttr("disabled");
		$(VOYAGE_BUTTON_CARTE+" span").empty( )
									  .append("Fermer Carte");
		$(VOYAGE_BUTTON_CARTE).css("background-color","#E37A08");
		$(VOYAGE_BUTTON_CARTE+ " img").attr("src",VOYAGE_BUTTON_CARTE_ICONCLOSE);
	
		g_voyage_GCarte.setMapType(g_voyage_listeCoordsCarte[ numChapitre ].type );
		g_voyage_GCarte.setCenter( 	new GLatLng( g_voyage_listeCoordsCarte[numChapitre].lat, g_voyage_listeCoordsCarte[numChapitre].lng ),
									g_voyage_listeCoordsCarte[numChapitre].zoom );      
		g_voyage_GCarte.removeOverlay( g_voyage_circle );	
		
		//Google Analytics
		pageTracker._trackPageview("/cartes/carte"+numChapitre+"_"+g_voyage_listeCoordsCarte[numChapitre].nom); 											
}

function gCarte_resetMap( )
{
		g_voyage_GCarte.setMapType(g_voyage_listeCoordsCarte[ 0 ].type );
		g_voyage_GCarte.setCenter( 	new GLatLng( g_voyage_listeCoordsCarte[0].lat, g_voyage_listeCoordsCarte[0].lng ),
										g_voyage_listeCoordsCarte[0].zoom );										      	
}

//voir : http://dawsdesign.com/drupal/google_maps_circle_overlay

// This file adds a new circle overlay to GMaps2
// it is really a many-pointed polygon, but look smooth enough to be a circle.
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
	this.latLng = latLng;
	this.radius = radius;
	this.strokeColor = strokeColor;
	this.strokeWidth = strokeWidth;
	this.strokeOpacity = strokeOpacity;
	this.fillColor = fillColor;
	this.fillOpacity = fillOpacity;
}

// Implements GOverlay interface
CircleOverlay.prototype = GOverlay;

CircleOverlay.prototype.initialize = function(map) {
	this.map = map;
}

CircleOverlay.prototype.clear = function() {
	if(this.polygon != null && this.map != null) {
		this.map.removeOverlay(this.polygon);
	}
}

// Calculate all the points and draw them
CircleOverlay.prototype.redraw = function(force) {
	var d2r = Math.PI / 180;
	circleLatLngs = new Array();
	var circleLat = this.radius * /*0.014483*/ 0.008999318973736;  // Convert statute km into degrees latitude
	var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
	var numPoints = 40;
	
	// 2PI = 360 degrees, +1 so that the end points meet
	for (var i = 0; i < numPoints + 1; i++) { 
		var theta = Math.PI * (i / (numPoints / 2)); 
		var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta)); 
		var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
		var vertextLatLng = new GLatLng(vertexLat, vertexLng);
		circleLatLngs.push(vertextLatLng); 
	}
	
	this.clear();
	this.polygon = new GPolygon(circleLatLngs, this.strokeColor, this.strokeWidth, this.strokeOpacity, this.fillColor, this.fillOpacity);
	this.map.addOverlay(this.polygon);
}

CircleOverlay.prototype.remove = function() {
	this.clear();
}

CircleOverlay.prototype.containsLatLng = function(latLng) {
	// Polygon Point in poly 
	if(this.polygon.containsLatLng) {
		return this.polygon.containsLatLng(latLng);
	}
}

CircleOverlay.prototype.setRadius = function(radius) {
	this.radius = radius;
}

CircleOverlay.prototype.setLatLng = function(latLng) {
	this.latLng = latLng;
}


