function smoothScroll(destinationY,callbackFunction,timing){

	if(timing == undefined || timing == '') timing = 550 ;

	var MaxScroll = window.innerHeight || document.documentElement.clientHeight;
	MaxScroll = $('body').height() - MaxScroll;
	if(MaxScroll > 1 && destinationY > MaxScroll) destinationY = MaxScroll ;

	if(destinationY >= 1){
		$('html,body').animate({
			scrollTop: destinationY
		},timing,'easeOutExpo',function(){
			if( $.isFunction(callbackFunction) ) callbackFunction() ;
		});
	}else{
		if( $.isFunction(callbackFunction) ) callbackFunction() ;
	}
	return false;
}



$(document).ready(function(){

	var depRegex = new RegExp('\(([0-9]{2}|2A|2B)\)');
	var nameRegex = new RegExp("^France - \\(([0-9]{2}|2A|2B)\\) (.*)$",'i');
	var tooltipTest = $('<div style="display:none" id="#tooltip-test" class=".toolTip-1"><span class="right"><span class="content"></span></span></div>');
	$('body').append(tooltipTest);
	var popupValidation = $('.popupMap');
	var popupDpt = $('#popupMap');

	popupValidation.find('a.jsClose').click(function(){
		popupValidation.fadeOut(300);
		return false;
	});

	popupDpt.find('.jsClose').click(function(){
		popupDpt.fadeOut(300);
		return false;
	});

/**
 *  Départements 
 */
	$('#mapFrance area').each(function(){

		var elem = $(this);
		var dep = $(this).attr('alt');
	

		// text 
		var match = depRegex.exec(dep);
		if(match != null){
			if(match[0] == '2A' || match[0] == '2B') match[0] = 20;
			var text = $('select[name="dep"] option[value='+match[0]+']').html();
		}else {
			var text = dep;
		}
		var match = nameRegex.exec(text);
		if(match != null){
			var depName = '<span>'+dep+'</span> '+ match[2];
		}else {
			var depName = text;
		}
		// width compute 
		tooltipTest.find('span.content').html(depName) ;
		var width = tooltipTest.width() ;


		// tooltip 
		elem.tooltip({
			track: true,
			delay: 0,
			showURL: false,
			extraClass: 'toolTip-1',
			bodyHandler: function(){
				return '<span class="right"><span class="content">' + depName + '</span></span>';
			},
			top: -44,
			left: (-width / 2) - 10 
		});

		// validation popup
		elem.click(function(){
			//gestion des cas particulier 
			switch(elem.attr('href')){
				case '/promo-express.html?dep=Paris':
					choicePopup('Paris');
					return false;
				break;
				case '/promo-express.html?dep=Lyon':
					choicePopup('Lyon');
					return false;
				break;
				case '/?dep=Marseille':
					choicePopup('Marseille');
					return false;
				break;
			}

		/*	// affiche la pop-up de validation a partir de la carte de la france
			popupValidation.find('span.zoneName').html(depName);
			popupValidation.find('.valid a').attr('href',elem.attr('href'));
			popupValidation.fadeIn(250);
			return false;
		*/

		});
	});

/**
 * Paris 
 */
	$('.block-2 a[href$="?dep=Paris"]').each(function(){
		var elem = $(this);
		elem.click(function(){
			choicePopup('Paris');
			return false;
		});
	});
/**
 * Belgique / Luxembourg  
 */

	$('#mapBelgLux area').each(function(){
		var elem = $(this);
		elem.click(function(){
			switch(elem.attr('href')){
				case '/promo-express.html?dep=Belgique':
					//var depName = 'Belgique';
					choicePopup('Belgique');
					return false;
					break;
				case '/promo-express.html?dep=Luxembourg':
					var depName = 'Luxembourg';
				break;
			}
			popupValidation.find('span.zoneName').html(depName);
			popupValidation.find('.valid a').attr('href',elem.attr('href'));
			popupValidation.fadeIn(250);
			return false;
		});

		
	});
/**
 * Europe  
 */

	$('.block-2 a[href$="?dep=Europe"]').each(function(){
		var elem = $(this);
		elem.click(function(){
			choicePopup('Europe');
			return false;
		});
	});

/**
 *  Arrondissements Paris 
 */

	popupDpt.find('#mapIdfBig area').each(function(){
		var destination = $(this).attr('href');
		if( destination == '#blockArrondissement' ){
			$(this).click(function(){
				smoothScroll($(destination).offset().top);
				return false;
			});
		}
	});


});

function choicePopup(part){
	var popup = $('#popupMap');

	popup.find('.blockMapDpt , .blockMapEurope , .blockMapBelgique').hide();

	switch(part){
		case 'Paris':
			popup.find('.jsParis').show();	
		break;
		case 'Marseille':
			popup.find('.jsMarseille').show();	
		break;
		case 'Lyon':
			popup.find('.jsLyon').show();	
		break;
		case 'Europe':
			popup.find('.jsEurope').show();	
		break;
		case 'Belgique':
			popup.find('.jsBelgique').show();
		break;
	}

	popup.fadeIn(250);

}

