var listLocations = [];
var homeLocation = {};


function getOneLocation(location){
  return '<tr>'
        +'  <td width="10px">'
        +'    <input type="radio" name="myLocation" id="myLocation'+location.idLocation+'" onclick="changeLocation('+location.idLocation+');"'+(location.selected==1?' checked':'')+' />'
        +'  </td>'
        +'  <td>'
        +'    <label for="myLocation'+location.idLocation+'">'+location.label+'</label>'
        +'  </td>'
        +'  <td width="10px">'
        +(location.noDelete?'':'    <img width="10px" onclick="deleteLocation('+location.idLocation+');" style="cursor:pointer;" src="/Img/closeButton.gif" /></a>')
        +'  </td>'
        +'</tr>';
}


function processLocations(locationList){
  listLocations = locationList;
  
  var content='';
  var isALocationSelected = false;
  homeLocation.idLocation=-1;
  $('myLocationLabel').innerHTML = 'My location';
  for(var i=0; i<listLocations.length; i++){
    if(listLocations[i].selected==1){
      homeLocation.idLocation=listLocations[i].idLocation;
      $('myLocationLabel').innerHTML = listLocations[i].label;
      homeLocation.lat = listLocations[i].lat;
      homeLocation.lng = listLocations[i].lng;
      homeLocation.zoom = listLocations[i].zoom;
      isALocationSelected = true;
    }

    content+=getOneLocation(listLocations[i]);
  }
  content = '<div onclick="startAddLocationMode();" style="margin-top:5px; text-align:center; cursor:pointer; text-decoration:underline; font-weight:bold; font-size:0.8em;">'
          + ' New location'
          + '</div>'
          + '<hr />'
          + '<table width="100%" style="margin:-5px; margin-bottom:5px; margin-top:5px; font-weight:bold; font-size:0.8em;">'
          + getOneLocation({idLocation:0, selected:(isALocationSelected?0:1),label:'no location',noDelete:true})
          + content
          + '</table>';

  $('LocationList').innerHTML = content;
  gotoHome();
}

function changeLocation(idLocation){
  $('myLocation0').disabled=true;
  for(var i=0; i<listLocations.length; i++){
    $('myLocation'+listLocations[i].idLocation).disabled=true;
  }
  controller.requestController('setMyLocations', 'idLocation='+idLocation, onLocationUpdated);
  return false;
}

function onLocationUpdated(result){
  if(result.result=='OK'){
    processLocations(result.myLocations);
  }
}

function gotoHome(){
  if(homeLocation.idLocation!=-1 && map) {
    OnDragStart();
    map.setCenter(new GLatLng(homeLocation.lat, homeLocation.lng), homeLocation.zoom);
    OnDragEnd();
  }
}



var timeoutHandleMenuLocation = null;
function clearTimeoutHandleMenuLocation(){
  if(timeoutHandleMenuLocation != null){
    clearTimeout(timeoutHandleMenuLocation);
    timeoutHandleMenuLocation = null;
  }
}
function displayMenuLocation(){
  clearTimeoutHandleMenuLocation();
  $('menuLocation').style.display='block';
}
function hideMenuLocation(){
  clearTimeoutHandleMenuLocation();
  timeoutHandleMenuLocation = setTimeout(actionHideMenuLocation, 200);
}
function actionHideMenuLocation(){
  clearTimeoutHandleMenuLocation();
  $('menuLocation').style.display='none';
}






function startAddLocationMode(lat, lng){
	var center = map.getCenter();
	if((!Object.isUndefined(lat)) && (!Object.isUndefined(lat))){
	  center = new GLatLng(lat, lng);
	}
  map.addMarker(new CMarkerManagerDrag(center.lat(), center.lng(), 'Add Pod', {
		'imageUrl' : getUserAvatarLink(myLoginStatus.idUser, 'map_edit', myLoginStatus.avatar),
		'imageMap' : [0,0,28,0,28,28,0,28,0,0],
		'iconSize' : new GSize(42,32),
    'submitfunction':'addLocation'
  }));
}



function addLocation(name, lat, lng){
  if(name==''){
    alert('The location name is mandatory.');
  } else {
    openWaitingForm();
    controller.requestController( 'addLocation',
                                  'label='+encodeURIComponent(name)
                                + '&lat='+lat
                                + '&lng='+lng
                                + '&zoom='+map.getZoomLevel(),
                                  onLocationAdded);
  }
}

function onLocationAdded(response){
  closeWaitingForm();
  if(response.result=='OK'){
    infoWindowManager.closeActiveInfoWindow();
    processLocations(response.myLocations);
    OnDragEnd();
  }
}



function deleteLocation(idLocation){
  $('myLocation'+idLocation).disabled=true;
  controller.requestController( 'deleteLocation',
                                'idLocation='+idLocation,
                                onLocationDeleted);
}


function onLocationDeleted(response){
  if(response.result=='OK'){
    processLocations(response.myLocations);
  }
}



