var imageAddOk = "L'image a bien été ajoutée à votre bibliothèque.<br/>Ajoutez un autre fichier ou cliquez dessus pour l'ajouter à l'invitation.";

/******************************************************************************/
/*******************   Functions of images management  ************************/
/******************************************************************************/

function reloadUserImages(idLogin,type) 
//=====================================
{
  //we empty the container
  document.getElementById(type+"Container").innerHTML='';
  document.forms.uploadimage.file.value='';
   
  var element = ajaxGetElement('invitation?service=getContentsType&type='+type);
  
  var block = false;
  
  var profilePhoto = getFirstElementValue(element, 'photo'); //<profile><photo>theURL</photo></profile> 
  if( profilePhoto != '' )
  { 
    //create block for profile
    block = createSpan( "content/"+profilePhoto );
    document.getElementById(type+"Container").appendChild(block);  
  }
  
  var elemObj = element.getElementsByTagName('file');
  var len = 0;
  if( elemObj ) 
  {
    len = elemObj.length;
    
    for (var i=0; i<len; i++) 
    {
      var aElem = elemObj[i];
      
      var basic = getFirstElementValue(aElem, 'filesystemBaseName');
      var id = getFirstElementValue(aElem, 'id');

      /*  Use a link instead of directly an image, just to have 
  <span class="block">
    <a class="thumb" href="#"><img src="content/1/image/9_NqQPnS.jpg"/><br/></a>
  </span>
      */   
      block = createSpan( "content/"+idLogin+"/image/"+id+"_"+basic );
      document.getElementById(type+"Container").appendChild(block);  
    } 
  }
  if( block )   //profile or other photos
    document.getElementById('imagesGallery').style.display='block';
}

function createSpan(theUrl)
//=========================
{
  var link = document.createElement('a');
  //link.setAttribute("class","thumb");
  link.className = "thumb";
  link.setAttribute("href","#");
  //this does not work on IE
  //link.setAttribute("onclick","addImageToInvitation('content/"+idLogin+"/image/"+id+"_"+basic+"'); return false;")
  link.onclick = function(){addImageToInvitation(this); return false;};
      
  var img = document.createElement('img');
  img.src = theUrl;
  link.appendChild(img);
      
  var br = document.createElement('br');
  link.appendChild(br);

  block = document.createElement('span');
  //block.setAttribute("class","block");
  block.className = "block";
  block.appendChild(link);
      
  return block; 
}

function manageAddPhoto(idLogin)
//==============================
{
  hideSelectBeforeModal();
  if( idLogin )
  {
    modalImageManager.showModal();
    reloadUserImages(idLogin, 'image');
  }
  else
    modalImageSimpleManager.showModal();
}
function manageRemovePhoto()
//==========================
{
  var targetDiv = document.getElementById('invitationPhoto');
  targetDiv.innerHTML = '';
  
  show('linkAddPhoto');
  hide('linkRemovePhoto');
  
  document.forms.invitation.invitPhoto.value = '';
}
function addImageToInvitation(objectLink)
//=======================================
{
  var objectImg = objectLink.getElementsByTagName("img")[0];
  var url = objectImg.src;

  var targetDiv = document.getElementById('invitationPhoto');
  var img = document.createElement('img');
  img.src = url;          //src is the full web address. We could have build the address like in setImageToInvitation
                          //We let it like that, may be used later if we can import images from facebook
  img.alt = "ma photo";
  targetDiv.appendChild(img);
  
  clearError('msgDiv');
  document.forms.invitation.invitPhoto.value = url;

  hide('linkAddPhoto');
  show('linkRemovePhoto');
  
  showSelectAfterModal();
  modalImageManager.hideModal();
}
function setImageToInvitation(url)
//================================
{
/*
  var objectImg = objectLink.getElementsByTagName("img")[0];
  var url = objectImg.src;
*/
  var targetDiv = document.getElementById('invitationPhoto');
  var img = document.createElement('img');
  img.src = url;
  img.alt = "ma photo";
  targetDiv.appendChild(img);
  
  clearError('msgDiv');
  document.forms.invitation.invitPhoto.value = url;

  hide('linkAddPhoto');
  show('linkRemovePhoto');
  
  showSelectAfterModal();
  modalImageSimpleManager.hideModal();
}

function closeImageManagerModal()
//===============================
{
  clearError('msgDiv');
  
  showSelectAfterModal();
  modalImageManager.hideModal();
}
function closeImageSimpleManagerModal()
//=====================================
{
  clearError('msgDivSimple');
  
  showSelectAfterModal();
  modalImageSimpleManager.hideModal();
}