
/* ---------------------------------------------------------------
 *          Fonctions PETITES ANNONCES
 * -------------------------------------------------------------*/

function initColorBoxAnnonces() {
   $('#contenu_pages .pa_cat').each(function() {
      var t_cat = $(this).find('h3:first').text();
      $(this).find('li').each(function(){
         $(this).find('a[rel="pa_cb"]').attr('title',$(this).find('.ann_titre').text());
      });
      $(this).find('a[rel="pa_cb"]').colorbox({
          open : false,
          transition: 'fade',
          speed: 350,
          width: '720px',
          height: '720px',
          onLoad: function() {
             $('#cboxContentDescription').hide();
          },
          onCleanup: function() {
             $('#cboxContentDescription').hide();
          },
          onComplete: function() {
             p = $(this).parent().parent();
             new_d = '<h3>'+p.find('.ann_titre').text()+'</h3><h4>'+p.find('.ann_telephone').text()+'</h4>'+p.find('.ann_description').html();
             $('#cboxContentDescription').html(new_d).slideDown({duration: 'fast', easing: 'swing', queue:false});
          },
          previous:'Précédente',
          next:'Suivante',
          close:'Fermer',
          current: '<b>'+t_cat+'</b>  -  Annonce {current} de {total}'
      });
   });
   if (!ZEN('cboxContentDescription'))
      $('#cboxLoadedContent').after('<div id="cboxContentDescription"></div>');
   $('#cboxContentDescription').hide();
}


var annonce_signal = -1;
function PaInitial() {
  id = "";
  if ($('#pa_initial')[0]) id = parseInt($('#pa_initial').text());
  if (annonce_signal > 0) id = annonce_signal;
  if (id != "" && !isNaN(id))
    $('#ann_'+id).find('a[rel="pa_cb"]').colorbox({ open : true }); // c'est TOUT !!
  annonce_signal = -1;
}
function getAnnonce(as) {
   annonce_signal = as;
   if (page_active != liste_mod['annonces']) getContenu(liste_mod['annonces']);
   else { $.colorbox.close(); PaInitial(); }  // si on est déjà dans la page..
}

var backAnnonce = function(str) {
   if ($('msg_annonce')) {
      if (str.substring(0,2)=="OK") {
         $('#form_annonce, #msg_annonce').hide();

         code = str.substring(3,18);
         idannonce = str.substring(19);
         //$('msg_annonce').innerHTML = "<div class='note'>Un courriel a été envoyé à vous et à un responsable, votre annonce sera approuvée bientôt.  Si vous voulez ajouter une image, vous pouvez le faire maintenant ou utiliser le lien codé dans le courriel qui vous a été envoyé pour modifier ou effacer votre annonce plus tard.</div>";

         $.colorbox({href:"modifier_annonce.php?code="+code,iframe:true,
             width:'600px', height:'500px',overlayClose:false,escKey:false,
             onCleanup:function(){ getContenu(page_active); }
         });
      }
      else {
         ZEN('msg_annonce').innerHTML = str;
         ZEN('form_annonce').code.value="";
         ZEN('img_captcha_annonce').src='securimage_show.php?sid='+(Math.random()*1000000000);
      }
   }
}


function ValideAnnonce() {
  if (f = ZEN('form_annonce')) {
    message = "";

    testc = f.courriel.value;
    testl = testc.length;
    at_pos = testc.indexOf("@");
    dot_pos = testc.lastIndexOf(".");

    if ((testl < 8) || at_pos == null || dot_pos == null || (at_pos < 1) || (dot_pos < (at_pos+2)))
        message += "Vous devez entrer un courriel valide.<br />";

    tel = f.telephone.value;
    len = tel.replace(/[^0-9]/gi,'').length;
    if (len != 10)
       message += "Vous devez entrer un numéros de téléphone valide, à 10 chiffres,<br />par exemple (819) 555-5555 ou encore 8195555555.<br />";

    if (f.titre.value == "") message += "Vous devez entrer un titre.<br />";

    if (f.description.value == "") message += "Vous devez écrire une description.<br />";

    if (message == "") {
       $.post('zen_traite_annonces.php',$('#form_annonce').serialize(),backAnnonce);
    }
    else {
     message = '<div class="avertissement">'+message+'</div>';
     ZEN('msg_annonce').innerHTML = message;
    }
  }
}
