var titre;
$(document).ready(function(){
 titre=document.title;
 $(".menu2 a")
  .each(function(){
   $(this).attr("href",$(this).attr("href").replace("?","#"));
  })
  .click(function(){
   $.history.load(this.href.recupAncre());
   $(this).blur();
   return false;
  });

 $(".menu2").css("backgroundPosition", "100% 0");
 $.history.init(function(){
  chargePage($.history._curHash);
 });
 $('img[@src$=.png],.bandeau div').ifixpng();
 $(".menu2>div:last").siblings().css("border-bottom","1px solid #B6A473");
 var imgPreload = new Array();
 $(".menu2 a").each(function(){
  imgPreload.push(this.href.recupAncre());
 });
 i=0;
 while(i<imgPreload.length)
 {
  $("<img>").attr("src","illustrations/"+imgPreload[i++]+".png");
 }
 $("a.externe").click(function(){
  window.open($(this).attr("href"));
  return false;
 });
});

function selMenu(elm)
{
 elm.parent().addClass("courent")
  .children(".sous_menu").fadeIn(300)
  .end().siblings().removeClass("courent")
  .children(".sous_menu").hide();
 pos=elm.findPos();
 posMenu=$(".menu2").findPos();
 final="(100% "+Math.round(pos-posMenu+(elm.outerHeight(true)/2)+1)+"px)";
 $(".menu2").animate({backgroundPosition: final},150);
}

function chargePage(ancre)
{
 lien=ancre.substring(1);
 elm=$(".menu2 a[@href=#"+lien+"]:last");
 if(!(lien.length>0 && elm.length>0))
 {
  $.history.load("maison");
  return;
 }
 document.title=titre + " - " + elm.text();
 if(elm.parents(".sous_menu").length>0)
 {
  elm.addClass("courent").siblings().removeClass("courent");
  elm=elm.parents().parent().children(".m_titre");
 }
 selMenu(elm);
 $(".contenu2").load("pages/"+lien+".php?"+(new Date().getTime()),{},function(){
  $(".photo img").attr("src","illustrations/"+lien+".png").ifixpng();
  if(!($.browser.msie && $.browser.version==7))
   $(".imgbouteille img").hide().load(function(){$(this).fadeIn(800)});
  $(".contenu2 a.externe").click(function(){
   window.open($(this).attr("href"));
   return false;
  });
  if($(".formpage").length>0)
  {
   $(".formpage:not(:first)").hide();
   $(".divbdc input").change(function(){
    $(this).val(parseInt("0"+$(this).val(),10));
   });
   var page=0;
   $(".contenu2 form").submit(function(){
    if(page==0)
    {
     ok=false;
     $(".divbdc input").each(function(){
      if(parseInt("0"+$(this).val(),10)!=0)
      {
       ok=true;
       return false;
      }
     });
     if(!ok)
     {
      alert2("Vous devez séléctioner au moins une bouteille");
      return false;
     }
     if(!$("#cgv")[0].checked)
     {
      alert2("vous devez accepter les condition générales de vente");
      return false;
     }
     $(".formpage:first").hide().next().show();
     $("#prenom").focus();
     page=1;
     return false;
    }
    if(!verifform())return false;
    var champs = new Object();
    $(this).find("input:not(:image),textarea").each(function(){
     champs[$(this).attr("name")]=$(this).val();
    });
    $(".contenu2").load($(this).attr("action"),champs);
    return false;
   });
  }
 });
}

function alert2(message)
{
 alert(message);
}

function verifform()
{
 if(!verif("prenom","prénom"))return false;
 if(!verif("nom","nom"))return false;
 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 var mail = document.getElementById("email");
 if(reg.test(mail.value) == false)
 {
  alert('Votre adresse e-mail est invalide');
  mail.focus();
  return false;
 }
 if(!verif("tel","téléphone"))return false;
 if(!verif("adresse","adresse"))return false;
 if(!verif("cp","code postal"))return false;
 if(!verif("ville","ville"))return false;
 if(!verif("pays","pays"))return false;
 return true;
}

function verif(id,nom)
{
 var elm = document.getElementById(id);
 if(elm.value=="")
 {
  alert("Merci d'indiquer votre "+nom);
  elm.focus();
  return false;
 }
 return true;
}
String.prototype.recupAncre=function()
{
 ancre="";
 if(this.indexOf("#")>-1)
  ancre=this.substring(this.indexOf("#")+1);
 return ancre;
}

