/* TXCarto Js */

$(document).ready(function () {
	txc_loadmap();
	txc_loadlayers(true);
	
	$('#txcarto_legende_btn').click(function(){
		$('#txcarto_legende').hide();
		$('#txcarto_legende_open').show();
		$('#txcarto_map').css('width','927px');
	});
	
	$('#txcarto_legende_open').click(function(){
		$('#txcarto_map').css('width','747px');
		$('#txcarto_legende_open').hide();
		$('#txcarto_legende').show();
	});
	
	setTimeout("reinitialiseScrollPane('#txcarto_layers');",100);
	//setTimeout("txc_loadlayers();",100);
});

function showAddress(address) {
	var reg = /dijon/gi;
	if (!reg.test(address)){
		var reg = /21000/gi;
		if (!reg.test(address)){ address += ' 21000 dijon'; }
		else { address += ' dijon'; }
	}
	if (geocoder) {
		geocoder.getLatLng(address,
			function(point) {
			  if (!point) {
				alert(address + " : adresse non trouvée");
			  } else {
				map.setCenter(point, 17);
			  }
			}
		);
	}
}

function tracerEvenementOnOpenFn(id){
	$('#tabs'+id).tabs();
	$('#txcarto_ix_close'+id).click(function(){
		if(currentWindow){ map.removeOverlay(currentWindow); }
	});
	reinitialiseScrollPane('#fragment'+id+'-1-s');
	$('#tabs'+id).bind('tabsshow', function(event, ui) {
		reinitialiseScrollPane('#'+ui.panel.id+'-s');
	});
}

function txc_layers_toggle(){

	jQuery('#txcarto_layers').toggle('fast');
	if(jQuery('#txcarto_layers_img').attr('src')=='/assets/snippets/txcarto/images/btn_open_on.png'){ jQuery('#txcarto_layers_img').attr('src','/assets/snippets/txcarto/images/btn_open_off.png'); }
	else { jQuery('#txcarto_layers_img').attr('src','/assets/snippets/txcarto/images/btn_open_on.png') }
	return false;
	
}

function txc_pointer(coor){

	var thiscoor = coor.split('||');
	var point = thiscoor[0].split(',');
	var zoom = thiscoor[1];
	var centerLatLng = new GLatLng(point[0],point[1]);
	map.setCenter(centerLatLng, parseInt(zoom));

}

function txc_loadlayers(reloaddata) {
	var txaFormData =  $("#txcarto_layers_form").serialize();
	//if(debug=='true'){ jQuery('#txcarto_debug').html('<pre>'+txaFormData+'</pre>'); }
	
	if(reloaddata){ 
	
	jQuery('#txcarto_loading').css('opacity',0.4);
    jQuery('#txcarto_loading').css('filter','alpha(opacity=40)'); 

	jQuery('#txcarto_loading').fadeIn('fast', function(){
	jQuery('#txcarto_loading_int').fadeIn('fast', function(){
	
	$.ajax({
		type: 'POST',
		url: txcarto_ajaxurl,
		data: txaFormData,
		async: false,
		timeout : 5000,
		success : function(text)		
		{
			layersdatasjs = text;
		}
	});
	
	if(debug=='true'){ jQuery('#txcarto_debug').html('<pre>'+layersdatasjs+'</pre>'); }
	//jQuery('#txcarto_debug').html('<pre>'+layersdatasjs+'</pre>');
	
	try {
		carto_datas	= new Array();
		eval(layersdatasjs);
	}
	catch(error){
		alert('erreur ajax !'+error);
	}
	
	markers = new Array();
	
	nbd = carto_datas.length;
	nba=0;
	for(d=0;d<nbd;d++){
		if(carto_datas[d]["iselements"]=="true"){
		nbi = carto_datas[d]["elements"].length;

		for(i=0;i<nbi;i++){
			if(carto_datas[d]["elements"][i]["maxzoom"]<carto_datas[d]["elements"][i]["minzoom"]){ carto_datas[d]["elements"][i]["maxzoom"] = carto_datas[d]["elements"][i]["minzoom"];}

			if(carto_datas[d]["elements"][i]["template"]==txcarto_templates["image"]){
				markers[nba] = txc_loadimage(carto_datas[d]["elements"][i]["coor"], carto_datas[d]["elements"][i]["url"]);
				markers[nba]["minzoom"] =carto_datas[d]["elements"][i]["minzoom"];
				markers[nba]["maxzoom"] =carto_datas[d]["elements"][i]["maxzoom"];
				
				nba++;
			}
			else if(carto_datas[d]["elements"][i]["template"]==txcarto_templates["point"]){
				markers[nba] = txc_loadpoint(carto_datas[d]["elements"][i]);
				markers[nba]["minzoom"] =carto_datas[d]["elements"][i]["minzoom"];
				markers[nba]["maxzoom"] =carto_datas[d]["elements"][i]["maxzoom"];
				
				nba++;
			}
			else {
				if(carto_datas[d]["elements"][i]["iselements"]=="true"){
				nbj = carto_datas[d]["elements"][i]["elements"].length;
				for(j=0;j<nbj;j++){
					if(carto_datas[d]["elements"][i]["elements"][j]["maxzoom"]<carto_datas[d]["elements"][i]["elements"][j]["minzoom"]){ carto_datas[d]["elements"][i]["elements"][j]["maxzoom"] = carto_datas[d]["elements"][i]["elements"][j]["minzoom"];}
						if(carto_datas[d]["elements"][i]["elements"][j]["template"]==txcarto_templates["image"]){
							markers[nba] = txc_loadimage(carto_datas[d]["elements"][i]["elements"][j]["coor"], carto_datas[d]["elements"][i]["elements"][j]["url"]);
							markers[nba]["minzoom"] =carto_datas[d]["elements"][i]["elements"][j]["minzoom"];
							markers[nba]["maxzoom"] =carto_datas[d]["elements"][i]["elements"][j]["maxzoom"];
	
							nba++;
						}
						else if(carto_datas[d]["elements"][i]["elements"][j]["template"]==txcarto_templates["point"]){
							markers[nba] = txc_loadpoint(carto_datas[d]["elements"][i]["elements"][j]);
							markers[nba]["minzoom"] =carto_datas[d]["elements"][i]["elements"][j]["minzoom"];
							markers[nba]["maxzoom"] =carto_datas[d]["elements"][i]["elements"][j]["maxzoom"];

							nba++;
						}
						else {

						}
					}
				}
			}

			}
		}
	}
	
	map.clearOverlays();
	nbm = markers.length;
	currentzoom = map.getZoom();
	for(m=0;m<nbm;m++){
		
		if(currentzoom>=markers[m]["minzoom"] && currentzoom<=markers[m]["maxzoom"]){
			
			try { 
				Omarkers[m] = markers[m]["marker"]; 
				map.addOverlay(markers[m]["marker"]);
			}
			catch(error){  }
		}

	}
	
	jQuery('#txcarto_loading_int').fadeOut('fast', function(){
	jQuery('#txcarto_loading').fadeOut('fast');
	});
	
	});	
	});
	
	}
	else {

	var debughtml = ">> ";
	nbm = markers.length;
	if(currentWindow){ map.removeOverlay(currentWindow); }
	currentzoom = map.getZoom();
	for(m=0;m<nbm;m++){
		if(currentzoom>=markers[m]["minzoom"] && currentzoom<=markers[m]["maxzoom"]){
			if(Omarkers[m]){
				debughtml = debughtml+"dejà affiché : "+m;
				try { 
					map.removeOverlay(Omarkers[m]);
					map.addOverlay(markers[m]["marker"]);
				}
				catch(error){  }
			}
			else {
				debughtml = debughtml+"de nouveau affiché : "+m;
				try { 
					Omarkers[m] = markers[m]["marker"]; 
					map.addOverlay(markers[m]["marker"]);
				}
				catch(error){  }
			}
		}
		else {
			if(Omarkers[m]){
				try { 
					debughtml = debughtml+"retiré : "+m;
					map.removeOverlay(Omarkers[m]); 
					delete Omarkers[m];
				}
				catch(error){  }
			}
		}
	}
	
	if(debug=='true'){ jQuery('#txcarto_debug').html('<pre>'+debughtml+'</pre>'); }

	}

}

function txc_loadpoint(datas){

if(datas["coor"]!='' && jQuery.trim(datas["coor"])!=''){
	var point = new Array();
	var thiscoor = datas["coor"].split('||');
	var point = thiscoor[0].split(',');
	var width = thiscoor[2];
	var height = thiscoor[3];
	var centerLatLng = new GLatLng(point[0],point[1]);
	
	if(datas["url"]!='') {
		icon_move = new GIcon();
		icon_move.image = datas["url"];
		icon_move.shadow = '';
		icon_move.iconSize = new GSize(32, 32);
		//icon_move.shadowSize = new GSize(150, 150);
		//icon_move.iconSize = new GSize(width, height);
		//icon_move.shadowSize = new GSize(width, height);
		icon_move.iconAnchor = new GPoint((32/2), (32/2));
		icon_move.dragCrossImage = '';
		icon_move.infoWindowAnchor = new GPoint((32/2), (32/2));
		var options = new Array();
		options['icon'] = icon_move;
		var marker = new GMarker( centerLatLng, options) ;
	} 
	else {
		icon_move = new GIcon();
		icon_move.image = 'http://imagine-it.org/google/marker_move_icon.png';
		icon_move.shadow = '';
		icon_move.iconSize = new GSize(width, height);
		icon_move.shadowSize = new GSize(width, height);
		icon_move.iconAnchor = new GPoint((width/2), (height/2));
		icon_move.dragCrossImage = '';
		icon_move.infoWindowAnchor = new GPoint((width/2), (height/2));
		var options = new Array();
		options['icon'] = icon_move;
		var marker = new GMarker( centerLatLng, options) ;
	}

	var html = '<div id="tabs'+datas["id"]+'" class="txcarto_iw_header">';
	html += '<ul>';
	if(jQuery.trim(datas["html_infos"])!='' && jQuery.trim(datas["html_infos"])!=' '){ html += '<li><a href="#fragment'+datas["id"]+'-1"><span>'+txcarto_tab_1+'</span></a></li>'; }
	if(jQuery.trim(datas["html_mediateurs"])!='' && jQuery.trim(datas["html_mediateurs"])!=' '){ html += '<li><a href="#fragment'+datas["id"]+'-2"><span>'+txcarto_tab_2+'</span></a></li>'; }
	if(jQuery.trim(datas["html_contacts"])!='' && jQuery.trim(datas["html_contacts"])!=' '){ html += '<li><a href="#fragment'+datas["id"]+'-3"><span>'+txcarto_tab_3+'</span></a></li>'; }
	html += '</ul><div class="txcarto_iw_fond"><div class="txcarto_iw_fond_int"><div id="txcarto_ix_close'+datas["id"]+'" class="txcarto_ix_close">&nbsp;</div>';
	if(jQuery.trim(datas["html_infos"])!='' && jQuery.trim(datas["html_infos"])!=' '){ html += '<div style="padding:10px 20px 10px 0px; height:210px; overflow:auto; " id="fragment'+datas["id"]+'-1"><div style="width:100%; height:206px;" id="fragment'+datas["id"]+'-1-s" class="scroll-pane">'+datas["html_infos"]+'</div></div>'; }
	if(jQuery.trim(datas["html_mediateurs"])!='' && jQuery.trim(datas["html_mediateurs"])!=' '){ html += '<div style="padding:10px 20px 10px 0px; height:210px; overflow:auto; " id="fragment'+datas["id"]+'-2"><div style="width:100%; height:206px;" id="fragment'+datas["id"]+'-2-s" class="scroll-pane">'+datas["html_mediateurs"]+'</div></div>'; }
	if(jQuery.trim(datas["html_contacts"])!='' && jQuery.trim(datas["html_contacts"])!=' '){ html += '<div style="padding:10px 20px 10px 0px; height:210px; overflow:auto; " id="fragment'+datas["id"]+'-3"><div style="width:100%; height:206px;" id="fragment'+datas["id"]+'-3-s" class="scroll-pane">'+datas["html_contacts"]+'</div></div>'; }
	html += '</div></div></div>';
	
	if(( jQuery.trim(datas["html_infos"])!='' && jQuery.trim(datas["html_infos"])!=' ' ) 
		||	( jQuery.trim(datas["html_mediateurs"])!='' && jQuery.trim(datas["html_mediateurs"])!=' ' )
		||	( jQuery.trim(datas["html_contacts"])!='' && jQuery.trim(datas["html_contacts"])!=' ' )
	){
		GEvent.addListener(marker, 'click', function() {
			var t = map.getCurrentMapType().getName();
			if(currentWindow){ map.removeOverlay(currentWindow); }
			if(t=='Mixte 3D' || t=='Earth'){
				if(showstreet){
					map.setMapType(G_HYBRID_MAP);
				}
				else {
					map.setMapType(G_SATELLITE_MAP);
				}
				$('#txcarto_btn_plan').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
				$('#txcarto_btn_plan').css('color', '#dc4fa8');
				$('#txcarto_btn_satellite').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_on.png)');
				$('#txcarto_btn_satellite').css('color', '#a81c68');
				$('#txcarto_btn_earth').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
				$('#txcarto_btn_earth').css('color', '#dc4fa8');
			}
			map.panTo(centerLatLng);
			currentWindow = new Infowin(centerLatLng, html);
			map.addOverlay(currentWindow);
			tracerEvenementOnOpenFn(datas["id"]);
		});

		GEvent.addListener(marker, 'mouseout', function() {
			markerout = GEvent.addListener(map, "click", function() {
				if(currentWindow){ map.removeOverlay(currentWindow); }
			});
		});
		
		GEvent.addListener(marker, 'mouseover', function() {
			if(markerout){ GEvent.removeListener(markerout); }
			var t = map.getCurrentMapType().getName();
			if(currentWindow){ map.removeOverlay(currentWindow); }
			if(t=='Mixte 3D' || t=='Earth'){
				if(showstreet){
					map.setMapType(G_HYBRID_MAP);
				}
				else {
					map.setMapType(G_SATELLITE_MAP);
				}
				$('#txcarto_btn_plan').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
				$('#txcarto_btn_plan').css('color', '#dc4fa8');
				$('#txcarto_btn_satellite').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_on.png)');
				$('#txcarto_btn_satellite').css('color', '#a81c68');
				$('#txcarto_btn_earth').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
				$('#txcarto_btn_earth').css('color', '#dc4fa8');
			}
			currentWindow = new Infowin(centerLatLng, html);
			map.addOverlay(currentWindow);
			tracerEvenementOnOpenFn(datas["id"]);
		});
	
	}

	point["marker"] = marker;
	return point;
}
else {
	return false;
}
	
}

function txc_loadimage(coor, url){

if(coor!='' && jQuery.trim(coor)!='' && url!='' && jQuery.trim(url)!=''){
	var marker = new Array();
	var thiscoor = coor.split('||');
	var coortab = new Array();
	coortab['newSW'] = thiscoor[1].split(',');
	coortab['newNE'] = thiscoor[2].split(',');
	var newSW = new GLatLng(coortab['newSW'][0],coortab['newSW'][1]);
	var newNE = new GLatLng(coortab['newNE'][0],coortab['newNE'][1]);
	var newBounds = new GLatLngBounds(newSW, newNE);
	
	marker["NE"] = new GMarker(newBounds.getNorthEast(), {clickable: false, icon: icon_NE});
	marker["SW"] = new GMarker(newBounds.getSouthWest(), {clickable: false, icon: icon_SW});
	marker["move"] = new GMarker( new GLatLng(((marker["SW"].getPoint().lat() + marker["NE"].getPoint().lat()) / 2),((marker["NE"].getPoint().lng() + marker["SW"].getPoint().lng()) / 2)), {clickable: false, icon: icon_move}) ;
	marker["marker"] = new GGroundOverlay ( 
		url,
		new GLatLngBounds(marker["SW"].getPoint(), marker["NE"].getPoint())
	);

	return marker;
}
else {
	return false;
}
	
}

function txc_loadmap(){
	map = new google.maps.Map2(document.getElementById("txcarto_map"));
	//map.addMapType(G_SATELLITE_3D_MAP);

	map.setMapType(G_SATELLITE_MAP);	

	var centerLatLng = new GLatLng(47.321836, 5.041823);
	map.setCenter(centerLatLng, 13);

	var mapui = map.getDefaultUI();
	mapui.maptypes.physical = false;
	map.setUI(mapui);	
	
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
	map.continuousZoomEnabled();

	map.addControl(new GLargeMapControl3D());
	//map.enableScrollWheelZoom();
	
	//myoverview = new google.maps.OverviewMapControl();
	//myoverview.setMapType(G_NORMAL_MAP);
    	//map.addControl(myoverview);
	//map.addControl(new google.maps.OverviewMapControl());
	
	//mycontrol = new MakeMyControl(map);
	//map.addControl(mycontrol);
	
	//map.addOverlay(new GStreetviewOverlay());
	
	icon_NE = new GIcon(); 
	icon_NE.image = "http://imagine-it.org/google/marker_NE_icon.png";
	icon_NE.shadow = "";
	icon_NE.iconSize = new GSize(32, 32);
	icon_NE.shadowSize = new GSize(22, 20);
	icon_NE.iconAnchor = new GPoint(22, 10);
	icon_NE.dragCrossImage = "";

	icon_SW = new GIcon(); 
	icon_SW.image = "http://imagine-it.org/google/marker_SW_icon.png";
	icon_SW.shadow = "";
	icon_SW.iconSize = new GSize(32, 32);
	icon_SW.shadowSize = new GSize(22, 20);
	icon_SW.iconAnchor = new GPoint(6, 20);
	icon_SW.dragCrossImage = "";

	icon_move = new GIcon();
	icon_move.image = "http://imagine-it.org/google/marker_move_icon.png";
	icon_move.shadow = "";
	icon_move.iconSize = new GSize(32, 32);
	icon_move.shadowSize = new GSize(6, 20);
	icon_move.iconAnchor = new GPoint(6, 20);
	icon_move.dragCrossImage = '';

	geocoder = new GClientGeocoder();
	
	GEvent.addListener(map, 'maptypechanged', function() {
		var t = map.getCurrentMapType().getName();
		if(t=='Earth' || t=='Mixte 3D'){ 

		}
		else{ 

		}
		txc_loadlayers(false);
	});
	
	GEvent.addListener(map, 'zoomend', function() {
		var t = map.getCurrentMapType().getName();
		if(t=='Earth' || t=='Mixte 3D'){ 

		}
		else{ 

		}
		txc_loadlayers(false);
	});
		
	$('#txcarto_btn_plan').click( function(){
		map.setMapType(G_NORMAL_MAP);
		$('#txcarto_btn_plan').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_on.png)');
		$('#txcarto_btn_plan').css('color', '#a81c68');
		$('#txcarto_btn_satellite').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_satellite').css('color', '#dc4fa8');
		$('#txcarto_btn_earth').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_earth').css('color', '#dc4fa8');
	});
	
	$('#txcarto_btn_satellite').click( function(){
		if(showstreet){
			map.setMapType(G_HYBRID_MAP);
		}
		else {
			map.setMapType(G_SATELLITE_MAP);
		}
		$('#txcarto_btn_plan').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_plan').css('color', '#dc4fa8');
		$('#txcarto_btn_satellite').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_on.png)');
		$('#txcarto_btn_satellite').css('color', '#a81c68');
		$('#txcarto_btn_earth').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_earth').css('color', '#dc4fa8');
	});
	
	$('#txcarto_btn_earth').click( function(){
		if(showstreet){
			map.setMapType(G_HYBRID_3D_MAP);
		}
		else {
			map.setMapType(G_SATELLITE_3D_MAP);
		}
		$('#txcarto_btn_plan').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_plan').css('color', '#dc4fa8');
		$('#txcarto_btn_satellite').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_off.png)');
		$('#txcarto_btn_satellite').css('color', '#dc4fa8');
		$('#txcarto_btn_earth').css('background-image', 'url(/assets/snippets/txcarto/images/txcarto_legende_btn_on.png)');
		$('#txcarto_btn_earth').css('color', '#a81c68');
	});
	
	$('#showstreet').click( function(){
		var t = map.getCurrentMapType().getName();
		if(showstreet){ 
			if(t=='Mixte 3D'){ map.setMapType(G_SATELLITE_3D_MAP); }
			if(t=='Mixte'){ map.setMapType(G_SATELLITE_MAP); }
			showstreet = false;
		}
		else { 
			if(t=='Earth'){ map.setMapType(G_HYBRID_3D_MAP); }
			if(t=='Satellite'){ map.setMapType(G_HYBRID_MAP); }
			showstreet = true;
		}
		return false;
	});
	
	$("input:checkbox").each( function() {
		//jQuery('#txcarto_debug').html(jQuery('#txcarto_debug').html()+'<pre> test : '+this.checked+'</pre>');
		(this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
	});
	// function to 'check' the fake ones and their matching checkboxes
	$(".fakecheck").click(function(){
		($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
		$(this.hash).trigger("click");
		setTimeout("txc_loadlayers(true);",100);
		setTimeout("reinitialiseScrollPane('#txcarto_layers');",100);
		return false;
	});	
}

function reinitialiseScrollPane(id) {
	$(id).jScrollPane({scrollbarWidth:8, scrollbarMargin:4});
}

function MakeMyControl(map) {
    this.map = map;
    this.button = document.createElement("div");
}

MakeMyControl.prototype = new GControl();

MakeMyControl.prototype.initialize = function() {
    var map = this.map;
	
	var mycontrol = '';
	mycontrol += '<div id="txcarto_btn_plan"><div class="btn_int">PLAN</div></div>';
	mycontrol += '<div id="txcarto_btn_satellite"><div class="btn_int">SATELLITE</div></div>';
//	mycontrol += '<div id="txcarto_btn_hybride"><div class="btn_int">HYBRIDE</div></div>';
	//mycontrol += '<div id="txcarto_btn_earth"><div class="btn_int">3D</div></div>';
//	mycontrol += '<div style="width:100%; float:left; font-size:0pt;">&nbsp;</div>';
//	mycontrol += '<a href="#showstreet" class="fakecheck" id="fakeshowstreet" style="float:left; margin:0px 0px 0px 2px;">afficher le nom des rues</a>';
//	mycontrol += '<input type="checkbox" name="showstreet" id="showstreet" style="display:none"/>';
	mycontrol += '';

    this.button.className = "txcarto_map_btn";
    //this.button.href = "javascript:;";
    //this.button.title = "Move the map to the top";
    this.button.innerHTML = mycontrol;

    //GEvent.addDomListener(this.button, "click", function() {
    //    map.panBy(new GSize(0, 100));
    //});

    map.getContainer().appendChild(this.button);

    return this.button;
}

MakeMyControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
}

function Infowin(latlng, html) {

	this.latlng_ = latlng;
	this.html_ = html;
	this.prototype = new GOverlay();

	// Creates the DIV representing the infowindow
	this.initialize = function(map) {
		var div = $('<div />');

		div.mouseenter( function(){
			if(markerout){ GEvent.removeListener(markerout); }
		});
		
		div.mouseleave( function(){
			markerout = GEvent.addListener(map, "click", function() {
				if(currentWindow){ map.removeOverlay(currentWindow); }
			});
		});
		
		//div.addClass("ui-widget-content");
		
		div.css({
			position : 'absolute',
			width : 344
		}).appendTo(map.getPane(G_MAP_FLOAT_PANE))
	
		this.map_ = map;
		this.div_ = div;

		this.update(html);
	}

	this.update = function(html){
		this.html_ = html;
		
		this.div_.empty();
		
		$('<div />').css({
			height : 14,
			padding: '0 0 0 0'
		}).appendTo(this.div_);

		var content = $('<div/>').addClass('infowin-content').css({
			'position' : 'relative',
			'overflow' : 'hidden',
			'top' : -5
		}).html(html);
		
		$('<div />').css({
			'padding' : '0px 0px 0px 0px'
		}).append(content).appendTo(this.div_);
		
		this.redraw(true);
	}
	
	// Remove the main DIV from the map pane
	this.remove = function() {
	  this.div_.remove();
	}

	// Copy our data to a new instance
	this.copy = function() {
	  return new Infowin(this.latlng_, this.html_);
	}

	// Redraw based on the current projection and zoom level
	this.redraw = function(force) {
		if (!force) return;

		var point = this.map_.fromLatLngToDivPixel(this.latlng_);

		// Now position our DIV based on the DIV coordinates of our bounds
		var vleft = (point.x +16); //16 = decalage
		var vtop = (point.y - 261 + 110); //261 = height    130 = decalage
		
		this.div_.css({
			left : vleft,
			top : vtop
		});
	}
}
