if(window.location.hostname=='localhost') {//LOCAL
   URL = '/Attoma/';
}
else{//SITE EN LIGNE
   //URL = 'http://attoma-design.com/test_01+-/';
	 URL = '/test_01+-/';//en test
    URL = '/';//site réel
}


var pbConnexionTxt = 'Problême de connexion';
var erreurTentativeTxt = "Le nombre de tentatives de connexion est atteint. <br />Veuillez essayer à nouveau dans 10 minutes.";
var succeeEmailTxt = "Un e-mail vous a été envoyé. Il contient un nouveau mot de passe vous permettant de vous connecter à votre espace client Attoma.";

/*----------------------------------  actualités -----------------------------------*/
(function($) {
  $.fn.actualite = function(options) {
    var defaults = {
      next: true,
      hide: {opacity:0},
      hideDur: {duration: 'fast'},
      show: {opacity:1},
      showDur: {duration: 500}
    };
    options = $.extend({}, defaults, options);

    var index = parseInt($('.actuBloc #actuNuPage').html());
    var indexMax = parseInt($('.actuBloc #actuMaxPage').html());
   
    if (options.next) { index++; if (index>indexMax) index = 1 ; }
    else { index--; if (index<1) index = indexMax ; }

    $.ajax({
      url: URL+'ajax.php',
      data: 'module=actu&index='+index,
      success: function(data) {
        try {
          var obj = jQuery.parseJSON(data);
          $('.actuBloc #actuNuPage').html(index);
          $('.actuBloc #actuMaxPage').html(obj.maxPage);
          $('.actuBloc #actuDate').html(obj.laDate);
          $('.actuBloc #actuAccroche').css(options.hide,options.hideDur)
                         .html(obj.accroche)
                         .animate(options.show,options.showDur);
          $('.actuBloc .lienPrem').attr('href', obj.lien);
          $('.actuBloc .lienPrem').attr('rel', '{"id":"'+obj.id+'"}');
        }
        catch(e) {
        } // try
      } // success  
    }); // ajax

    return this;
  } // $.fn.actualite
})(jQuery);

$(document).ready(function(){

  function boucle_actu() {
    if (timeout_actu != null) {
      $.fn.actualite();
      setTimeout(boucle_actu,ms_actu);
    }
  } // boucle_actu
 
  $('a.fd').click(function(event){
    event.preventDefault(); // prevoir un lien sur le fd si pas de javascript...
    if (timeout_actu) { 
      clearTimeout(timeout_actu);
      timeout_actu = null;
    }  
    $.fn.actualite({next:true});
    timeout_actu = setTimeout(boucle_actu,30*1000 ); //relance l'actu en boucle dans 30s 
  }); //click

  $('a.fg').click(function(event){
    event.preventDefault();
    if (timeout_actu) { 
      clearTimeout(timeout_actu);
      timeout_actu = null;
    }  
    $.fn.actualite({next:false});
    timeout_actu = setTimeout(boucle_actu,30*1000 ); //relance l'actu en boucle dans 30s 
  }); //click

  
  var timeout_actu = null; // pour relancer la boucle
  var ms_actu = 15*1000;
  timeout_actu = setTimeout(boucle_actu,ms_actu ); 
});

/*---------------------------------- slide -----------------------------------*/
(function($) {
  var timeout_slide = null; // pour relancer la boucle
  var default_delay = 5*1000;  // delai entre deux slides...

     $.fn.startSlide = function(delay) {
       delay = delay || default_delay;
       if($('.slideRond li').length>1){ // s'il y au moins 2 infos
         timeout_slide = setTimeout($.fn.slide,delay); // lance le slide
         }
     }

     $.fn.slide = function(options) {
       if (timeout_slide) {
         clearTimeout(timeout_slide);
         timeout_slide = null;
       }

       options = $.extend({},
       {//default
           next: true,     // ordre de défilement
           index: 0,       // index du slide
           hide: 'fast',   // durée fade out
           show: 1000,     // durée fade in
           delay: default_delay   // delai entre deux slides...
       }, options);

       var indexMax = parseInt($('.slidePage .bulletTotal').html());

       if(options.index==0) {
         options.index = parseInt($('.slidePage .bullet').html());//slideRond1 slideRond2
         if (options.next) {
           options.index++;
           if (options.index>indexMax)
             options.index = 1 ;
         }
         else {
           options.index--;
           if (options.index<1)
             options.index = indexMax ;
         }
       }
       $.ajax({
         url: URL+'ajax.php',
         data: 'module=slide&index='+options.index,
         success: function(data) {
            try {
               var obj = jQuery.parseJSON(data);
               var main = $('#slideBloc');

               //main.find('.slideImg img').stop().animate({opacity:0},options.hide,function(){
               main.find('.slideImg img').animate({opacity:0},options.hide,function(){
                  main.find('.slideImg img').attr('src', URL+'img/upload/'+obj.image).attr('alt', obj.alt);
                  if(obj.lienExterne==1) {target='_blank';}  else {target ='';}

                  main.find('.slideImg a').attr('href', obj.pageLien)
                                           .attr('rel', '{"pageIni":"1","id":"'+obj.pageLienRel+'"}')
                                           .attr('target', target);
                  main.find('.slidePage .bullet').html(options.index);
                  main.find('.slideTxt').html(obj.txt);

                  main.find('.slideRond li').each(function() {
                     $(this).find('.active').toggleClass('active');//on les deselectionne
                  }); //each
                  main.find('.slideRond li:eq('+(options.index-1)+') a').toggleClass('active');

                  main.find('.slideImg img').animate({opacity:1},options.show, function() {
                      $.fn.startSlide(options.delay); // boucle
                  });
               });
            }
            catch(e) {
           } //try
        } //success
    }); // ajax
    return this;
  } //$.fn.slide

})(jQuery);


$(document).ready(function(){
  $('.slideRond li').live('click', function(event){
    event.preventDefault();
    newIndex = $('.slideRond li').index(this)+1;
    $.fn.slide({index:newIndex, delay: 5*1000});
  }); //click

  $.fn.startSlide(); // lance le slide
});


/*------------------------------------ login - espace client ----------------------------*/
/**
* Affiche ou ferme le login en surimpression et assombrie le site
**/
var loginIsOpen = false;
function showLogin() {
   if (loginIsOpen) {
      $('#blocPassword').show();
      $('#connexion').show();
      $('#blocEmail').hide();
      $('#ok').hide();
      $('#annuler').show();
      $('#submitEmail').hide();
      $('#blocRememberMe').show();
      $('#blocExplication').hide();

      $('#loginBox').fadeOut('fast');
      $('#loginBox').css('display','none');
      $('#loginBox .showMsg').css('display','none');
      $('#voileTransparent').fadeOut('fast');
   //$('#voileTransparent').css('display','none');
   }
   else {//Reinitialisation
      $('.input-border input').css('border-color','#e3e3e3')
      .parent().css('background-color','#bfbfbf')
      .css('border-color','#e3e3e3');
      $('#password').val('');
      //$('#login').val('');
      $('#email').val('');
      $('#rememberMe').val(0);

      $('#voileTransparent').fadeIn('fast');
    	//$('#voileTransparent').css('display','block');
      $('#loginBox').center();
      //$('#loginBox').css('display','block');
      $('#loginBox').fadeIn('fast');
    }
    loginIsOpen  = !loginIsOpen;
	}

  //////// Check empty login  ////////////
  function checkEmpyLogin(login, password) {
    //return (trim(login)!='') && (trim(password)!= '');
    return (login!='') && (password!= '');
  }

  //////// Check empty email  ////////////
  function checkEmpyLoginEmail(login, email) {
    return (login!='') && (email!= '');
  }

  //////// Erreur msg  ////////////
  function erreurMsg(msg) {
    //console.error(msg);
    $('.input-border input').css('border-color','#f9d7bd')
      .parent().css('background-color','#c3b1b1')
      .css('border-color','#f9d7bd');
    if (msg != '')
      $('#loginBox .showMsg p').html(msg);
    $('#loginBox .showMsg').css('display','block');
  }

  //////// Info msg  ////////////
  function infoMsg(msg) {
    //console.error(msg);
    if (msg != '')
      $('#loginBox .showMsg p').html(msg);
    $('#loginBox .showMsg').css('display','block');
  }

$(document).ready(function(){

  //////// Centrage  ////////////
   $(window).resize(function() {
    if ( $('#loginBox').css("display")=="block") {
      $('#loginBox').center();
    }//if
  });//resize


  $('div#espaceClient a').click(function(event){
    event.preventDefault();
    var url = $(this).attr("href"); // pick url for redirecting via javascript
    if (url=='#') {// on affiche boite de dialogue
     showLogin();
    }
    else {
     document.location.href=url;
    }
  });

  $('#annuler').click(function(event){
    event.preventDefault();
    showLogin();
  });

  $('#ok').click(function(event){
    event.preventDefault();
    showLogin();
  });


  //////// CONNEXION  ////////////
  $('#connexion').click(function(event){
    event.preventDefault();
    var password= $.trim($('#password').val()),
        login= $.trim($('#login').val()),
        rememberMe = 0;
    if($('#rememberMe').attr('checked'))   rememberMe = 1;
      
    $.ajax({
      url: URL+'ajax.php',
      data: 'module=login&connexion=1&password='+password+'&login='+login+'&rememberMe='+rememberMe,
      success: function(json){
        try {
          var obj = $.parseJSON( json );
          if (obj!= null) {
            if (obj.connexion==1) {
               //alert('connexion: '+obj.message);
              $(location).attr('href',obj.url);

            }
            else {
              erreurMsg(obj.erreur);
            } 
          }
          else
            erreurMsg(pbConnexionTxt);
        }
        catch(e) {
        }
      //$('#data').html(json);   $('#data').html(obj.message);
      //console.log(json); console.log(obj.message);
      }
      //,complete: function(data){console.log(data);}
      //,error: function(){console.log('Erreur AJAX ');}
    });//ajax
  });

  $('#oubliePassword').click(function(event){
    event.preventDefault();
    $('#blocPassword').hide();
    //$('#ok').hide();
    $('#connexion').hide();
    $('#blocEmail').show();    
    $('#submitEmail').show();
    $('#blocRememberMe').hide();
    $('#blocExplication').show();
    
    
    //erreurMsg(succeeEmailTxt);
    //successMsg(succeeEmailTxt);
//alert('email envoyé');
  });

  //////// EMAIL  ////////////
  $('#submitEmail').click(function(event){
    event.preventDefault();
    var email= $.trim($('#email').val());
    var login= $.trim($('#login').val());

    $.ajax({
      url: URL+'ajax.php',
      data: 'module=lostPassword&submitEmail=1&email='+email+'&login='+login,
      success: function(json){
        try {
          var obj = $.parseJSON( json );

          if (obj!= null) {
            if (obj.submitEmail==1) {
              $('.input-border input').css('border-color','#e3e3e3')
              .parent().css('background-color','#bfbfbf')
              .css('border-color','#e3e3e3');                
              $('#ok').show();
              $('#annuler').hide();
              $('#submitEmail').hide();
              infoMsg(obj.message);
            }
            else {
              erreurMsg(obj.erreur);
            } 
          }
          else
            erreurMsg(pbConnexionTxt+' Erreur');
        }
        catch(e) {
        }
//$('#data').html(json);
      }
    }); //ajax
  });


  $('.input-border input').focus(function(){
    $(this).css('border-color','#d2e1e1');
    $(this).parent().css('background-color','#7e9eb3')
                    .css('border-color','#d2e1e1');//#cad9d9
  }).blur(function(){
    $(this).css('border-color','#e3e3e3');//#8c8c8c
    $(this).parent().css('background-color','#bfbfbf')//#8c8c8c
                    .css('border-color','#e3e3e3');//#d9d9d9
  });


});
