//Bookmarks****************************************************************
function bookmarksite(title, url)
{
  if (document.all)
    window.external.AddFavorite(url, title);
  else if (window.sidebar)
    window.sidebar.addPanel(title, url, "")
}

//Alternative _blank valide W3C********************************************
function lien(monLien) {
window.open(monLien, '_blank');
return false;
}
//******************************************************************************
function verifMail(email)
{
  var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
  var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
  var regex  = "^" + usr + "\@" + domain + "$";

  var rgx    = new RegExp(regex);
  return rgx.exec(email) ? true : false;
}

//******************************************************************************
function placePop(evt, fenetre, largeur, hauteur)
{
  if(!document.all && document.getElementById)
  {
    larg = window.innerWidth;
    haut = window.innerHeight;
  }
  else
  {
    larg = document.body.clientWidth;
    haut = document.documentElement.clientHeight;
  }

  if (typeof( window.pageYOffset ) == 'number') //Netscape compliant
    scrOfY = window.pageYOffset;
  else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) //DOM compliant
    scrOfY = document.body.scrollTop;
  else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  else
    scrOfY = document.documentElement.scrollTop;

  fenetre.style.left = ((larg-largeur)/2) + 'px';

  /*Ydoc = evt.clientY + scrOfY;
  if (Ydoc-hauteur < scrOfY)
    fenetre.style.top = scrOfY + 'px';
  else
    fenetre.style.top = (Ydoc-hauteur) + 'px';*/
  fenetre.style.top = ((haut-hauteur)/2 + scrOfY) + 'px';

}

//******************************************************************************
function showPhoto(evt, nomPhoto, show)
{
  aide = document.getElementById('bullePhoto');

  if (show)
  {
    placePop(evt, aide, 260, 355);
    document.getElementById('photo').innerHTML= '<img src="screen/' + nomPhoto +'" width="252" height="335" alt=""/>';
  }
  aide.style.visibility = show?"visible":"hidden";
}

//******************************************************************************
function showPhotoG(evt, nomPhoto, show)
{
  aide = document.getElementById('bullePhoto');

  if (show)
  {
    placePop(evt, aide, 510, 680);
    document.getElementById('photo').innerHTML=
      '<a href="#" onclick="showPhoto(event, \'\', false); return false;">' +
      '<img src="screen/' + nomPhoto +'" width="500" height="660" border="0" alt="Cliquer pour fermer"/></a>';
  }
  aide.style.visibility = show?"visible":"hidden";
}

//******************************************************************************
function photoInvalide(id)
{
  if (confirm('Etes-vous sur que cette photo est invalide ?'))
  {
    url = 'ajax_invalidephoto.php?id=' + id;
    appelAjax(null, url, false, null);
    alert('Le webmaster du site vient d\'être prévenu. Merci pour votre aide');
  }
  return false;
}

//******************************************************************************
function showRecherche(evt, show)
{
  aide = document.getElementById('bulleRecherche');

  if (show)
  {
    placePop(evt, aide, 300, 200);
    nom = document.getElementById('to').value;
    document.getElementById('nompseudo').value = nom;
    if (nom != '')
      getRecherche();
  }

  aide.style.visibility = show?"visible":"hidden";
}

//******************************************************************************
function getRecherche()
{
  valeur = document.getElementById('nompseudo').value;
  appelAjax(null, 'ajax_recherche.php?nom='+valeur, true, callback1);
}

//******************************************************************************
function trouveRecherche(nom)
{
  document.getElementById('to').value = nom;
  showRecherche(false, false);
}

//******************************************************************************
function showAttente(evt, texte, show)
{
  aide = document.getElementById('charge_attente');

  if (show)
  {
    placePop(evt, aide, 260, 50);
    document.getElementById('charge_attente').innerHTML= texte;
  }
  aide.style.visibility = show?"visible":"hidden";
}

//******************************************************************************
function depli(idBloc)
{
  obj = document.getElementById(idBloc);
  obj.style.display = obj.style.display==''?'none':'';
  return false;
}

//******************************************************************************
var xhr_object = null;
var lastobj = null;

var callback1 = function getNextCallback1()
{
  if ((xhr_object.readyState == 4))
  {
    //alert(xhr_object.responseText);
    document.body.style.cursor = 'default';
    texte = xhr_object.responseText.split('#');
    var s = '';
    for (i=0; i < texte.length; i++)
      s += '<a href="#" onclick="trouveRecherche(\'' + texte[i] + '\'); return false;">' + texte[i] + '</a><br />';
    document.getElementById('recherche').innerHTML = s;
  }
}

//******************************************************************************
function appelAjax(obj, url, retour, fn)
{
  if (xhr_object == null)
    if(window.XMLHttpRequest)
      xhr_object = new XMLHttpRequest();
    else if(window.ActiveXObject)
      xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else
      return true;

  xhr_object.open("GET", url, true);
  if (retour)
    xhr_object.onreadystatechange = fn;
  xhr_object.send(null);

  if (retour)
  {
    document.body.style.cursor = 'wait';
    if (obj != null)
      obj.style.cursor = 'wait';
    lastobj=obj;
  }

  return false;
}

//******************************************************************************
// Fonctions pour le diaporama
//******************************************************************************
var callbackDiapo = function getNextCallbackDiapo()
{
  if ((xhr_object.readyState == 4))
  {
    //alert(xhr_object.responseText);
    document.body.style.cursor = 'default';
    texte = xhr_object.responseText.split('#');
    position = texte[0];
    document.getElementById('idPhoto').value = texte[1];
    document.getElementById('phototext').innerHTML = texte[4];
    document.getElementById('notemoy').innerHTML = texte[2] + ' ' + texte[3];

    idItem = texte[1];
    defautNote = texte[2];
    afficheNote(-1);
  }
}

function changePhoto()
{
  photoSuiv();
  timerAuto = setTimeout("changePhoto()",5000);
}

function changeAuto(auto)
{
  if (!auto)
    clearTimeout(timerAuto);
  else
    changePhoto();
}

function changeNote(note)
{
  allPhoto = note?0:1;
}

var callbackNote = function getNextCallbackNote()
{
  if ((xhr_object.readyState == 4))
  {
    //alert(xhr_object.responseText);
    document.body.style.cursor = 'default';
    if (xhr_object.responseText == 1)
      alert('Votre vote a bien été pris en compte');
    else if (xhr_object.responseText == 2)
      alert('Vous avez déjà noté pour cette photo');
    else
      alert('Impossible de prendre en compte votre vote');
  }
}

function noter()
{
  obj = document.getElementById('manote');
  manote = obj.options[obj.selectedIndex].value;
  url = 'ajax_note.php?id=' + document.getElementById('idPhoto').value + '&note=' + manote;
  appelAjax(null, url, true, callbackNote);
  return false;
}

//******************************************************************************
var callbackNotation = function getNextCallback()
{
  if ((xhr_object.readyState == 4))
  {
    document.body.style.cursor = 'default';
    texte = xhr_object.responseText.split('#');

    if (texte[2] >= 0) {
      defautNote = Math.round(texte[2]);
      obj = document.getElementById('notemoy');
      if (obj != null)
        obj.innerHTML = texte[2] + ' ' + texte[3];
    }
    afficheNote(-1);

    if (texte[0] != '')
      alert(texte[0]);
  }
}

//******************************************************************************
function afficheNote(note)
{
  if (note == -1) note = defautNote;
  for (i = 1; i <= 10; i++)
    document.getElementById('note_etoile'+i).src='img/note_' + (note>=i?'pleine':'vide') + '.png';
}

//******************************************************************************
function NoteItem(id, note)
{
  appelAjax(null, 'ajax_notation.php?id=' + id + '&note=' + note, true, callbackNotation);
  return true;
}
