jQuery(document).ready(function($) {

  // ----- HOME PAGE ------
  $('#decouvrez_lsr').click(function(){
    $('#splash_box').animate({ left: -1200},1500,'easeOutBack');
    return false;
  });
  
  // homepage
  $('.divisions li').hover(function() {
      $(this).find('.cover')
        .stop()
        .animate({ opacity: 0 },1500,'easeOutBack')
        
      $(this).find('.film')
        .stop()
        .animate({ height: '133px' },500,'easeOutBack')
        .find('.more').show();
  }, function() {
      $(this).find('.cover')
        .stop()
        .animate({ opacity: 1 },1500,'easeOutBack')
        .css('left', 0);
      
      $(this).find('.film').stop().css('height','0')
      .find('.more').hide();
  });
  
  
  // panneau featured
  
  if($('body').attr('id') == 'homepage'){
    toggle_panel('on');
  }
  $('#tab_handle').click(function(){      toggle_panel('on');  });
  
  $('#featured a.close').click(function(){    toggle_panel('off'); return false; });
  
  function toggle_panel(state){
    switch( state ){
      case 'on':
        show_panel();
        hide_tab_handle();
      break;
      case 'off':
        hide_panel();
        show_tab_handle()
      break;
    }
    return false;
  }
  
  function hide_panel(){
    $('#featured').stop().animate({ right: -342},{duration :500,queue:true},'easeOutBack'  );
       $('#panel').animate({ width: 34},{duration :500,queue:true},'easeOutBack'  );
  }
  function show_panel(){
     $('#panel').animate({ width: 342},{duration :500,queue:true},'easeOutBack'  );
     $('#featured')
        .stop()
        .animate({ right: 0},{duration :500,queue:false},'easeInOutExpo');
  }
  function hide_tab_handle(){
   $('#tab_handle').stop().animate({ right: -34},{duration :1000,queue:false},'easeOutBack');
  }
  function show_tab_handle(){
    $('#tab_handle').stop().animate({ right: -0},{duration :800,queue:false},'easeInOutExpo');
  }
  // panneau featured
  
  
  // expertises
  
  
  
  
  // filters
  $('.filters a').click(function(e){  

          //prevent the default behaviour of the link  
          e.preventDefault();  

          //get the id of the clicked link(which is equal to classes of our content  
          var filter = $(this).attr('rel');  
          
          //show all the list items(this is needed to get the hidden ones shown)  
          $(this).parents('.page').find('ul.tiles li').show();
          
          /*using the :not attribute and the filter class in it we are selecting 
          only the list items that don't have that class and hide them
          =>>> will be skipped if filter is 'all'
          */  
           if(filter != 'all'){
             $(this).parents('.page').find('ul.tiles li:not(.' + filter + ')').hide();  
           }
           
           
           
           $(this).parent().find('a').removeClass('on');
            $(this).addClass('on');
            
          
  });
  
  
  
  $('li.tile').hover(function() {
      $(this).addClass('hand');
  }, function() {
      $(this).removeClass('hand');
  });
  
  
  if($('.tile.pop').length >0) {
    

    
    $('.tile.pop').colorbox( {         
            width: '744px',
            height: '298px',
            onLoad:function(){ $('#colorbox').addClass('gradient round5 pop-shadow'); },
            onOpen:function(){    start_timer(); },
            onClosed: function() { stop_timer(); },
            html: function(){
              
              var html = $(this).find('.bubble').clone(); 
              var gallery = html.find('.gallery');
              
              // ajouter les images au popup sur demande
              var path_list = $(this).find('.extra_images').attr('value').split(';');                  
              $.each(path_list, function(i, path){
                if(path != ""){
                  var img = new Image();
                  $(img).attr('src', path);
                  gallery.append($(img));                  
                }
              });

              return html;
            }
    });
    
  }
  
  //expertises more links
  $('a.more_link').toggle(function() {
		toggleMoreLink($(this));
		$(this).parent().siblings('div.more_invite').slideDown();
		  
 	 },
	 function() {
		toggleMoreLink($(this));
		$(this).parent().siblings('div.more_invite').slideUp(); 
	});
	
	$('#alt-lang-link').click(function() {
	  var url_map = new Array(); // Tableau de correspondance des urls français et anglais.
	  var page = Manivelle.get_current_page(); // La page courante
	  var alt_page = ''; // La page de la langue alternative.
	  
	  // Si la page est vide, nous sommes sur la home. On redirige sur la home dans l'autre langue.
	  if (page == '') {
  	  if (Manivelle.lang = 'fr') {
  	    location.href = '/en';
  	    return;
  	  } else {
  	    location.href = '/fr';
  	    return;
  	  }
	  }
	  
	  // Gérer le cas particulier des 
	  	  
    // Ajouter le hash à la page.
    page = page + location.hash;
    
    // Déclarer les correspondances des urls dans les deux langues. Les clés du tableau peuvent
    // contenir des regex.
    if (Manivelle.lang == 'en') {
  	  url_map['about/#chrono'] = 'a-propos/#realisations-temporelles';
  	  url_map['about/#history-philosophy'] = 'a-propos/#historique-philosophie';
  	  url_map['about/#awards-philanthropy'] = 'a-propos/#laureats-philanthropie';
  	  url_map['about/#careers'] = 'a-propos/#carrieres';
  	  url_map['about/job/[0-9]*#careers'] = 'a-propos/#carrieres';
  	  url_map['expertise/#rental-properties'] = 'expertise/#immeubles-locatifs';
  	  url_map['expertise/#residential-developments'] = 'expertise/#developpement-residentiel';
  	  url_map['expertise/#commercial-buildings'] = 'expertise/#immeubles-commerciaux';
  	  url_map['expertise/#care-facilities'] = 'expertise/#residences-avec-soins';
  	  url_map['partnership'] = 'partenariat';
  	  url_map['news'] = 'nouvelles';
  	  url_map['contact'] = 'contact';
  	  url_map['lsr/confidentialite'] = 'lsr/confidentialite';
  	  url_map['lsr/legal'] = 'lsr/legal';
	  } else {
	    url_map['a-propos/#realisations-temporelles'] = 'about/#chrono';
  	  url_map['a-propos/#historique-philosophie'] = 'about/#history-philosophy';
  	  url_map['a-propos/#laureats-philanthropie'] = 'about/#awards-philanthropy';
  	  url_map['a-propos/#carrieres'] = 'about/#careers';
  	  url_map['a-propos/poste/[0-9]*#carrieres'] = 'about/#careers';
  	  url_map['expertise/#immeubles-locatifs'] = 'expertise/#rental-properties';
  	  url_map['expertise/#developpement-residentiel'] = 'expertise/#residential-developments';
  	  url_map['expertise/#immeubles-commerciaux'] = 'expertise/#commercial-buildings';
  	  url_map['expertise/#residences-avec-soins'] = 'expertise/#care-facilities';
  	  url_map['partenariat'] = 'partnership';
  	  url_map['nouvelles'] = 'news';
  	  url_map['contact'] = 'contact';
  	  url_map['lsr/confidentialite'] = 'lsr/confidentialite';
  	  url_map['lsr/legal'] = 'lsr/legal';
	  }

    // Trouver l'url correponsdant dans la langue alternative.
    for (url in url_map) {
      var pat = new RegExp(url, "i");
      // Rechercher un url existe dans la langue correspondante.
      if (pat.test(page) == true) {
        alt_page = url_map[url];
      }
    }

	  // Rediriger vers la version de la page dans la langue correspondante.
	  document.location.href = '/' + Manivelle.alt_lang() + '/' + alt_page;
	  
	  return false;
	});
  
 
  
  // end document ready
});


var timer="";

function start_timer(){
  if(timer==""){
    timer = window.setInterval('swap_images()', 5000);
  }
} // start_timer

function stop_timer(){
  if(timer != ""){
    window.clearInterval(timer);
    timer = "";
  }
} // stop_timer

function swap_images(){
 
 var $active = $('.gallery:visible img.on');
 
 if($('.gallery:visible img').length > 1){
   var $next =  $active.next().length > 0 ? $active.next() : $('.gallery:visible img:first');
     $active.fadeOut(function(){
       $active.removeClass('on');
       $next.fadeIn().addClass('on');
     });
 }
 
} // swapImages

function toggleMoreLink(element) {
	var currentValue = element.html();
	var newValue = element.attr('rel');
	
	element.html(newValue);
	element.attr('rel', currentValue);
}
