
// FONCTIONS GOOGLEMAP //

var map;
var latit =30;
var longit=0;
var zoom = 2;
var prevpiracy=false;

google.load("maps", "2");//init load
google.setOnLoadCallback(initialize);

function mode(num){//display mode
	switch(num){
		case 0:map.setMapType(G_NORMAL_MAP);break;
		case 1:map.setMapType(G_SATELLITE_MAP);break;
		case 2:map.setMapType(G_HYBRID_MAP);break;
	}
}

function initialize(){

	map = new google.maps.Map2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(latit, longit), zoom);// latitude - longitude - zoom 
	map.addControl(new GSmallMapControl());//mini controls
	map.addControl(new GMapTypeControl());//
	map.enableScrollWheelZoom();

	iconPort = new GIcon(); 
	iconPort.image = 'gif/portpoint.png';
	iconPort.iconAnchor = new GPoint(18, 42);

	iconStart = new GIcon(); 
	iconStart.image = 'gif/startpoint.png';
	iconStart.iconAnchor = new GPoint(18, 42);

	iconEnd = new GIcon(); 
	iconEnd.image = 'gif/endpoint.png';
	iconEnd.iconAnchor = new GPoint(18, 42);

	iconmini = new GIcon(); 
	iconmini.image = 'gif/minivia.png';
	//iconmini.iconSize = new GSize(15, 18);
	iconmini.iconAnchor = new GPoint(7, 18);
	//iconmini.clickable=false;

	iconBnk = new GIcon(); 
	iconBnk.image = 'gif/bunker.gif';
	iconBnk.iconAnchor = new GPoint(10, 20);

	iconVia = new GIcon(); 
	iconVia.image = 'gif/viapoint.png';
	iconVia.iconSize = new GSize(36, 42);
	iconVia.iconAnchor = new GPoint(18, 42);
	iconVia.infoWindowAnchor = new GPoint(5, 1);
	
	prevpiracy=false;

	new GKeyboardHandler(map);
}



function cleare(){//CLEAR MAP
	msg("cleare()");
	map.clearOverlays();
	//map.addOverlay( polypirat );//show piracy zone
	
}

function clear_all(){//clr map + sequence
	
	if(!confirm("Clear all ?"))return;
	kill();

	sequence = new Array();	
	recording = false;
	
	build();
	initialize();//init map
	autoFocus();
}



// DRAWING ///////////////////////////////////////////////////////////////////////
// DRAWING ///////////////////////////////////////////////////////////////////////
// DRAWING ///////////////////////////////////////////////////////////////////////

function draw_seg( num )
{

	if(num)c = sequence[num][8];
	if(!c)return;
	if(c.length==0)
	{
		msg("draw_seg() c.length==0;");
		return;
	}

	msg("Draw segment #" + num );

	tab = new Array();	
	for(i=0;i < c.length; i++)tab[tab.length] = new GLatLng( c[i][0] , c[i][1] );
	var polyline = new GPolyline(tab, "#FF0000", 5);			
	map.addOverlay(polyline);
	var x=0;
	var y=0;
	x = c[0][0];
	y = c[0][1];
}

function draw(){drawit();}

function drawit(){//DRAW ALL
	
	//window.status="drawit()";
	if(sequence.length==0)return;
	cleare();//clear routes
	
	//START POINT
	//alert("start from "+sequence[0][1] + ", end at "+sequence[sequence.length][3]);
	//END POINT


	for(var i=0;i<sequence.length;i++){

		c = sequence[i][8];

		if(i==0){//start point
			//if(!sequence[1][8])continue;
			var point = new GLatLng(sequence[1][8][0][0], sequence[1][8][0][1]);
			var pda_id= sequence[0][0];
			var title = sequence[0][1];
			//if(sequence[0][5])title+=" - MaxDraft: "+sequence[0][5]+"m";//
			var marker = new GMarker(point,{icon:iconStart, title: title });
			marker.title=title;
			map.addOverlay(marker);			
			GEvent.addListener(marker, "click", function(){point_detail(this.title);});


		}else if(i<sequence.length-1){//med step
			
			if(!c)continue;
			
			var point = new GLatLng(c[c.length-1][0], c[c.length-1][1]);
			var pda_id= sequence[i][0];
			var title = sequence[i][1];

			//if(sequence[i][5])title+=" - MaxDraft: "+sequence[i][5]+"m";//A
			var marker = new GMarker(point,{icon:iconPort, title: title });
			marker.title=title;
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function(){point_detail(this.title);});
		}

		if(i == sequence.length-1){//end point
			
			if(!c || c.length<1)continue;
			
			var point = new GLatLng(c[c.length-1][0], c[c.length-1][1]);
			var pda_id= sequence[i][0];
			var title = sequence[i][1];
	
			//if(sequence[i][5])title+=" - MaxDraft: "+sequence[i][5]+"m";//A
			var marker = new GMarker(point,{icon:iconEnd, title: title });
			marker.title=title;
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function(){point_detail(this.title);});
		}
		

		if(sequence[i][7]){
			var viapoints = sequence[i][7];
			for(var j=0;j<viapoints.length;j++)createviapoint(viapoints[j]);
		}

		if(sequence[i][6]){
			var viapoints = sequence[i][6];
			for(var j=0;j<viapoints.length;j++)createminipoint(viapoints[j]);
		}

		draw_seg(i);//finaly
		//msg("ok");

	}

}

function point_detail(str){
	msg("Port info");
	new Ajax.Request("port-info.php?id="+str, 
	{
		onComplete: function(o){
			//alert(o.responseText);
			eval(o.responseText);
		}
	});
}




function drawline(step)
{
	
	//msg("drawline("+step+")");

	var c = sequence[step][8];

	cleare();//clear routes
	
	tab=new Array();			
	for( i=0; i < c.length; i++ )tab[tab.length] = new GLatLng(c[i][0], c[i][1]);
	
	var polyline = new GPolyline(tab, "#FF0000", 5);			
	map.addOverlay(polyline);
	
	var x = 0;
	var y = 0;
	
	x = c[0][0];
	y = c[0][1];
	
	//alert("x="+x+" - y="+y);
	map.panTo(new GLatLng(x, y));


	//START POINT
	var point = new GLatLng(c[0][0], c[0][1]);
	var pda_id= sequence[(step-1)][0];
	var title = sequence[(step-1)][1];
	if(sequence[step][10])title+=" - MaxDraft: "+sequence[step][5]+"m";//A
	var marker =new GMarker(point,{icon:iconStart, title: title });
	marker.title=sequence[step][1];
	map.addOverlay(marker);			
	
	GEvent.addListener(marker, "click", function(){point_detail(this.title);});

	//END POINT
	var point = new GLatLng(c[c.length-1][0], c[c.length-1][1]);
	var pda_id= sequence[step][0];
	var title = sequence[step][1];
	if(sequence[step][11])title+=" - MaxDraft: "+sequence[step][5]+"m";//B
	var marker =new GMarker(point,{icon:iconEnd, title: title });
	marker.title=sequence[step][3];
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function(){point_detail(this.title);});


	// SHOW (user) VIA // (on en affiche qu'un seul)
	var viacodes = sequence[step][7];
	for(var n = 0; n < viacodes.length; n++)createviapoint( viacodes[n] );

	// SHOW (server) VIAPOINTS //
	var points = sequence[step][6];
	for(var n = 0; n < points.length; n++)createminipoint( points[n] );

}


function show_startpoint( id )
{
	if(!id)return;

	msg("show_startpoint(id)");

	cleare();
	
	new Ajax.Request("get-port-pos.php?id=" + id, 
	{
		onComplete: function(o)
		{
			try{
				eval(o.responseText);
			}
			catch(error)
			{
				alert("Error:" + error + "\n" + o.responseText );
			}
			//msg("Start point");
		}
	});
	
}



////////////////////////////////////////////////////////////////////////////////
// VIAPOINTS 
////////////////////////////////////////////////////////////////////////////////

function show_viapoints(){//SHOW EVERY VIAPOINTS (debug)
	for(var n=0; n<viap.length; n++)createviapoint(n);
}



var vip = new Array()
vip["baltic"]=Array(3,14,15,49,50,51,52,53,54,55,74,75,89);
vip["carribbean"]=Array(1,16,17,57,58,66,67,68,80,92);
vip["philippines"]=Array(10,24,25,30,31,32,33,34,35,37,38,39,40,41,56);
vip["world"]=Array(6,4,11,12,13,71,72);
vip["australia"]=Array(7,8,20,21,59,61);
vip["greatlakes"]=Array(19,28,81);
vip["europe"]=Array(22,23,73);
vip["indian"]=Array(29,64);
vip["china"]=Array(42,62,83);
vip["med"]=Array(2,43,44,45,46,47,48,76,77,78,79,84);
vip["japan"]=Array(18,26,69,70);
vip["gulf"]=Array(63,65,82);


function showpoints(r)//show VIA points for a given zone
{
	map.clearOverlays();//CLEAR MAP
	msg("Show "+r+" via points");
	switch(r)
	{		
		case "baltic":	map.setCenter(new google.maps.LatLng(56.77,17.77), 5);break;
		case "carribbean":	map.setCenter(new google.maps.LatLng(22.10, -75.23), 4);break;
		case "philippines":	map.setCenter(new google.maps.LatLng(19, 130), 3);break;
		case "world":	map.setCenter(new google.maps.LatLng(18.6, -6), 3);break;
		case "australia":	map.setCenter(new google.maps.LatLng(-26, 153), 3);break;
		case "greatlakes":	map.setCenter(new google.maps.LatLng(48, -58), 5);break;
		case "europe":	map.setCenter(new google.maps.LatLng(55, 0), 4);break;
		case "indian":	map.setCenter(new google.maps.LatLng(-6.2, 69), 3);break;
		case "china":	map.setCenter(new google.maps.LatLng(27.8, 125), 4);break;
		case "med":		map.setCenter(new google.maps.LatLng(37.7, 22), 5);break;
		case "japan":	map.setCenter(new google.maps.LatLng(43.5, 144), 4);break;
		case "gulf":	map.setCenter(new google.maps.LatLng(7.5, 92), 4);break;
		default:alert("error:"+r);break;
	}
	for(var n=0; n<vip[r].length; n++)proposeviapoint(vip[r][n]);
}


function createviapoint(n){
	if(!n)return;
	var lati =viap[n][1];
	var longi=viap[n][2];
	//var maxdraft=viap[n][3];
	var point = new GLatLng(lati, longi);
	if(lati==0 && longi==0)return;
	
	var titlestr=viap[n][0];
	if(viap[n][3]>0)titlestr+=" - Max draft:"+viap[n][3];
	else titlestr+=" - Max draft:unknown";
	
	var marker = new GMarker(point,{icon:iconVia, title: titlestr });
	map.addOverlay(marker);
}

function proposeviapoint(n){//double click on those points to force the viapoint
	if(!n)return;
	var lati =viap[n][1];
	var longi=viap[n][2];
	var point = new GLatLng(lati, longi);
	if(lati==0 && longi==0)return;

	var titlestr=viap[n][0];
	if(viap[n][3]>0)titlestr+=" - Max draft:"+viap[n][3];
	else titlestr+=" - Max draft: unknown";

	var marker = new GMarker(point,{icon:iconVia, title: titlestr });
	GEvent.addListener(marker, "dblclick", function(){
		kill();
		var v=new Array();
		v[0] = n;
		sequence[num][7] = v;
		get_dist( num );
	});
	map.addOverlay(marker);
}

function cancel_via()
{
	//alert('cancel_via()');
	kill();
	build();
	if(!num)return;
	
	if( sequence[num][7] )
	{
		sequence[num][7] = "";//
		get_dist( num );
	}
}


function createminipoint(n){//mini via point label
	//console.log("createminipoint("+n+")");
	if(!n)return;
	if(n==49)return;//?

	var lati =viap[n][1];
	var longi=viap[n][2];
	var point = new GLatLng(lati, longi);
	if(lati==0 && longi==0)return;

	var titlestr=viap[n][0];
	if(viap[n][3]>0)titlestr+=" - Max draft:"+viap[n][3];
	//else titlestr+=" - Max Draft: unknown";

	var marker = new GMarker(point,{icon:iconmini, title: titlestr });

	GEvent.addListener(marker, "click", function(){
		kill();
		via_detail(n);
	});

	map.addOverlay(marker);
}



function via_detail(id)
{
	msg("Way point info");
	new Ajax.Request("via-info.php?id="+id, 
	{
		onComplete: function(o){
			eval(o.responseText);
		}
	});
}


//////////////////////////////////////////////////
// BUNKERS 
//////////////////////////////////////////////////

function get_bunkers(){//SHOW BUNKER POSISITIONS
	cleare();
	msg("get_bunkers()");
	new Ajax.Request("bunkers.php", 
	{
		onComplete: function(o){eval(o.responseText);}
	});
}

function createBunker(lati,longi,str){// SHOW MARKER //
	var marker =new GMarker(new GLatLng(lati, longi),{icon:iconBnk, title: str });
	GEvent.addListener(marker, "click", function() {
		//alert("Bunker:"+str);
		point_detail(str);
	});
	map.addOverlay(marker);
}

//////////////////////
// PIRACY
//////////////////////
var polypirat;//

function show_piracy()
{
	
	alert("show_piracy(), polypirat="+polypirat);
	
	msg('show_piracy()' + polypirat );
	//map.clearOverlays();//CLEAR MAP
	if(!polypirat)
	{
		//prevpiracy = $("piracy").checked;
		var points = [];
		points[0] = new GLatLng( 12, 45);
		points[1] = new GLatLng( 15, 52);
		points[2] = new GLatLng( 15, 65);
		points[3] = new GLatLng( -12, 65);
		points[4] = new GLatLng( -12, 45);
		points[5] = points[0];
	
		polypirat = new GPolygon( points , "#ff0000", 1 , 1 , "#cc0000");
		polypirat.dash = true;
		map.addOverlay( polypirat );
	}
		/*
		var shape = new Array();
		shape.push({lat:12,lng:45});
		shape.push({lat:15,lng:52});
		shape.push({lat:15,lng:65});
		shape.push({lat:50,lng:100});

		var shape = map.addPolygon({points:shape, stroke:false,fillRGB:0xFF0000,fillAlpha:50});
	
	
	/*
		GEvent.addListener(polypirat, 'click', 
			function(para)
			{
				map.openInfoWindowHtml(para,"<h4> Piracy Zone </h4>" + para)
				//polypirat.opacity = 0;
			}
		);
	}
	*/
	
	
	/*
	if( $("piracy").checked == true)
	{
		polypirat.opacity = 0.25;
	}
	else
	{
		polypirat.opacity = 0;
	}
	*/
	//map.refresh();
	//polypirat.redraw();
	
}

