// Points X,Y en coordonnées Lambert 2 des deux villes
var y_max = 130934.498619;// Viry
var y_min = 108641.237628;// La clusaz
var x_max = 916985.066724;// La clusaz
var x_min = 885917.525769;// Viry

// Points X,Y en pixels de deux villes
var posxmin = new Array();	// Viry
var posymin = new Array();	// Viry
var posxmax = new Array();	// La clusaz
var posymax = new Array();	// La clusaz

// Points X,Y en pixels de deux villes (zoom 1)
posxmin[1] = 350;	// Viry
posymin[1] = 252;	// Viry
posxmax[1] = 496;	// La clusaz
posymax[1] = 358;	// La clusaz

// Points X,Y en pixels de deux villes (zoom 3)
posxmin[3] = 1400;	// Viry
posymin[3] = 1007;	// Viry
posxmax[3] = 1996;	// La clusaz
posymax[3] = 1431;	// La clusaz

// Calcul des intervalles
var dy = y_max-y_min;
var dx = x_max-x_min;

var posdy = posymax[1]-posymin[1];
var posdx = posxmax[1]-posxmin[1];



//width and height of map
var map_width_base = 989;
var map_height_base = 525;

//width and height of result layer
var map_window_width = 724;
var map_window_height = 394;

//distance of map and result layer
var map_content_left = -(map_width_base - map_window_width)/2;
var map_content_top = -(map_height_base - map_window_height)/2;

////var map_window_left = 113;
//var map_window_left = 258;
////var map_window_top = 38;
var map_window_left = 241;
var map_window_top = 21;

// Pour bouger la carte une fois zoomée
var move_to_x = null;
var move_to_y = null;

// Sous-région sélectionnée
var sous_region_select = 0;

// Correspondances pour les sous-régions : ID value <-> Numéro de calque(1 à 9)
var correspondances_id_sous_regions = new Array();
correspondances_id_sous_regions[0] = 0;
correspondances_id_sous_regions[32] = 9;
correspondances_id_sous_regions[31] = 2;
correspondances_id_sous_regions[24] = 3;
correspondances_id_sous_regions[25] = 4;
correspondances_id_sous_regions[27] = 7;
correspondances_id_sous_regions[28] = 8;
correspondances_id_sous_regions[29] = 5;
correspondances_id_sous_regions[30] = 1;
correspondances_id_sous_regions[100] = 6; // Pays du Mont-Blanc

// Correspondances inversées pour les sous-régions : Numéro de calque(1 à 9) <-> ID value 
var correspondances_id_sous_regions_inv = new Array();
correspondances_id_sous_regions_inv[0] = 0;
correspondances_id_sous_regions_inv[9] = 32;
correspondances_id_sous_regions_inv[2] = 31;
correspondances_id_sous_regions_inv[3] = 24;
correspondances_id_sous_regions_inv[4] = 25;
correspondances_id_sous_regions_inv[7] = 27;
correspondances_id_sous_regions_inv[8] = 28;
correspondances_id_sous_regions_inv[5] = 29;
correspondances_id_sous_regions_inv[1] = 30;
correspondances_id_sous_regions_inv[6] = 100; // Pays du Mont-Blanc

// Correspondances pour les sous-régions : Numéro de calque(1 à 9) <-> Positions X,Y
var correspondances_xy_sous_regions = new Array();
correspondances_xy_sous_regions[0] = new Array(-626,-347);
correspondances_xy_sous_regions[9] = new Array(-350,-500);
correspondances_xy_sous_regions[2] = new Array(-850,-150);
correspondances_xy_sous_regions[3] = new Array(-630,-300);
correspondances_xy_sous_regions[4] = new Array(-380,-300);
correspondances_xy_sous_regions[7] = new Array(-630,-500);
correspondances_xy_sous_regions[8] = new Array(-500,-560);
correspondances_xy_sous_regions[5] = new Array(-850,-300);
correspondances_xy_sous_regions[1] = new Array(-700,-100);
correspondances_xy_sous_regions[6] = new Array(-850,-510); // Pays du Mont-Blanc


//change the mode of legend
function change_legend_image(div_id,checkbox_id){
	
	var arr_legend_img = div_id.split(",");
	
	var legend_checkbox = document.getElementById(checkbox_id);
	
	for(var l = 0;l < arr_legend_img.length;l++){
	
		var legend_img = document.getElementById(arr_legend_img[l]);
		if(legend_img.style.display == "block" && legend_img.style.top == "0px" && legend_img.style.visibility!="hidden"){
			if(div_id=="div_carto_region"){
				document.getElementById("image_zones").style.display="none";
			}
			legend_img.style.display = "none";
			legend_checkbox.src = "images/legendes/legende_check.gif";
		}
		else{
			legend_img.style.visibility = "visible";
			legend_img.style.display = "block";
			if(div_id=="div_carto_region"){
				document.getElementById("image_zones").style.display="block";
			}
			legend_img.style.top = "0px";
			legend_checkbox.src = "images/legendes/legende_checked.gif";
			
		}
	}
}
function big_hotel_poplayer(zipcode,city,g,f){
	
	hidelayer("div_hotel_pop_layer");
	
	var img_hotel_big = document.getElementById("img_hotel_big_"+g);
	
	var big_hotel_left = parseInt(img_hotel_big.style.left);
	var big_hotel_top = parseInt(img_hotel_big.style.top);
	
	var div_big_hotel_content = "";
	
	var big_poplayer_div_obj = document.getElementById("div_big_hotel_pop_layer");
	
	var div_left_init = parseInt(document.getElementById('div_result_map_content').style.left);
	var div_top_init = parseInt(document.getElementById('div_result_map_content').style.top);
	
	div_big_hotel_content  = "<table width='90%' height='100%' align='center'>";
	div_big_hotel_content += "	<tr>";
	div_big_hotel_content += "		<td valign='top' class='div_hotel_pop_lable'>"+f+" resultats dans le </br>"+city+"</td>";
	div_big_hotel_content += "	</tr>";
	div_big_hotel_content += "	<tr>";
	div_big_hotel_content += "		<td class='div_hotel_pop_layer_detail' align='left'><div style='width:150px' onclick='hidelayer(\"div_big_hotel_pop_layer\");generateResultByZipcode(\""+zipcode+"\");'>Voir la liste des resultats</div></td>";
	div_big_hotel_content += "	</tr>";
	div_big_hotel_content += "</table>";
	
	big_poplayer_div_obj.innerHTML = div_big_hotel_content;
	
	
	
	big_poplayer_div_obj.style.left = big_hotel_left + div_left_init+ 28 + "px";
	big_poplayer_div_obj.style.top = big_hotel_top + div_top_init + 25 + "px";
	big_poplayer_div_obj.style.display = "block";
	
}

var sto_hidelayer;

function hotel_poplayer(div_id,n){
	
	//hidelayer("div_big_hotel_pop_layer");
	if(sto_hidelayer){
		clearTimeout(sto_hidelayer);
	}
	
	var hotel_id = pop_layer_content_arr[n][0];
	var hotel_type_id = pop_layer_content_arr[n][1];
	var hotel_name = pop_layer_content_arr[n][2];
	var hotel_duree = pop_layer_content_arr[n][3];
	var reg=new RegExp("( )", "g");
	if(hotel_duree){
		hotel_duree = hotel_duree.replace(reg,"");
	}
	var hotel_denivele = pop_layer_content_arr[n][4];
	var hotel_resume = pop_layer_content_arr[n][5];
	var hotel_photo = pop_layer_content_arr[n][6];
	var hotel_depart = pop_layer_content_arr[n][3];
	var hotel_arrivee = pop_layer_content_arr[n][4];
	var longueur = pop_layer_content_arr[n][7];
	var xml_file_name = pop_layer_content_arr[n][9];
	var picto_type = pop_layer_content_arr[n][10];
	
	var img_hotel = document.getElementById("img_hotel_"+n);
	
	var hotel_left = parseInt(img_hotel.style.left);
	var hotel_top = parseInt(img_hotel.style.top);
	
	var map = document.getElementById("div_result_map_content");
	
	hotel_left = hotel_left+map.offsetLeft;
	hotel_top = hotel_top+map.offsetTop;
	
	var y_a_une_photo = (hotel_photo.length>0);
	
	var div_content = "";
	
	var poplayer_div_obj = document.getElementById(div_id);
	
	div_content = '<div class="div_cartouche_rando_carto" id="divsurvol" onmouseover="clearTimeout(sto_hidelayer);" onmouseout="hidelayer_with_timeout(\'div_hotel_pop_layer\');">';
	
	var strClass = (n > 99) ? 'workspace_cartouche_rando_itineraire_num_petit' : 'workspace_cartouche_rando_itineraire_num';
	div_content += '	<div class="workspace_cartouche_rando_itineraire">';
	div_content += '	<img src="'+picto_type+'" width="45" height="38" />';
	div_content += '	<div class="'+strClass+'">'+n+'</div>';
	div_content += '	</div><!--';
	
	div_content += '	--><img src="/download/site-principal/image/charte/carto/cartouche/cartouche_carte_haut.png" width="182" height="9" border="0" alt="" title="" /><!--';
	//if (y_a_une_photo) div_content += '	--><img src="/module/carto/image/pratique2_pedestre.png" border="0" alt="Pédestre" title="Pédestre"  style="position:absolute;top:90px;left:10px;z-index:100;" /><!--';
	//else div_content += '	--><img src="/module/carto/image/pratique2_pedestre.png" border="0" alt="Pédestre" title="Pédestre"  style="position:absolute;top:10px;left:150px;z-index:100;" /><!--';
	
	div_content += '	--><div class="workspace_cartouche_rando_carto">';
			
	if (y_a_une_photo) {
		div_content += '		<div class="cartouche_rando_carto_cadre_image">';
		div_content += '			<div class="div_image_carto_carto">';
		div_content += '				<a href="#" onclick="hidelayer(\'div_hotel_pop_layer\');generateFiche('+hotel_id+','+hotel_type_id+',\''+xml_file_name+'\')" border="0">';
		div_content += '				<img src="/download/site-principal/image/charte/carto/cartouche/masque.png" border="0" alt="" title="" width="104" height="104" style="position:absolute;top:-1px;left:39px;" />';
		div_content += '				<img src="'+hotel_photo+'" width="102" height="102" border="0">';
		div_content += '				</a>';
		div_content += '			</div>';
		div_content += '		</div>';
	}else{
		div_content += '		<div style="margin:0px;padding:0px;">&nbsp;</div>';
	}
	div_content += '		<div class="div_description_rando_carto">';
	div_content += '			<div class="titre_carto">';
	div_content += '				<a href="#" onclick="hidelayer(\'div_hotel_pop_layer\');generateFiche('+hotel_id+','+hotel_type_id+',\''+xml_file_name+'\')">'+hotel_name+'</a>';
	div_content += '			</div>';
	div_content += '			<div class="texte_carto">';
	if(hotel_type_id == 1){
		div_content += '				Durée : ';
		div_content += '				<b>'+hotel_duree+'</b>';
	}
	if(hotel_type_id == 4){
		div_content += '				Distance : ';
		div_content += '				<b>'+pop_layer_content_arr[n][7]+' km</b>';
	}
	
	div_content += '				<br />';
	
	if(hotel_type_id == 5){
		div_content += '				Départ :'; 
		div_content += '				<b>'+hotel_depart+'</b>';
		div_content += '				<br />';
		div_content += '				Arrivée :'; 
		div_content += '				<b>'+hotel_arrivee+'</b>';
	}else{
		div_content += '				Dénivelé :'; 
		div_content += '				<b>'+hotel_denivele+'</b>';
		div_content += '				<br />';
		div_content += '				Intérêts :'; 
		div_content += '				<b>'+hotel_resume+'</b>';
	}
	div_content += '			</div>';
	div_content += '		</div>';
	div_content += '		<div class="div_lien_randodumois_carto">';
	div_content += '			<img src="/download/site-principal/image/charte/carto/cartouche/fleche.png" width="12" height"9" border="0" alt="En savoir plus" title="En savoir plus" />';
	div_content += '			&nbsp;&nbsp;';
	div_content += '			<a href="#" onclick="hidelayer(\'div_hotel_pop_layer\');generateFiche('+hotel_id+','+hotel_type_id+',\''+xml_file_name+'\')">Voir le détail du parcours</a>';
	div_content += '		</div>';
	div_content += '	</div>';
	div_content += '<img src="/download/site-principal/image/charte/carto/cartouche/cartouche_carte_bas.png" width="182" height="9" border="0" alt="" title="" />';

	
	
	div_content += '</div>';

	/*
	div_content = '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />';
	div_content += '<div id="divsurvol" onmouseover="clearTimeout(sto_hidelayer);" onmouseout="hidelayer_with_timeout(\'div_hotel_pop_layer\');" >';
	div_content += "<table width='90%' height='100%' align='center'>";
	div_content += "	<tr>";
	div_content += "		<td height='20px'  colspan='2' class='div_hotel_pop_label'><b>ITINERAIRE&nbsp;&nbsp;N°"+n+"</b></td>";
	div_content += "	</tr>";
	div_content += "	<tr>";
	div_content += "		<td height='20px' colspan='2'><div style='position:relative'>";
	div_content += "<div style='position:absolute;top:0px;'><a href='#' onclick='hidelayer(\"div_hotel_pop_layer\");generateFiche("+hotel_id+","+hotel_type_id+",\""+xml_file_name+"\")'><img src='image/masque_img_liste_resultats.png' border='0' alt='' title='' /></a></div><img src='"+hotel_photo+"'></div>";
	div_content += "		</td>";
	div_content += "	</tr>";
	div_content += "	<tr>";
	div_content += "		<td height='20px' colspan='2' class='div_hotel_pop_hotel_name'><a href='#' onclick='hidelayer(\"div_hotel_pop_layer\");generateFiche("+hotel_id+","+hotel_type_id+",\""+xml_file_name+"\")'><b>"+hotel_name+"</b></a>&nbsp;</td>";
	div_content += "	</tr>";
	div_content += "	<tr>";
	div_content += "		<td valign='bottom' align='left' colspan='2'><div class='div_hotel_pop_layer_details'>Durée : "+hotel_duree+"&nbsp;<br />Dénivelé : "+hotel_denivele+"&nbsp;<br />Pratique : Pédestre&nbsp;<br />Intérêts : "+hotel_resume+"&nbsp;</div></td>";
	div_content += "	</tr>";
	div_content += "	<tr>";
	div_content += "		<td valign='bottom' align='right'><div class='div_hotel_pop_layer_detail'><b><img src='image/fleches_vertes.png' border='0' alt='' title='' />&nbsp;<a href='#' onclick='hidelayer(\"div_hotel_pop_layer\");generateFiche("+hotel_id+","+hotel_type_id+",\""+xml_file_name+"\")'>Voir le détail du parcours</a>&nbsp;</b></div></td>";
	div_content += "	</tr>";
	div_content += "</table>";
	div_content += "</div>";
	*/
	
	poplayer_div_obj.innerHTML = div_content;
		
	poplayer_div_obj.style.display = "block";
	
	var divsurvol = document.getElementById("divsurvol");
	if(divsurvol){
		var hauteur = divsurvol.offsetHeight;
//alert(hotel_top+hauteur);
		if(hotel_top+hauteur>394){
			hotel_top = 394-hauteur;
		}
		
		//alert(hotel_top+hauteur);
	}
	
	poplayer_div_obj.style.left = hotel_left + 48 + "px";
	poplayer_div_obj.style.top = hotel_top + "px";
	//poplayer_div_obj.style.left = "0px";
	//poplayer_div_obj.style.top = "0px";
	
	updatePNG();
	
}


function limitResultsToList(picto_num){
	var selectCommunes = document.getElementById("select_communes");
	if(selectCommunes && pop_layer_content_arr[picto_num][14]){
		var options = selectCommunes.options;
		for(var i=0;i<options.length;i++){
			if(options[i].text == pop_layer_content_arr[picto_num][14]){
				selectCommunes.options[i].selected = true;
			}else{
				selectCommunes.options[i].selected = false;
			}
		}
		setSwitchToTab(2);
		generateXml();
	}
}

function hidelayer(div_id){
	var hidelayer_div_obj = document.getElementById(div_id);
	hidelayer_div_obj.style.display = "none";
}

function hidelayer_with_timeout() {
	var div_id = "div_hotel_pop_layer";
	sto_hidelayer = setTimeout("hidelayer('"+div_id+"')", 450);
}
function hidelayer_stop_timeout() {
	clearTimeout(sto_hidelayer);
}


function hidelayer2_with_timeout() {
	
	var div_id = "div_big_hotel_pop_layer";
	sto_hidelayer2 = setTimeout("hidelayer('"+div_id+"')", 450);
}
function hidelayer2_stop_timeout() {
	
	clearTimeout(sto_hidelayer2);
}


//for drag map
var obj_left_init = 0;
var obj_top_init = 0;

var mouse_begin_x = 0;
var mouse_begin_y = 0;

var mouse_end_x = 0;
var mouse_end_y = 0;

var mouse_down = false;
var div_obj = "";
var div_drag = "";

function map_move_init(e){
	
	if(current_zoom!="1"){
	
		e = e?e:(window.event?window.event:null);
		
		div_obj = document.getElementById('div_result_map_content');
		div_drag = document.getElementById('div_carto_pictos_hotel');
		
		if(div_drag.setCapture){
			
			div_drag.setCapture();
		} 
		else if(window.captureEvents){
			
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}
		
		mouse_begin_x = e.clientX;
		mouse_begin_y = e.clientY;
		
		obj_left_init = parseInt(div_obj.style.left);
		obj_top_init = parseInt(div_obj.style.top);
		
		mouse_down = true;
		
		document.onmouseup = map_move_stop;
		
		return false;
		
	}
		
}

 

function map_move(e){
	
	if(current_zoom!="1"){
		e = e?e:(window.event?window.event:null);
		
		if(mouse_down){
			
			mouse_end_x = e.clientX;
			mouse_end_y = e.clientY;
			
			div_obj.style.left = mouse_end_x - mouse_begin_x + obj_left_init + "px";
			div_obj.style.top = mouse_end_y - mouse_begin_y + obj_top_init + "px";	

			IDfr.map.viewport();
		}
	} 
} 

function map_move_stop(){
	
	if(current_zoom!="1"){
		mouse_down = false;
		document.onmouseup = null;
		if(parseInt(div_obj.style.left) > 0){
			
			div_obj.style.left = "0px";
		}
		if(parseInt(div_obj.style.top) > 0){
			
			div_obj.style.top = "0px";
		}
		if(parseInt(div_obj.style.left) < -(map_width_base - map_window_width)){
			
			div_obj.style.left = -(map_width_base - map_window_width) + "px";
		}
		if(parseInt(div_obj.style.top) < -(map_height_base - map_window_height)){
			
			div_obj.style.top = -(map_height_base - map_window_height) + "px";
		}
		
		if(div_drag.releaseCapture){
			
			div_drag.releaseCapture();
		}
		else if(window.captureEvents){
			
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}
	}
	
	IDfr.map.viewport();
}



//for zoom in/out map
var zoom_max = 3;

var center_x = map_window_width / 2;
var center_y = map_window_height / 2;

var mouse_x = 0;
var mouse_y = 0;

var map_style_left = 0;
var map_style_top = 0;

var tab_image_size = new Array();

tab_image_size[1] = new Array();
//tab_image_size[1][0] = 724;
//tab_image_size[1][1] = 394;
tab_image_size[1][0] = 989;
tab_image_size[1][1] = 525;

tab_image_size[2] = new Array();
tab_image_size[2][0] = 1977;
tab_image_size[2][1] = 1049;

tab_image_size[3] = new Array();
tab_image_size[3][0] = 3954;
tab_image_size[3][1] = 2098;


var evn_tmp = "";
var root_path = "/module/carto";

function map_zoom(i,act,evn){
	
	// on cache le zoom niveau 2 => pour le reactiver, juste decommenter cette ligne
	if (i==2) {
		if (act == "in") {
			i = 3;
		}
		else {
			i = 1;
		}
	}	
	
	// Ajustement des intervalles
	posdy = posymax[i]-posymin[i];
	posdx = posxmax[i]-posxmin[i];
	
	
	if(i==current_zoom) return;
	
	evn = evn?evn:(window.event?window.event:null);
	
	if(evn_tmp == "" || evn_tmp == null) evn_tmp = evn;
	
	var div_result_map_content = document.getElementById('div_result_map_content');
	var div_carto_pictos_hotel = document.getElementById('div_carto_pictos_hotel');
	var div_carto_map = document.getElementById('div_carto_map');
	
	//var picto_zoom_1 = document.getElementById('map_zoom_class_img_1');
	//var picto_zoom_2 = document.getElementById('map_zoom_class_img_2');
	
	var map_zoom_out_img = document.getElementById('map_zoom_out_img');
	var map_zoom_in_img = document.getElementById('map_zoom_in_img');
	//var map_zoom_class_curseur = document.getElementById('map_zoom_class_curseur');
	
	var div_result_map_zoom = document.getElementById('div_result_map_zoom');
	var div_result_map_legend = document.getElementById('div_result_map_legend');
	
	var div_carto_routes = document.getElementById('div_carto_routes');
	var div_carto_villes = document.getElementById('div_carto_villes');
	var div_carto_region = document.getElementById('div_carto_region');
	var div_carto_coursdeau = document.getElementById('div_carto_coursdeau');
	
	var hidden_zoom_class = document.getElementById('hidden_zoom_class');
	hidden_zoom_class.value = i;
	
	var div_images_zones = document.getElementById('image_zones').useMap="#"+i;
	
	if(i == 1){
		map_zoom_in_img.src = "images/legendes/legende_zoom_loupe_plus.png";
		map_zoom_in_img.onclick = Function("map_zoom(" + (i + 1) + ",\"in\")");
		//picto_zoom_2.onclick = Function("map_zoom(" + (i + 1) + ",\"in\")");
		map_zoom_out_img.src = "images/legendes/legende_zoom_loupe_pmoins.png";
		map_zoom_out_img.onclick = Function("");
		
		div_carto_pictos_hotel.style.cursor = "default";
		
		//picto_zoom_1.onclick = Function("");
		
	}
	else if(i == zoom_max){
		
		map_zoom_in_img.src = "images/legendes/legende_zoom_loupe_plus.png";
		map_zoom_in_img.onclick = Function("");
		//picto_zoom_2.onclick = Function("");
		map_zoom_out_img.src = "images/legendes/legende_zoom_loupe_pmoins.png";
		map_zoom_out_img.onclick = Function("map_zoom(" + (i - 1) + ",\"out\")");
		//picto_zoom_1.onclick = Function("map_zoom(" + (i - 1) + ",\"out\")");
		div_carto_pictos_hotel.style.cursor = "move";
		div_carto_pictos_hotel.ondblclick = Function("");
	}
	else{
		
		map_zoom_in_img.src = "images/legendes/legende_zoom_loupe_plus.png";
		map_zoom_in_img.onclick = Function("map_zoom(" + (i + 1) + ",\"in\")");
		map_zoom_out_img.src = "images/legendes/legende_zoom_loupe_pmoins.png";
		map_zoom_out_img.onclick = Function("map_zoom(" + (i - 1) + ",\"out\")");
		
	}
	//map_zoom_class_curseur.style.left = (i-1)*12 + "px";
	//map_zoom_class_img.src = root_path + "/images/map/legende_boutonzoom" + i + ".gif";
	
	var count_picto = 0;
	var count_picto_big = 0;
	if(document.getElementById('hidden_count_picto')) count_picto = document.getElementById('hidden_count_picto').value;
	if(document.getElementById('hidden_count_picto_big')) count_picto_big = document.getElementById('hidden_count_picto_big').value;
	var img_hotel = "";
	var img_hotel_big = "";
	var div_carto_pictos_hotel_pop = "";
	if(act == "in"){
		
		map_width_base = tab_image_size[i][0];
		map_height_base = tab_image_size[i][1];
		
		map_style_left = parseInt(div_result_map_content.style.left);
		map_style_top = parseInt(div_result_map_content.style.top);
		
		map_style_left = 0-(map_width_base - map_window_width)/2;
		map_style_top = 0-(map_height_base - map_window_height)/2-20;
		
		
		// Gets the ratio in order to place the pictos
		
		if(document.getElementById('hidden_count_picto')){
			for(var j = 1;j < count_picto+1;j++){
				if(document.getElementById('img_hotel_'+j)){
					img_hotel = document.getElementById('img_hotel_'+j);
					var lambert_x = pop_layer_content_arr[j][11];
					var lambert_y = pop_layer_content_arr[j][12];
					var position_y = posymin[i]+posdy*(y_max-lambert_y)/dy-22;
					var position_x = posxmin[i]+posdx*(lambert_x-x_min)/dx-22;
					img_hotel.style.left = position_x + "px";
					img_hotel.style.top = position_y + "px";
				}
			}
		}
		if(document.getElementById('hidden_count_picto_big')){
			
			for(var m = 0;m < count_picto_big;m++){
				
				img_hotel_big = document.getElementById('img_hotel_big_'+m);
				img_hotel_big.style.left = parseInt((parseInt(img_hotel_big.style.left)+22)*ratioZoomW)-22 + "px";
				img_hotel_big.style.top = parseInt((parseInt(img_hotel_big.style.top)+22)*ratioZoomH)-22 + "px";
			}
		}
		
	}
	else if(act == "out"){
		
		map_width_base = tab_image_size[i][0];
		map_height_base = tab_image_size[i][1];
		
		map_style_left = parseInt(div_result_map_content.style.left);
		map_style_top = parseInt(div_result_map_content.style.top);
		
		
		map_style_left = 0-(map_width_base - map_window_width)/2;
		map_style_top = 0-(map_height_base - map_window_height)/2-20;
		
		
		// Gets the ratio in order to place the pictos
		var ratioZoomW = getRatio(current_zoom,i,"w");
		var ratioZoomH = getRatio(current_zoom,i,"h");

		
		if(document.getElementById('hidden_count_picto')){
			
			for(var k = 0;k < count_picto+1;k++){
				if(document.getElementById('img_hotel_'+k)){
					img_hotel = document.getElementById('img_hotel_'+k);
					var lambert_x = pop_layer_content_arr[k][11];
					var lambert_y = pop_layer_content_arr[k][12];
					var position_y = posymin[i]+posdy*(y_max-lambert_y)/dy-22;
					var position_x = posxmin[i]+posdx*(lambert_x-x_min)/dx-22;
					img_hotel.style.left = position_x + "px";
					img_hotel.style.top = position_y + "px";
				}
			}
		}
		if(document.getElementById('hidden_count_picto_big')){
			for(var q = 0;q < count_picto_big;q++){
				
				img_hotel_big = document.getElementById('img_hotel_big_'+q);
				img_hotel_big.style.left = (parseInt(img_hotel_big.style.left)+22)*ratioZoomW-22 + "px";
				img_hotel_big.style.top = (parseInt(img_hotel_big.style.top)+22)*ratioZoomH-22 + "px";
			}
		}
		
		//generateXml();
		
	}
	
	div_result_map_content.style.left = map_style_left + "px";
	div_result_map_content.style.top = map_style_top + "px";
	
	// Mets la sous-région sélectionnée en surbrillance
	if(sous_region_select>0){
		document.getElementById("div_sous_region"+sous_region_select).style.display = "block";
		if(act=="in"){
			var coeff_x = 1;
			var coeff_y = 1;
			if (i=="3") { coeff_x = 2.4; coeff_y = 2.7;}
			move_to_x = coeff_x*correspondances_xy_sous_regions[sous_region_select][0];
			move_to_y = coeff_y*correspondances_xy_sous_regions[sous_region_select][1];
		}
	}
	
	if(move_to_x!=null && move_to_y!=null){
		div_result_map_content.style.left = move_to_x + "px";
		div_result_map_content.style.top = move_to_y + "px";
		move_to_x = null;
		move_to_y = null;
	}
	
	div_result_map_content.style.width = map_width_base + "px";
	div_result_map_content.style.height = map_height_base + "px";
	
	div_carto_pictos_hotel.style.width = map_width_base + "px";
	div_carto_pictos_hotel.style.height = map_height_base + "px";
	
	div_carto_map.style.width = map_width_base + "px";
	div_carto_map.style.height = map_height_base + "px";
	//div_carto_map.style.backgroundImage = "url('" + root_path + "/images/map/zoom" + i + "/ZOOM" + i + "_fond.jpg')";
	
	div_carto_routes.style.width = map_width_base + "px";
	div_carto_villes.style.width = map_width_base + "px";
	div_carto_region.style.width = map_width_base + "px";
	div_carto_coursdeau.style.width = map_width_base + "px";
	
	div_carto_routes.style.height = map_height_base + "px";
	div_carto_villes.style.height = map_height_base + "px";
	div_carto_region.style.height = map_height_base + "px";
	div_carto_coursdeau.style.height = map_height_base + "px";
	
	var echelle = document.getElementById("div_echelle");
	echelle.innerHTML = '<img src="images/map/legende_echelle_z'+i+'.png" border="0" alt="" title="" />';
	
	var url_img = root_path + "/images/map/zoom" + i + "/ZOOM" + i;
	var rep_zoom = root_path + "/images/map/zoom" + i + "/";
	
	
	var largeur_image = 0;
	var hauteur_image = 0;
	if(i==1){
		largeur_image = 987;
		hauteur_image = 524;
	}
	if(i==2){
		largeur_image = 1975;
		hauteur_image = 1049;
	}
	if(i==3){
		largeur_image = 3954;
		hauteur_image = 2098;
	}
	
	div_carto_routes.innerHTML = '<img src="'+url_img+'_routes.png" border="0" alt="" title="" width="'+largeur_image+'" height="'+hauteur_image+'" />';
	div_carto_villes.innerHTML = '<img src="'+url_img+'_villes.png" border="0" alt="" title="" width="'+largeur_image+'" height="'+hauteur_image+'" />';
	div_carto_region.innerHTML = '<img src="'+url_img+'_microregions.png" border="0" alt="" title="" width="'+largeur_image+'" height="'+hauteur_image+'" />';
	div_carto_coursdeau.innerHTML = '<img src="'+url_img+'_coursdeau.png" border="0" alt="" title="" width="'+largeur_image+'" height="'+hauteur_image+'" />';

	
	for(var j=1;j<10;j++){
		document.getElementById("div_sous_region"+j).innerHTML = '<img src="'+url_img+'_microregion'+j+'.png" border="0" alt="" title="" />';;
	}
	

	updatePNG();
	
	document.getElementById('div_hotel_pop_layer').style.display = "none";
	
	current_zoom = i;
	IDfr.map.split(i);	
}


// Fonction permettant de zoomer puis de centrer sur un point en X,Y
function map_zoom_and_move(i,act,evn,x,y,select){
	var regions = document.getElementById("div_carto_region");
	if(regions.style.display=="block"){
		move_to_x = x;
		move_to_y = y;
		sous_region_select = select;
		map_zoom(i,act,evn);
		select_sous_region(select);
	}
}

// Déplace la carte en X,Y
function move_map_to(x,y){
	var div_to_move = document.getElementById("div_result_map_content");
	div_to_move.style.top = y+"px";
	div_to_move.style.left = x+"px";
}


//
// DIV Legend
//
var to_hidelegend;
function display_legend_window(){
	if(to_hidelegend){
		clearTimeout(to_hidelegend);
	}
	var legend_window = document.getElementById("legend_window");
	legend_window.style.display = "block";
	
	document.getElementById("map_zoom_legend_calques").style.backgroundColor = "#FFFFFF";
	document.getElementById("map_zoom_legend_calques").style.filter = "Alpha(opacity=80)";
	document.getElementById("map_zoom_legend_calques").style.opacity = "0.8";
	
	hide_infos_window();
	
}
function hide_legend_window(){
	var legend_window = document.getElementById("legend_window");
	legend_window.style.display = "none";
	document.getElementById("map_zoom_legend_calques").style.backgroundColor = "";
}

function hide_legend_window_with_timeout(){
	to_hidelegend = setTimeout(hide_legend_window,500);
}
function over_legend_window(){
	clearTimeout(to_hidelegend);
}


//
// DIV Infos
//
var to_hideinfos;
function display_infos_window(){
	if(to_hideinfos){
		clearTimeout(to_hideinfos);
	}
	var infos_window = document.getElementById("infos_window");
	infos_window.style.display = "block";
	hide_legend_window();
}
function hide_infos_window(){
	var infos_window = document.getElementById("infos_window");
	infos_window.style.display = "none";
}

function hide_infos_window_with_timeout(){
	to_hideinfos = setTimeout(hide_infos_window,500);
}
function over_infos_window(){
	clearTimeout(to_hideinfos);
}





function change_image_mouseover(img_id,img_name,img_type,img_route){
	img_id = document.getElementById(img_id);
	img_id.src = img_route + "/" +img_name + "_over." + img_type;
}
function change_image_mouseout(img_id,img_name,img_type,img_route){
	img_id = document.getElementById(img_id);
	img_id.src = img_route + "/" +img_name + "." + img_type;
}

function picto_mouseover(em){
	em.style.cursor = "pointer";
}

var x,y;
function position(e) {
	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
}

if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = position;

// Timeout pour la resélection de la sous-région préselectionnée
var timeout_reselect;

function over_microregion(name){
	var divlegend = document.getElementById("legende_microregion");
	var contenu = document.getElementById("contenu_legende_microregion");
	var glob = document.getElementById("div_result_map");
	var regions = document.getElementById("div_carto_region");
	
	if(divlegend && regions.style.display=="block"){
		divlegend.style.top = (y+2)+"px";
		divlegend.style.left = (x+15)+"px";
		
		if(isNav!="net"){
			divlegend.style.top = (y+2+glob.offsetTop)+"px";
			divlegend.style.left = (x+10+glob.offsetLeft)+"px";
		}
		divlegend.style.display = "block";
		contenu.innerHTML = name;
	}
	
	var num = 1;
	
	switch(name){
		case "Aravis":
			num = 7;
		break;
		case "Lac d'Annecy":
			num = 8;
		break;
		case "Albanais - Pays du Rhône":
			num = 9;
		break;
		case "Genevois Haut Savoyard":
			num = 4;
		break;
		case "Pays du Léman - Vallée Verte":
			num = 1;
		break;
		case "Haut-Chablais - Portes du Soleil":
			num = 2;
		break;
		case "Grand Massif":
			num = 5;
		break;
		case "Faucigny - Moyenne Vallée de l'Arve":
			num = 3;
		break;
		case "Pays du Mont-Blanc":
			num = 6;
		break;
	}
	
	
	for(var i=1;i<10;i++){
		if(i==num){
			document.getElementById("div_sous_region"+num).style.display = "block";
		}else{
			document.getElementById("div_sous_region"+i).style.display = "none";
		}
	}
	
	clearTimeout(timeout_reselect);
	
}

function out_microregion(){
	var divlegend = document.getElementById("legende_microregion");
	if(divlegend){
		divlegend.style.display = "none";
	}
	
	for(var i=1;i<10;i++){
		if(i!=sous_region_select){
			document.getElementById("div_sous_region"+i).style.display = "none";
		}
	}
	
	timeout_reselect = setTimeout("reselectionner_sous_region()",100);
	
}


function reselectionner_sous_region(){
	if(sous_region_select>0){
		document.getElementById("div_sous_region"+sous_region_select).style.display = "block";
	}
	clearTimeout(timeout_reselect);
}


// Selectionne la sous-région et modifie l'option sélectionnée dans le formulaire
function select_sous_region(num){
	sous_region_select = num;
	for(var i=1;i<10;i++){
		document.getElementById("div_sous_region"+i).style.display = "none";
	}
	var a_afficher = document.getElementById("div_sous_region"+sous_region_select);
	if(a_afficher){
		a_afficher.style.display = "block";
	}
	
	var id = correspondances_id_sous_regions_inv[num];
	// On récupère le select qui contient les sous-régions
	var selects = document.getElementsByTagName("select");
	var select = document.getElementById(selects[0].id);
	
	if(select){
		select.selectedIndex = 0;
		for(var i=0;i<select.length;i++){
			var option = select[i];
			if(option.value==id && id>0){
				select.selectedIndex = i;
			}
		}
	}
}

// Returns the ratio between widths of both zooms passed as parameters
function getRatio(a,b,type){
	var indice = 0;
	if(type == "h"){
		indice = 1;
	}
	return tab_image_size[b][indice]/tab_image_size[a][indice];
}


/*******************************************************************************
* Modification CR
* Background splitting + viewport
*******************************************************************************/
IDfr = {};
IDfr.map = {
	'grid' : {
		1 : {'y' : 3, 'x' : 4, 'width': 247, 'height': 247},
		2 : {'y' : 4, 'x' : 7, 'width': 247, 'height': 247},
		3 : {'y' : 8, 'x' : 16, 'width': 247, 'height': 247}
	},
	'lvl' : 1,
	'split': function(lvl, callback) {
		
		this.lvl = (typeof lvl == 'undefined') ? this.lvl : lvl;
		
		var x = this.grid[this.lvl].y;
		var y = this.grid[this.lvl].x;
		var width = this.grid[this.lvl].width;
		var height = this.grid[this.lvl].height;
		
		var html = '';
		
		for(var xx = 0; xx < x; xx++) {
			for(var yy = 0; yy < y; yy++) {					
				html += '<div id="map_'+xx+'_'+yy+'" class="mapRemover" style="float:left;width:'+this.grid[this.lvl].width+'px;height: '+this.grid[this.lvl].height+'px"></div>';
			}
		}
		
		$j('#div_carto_map')
			.html(html)
			.css({
				'width': 247 * y,
				'height': 247 * x
			});
			
			
		this.viewport();	
	},

	'viewport' : function() {
		var viewport = $j('#div_result_map_content');
		var pos = viewport.position();
		var view = false;
		
		var t = {};
		t.top = pos.top * (-1);
		t.left = pos.left * (-1);
		
		var point1 = {};
		point1.left = Math.floor(t.left / this.grid[this.lvl].width);
		point1.top = Math.floor(t.top / this.grid[this.lvl].height);
		
		var point2 = {};
		point2.left = Math.ceil((t.left + $j('#div_result_map').width()) / this.grid[this.lvl].width);
		point2.top = Math.ceil((t.top + $j('#div_result_map').height()) / this.grid[this.lvl].height);
		
		$j('.mapRemover').css('background','');
		for(var x = point1.top; x <= point2.top; x++) {			
			for(var y = point1.left; y <= point2.left; y++) {				
				$j('#map_'+x+'_'+y).css('background','url(/module/carto/images/map/zoom'+this.lvl+'/'+x+'_'+y+'.jpg) top left');
			}
		}
	}		
};

$j(window).load(function(){
	IDfr.map.split(1);
});