/********************************************************/
// Variables globales
/********************************************************/

ongletHover = 0;
_currentItem = 1;
var _map;
/********************************************************/
// Fonctions
/********************************************************/

// Changement de langue
function activeLangChange(){
	$('#topNav a.lang').bind('click', function(){
		$(this).next('.select').toggleClass('hide');
		return false;
	});
}

function activeBtmNav(){
	if($('#header .btmInner').length){
		ongletHover = ongletActif;
	
		$('#nav li').hover(function(){
			var classe = $(this).attr('class');
			var numOnglet = parseInt(classe.substring(6,classe.length), 10);
			$('#nav li a').removeClass('active');
			$(this).find('a').addClass('active');
			
			if(ongletHover != numOnglet){
				ongletHover = numOnglet;
				
				$('#header .bottom').hide();
				$('#header .bottom.btm'+ongletHover).show();
			}
		},
		function(e){
			// if cursor is above the nav hide the nav2Roll
			if (e.clientY <= 170) {
				if(ongletHover != ongletActif){
					$('#header .bottom').hide();
					$('#header .bottom.btm'+ongletActif).show();
					$('#nav li a').removeClass('active');
					$('#nav li a').eq(ongletActif-1).addClass('active');
					ongletHover = ongletActif;
				}
			}else{
				var li = $(this);
				
				$('#header .btmInner').hover(function(){
					// don't do anything !
				},function(){
					// hide the stuff on roll out
					if(ongletHover != ongletActif){
						$('#header .bottom').hide();
						$('#header .bottom.btm'+ongletActif).show();
						$('#nav li a').removeClass('active');
						$('#nav li a').eq(ongletActif-1).addClass('active');
						ongletHover = ongletActif;
					}
				});
			}
		});
	}
}

/********************************************************/
// Chargement de la page
/********************************************************/

$(document).ready(function(){
	activeLangChange();
	activeBtmNav();
	
	/* Fix pb titre Home 3 */
	$('#inner .homeNiveau3 .intro h2').css('display', 'block');
}); 
/********************************************************/
// Carte et itineraire
/********************************************************/
function showOptions(obj){
	if ($('#blockOption').hasClass("hidden")==false){
		$('#blockOption').addClass("hidden");
		$(obj).html("Afficher les options");
	}else{
		$('#blockOption').removeClass("hidden");
		$(obj).html("Masquer les options");
	}	
}

function showOngletItineraire(obj){
	if ($(obj).hasClass("selected")==false){
		$('#map_canvas').show();
		$('#map_detail').hide();
		$(obj).addClass("selected");
		$('#ongletItineraireD').removeClass("selected");
	}
}

function showOngletDetail(obj){
	if ($(obj).hasClass("selected")==false){
		$('#map_detail').show();
		$('#map_canvas').hide();
		$(obj).addClass("selected");
		$('#ongletItineraire').removeClass("selected");
	}
}

function addDestionation(){
	var i=$('#destinationInput p').length;
	$('#destinationInput').append('<p><img src="./img/common/dest_'+(i+1)+'.gif" alt="" /><input type="text" name="dest_'+(i+1)+'" id="dest_'+(i+1)+'" /></p>');	
	if($('#destinationInput p').length==4)
		$("#lieuItineraire ul.arrows").remove();
}

function getMode(){
	if($('#modeTravel').val()==1){
		$('p.check').show();
	}
	else{
		$('p.check').hide();
	}
}

//  MAP 

function verifyFields(){
	var flag=true;
	for(i=1;i<=$('#destinationInput p').length;i++){
		if(!$('#dest_'+i+'').val()){
			flag = false;
			break;
		}
	}
	return flag;
}

function showInfo(destination){
		var pointDest = "" ;
		var dist = "";
		
		for(i=1 ;i<=$('#destinationInput p').length;i++){			
			pointDest = pointDest + $('#dest_'+i).val();			
			if(i!=$('#destinationInput p').length && $('#dest_'+(i+1)).val())
				pointDest = pointDest + " , ";
		}
		GEvent.addListener(destination, "load", function (){ 
				var duration = destination.getDuration().html;
				var distance = destination.getDistance();
				var distKm = (distance.meters/1000).toFixed(1);
				var distMile = (distKm * 0.621).toFixed(1);
				if($('#km').attr('checked'))
					dist = distKm+" Km";
				 else
					dist = distMile+" Miles";
				$(".suggestion").hide();
				$('.blockInfo').show();
				$('span#duree').html(" - environ <b>"+duration+"</b>");
				$('span#distance').html(dist);
				$('b#pointDest').html(pointDest);						
			});	
}

function showSuggestion(map,result){
		//var map = new GMap2(document.getElementById("map_canvas"));
		var points=[];
		var lat = [];
		var lng = [];	
		var Accu = [];
		var precis=[];
		var marker=[];
		var miniMarker=[];
		lat.length=0;
		var dest ="";
		
		$(".blockInfo").hide();
		if (result.length > 1)
		$(".suggestion").show();
		else
			$(".suggestion").hide();
		for (var i=0; i<result.length; i++){
			lat[i]=result[i].Point.coordinates[1];
			lng[i]=result[i].Point.coordinates[0];
			points[i]=result[i].address;
			precis[i]=3*points[i].split(",").length;
			dest = dest + '<small>'+(i+1)+', </small><a href="javascript:go('+lat[i]+','+lng[i]+','+precis[i]+','+i+', \''+points[i]+'\', '+_currentItem+')"> '+points[i]+' </a><br/>';			
		 }
		 $(".suggestion").html(dest);
		 if (result.length > 1)
			return true;
		return false;
}

function go(lat,lng,zoom,num, label, input){	
		$("#dest_"+input).val(label);
		_currentItem = 1;
		verifyNextItem();		
}

function getDestination(){
		_currentItem = 1;
		var flag = verifyFields();
		
		if(!flag){
			alert(" Veuillez saisir les destinations ");
		}else{
			_map = new GMap2(document.getElementById("map_canvas"));
			_map.addControl(new GLargeMapControl());
			_map.addControl(new GScaleControl());			
			verifyNextItem();
		}	
}

function launchSearch(){
	var highWaysValue = false;
	var travel = G_TRAVEL_MODE_DRIVING;
	
	if($('#modeTravel').val()==1){
		if ($('#eviter_autoroute').attr('checked')) {
			highWaysValue = true;
		}
	}else{
		travel = G_TRAVEL_MODE_WALKING;
	}
	

	var point_depart = $('#dest_1').val();
	var dest = "from: "+point_depart+" ";
	var geo = new GClientGeocoder();		
	
	for(i=1 ;i<=$('#destinationInput p').length;i++){					
		if(i>1 && $('#dest_'+i).val()){
			dest = dest+"to:"+$('#dest_'+i).val()+" ";
		}
	}
	var destination = new GDirections(_map, document.getElementById("map_detail"));							
	destination.load(dest, {"locale": "fr",avoidHighways:highWaysValue,travelMode:travel});
	showInfo(destination);
}

function verifyNextItem(){
		if (_currentItem > $('#destinationInput input').length){
			launchSearch();
			return ;
		}
		var search = $("#dest_"+_currentItem).val();
		if (search != undefined && search != "")
		{
			var geo = new GClientGeocoder();	
			geo.getLocations(search, function (result)
			{
				if(result.Status.code == G_GEO_SUCCESS){						
					var flag = showSuggestion(_map,result.Placemark);
				
					$('#map_detail').html("");
					if (flag == false)
					{
						_currentItem++;
						verifyNextItem();
					}						
				}else if(result.Status.code == G_GEO_UNKNOWN_ADDRESS){
					$(".blockInfo").hide();
					$(".suggestion").show();
					$(".suggestion").html('<p  style="text-decoration: blink; color:red;"><strong>Impossible de trouver le lieu</b>.<br/><br/>V&eacute;rifiez l\'orthographe de tous les noms.</strong>');
				}				
			});	
		}else{
			_currentItem++;
			verifyNextItem();
		}
	}
	

