$(document).ready(function() {
  
  // Slide
  $('ul > li.ssmenu').click(function() {
    $(this).children('ul').slideToggle();
	});

  
  $('#nav > td > a > img').mouseout(function(){
      var  reg=new  RegExp("(_on.gif)", "g");
      src = $(this).attr('src');
      src = src.replace(reg,'.gif');
      $(this).attr('src',src);
  });
  $('#nav > td > a > img').mouseover(function(){
      var  reg=new  RegExp("(.gif)", "g");
      src = $(this).attr('src');
      src = src.replace(reg,'_on.gif');
      $(this).attr('src',src);
  });
  
  // Lightbox
  $("a[rel='lightbox']").lightBox();
  $("a[type='zoom']").lightBox();
  
  //carte
  $('#nordest').mouseover(function(){
    $('#carte').attr('src','images/carte3.png');
  })
  $('#idf').mouseover(function(){
    $('#carte').attr('src','images/carte6.png');
  })
  $('#nordouest').mouseover(function(){
    $('#carte').attr('src','images/carte2.png');
  })
  $('#sudest').mouseover(function(){
    $('#carte').attr('src','images/carte5.png');
  })
  $('#sudouest').mouseover(function(){
    $('#carte').attr('src','images/carte4.png');
  })
  $('#mapcarte > area').mouseout(function(){
    $('#carte').attr('src','images/carte.png');
  })
  
  // Moteur de recherche
  var recherche_input = $('#recherche_input');
  if(recherche_input) {
    recherche_input.click(function(){
      if(recherche_input.val() == 'Recherche') {
        recherche_input.val('');
      }
    });
  }
  
  // Newsletter
  $('#boxNewsletter').dialog({
    title: 'Inscription newsletter adhérent',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#newsletter_register').submit();
      },
      "Se désinscrire": function() {
        $('#newsletter_register #act').attr("value","newsletter_unregister")
        $('#newsletter_register').submit();
      }
    }
  });
  
  $('#newsletterLink').click(function(){
    $('#boxNewsletter').dialog('open');
    return false;
  });
  
  // Formulaire de contact
  $('#boxContact').dialog({
    title: 'Contact',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    width: 400,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_contact').submit();
      }
    }
  });
  
  $('#contactLink').click(function(){
    $('#boxContact').dialog('open');
    return false;
  });
  
  $('#contactFooterLink').click(function(){
    $('#boxContact').dialog('open');
    return false;
  });
  
  // Concours - voter
  $('#boxConcoursVoter').dialog({
    title: 'Voter',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Voter": function() { 
        $('#form_concours_vote').submit();
      }
    }
  });
  
  $('a.voterLink').click(function(){
    $('#concours_vote_id').val($(this).attr('rel'));
    $('#boxConcoursVoter').dialog('open');
    return false;
  });
  
  // Concours - participer
  $('#boxConcoursAdd').dialog({
    title: 'Participer au concours',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    width: 400,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_concours_add').submit();
      }
    }
  });
  
  $('#concoursLink_add').click(function(){
    $('#boxConcoursAdd').dialog('open');
    return false;
  });
  
  $('a.concoursLink_add').each(function() {
    $(this).click(function(){
      $('#boxConcoursAdd').dialog('open');
      return false;
    });
  });
  
  // Connexion
  $('#boxConnexion').dialog({
    title: 'Réservé aux Maîtres d’Equipage',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_connexion').submit();
      }
    }
  });
  
  $('#connexionLink').click(function(){
    $('#boxConnexion').dialog('open');
    return false;
  });
  
  // Envoyer page à un ami
  $('#boxEnvoyerAmi').dialog({
    title: 'Envoyer cette page à un ami',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_envoyer_ami').submit();
      }
    }
  });
  
  $('#envoyerAmiLink').click(function(){
    $('#boxEnvoyerAmi').dialog('open');
    return false;
  });
  
  // Demande de compte
  $('#boxDemandeCompte').dialog({
    title: 'Demande de compte',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_demande_compte').submit();
      }
    }
  });
  
  $('#demandeCompteLink').click(function(){
    $('#boxConnexion').dialog('close');
    $('#boxDemandeCompte').dialog('open');
    return false;
  });
  
  // Mot de passe oublié
  $('#boxResetMdp').dialog({
    title: 'Mot de passe oublié',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_reset_mdp').submit();
      }
    }
  });
  
  $('#resetMdpLink').click(function(){
    $('#boxConnexion').dialog('close');
    $('#boxResetMdp').dialog('open');
    return false;
  });
  
    // Formulaire proposition annonce
  $('#boxAnnonce').dialog({
    title: 'Proposer une annonce',
    autoOpen: false,
    draggable: false,
    modal: true,
    resizable: false,
    width: 400,
    buttons: {
      "Annuler": function() { 
        $(this).dialog("close"); 
      },
      "Envoyer": function() { 
        $('#form_annonce').submit();
      }
    }
  });
  
  $('#annonceAddLink').click(function(){
    $('#boxAnnonce').dialog('open');
    return false;
  });
  
  /*** Correctif <object> (animations Flash) ***/
  
  // Solution jQuery (ne marche pas pour IE)
  /*$('object').each(function() {
    $(this).append('<param name="wmode" value="transparent">');
  });
  $('embed').each(function() {
    $(this).attr('wmode', 'transparent');
  });*/
  
  // Pour IE, il faut recréer tous les éléments...
  // Source: http://www.onlineaspect.com/2009/08/13/javascript_to_fix_wmode_parameters/
  function fix_flash() {
      // loop through every embed tag on the site
      var embeds = document.getElementsByTagName('embed');
      for(i=0; i<embeds.length; i++)  {
          embed = embeds[i];
          var new_embed;
          // everything but Firefox & Konqueror
          if(embed.outerHTML) {
              var html = embed.outerHTML;
              // replace an existing wmode parameter
              if(html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                  new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i,"wmode='transparent'");
              // add a new wmode parameter
              else
                  new_embed = html.replace(/<embed\s/i,"<embed wmode='transparent' ");
              // replace the old embed object with the fixed version
              embed.insertAdjacentHTML('beforeBegin',new_embed);
              embed.parentNode.removeChild(embed);
          } else {
              // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
              new_embed = embed.cloneNode(true);
              if(!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase()=='window')
                  new_embed.setAttribute('wmode','transparent');
              embed.parentNode.replaceChild(new_embed,embed);
          }
      }
      // loop through every object tag on the site
      var objects = document.getElementsByTagName('object');
      for(i=0; i<objects.length; i++) {
          object = objects[i];
          var new_object;
          // object is an IE specific tag so we can use outerHTML here
          if(object.outerHTML) {
              var html = object.outerHTML;
              // replace an existing wmode parameter
              if(html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                  new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i,"<param name='wmode' value='transparent' />");
              // add a new wmode parameter
              else
                  new_object = html.replace(/<\/object\>/i,"<param name='wmode' value='transparent' />\n</object>");
              // loop through each of the param tags
              var children = object.childNodes;
              for(j=0; j<children.length; j++) {
                if(children[j].getAttribute('name')) {
                  if(children[j].getAttribute('name').match(/flashvars/i)) {
                      new_object = html.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i,"<param name='flashvars' value='"+children[j].getAttribute('value')+"' />");
                  }
                }
              }
              // replace the old embed object with the fixed versiony
              object.insertAdjacentHTML('beforeBegin',new_object);
              object.parentNode.removeChild(object);
          }
      }
  }
  fix_flash();
  
});



function hideAlerte(){
    $('#alert').fadeOut(function(){
      $('#darkbg').fadeOut();
    });    
}
function showAlerte(){
    $('#darkbg').fadeIn(function(){
      $('#alert').fadeIn();
    });    
}

function showMessageBox() {
  $('#messageBox').dialog({
    title: 'Information',
    autoOpen: true,
    draggable: false,
    modal: true,
    resizable: false,
    buttons: {
      "Fermer": function() { 
        $(this).dialog("close"); 
      }
    }
  });
}
