﻿//V3


$(document).ready(function() { 
  $('html, body').css('background','none');
  findBestSize();  
  positionPortes(1); 
  bindPortes();  
});
$(window).resize(function() { 
  findBestSize();  
});


function bindPortes(){
  
  //On binde les battants
  $('.battant_1, .battant_2').each(function(){
    
    $(this).mouseenter(function(){
      ouvrePorte($(this)[0].id.substr(9,$(this)[0].id.length-9));      
    });
    
  });
  
  //On binde l'intérieur des portes
  $('.porte').each(function(){
    
    $(this).mouseleave(function(){
      fermePorte($(this)[0].id.substr(5,$(this)[0].id.length-5));      
    });
    
  });
  
  
}


function ouvrePorte(porteId){   
  
  //Unbind battants pour éviter l'accumulation d'events
  $('#battant_1'+porteId).unbind();
  //$('#debug').text($('#debug').text()+" "+'unbind_B1');
  $('#battant_2'+porteId).unbind();
  //$('#debug').text($('#debug').text()+" "+'unbind_B2');
  
  $('#battant_1'+porteId).animate({
    left: '-118px'    
  }, 500, 'swing', function() {
    
  });

  $('#battant_2'+porteId).animate({
    left: '244px'    
  }, 500, 'swing', function() {
     
  });
  
}
function fermePorte(porteId){ 
  //$('#debug').text($('#debug').text()+" "+'fermporte'+porteId);
  //Unbind porte pour éviter l'accumulation d'events
  $('#porte'+porteId).unbind(); 
  //$('#debug').text($('#debug').text()+" "+'unbind_P');
   
  $('#battant_1'+porteId).animate({
    left: '0px'    
  }, 500, 'swing', function() {
    
    //Rebind après la fermeture
    $('#battant_1'+porteId).mouseover(function(){
      ouvrePorte($('#battant_1'+porteId)[0].id.substr(9,$('#battant_1'+porteId)[0].id.length-9));      
    });  
    //$('#debug').text($('#debug').text()+" "+'rebind_B1');
  });

  $('#battant_2'+porteId).animate({
    left: '126px'    
  }, 500, 'swing', function() {
    
    //Rebind après la fermeture
    $('#battant_2'+porteId).mouseover(function(){
      ouvrePorte($('#battant_2'+porteId)[0].id.substr(9,$('#battant_2'+porteId)[0].id.length-9));      
    });   
    //$('#debug').text($('#debug').text()+" "+'rebind_B2');
    
    $('#porte'+porteId).mouseleave(function(){
      fermePorte($('#porte'+porteId)[0].id.substr(5,$('#porte'+porteId)[0].id.length-5));      
    });
    //$('#debug').text($('#debug').text()+" "+'rebind_P');
  });
}

function positionPortes(startIndex){
  
  var leftPorte = new Array(282,620,960);  
  
  //On cache les portes aux extrémités
  $('#porte_'+(startIndex-1)).hide();
  $('#porte_'+(startIndex+3)).hide();
  
  var counter=0;
  for(var i=startIndex;i<=(startIndex+2);i++){
    $('#porte_'+(i)).css('left',leftPorte[counter]+'px');
    $('#porte_'+(i)).show();
    counter++;
  }
}

function findBestSize(){
  
  var curwidth=$(window).width();
  
  //largeur < 1500 px : On centre les portes
  if(curwidth<=1500){
    $('#fond_page')[0].style.width=curwidth+'px';
    $('#fond_page')[0].style.left='-'+(1500-curwidth)/2+'px';
    $('#accueilPortesBM')[0].style.width='auto';
    $('#accueilPortesBM')[0].style.margin='auto';
  }
  //Largeur suffisante : On passe en margin auto
  else{
    $('#fond_page')[0].style.width=1500+'px';
    $('#fond_page')[0].style.left='0px';
    $('#accueilPortesBM')[0].style.width=1500+'px';
    $('#accueilPortesBM')[0].style.margin='auto';
  }
}



//V2
/*
var curdef=1;    
$(document).ready(function() {
  waitSlide();
  setBack();
}); 

function setBack(){
  $('.backlogin').fadeTo('fast',0.8);
  $('.backlogin').css('top',$('.login').position().top+'px');
  $('.backlogin').css('left',$('.login').position().left+'px');
  $('.backlogin').css('width',$('.login').width()+20+'px');
  $('.backlogin').css('height',$('.login').height()+5+'px');
  $('.backlogin').fadeIn(1000);
  
  $('.backinscr').fadeTo('fast',1);
  $('.backinscr').css('top',$('.inscr').position().top+'px');
  $('.backinscr').css('left',$('.inscr').position().left+'px');
  $('.backinscr').css('width',$('.inscr').width()+20+'px');
  $('.backinscr').css('height',$('.inscr').height()+15+'px');
  $('.backinscr').fadeIn(1000);
}        

function waitSlide(){
  setInterval("slide()",5000);
}          
function slide(){            
  if(isInf(curdef, nbdef)){ 
    $('#defilant_'+curdef).fadeOut(2500); 
    curdef++;
    $('#defilant_'+curdef).fadeIn(2500);
  }
  else{              
    $('#defilant_'+curdef).fadeOut(2500); 
    curdef=1;
    $('#defilant_'+curdef).fadeIn(2500);
  }
}
*/

