
var swfIsReady = false;
var SVGloadCompled=false;




function setSwfReady(){
	SVGloadCompled=true;
	// alert('set swf ready par map');
	swfIsReady=true;
}
function SVGloadCompled(){

	if(	SVGloadCompled){
		return 1;
	}else{
		
		return false;
	}
}

var HTTP_PREFIX = "http://";
try{
  if(parent.HTTP_PREFIX && parent.HTTP_PREFIX =="https://")
  {
  	HTTP_PREFIX = "https://";
  }
}catch( e ){
  if(window.HTTP_PREFIX && window.HTTP_PREFIX =="https://")
  {
  	HTTP_PREFIX = "https://";
  }
}


var SelectionStyle= Dynmap.Style.Selection;
SelectionStyle.prototype= Dynmap.Style.Selection.prototype;




var ModSel;



var DynmapLayer = Class.create();
DynmapLayer.prototype = {
		
	initialize:function(map){
		this._map=map;
	},
	setVisibility:function(flag){
		this.visibility = flag;
		this._map.chgStateLayer(this.layerId,flag);
		return this;
	},
	refresh:function(){
		this._map.chgStateLayer(this.layerId,this.visibility,1);
	},
	reset:function(){
		this._map.resetLayer(this.layerId);
	},
	mask:function(idObjects){
		var tp = idObjects.split(',');
		var tp2= [];
		for(var i=0;i<tp.length;i++){
			tp2.push(this.layerId+'.'+tp[i]);
		}
		this._map.mask(tp2.join(',',true,true));
	},
	setAttribute:function(attribute,value){
		this._map._setAttributeLayer(this.layerId,attribute,value);
	},
	setLayerIndex:function(value){
		this._map._setAttributeLayer(this.layerId,'position',value);
	}

}

function rmCircleLoca(){

	mainCarte.layer('draw').reset();
}

function strSvgToAnnotation(res){
	
	//	var str = res.responseText;
		mainCarte.addFeature('annotation','ajax','draw.new',res);
		//jsStrSvgToAnnotation(str);
	
}
function objetsToAnnotation(tabObjets,bufferObj){
	if(bufferObj==undefined){
		bufferObj='0';
	}

	mainCarte.getMapControl().addMsgWait('Calcul du dessin en cours','addFeature');
	var functionRetour = mainCarte.addFeature.bind(mainCarte,'annotation','ajax','draw.new');
	mainCarte.unionObjects(tabObjets,bufferObj,functionRetour);

}

/********************function de wrappage**********************/
function mapZoom(k){
	mainCarte.scale(k);
}
function selectionToRecherche(limitationCouche,objects){
	mainCarte.selectionToRecherche(limitationCouche,objects);
}
function panMap(dx,dy)
{
	if(mainCarte==undefined){
		alert ('la carte flash n\'est pas initialisée');
	}

	mainCarte.panMap(dx,dy);

}
// Modifie l'etat d'une couche,
// si la couche est deja chargee la fct modifie l'attribut de visibilite
// sinon la fct charge la couche.
try{
	parent.chgStateLayer = chgStateLayer;
}catch( e ){
	window.chgStateLayer = chgStateLayer;
}
function chgStateLayer(layerid,etat,force_unload,analyseid)
{
	mainCarte.chgStateLayer(layerid,etat,force_unload,analyseid);

}
function chgStateGroupes(str){
	mainCarte.chgStateGroupe(str);
}
function chgStateLayerEtiquettes(layerid,etat)
{
	mainCarte.chgStateLayerEtiquettes(layerid,etat);

}

//Modifie l'etat  d'une analyse
try{
	parent.chgStateAnalyse = chgStateAnalyse;
}catch( e ){
	window.chgStateAnalyse = chgStateAnalyse;
}
function chgStateAnalyse(analyseid,etat,dynmapAnalyseParam)
{
	if(typeof dynmapAnalyseParam=="undefined")dynmapAnalyseParam=null;
	mainCarte.chgStateAnalyse(analyseid,etat,dynmapAnalyseParam);

}
function SetevenementCl(typeCl){
	if(typeCl==0){
		mainCarte.chgContexte('DEFAULT');
	}
	if(typeCl==1){
		mainCarte.chgContexte('SELECTION');
	}
	if(typeCl==2){
		mainCarte.chgContexte('NONE');
	}

}
//fonction de localisation

function findAll(liste,replace_selection,select_mode,modifZoom, url_mode){
	mainCarte.findAndSelect(liste,replace_selection,select_mode,modifZoom);
}

function findAllCheck(liste,replace_selection,select_mode,modifZoom, url_mode){
	mainCarte.findAndSelect(liste,replace_selection,select_mode,modifZoom);
}
function init_selection(){
	mainCarte.resetSelection();
}
function initZoom(){
	mainCarte.goToInitPosition();
}
function doZoom(xmin,ymin,dx,dy){
	mainCarte.goToBBox({'xmin':xmin,'ymin':ymin,'dx':dx,'dy':dy});
}

function export_selection(format)
{
	var width=640;
	var height=500;
	var left=(screen.width-width)/2;
	var top=(screen.height-height)/2;
	var str_id="";
	var msg = "/dynmap/selection.php?format="+format+'&path_application='+path_application;

	if(format=="HTML")
	{
		if (openWindow)
		{
			try
			{
				openWindow(msg,'n','menubar=yes,scrollbars=yes,left='+left+', top='+top+',width='+width+',height='+height);
			}
			catch (e)
			{
				alert('impossible d\'ouvrir la fenetre html de visualisation veuillez régler votre bloqueur de pop up');
			}
		}
		else
		{
			alert('impossible d\'ouvrir la fenetre html de visualisation veuillez régler votre bloqueur de pop up');
		}
	}
	if(format=="XLS")
	{
		//DebugD2(dynmap_url+pars);
		var myAjax = new Ajax.Request(
		msg,
		{method: 'POST', parameters: null, onComplete: function(res){
			//alert(res.responseText);
			//openWindow('../'+res.responseText);
			width=400;
			height=165;
			left=(screen.width-width)/2;
			top=(screen.height-height)/2;
			w=openWindow("/dynmap/getXlsFile.php?f="+path_application+res.responseText,'Xls','resizable=yes,scrollbars=yes,left='+left+', top='+top+',width='+width+',height='+height);
		}
		}
		);
	}
}
/**
 * dessine un point aux c
 * @param posXsvg
 * @param posYsvg
 * 
 * 
 */
function drawCircleLoca(posXsvg,posYsvg,couleur){
	
	mainCarte.api().drawCible({'x':posXsvg,'y':posYsvg,'coords':'svg'});
}




function SVGFindComplete(){
	return mainCarte._findComplete;
}
//alert(SVGFindComplete);
function getDataById(objId){
	mainCarte.getDataById(objId);
}
function LoadInformation(obj){
	mainCarte.a_getDataById(obj);
}
function addFiltreByElements(chaine){
	mainCarte.addFiltreByElements(chaine);
}
function removeFiltreCouche(idcouche){
	mainCarte.removeFiltreCouche(idcouche);
}
function license(content){
	var arr = content.split("|");
	w = 450;
	h = 250;
	l = ((parent.screen.width-w)/2);
	t = ((parent.screen.height-h)/2);
	open(HTTP_PREFIX+arr[1]+"/dynmap/register.php?"+arr[2]+"path_application="+mainCarte.path_application,"","width="+w+",height="+h+",top="+t+",left="+l);
}
function logguer(content){
	var arr = content.split("|");
	w = 300;
	h = 200;
	l = ((parent.screen.width-w)/2);
	t = ((parent.screen.height-h)/2);
	GestEvtDyn.lanceEvenement('DATAINIT',"0");
	open(HTTP_PREFIX+arr[1]+"/dynmap/login.php?"+arr[2]+"path_application="+mainCarte.path_application,"","width="+w+",height="+h+",top="+t+",left="+l);
}
function printFlash(params)
{
	mainCarte.print(params);
}
function printSVG(params)
{
	mainCarte.print(params);
}
//Permet de changer le mode de representation pour la fonction de localisation
//mode = localisation,selection,localisationHilight
//flag = true|false
function SetTypeFind(mode,flag){
	mainCarte.setTypeFind(mode,flag);
}

function chgStateRasterUni(idRaster,state){
	mainCarte.changeStateRaster(idRaster,state);

}
function chgModeRasterUni(colorBool,idRaster){
	var color='';
	if (colorBool==1)
	{
		color = "rgb";
	}
	else
	{
		color = "grey-level";
	}
	mainCarte.changeColorRaster(idRaster,color);

}
function modOpacityRaster(opacity,idRaster){
	mainCarte.setOpacityLayer(idRaster,opacity,'raster');

}
function saveOpacityRaster(opacite,raster_map){
	mainCarte.saveOpacityLayer(raster_map,opacite,'raster');
}
function StartTimer(msg,key){

	mainCarte.getMapControl().addMsgWait(msg,key);
}
function StopTimer(key){
	mainCarte.getMapControl().endMsgWait(key);
}

//Ouvre la boite de recherche majic2
function majic2_search(){
 
	ArraySelectionMajic2 = getCurrentSelection();
 
	var selections = "0";

	if(ArraySelectionMajic2.length==0){
		alert("Aucune parcelle en selection");
	}else{
	
		flag = 0;
		for(var i=0;i<ArraySelectionMajic2.length;i++){
				selections += ","+ArraySelectionMajic2[i];
				flag = 1;
		}

		if(flag==1){

			try
			{
				var selection_mode = "parcelle";

				w=parent.openWindow("/dynmap/backoffice/majic/recherche_cadastre.php?selection_mode="+selection_mode+"&selections="+selections+"&path_application="+path_application,'','');
			}
			catch (e)
			{
			}
		}
	}
}
function getCurrentBBox(){
	return mainCarte.getCurrentBBox();	
}
function getViewBoxVars(){
	var b =mainCarte.getCurrentBBox();	

	var tabelems =  new Array();
	tabelems["Xmin"]= b.xmin;
	tabelems["Ymin"]= b.ymin;
	tabelems["YminREEL"]=b.yminReel;
	tabelems["Xmax"]= b.xmax;
	tabelems["Ymax"]= b.ymax;
	tabelems["winDX"]= b.dx;
	tabelems["winDY"]= b.dy;
	tabelems["icurZoom"]=b.zoom ;
	tabelems["E"]= mainCarte.getEchelle();
	tabelems["path_application"]= path_application;
	 
	return tabelems;

}

function chgStateAnnotations(){
	if(chgStateAnnotations._toogle==undefined){
		chgStateAnnotations._toogle=false;
	}
	if(chgStateAnnotations._toogle){
		mainCarte.chgStateLayer('annotation',1);
	}else{
		mainCarte.chgStateLayer('annotation',0);
	}
	chgStateAnnotations._toogle=!chgStateAnnotations._toogle;
}

 function localiseXY(X,Y,flag,newScale,params){
	 mainCarte.localiseXY(X,Y,flag,newScale,params);

}
function makeGeosignet(){
	mainCarte.makeGeosignet();
}
function getCurrentObject(vide,idobj){
	var datas=mainCarte.getObjectDescription(idobj);
	if(typeof(datas)!='undefined' && typeof(datas['dyn_desc'])!='undefined'){
		return datas['dyn_desc'];
	}else{
		return false;
	}
}
function refreshMapData (idLayer)
{

	mainCarte.refreshMapData(idLayer);
	

}
function NewFiche(idLayer){
	mainCarte.setNewObject(idLayer);
	
}

function initDraw(){
	//alert('demande de init');
	mainCarte.initDraw();
}
function dropElement(element_id){



	mainCarte.removeItem(element_id);
	//document.getElementById(document.getElementById(element_id).parentNode.getAttribute("id")).removeChild(document.getElementById(element_id));
}

function Debug(str){
	GestEvtDyn.lanceEvenement('DEBUG', [ str ] );
}
function switchDraw(layer,byMenu){
	if(layer=='close'){
		mainCarte.unloadModule('Draw');
	}else{
		mainCarte.loadModule('Draw',{'idLayer':layer,'showToolsBar':true});
	}
}
function getCurrentSelection(){
	return mainCarte.getSelectedItems();
}
function getSelArray(){
	return mainCarte.getSelectedItems();
}

function zoomPreced(){
	mainCarte.zoomPreced();
}
function inverseSelection(couche){
	mainCarte.inverseSelection(couche);
}
function createCircleDiscretise(Xpt,Ypt,rayon,funcRetour){

	mainCarte.createCircleDiscretise(Xpt,Ypt,rayon,funcRetour);
	
	
}
function 	resizeMap(w,h){
		mainCarte.resizeMap(w,h);
}
DynmapGeometry = Class.create();
DynmapGeometry.prototype={
		initialize:function(){
	
		},
		circle:function(x,y,rayon){
			this.type='circle';
			this.x=x;
			this.y=y;
			this.rayon=rayon;
			return this;
		}
		
}
function deleteAllAnnotations(){
	mainCarte.resetLayer('annotation');
}
function userMaps(mode)
{
	if(mode=="liste")
	{
		var width=700;
		var height=600;
		var left=(screen.width-width)/2;
		var top=(screen.height-height)/2;
	
		var msg = "/dynmap/extensions/indexcarte.php?cont=liste&mod=usermaps&event=prepare_liste&fullDisplay=1&path_application="+mainCarte.path_application;
	 
		if (openWindow)
		{
			try
			{
				openWindow(msg,'n','menubar=yes,scrollbars=yes,left='+left+', top='+top+',width='+width+',height='+height);
			}
			catch (e)
			{
				alert('Impossible d\'ouvrir la fenetre de visualisation. Veuillez paramétrer votre bloqueur de popup');
			}
		}
		else
		{
			window.open(msg,'n','menubar=yes,scrollbars=yes,left='+left+', top='+top+',width='+width+',height='+height);
		}
	}
	
	if(mode=="save")
	{
		
	 	var ajx= new Ajax.Request("/dynmap/extensions/indexcarte.php?cont=fiche&mod=usermaps&event=saveFiche&id=new&viewer=none&path_application="+mainCarte.path_application,
	 				{ 
	 					method: 'post',
	 					parameters: "&TITLE=AUTO",
	 					onComplete : function(r)
	 					{  
	 						//alert(r.responseText);
	 						alert('Enregistrement effectué');
	 					}
	 				}
	 			);
	}
	
	if(mode=="getMap")
	{
		
	 	var ajx= new Ajax.Request("/dynmap/extensions/indexcarte.php?cont=fiche&mod=usermaps&event=getFiche&id=lastFromUser&viewer=simpleJsonV2&path_application="+mainCarte.path_application,
	 				{  
	 					onComplete : function(r)
	 					{
	 		
	 						eval("var s="+r.responseText);
	 						var contexte = s["datasExtras"];   
	 						if(!isNaN(contexte)&&contexte!=null)
	 						{
	 							location.href="index.php?CONTEXTMAP="+contexte;
	 						}else{
	 							alert("Aucune carte n'a été enregistrée au préalable");
	 						}
	 					}
	 				}
	 			);
	}
}
function echelleToZoom(echelle){
	var pixPerCm = 0.03; // 2,54 cm/inch et 96pix/inch
	var val = Math.round((echelle/100)*(mainCarte.params.width*pixPerCm));
	return val;
}
function disableDefaultDrawSave(idL){
	mainCarte.disableDefaultDrawSave(idL);
}
function enableDefaultDrawSave(idL){
	mainCarte.enableDefaultDrawSave(idL);
}
function zoomEchelle(echelle){
	if(echelle > 0){
		//retourne un tableau d'elements
		var tabelems =  new Array();
		tabelems = getViewBoxVars();
		
		var temp_Xmin = tabelems["Xmin"];
		var temp_Ymin = tabelems["YminREEL"];
		var temp_winDX = tabelems["winDX"];
		var temp_winDY = tabelems["winDY"];
		
		var temp_largeur = tabelems["icurZoom"];
		//alert ("temp_winDX:" + temp_winDX + ",temp_winDY:" + temp_winDY+ ", temp_largeur:" +temp_largeur + ", temp_Xmin:"+temp_Xmin + ", temp_Ymin:"+temp_Ymin );
		
		var temp_X = temp_Xmin+((temp_winDX)/2);
		var temp_Y = temp_Ymin+((temp_winDY)/2);
		
		localiseXY(temp_X,temp_Y,false,echelleToZoom(echelle));
	}
}
function dynmap_export_img(){
	mainCarte.exportToImg();
}
function selectionToMask(){
	var objs = mainCarte.getSelectedItems();
	var objStr = objs.join(',');
	mainCarte.mask(objStr,true,true);
}
