
function OnSelecteurPortsZone(ev)
{
 var e=$evT(ev),s=$('SelecteurPortsZone'),z=Ports.Zone;
 if(!e||!s)return; 
 
 if( ev.type == 'mouseout' )
   if( $id(e,/^(Zone_[0-9]+)_(Txt|Selected)/) || e.id == 'Zone_0' )
     s.className=s.className.replace(/((^| )Zone_)[0-9]+(_Selected( |$))/,'$1'+z+'$3');
 
 if( ev.type == 'mouseover' || ev.type == 'click' )
   if( (z=$id(e,/^Zone_([0-9]+)/,1))!==false )
     {
      s.className=s.className.replace(/((^| )Zone_)[0-9]+(_Selected( |$))/,'$1'+((z=_int(z))?z:Ports.Zone)+'$3');
      if( ev.type == 'click' )
        GoPortsListe( z == Ports.Zone ? 0 : z );
     }
}

function OnSelecteurPortsPP(pp)
{
 var e=$('SelecteurPortsPP').firstChild;
 Ports.PP=e.checked=ifdef(pp,!e.checked);
 $cnT('PortsListe_Pages','PP',Ports.PP?1:-1);
 GoPortsListe( 0 );
}

function DoPortsListe(z)
{
 var e,s=$('SelecteurPortsZone');
 if(!s)return;
 Ports.Zone = z = (z&&inA(_int(z),Ports.Zones)?_int(z):0);
 s.className=s.className.replace(/((^| )Zone_)[0-9]+(_Selected( |$))/,'$1'+z+'$3');
 selChild('PortsListe_Pages',z);
 LoadPortsListe(z);
}

function LoadPortsListe(z,reload,now)
{
 var e=$('PortsListe_Content_'+z);
 if(!e||!window.Ports)return false;
 if(!$cn(e,'LoadFlag')&&!reload) return 1;
 if(!now&&$cn(e,'Loading')){window.setTimeout('LoadPortsListe("'+z+'",'+(reload?1:0)+',1)',5000);return 2;}
 $cnA(e,'Loading');
 return JS('/do/load-ports-liste.php?z='+z,'LoadPortsListe'+z) ? 3 : false;
}

function GoPortsListe(z)
{
 var e=$('PortsListe_Content_'+z)||((!z||inA(_int(z),Ports.Zones))&&$c('div','PortsListe_Content_'+z,'PortsListe_Content Selected LoadFlag','PortsListe_Pages',null,'&nbsp;'));
 $cnR('PortsListeResultat_Content','Selected');
 $cnA('PortsListe_Pages','Selected');
 DoPortsListe(z);
}

function DoPortsListeResultat(t)
{
 var e=$('PortsListeResultat_Content');
 if((t=ifdef(t,1))>0)
     DoPortsListe(0);
 else
    {
     $sHTML($cnR('PortsListeResultat_Content','Selected'),'');
     GoPortsListe(Ports.Zone);
    }
}

function GoPortsListeResultat()
{
 $cnR('PortsListe_Pages','Selected');
 $cnA('PortsListeResultat_Content','Selected');
 DoPortsListeResultat(1);
}

function OnPortsListeRechercheQ(v)
{
 var e=$('PortsListeRechercheQ');
 if(v===null){if(e.keyRQTID)window.clearTimeout(e.keyRQTID);e.keyRQTID=0;e.keyRQL=v=e.value='';e.blur();e.onblur()} else if(isstr(v)){e.value=v;e.onfocus()} else v=e.value;
 if(v===e.title)v='';
 v=txtsort(v);
 if(v.length<2&&!v.match(/^[a-j]$/))
   {
    DoPortsListeResultat(-1);
    return;
   }
 else if(v!=e.keyRQL){
  {
   if(e.keyRQTID)window.clearTimeout(e.keyRQTID);
   e.keyRQL=v;
   e.keyRQTID=window.setTimeout('$cnA(\'PortsListeResultat_Content\',\'Loading\');JS(\'/do/ports-liste-recherche.php?q='+escape(e.value)+'\',true,null,\'PortsListeRechercheJS\');',500);}
  }
 if(!$cn('PortsListeResultat_Content','Selected'))
   GoPortsListeResultat();
}

function PortsListeRechercheLOC()
{
 if( !navigator.geolocation ){$cnR('PortsListeRechercheF','LOC');return;}
 try {navigator.geolocation.getCurrentPosition(function(pos)
         {
          var e=$('PortsListeRechercheQ');
          OnPortsListeRechercheQ(round(pos.coords.latitude,4)+' '+round(pos.coords.longitude,4));
          e.onfocus();
          e=null;
        }
      ,PortsListeRechercheLOC_Err,{maximumAge:300000,timeout:1000});}catch (err){PortsListeRechercheLOC_Err(err);}
}

function PortsListeRechercheLOC_Err(err)
{
 alert('Désolé, la position n\'a pas pu être déterminée.');
}

// -----------------------------------------------------------------------------
function InitPortsListe(ev)
{
 var e=$evT(ev),port=$id(e,/^Port([0-9]+)(_|$)/,1,0);
 if(!port) return;
 if(e.onclick)return;
 e.onclick=OnPortClick;
 e.onmouseover=e.onmousemove=OnPortOver;
 e.onmouseout=OnPortOut;
 e.onmouseover(ev);
}

function OnPortOver()
{
 var l=$('PortLink');
 if( !l || l.parentNode!=this )
   {
    if(window.OnPort_IDT)
        window.clearTimeout(window.OnPort_IDT);
    window.OnPort_IDT=window.setTimeout('DoPortLink("'+this.id+'")',200);
   }
 $cnA(this,'Selected');   
}

function OnPortOut(ev)
{
 if(window.OnPort_IDT)window.clearTimeout(window.OnPort_IDT);
 window.OnPort_IDT=null;
 if( !$isChild(this,$evRT(ev||event)) )
   {
    $d('PortLink');
    $cnR(this,'Selected');
   }
}

function OnPortClick(ev)
{
 var e=$evT(ev||window.event),port=$id(e,/^Port([0-9]+)(_|$)/,1,0);
 if(e!=this&&$isChild(this,e)&&$cn(e,'Fav'))
   return OnPortFav(this);
 return true;
}

function DoPortLink(port)
{
 var e=$(port),l=$('PortLink');
 if(!e||!(port=$id(e,/^Port([0-9]+)(_|$)/,1,0)))return;
 if(l&&l.parentNode!=e){$cnR(this,'Selected');$d(l);}
 $c('div','PortLink',null,e,null,'<div class="Fav"></div><div class="SousMenu" onmouseover="$evS(event)"><i></i><div><a href="/'+port+'/meteo" class="Meteo">Horaires et météo</a><a href="/'+port+'/coefficients" class="Coef">Coefficients</a><a href="/'+port+'/calendrier" class="Cal">Calendrier</a>'+($cn(e,'PP')?'<a href="/'+port+'/ports-rattaches#Corrections" class="PS">Ports rattachés</a>':'')+'</div></div>');
} 

// ---

function PortFavUpdatePage(port,t)
{
 if(!window.Ports||!window.User||!window.User.PortsFav)return;
 t=ifdef(t,inA(port,User.PortsFav)?1:-1);
 $cnT('Port'+port+'_R','Fav',t);
 $cnT('Port'+port+'_0','Fav',t);
 for(var i=0;i<Ports.Zones.length;i++)
    $cnT('Port'+port+'_'+Ports.Zones[i],'Fav',t);
}


