var map_lat0=47.9903, map_lon0=6.7014;
var map_lat1=47.9240, map_lon1=6.8227;
var map_y1,map_x1;
var map_lat_init=47.9486; var map_lon_init=6.7692;
var mapimg_horiz=8; var mapimg_vert=6;
var mapimg_size=128;
var frame_width, frame_height;
var map_type, map_width, map_height, map_minx, map_miny, map_maxx, map_maxy, map_x, map_y;
var map_imgs, map_default;
newimg=new Image(); newimg.src='img/target.png';
function map(obj,type,lat,lon,c)
{                       // alert('obj='+obj+' type='+type+' lat='+lat+' lon='+lon+' c='+c);
  map_default=obj;
  if(GetObj('mapflow')){GetObj('mapflow').parentNode.removeChild(GetObj('mapflow'));}
  newdl=createTag(document,GetObj('divContenu'),'div','mapflow','mapflow');
   newdl.onmousedown=function(){ GetObj('p_map').className='p_map_move';};
   newdl.onmouseup=function(){ GetObj('p_map').className='p_map';};
   newdl.onmouseover=function(){ if(GetObj('map_coord')){GetObj('map_coord').style.display='block';} };
   newdl.onmouseout=function(){ if(GetObj('map_coord')){GetObj('map_coord').style.display='none';} };
  newtemp=createTag(document,newdl,'div','map_logo',null);
  newtemp=createTag(document,newdl,'div','div_map',null);
   newb=createTag(document,newtemp,'button',null,'map_picto');  setAttribut(newb,'title','Cliquez pour fermer la carte');
   newimg=createTag(document,newb,'img',null,null); newimg.src='img/picto_mapclose.png';
    newb.onclick=function(){ if(GetObj('mapflow')){ GetObj('mapflow').parentNode.removeChild(GetObj('mapflow'));} };
  newdt=createTag(document,newdl,'div','map_coord','mapopacity');
  SetOpacity(newdt,70);
  newdiv=createTag(document,newdl,'div','p_map','p_map');
   newp=createTag(document,newdiv,'p','p_target','p_target');
    newimg=createTag(document,newp,'img','target',null);
    newimg.src='img/target.png';
    SetOpacity(newimg,60);
    newspan=createTag(document,newp,'span','span_target','mapopacity');
    SetOpacity(newspan,70);
    newp.onmouseover=function(){GetObj('span_target').style.visibility='hidden';};
    newp.onmouseout=function(){GetObj('span_target').style.visibility='visible';};
    GetObj('span_target').innerHTML=unescape(c)+(c!="" && c!=" "?'<br />':'')+'Lat: '+Coordinate(lat)+' N<br />Lon: '+Coordinate(lon)+' E';
 if(type=='1')
 {
    newimg=createTag(document,newdl,'img','map_scale','mapopacity'); newimg.src='img/map_scale1.png';
    SetOpacity(newimg,70);
    newb=createTag(document,newtemp,'button',null,'map_picto');
     newimg=createTag(document,newb,'img',null,null); newimg.src='img/picto_map+.png';
     newb.onclick=function(){map(map_default,2,lat,lon,c);}; setAttribut(newb,'title','Cliquez pour agrandir la carte');
  setAttribut(newdiv,'title','Double-cliquez pour agrandir, cliquez et glissez pour vous déplacer');
  newdiv.ondblclick=function(){ map(map_default,2,lat,lon,c); };
  map_y1=(2770/4); map_x1=(3400/4);
  mapimg_horiz=8; mapimg_vert=6;
  mapimg_size=128;
 }
 else if(GetObj('mapflow'))
 {
    newimg=createTag(document,newdl,'img','map_scale','mapopacity'); newimg.src='img/map_scale2.png';
    SetOpacity(newimg,70);
    newb=createTag(document,newtemp,'button',null,'map_picto');
     newimg=createTag(document,newb,'img',null,null); newimg.src='img/picto_map-.png';
     newb.onclick=function(){map(map_default,1,lat,lon,c);}; setAttribut(newb,'title','Cliquez pour réduire la carte');
  setAttribut(newdiv,'title','Double-cliquez pour réduire, cliquez et glissez pour vous déplacer');
  newdiv.ondblclick=function(){map(map_default,1,lat,lon,c);};
  map_y1=(2770); map_x1=(3400);
  mapimg_horiz=16; mapimg_vert=12;
  mapimg_size=256;
 }
 newdl=obj.parentNode.insertBefore(newdl,map_default)
 newdl.style.width=GetObj('mapflow').parentNode.offsetWidth;

 GetObj('p_target').style.left=Math.round((CoordToPixel('x',lon))-(GetObj('target').offsetWidth/2))+'px';
 GetObj('p_target').style.top=Math.round((CoordToPixel('y',lat))-(GetObj('target').offsetHeight))+'px';
 frame_width=GetObj('mapflow').offsetWidth; frame_height=GetObj('mapflow').offsetHeight;
 map_type=type;
 map_width=parseInt(mapimg_horiz*mapimg_size); map_height=parseInt(mapimg_vert*mapimg_size);
 newdiv.style.width=map_width+'px'; newdiv.style.height=map_height+'px';
 map_x=(-CoordToPixel('x',lon)+(frame_width/2));
 map_y=(-CoordToPixel('y',lat)+(frame_height/2));
 Drag.init(document,'p_map','p_map',(map_x>20?20:(map_x<(-map_width+frame_width-20)?(-map_width+frame_width-20):map_x)),(map_y>20?20:(map_y<(-map_height+frame_height-20)?(-map_height+frame_height-20):map_y)),-map_width+frame_width-20,-map_height+frame_height-20,20,20);    //*/
 map_imgs=new Array();
 ShowMap();
 newdiv.onmousemove=MapXY;
};
function StartMap(type,lat,lon)
{
 if(lon==null){lon=map_lon_init;}
 if(lat==null){lat=map_lat_init;}
 if(!GetObj('tableMap'))
 {
  newtable=createTag(document,divOptions,'table','tableMap',null); newtbody=createTag(document,newtable,'tbody',null,null);
   newtr=createTag(document,newtbody,'tr',null,null);
    newtd=createTag(document,newtr,'td','td_titre','titre');
     createText(document,newtd,'Cliquer sur la carte pour localiser un endroit');
    newtd=createTag(document,newtr,'td',null,'right');
     newinput=createInput(document,newtd,'button','button',null,null,null,null,null,'img/preview.png',null);
   newtr=createTag(document,newtbody,'tr',null,null);
    newtd=createTag(document,newtr,'td','map_coord','text',2);
 }
  if(GetObj('mapflow')){divOptions.removeChild(GetObj('mapflow'));}
  newp=createTag(document,divOptions,'pre','mapflow','mapflow');
  newp.style.height=(divOptions_height-buttonHeight-hrHeight-GetObj('tableMap').offsetHeight)+'px';
  newp.onmousedown=function(){GetObj('p_target').style.display='none'; GetObj('p_map').className='p_map_move';};
  newp.onmouseup=function(){GetObj('p_map').className='p_map';};
  newdiv=createTag(document,newp,'pre','p_map','p_map');
   newp=createTag(document,newdiv,'p','p_target','p_target');
   newp.style.display='none';
    newimg=createTag(document,newp,'img','target',null);
    newimg.src='img/target.png';
    SetOpacity(newimg,60);
 if(type=='1')
 {
  setAttribut(newinput,'title','Cliquez pour agrandir la carte');
  setAttribut(newdiv,'title','Double-cliquez pour agrandir, cliquez et glissez pour vous déplacer');
  newinput.onclick=function(){ StartMap(2,lat,lon); };
  newdiv.ondblclick=function(){ StartMap(2,PixelToCoord('y',mousey-FindY(GetObj('p_map'))),PixelToCoord('x',mousex-FindX(GetObj('p_map')))); };
  map_y1=(2770/4); map_x1=(3400/4);
  mapimg_horiz=8; mapimg_vert=6;
  mapimg_size=128;
 }
 else if(GetObj('mapflow'))
 {
  setAttribut(newinput,'title','Cliquez pour réduire la carte');
  setAttribut(newdiv,'title','Double-cliquez pour créer un lien, cliquez et glissez pour vous déplacer');
  newinput.onclick=function(){ StartMap(1,PixelToCoord('y',FindY(GetObj('mapflow'))-FindY(GetObj('p_map'))+(GetObj('mapflow').offsetHeight/2) ),PixelToCoord('x',FindX(GetObj('mapflow'))-FindX(GetObj('p_map'))+(GetObj('mapflow').offsetWidth/2) )); };
  newdiv.ondblclick=function()
  {
   GetObj('p_target').style.display='block';
   GetObj('p_target').style.left=Math.round(mousex-FindX(GetObj('p_map'))-(GetObj('target').offsetWidth/2))+'px';
   GetObj('p_target').style.top=Math.round(mousey-FindY(GetObj('p_map'))-(GetObj('target').offsetHeight))+'px';
   if(edition=='locations' || edition=='annuaire')
   {
    lon=PixelToCoord('x',mousex-FindX(GetObj('p_map')));
    lat=PixelToCoord('y',mousey-FindY(GetObj('p_map')));
    if(confirm('Confirmez l\'ajout des coordonnées\nLat: '+lat+' N - Lon: '+lon+' E\nOu annulez'))
    {
     GetObj('form-longitude-').value=lon;
     GetObj('form-latitude-').value=lat;
     GetObj('localisation').value='Lat: '+lat+' N - Lon: '+lon+' E';
     OpenOptions('locate');
    }
    else{OpenOptions('locate');}
   }
   else{FormatText('httpmap','type=1&x='+PixelToCoord('x',mousex-FindX(GetObj('p_map')))+'&y='+PixelToCoord('y',mousey-FindY(GetObj('p_map')))+'&c=' );}
  };
  map_y1=(2770), map_x1=(3400);
  mapimg_horiz=16, mapimg_vert=12;
  mapimg_size=256;
 }
 frame_width=tableToolbox.offsetWidth, frame_height=(divOptions_height-buttonHeight-hrHeight-GetObj('tableMap').offsetHeight);

 map_type=type;
 map_width=parseInt(mapimg_horiz*mapimg_size); map_height=parseInt(mapimg_vert*mapimg_size);
 newdiv.style.width=map_width+'px'; newdiv.style.height=map_height+'px';

 map_x=(-CoordToPixel('x',lon)+(frame_width/2));
 map_y=(-CoordToPixel('y',lat)+(frame_height/2));

 Drag.init(document,'p_map','p_map',(map_x>20?20:(map_x<(-map_width+frame_width-20)?(-map_width+frame_width-20):map_x)),(map_y>20?20:(map_y<(-map_height+frame_height-20)?(-map_height+frame_height-20):map_y)),-map_width+frame_width-20,-map_height+frame_height-20,20,20);    //*/
 map_imgs=new Array();
 WriteLatLon(GetObj('map_coord'),CoordToPixel('x',lon),CoordToPixel('y',lat));
 ShowMap();
 newdiv.onmousemove=MapXY;
};

function ShowMap()
{
 map_minx=-mapimg_size+Math.round(Math.abs(FindX(GetObj('p_map'))-FindX(GetObj('mapflow')) )/mapimg_size)*mapimg_size;
 map_maxx=mapimg_size+Math.round((frame_width+Math.abs(FindX(GetObj('p_map'))-FindX(GetObj('mapflow')) ))/mapimg_size)*mapimg_size;
 map_miny=-mapimg_size+Math.round(Math.abs(FindY(GetObj('p_map'))-FindY(GetObj('mapflow')) )/mapimg_size)*mapimg_size;
 map_maxy=mapimg_size+Math.round((frame_height+Math.abs(FindY(GetObj('p_map'))-FindY(GetObj('mapflow')) ))/mapimg_size)*mapimg_size;
  map_minx=map_minx<0?0:map_minx;
  map_maxx=map_maxx>=map_width?map_width:map_maxx;
  map_miny=map_miny<0?0:map_miny;
  map_maxy=map_maxy>=map_height?map_height:map_maxy;
 for(j=map_minx; j<map_maxx; j=(j+mapimg_size))
 {
  for(k=map_miny; k<map_maxy; k=(k+mapimg_size))
  {
   if(!in_array(j+'_'+k+'.png',map_imgs))
   {
    var temp=map_imgs.push(j+'_'+k+'.png');
    var newimg=createTag(document,GetObj('p_map'),'img',null,'map');
    newimg.lowsrc='img/loading.png';
    newimg.src='img/map'+map_type+'/'+j+'_'+k+'.png';
    //newimg.width=mapimg_size; newimg.height=mapimg_size;
    newimg.style.left=j+'px';
    newimg.style.top=k+'px';
   }
  }
 }
};

function MapXY(e)
{
 if(!e){e=window.event;} if(e){ if(e.pageX || e.pageY) { mousex=e.pageX; mousey=e.pageY; }else if(e.clientX || e.clientY){ mousex=e.clientX+document.body.scrollLeft; mousey=e.clientY+document.body.scrollTop; } }
 WriteLatLon(GetObj('map_coord'),mousex,mousey);
};
function WriteLatLon(obj,mx,my)
{
 if(undefined===window.session_user)
 {
  if(GetObj('map_coord').firstChild){GetObj('map_coord').removeChild(GetObj('map_coord').firstChild);}
  newspan=createTag(document,GetObj('map_coord'),'span',null,null);
   createText(document,newspan,'Lat: '+Coordinate(PixelToCoord('y',my-FindY(GetObj('p_map'))))+' N - Lon: '+Coordinate(PixelToCoord('x',mx-FindX(GetObj('p_map'))))+' E');
 }
 else{ obj.innerHTML='Lat: '+Coordinate(PixelToCoord('y',my-FindY(GetObj('p_map'))))+' N ('+PixelToCoord('y',my-FindY(GetObj('p_map')))+') - Lon: '+Coordinate(PixelToCoord('x',mx-FindX(GetObj('p_map'))))+' E ('+PixelToCoord('x',mx-FindX(GetObj('p_map')))+')'; }
};
function CoordToPixel(direction,coord)
{
 return direction=='x'?
 Math.round((map_x1*coord)/(map_lon1-map_lon0)-(map_x1*map_lon0/(map_lon1-map_lon0))):
 Math.round((map_y1*coord)/(map_lat1-map_lat0)-(map_y1*map_lat0/(map_lat1-map_lat0)));
};
function PixelToCoord(direction,pixel)
{
 return direction=='x'?
 Math.round((((map_lon1-map_lon0)/map_x1*pixel)+map_lon0)*10000)/10000:
 Math.round((((map_lat1-map_lat0)/map_y1*pixel)+map_lat0)*10000)/10000;
};
function Coordinate(coord)
{
 return Math.floor(coord)+'° '+Math.floor((coord-Math.floor(coord))*60)+'\'\' '+(Math.floor((((coord-Math.floor(coord))*60)-Math.floor((coord-Math.floor(coord))*60))*60)<10?'0':'')+Math.floor((((coord-Math.floor(coord))*60)-Math.floor((coord-Math.floor(coord))*60))*60)+'\'';
};



