﻿$(document).ready(function () {
  
 
});


function createDiv(zeClass){
  d=document.createElement('div');
  if(zeClass){
    $(d).addClass(zeClass); 
  }
  $(d).text(' '); 
  return d;
}

/*--------------------------------------------------------------------*/
/*------------------------DOCUMENT AJAX-------------------------------*/
/*--------------------------------------------------------------------*/

function updateDocFade(arbo, cible, root , callback ){
  $('#'+cible).fadeOut(200, function(){
    updateDoc(arbo, cible, root , function(){
      $('#'+cible).fadeIn(200, function(){
        
        try{
          google.maps.event.trigger(map, 'resize');
          map.setCenter(latlngc);
        } 
        catch(ex){}
          
      });
    });
  });
}

function updateDoc(arbo, cible, root , callback ){
    
  if(callback){    
    $('#'+cible+' .contentToRefresh').load(root+'xslt.aspx?typerubrique=1&rubriqueid=3&elementid='+arbo, callback);  
  }
  else{
    $('#'+cible+' .contentToRefresh').load(root+'xslt.aspx?typerubrique=1&rubriqueid=3&elementid='+arbo);  
  }
}

function updateAriane(lien1,lien2){
  $('#ariane').html('<a href="javascript:closeDoc()">Accueil</a>&#160;>&#160;'+lien1+'&#160;>&#160;'+lien2);
  if(lastdirection=='' || lastdirection=='up'){
    $('#ariane a').css('color','#FFFFFF');
    $('#ariane').css('color','#FFFFFF');
  }
  else{    
    $('#ariane a').css('color','#9ca1a3');
    $('#ariane').css('color','#9ca1a3');
  }  
}

/*--------------------------------------------------------------------*/
/*------------------------POPIN + GLASS-------------------------------*/
/*--------------------------------------------------------------------*/
divShowed=null;
divGlass=null;
var divToShow_;
var caller_;
var rubriqueid_;
var elementid_;
function showPop(divToShow, caller, rubriqueid,elementid, supp){
  //debugger;
  divToShow_ = divToShow;
  caller_ = caller;
  rubriqueid_ = rubriqueid;
  elementid_ = elementid;
  if(!supp){supp='';}
  
  if(divGlass){return;}
  
  if(rubriqueid && !elementid){
    $('#'+divToShow+' .contentToRefresh').load('xslt.aspx?typerubrique=1&rubriqueid='+rubriqueid+'&'+supp, function(){showPopSuite();}); 
  }
  else if(rubriqueid && elementid){
    $('#'+divToShow+' .contentToRefresh').load('xslt.aspx?typerubrique=1&rubriqueid='+rubriqueid+'&elementid='+elementid+'&'+supp, function(){showPopSuite();}); 
  }
  else{
    showPopSuite()
  }  
  
}

function showPopSuite(){
  
  var divToShow = divToShow_;
  var caller = caller_;
  var rubriqueid = rubriqueid_;
  var elementid = elementid_;
  
  if(divGlass){return;}
    
  if(caller){
    var w_height = $(window).height();
    
    var w_scrollTop = $(window).scrollTop();
    
    //alert($(caller).position().top+'-'+w_height+'-'+w_scrollTop);
    
    var finalH = w_scrollTop+(w_height/2)-184; 
    
    
    $('#'+divToShow).css('top',finalH+'px');
    //alert(finalW);
    
  }
  
  var w_width = $('body').width();
  var finalW = (w_width/2)-(466/2);   
  $('#'+divToShow).css('left',finalW+'px');
  
  
  //Création du fond
  var d=document.createElement('div');
  //$(d).addClass('glasspaint');
  $(d).attr('id','glasspaint');
  $(d).text('  ');
  $(d)[0].style.width=$('body').width()+'px'  ;
  $(d)[0].style.height=$('body').height()+'px' ;
  $(d)[0].style.display='none';
  $('#global').append($(d));
  divGlass=d;
  
  $(divGlass).fadeTo("fast",0.72);       
  $(divGlass).fadeIn("fast");  
  
  
  //$('#'+divToShow).fadeTo("normal",0.72);
  $('#'+divToShow).fadeIn('normal');
  divShowed=divToShow;
  
  
  
  //Au dessus pour les blocs en absolu
  //var arrBlocs = new Array('#fiche_instal','#fiche_photo','#fiche_envoi_ami','#popup_generique',divToShow) 
  //for(var i=0;i<arrBlocs.length;i++){
  //  $(arrBlocs[i]).css('z-index','10000');
  //} 
  $('#'+divToShow).css('z-index','10000');
}

function removeGlass(){
  if(!divShowed){return;}
  
  
  //debugger;
  //Au dessus pour les blocs en absolu
  //var arrBlocs = new Array('#fiche_instal','#fiche_photo','#fiche_envoi_ami','#popup_generique')  
  //for(var i=0;i<arrBlocs.length;i++){
  //  $(arrBlocs[i]).css('z-index','');
  //} 
  $('#'+divToShow_).css('z-index','');
  
  $('#'+divShowed).fadeOut('fast');
  
  $(divGlass).fadeOut('normal', function(){$(divGlass).remove();});
    
  
  divShowed=null;
  divGlass=null;
}

/*--------------------------------------------------------------------*/
/*------------------------GESTION PAGE DOCUMENTAIRE-------------------*/
/*--------------------------------------------------------------------*/


function retracteBarres(callback){
  $('#barres_bottom_left').animate({
    opacity: 1,
    left: '-=392'         
  }, 500, function() {
    // Animation complete.
  });
  $('#barres_bottom_right').animate({
    opacity: 1,
    left: '+=167'         
  }, 500, function() {
    // Animation complete.         
  });
  $('#barres_top_right').animate({
    opacity: 1,
    left: '1010px'         
  }, 500, function() {
    // Animation complete.     
  });        
  $('#barres_top_right_on').animate({
    opacity: 1,
    left: '1010px'         
  }, 500, function() {
    if(callback){callback();}
  });
}

function cacheBarresBas(yesno){
  if(yesno) {
    $('#barres_bottom_left').hide();
    $('#barres_bottom_right').hide();
    $('#barres_central').hide();
  }
  else{
    $('#barres_bottom_left').show();
    $('#barres_bottom_right').show();
    $('#barres_central').show();
  }
}

function extraitBarres(callback){
  $('#barres_bottom_left').animate({
    opacity: 1,
    left: '+=392'         
  }, 500, function() {
    // Animation complete.
  });
  $('#barres_bottom_right').animate({
    opacity: 1,
    left: '-=167'         
  }, 500, function() {          
    if(callback){callback();}      
  });
  
  if(lastdirection=='' || lastdirection=='up'){
    $('#ariane a').css('color','#FFFFFF');
    $('#ariane').css('color','#FFFFFF');
    $('#barres_top_right').animate({
      opacity: 1,
      left: '615px'         
    }, 500, function() {
      // Animation complete.   
    });
  }
  else{
    $('#ariane a').css('color','#9ca1a3');
    $('#ariane').css('color','#9ca1a3');
    $('#barres_top_right_on').animate({
      opacity: 1,
      left: '615px'         
    }, 500, function() {
      // Animation complete.   
    });
  }
  
}

function animDoc(direction) {
  lastdirection=direction;
  
  retracteBarres();
  
  $('#enter').fadeOut(500);
  $('#menugauche').slideUp(300, function(){$('#sousmenugauche').hide();});
  $('#menudroit').slideUp(500, function(){$('#sousmenudroit').hide();});    
  
  $('#flash').fadeOut(500);
  $('#logo').fadeOut(500, function(){
    
    if(lastdirection=='up'){
      cacheBarresBas(true);
      $('#divbottom').css('z-index','10');
      
      $('#afficheDoc').css('top','768px');
      $('#afficheDoc').animate({            
        top: '336px'         
      }, 1000);
      
      $('#divbottom').animate({            
        top: '0px'         
      }, 1000, function() {
        $('#divbottom').css('z-index','2');
        
        extraitBarres();
        $('#ariane').fadeIn(600);          
        
      });
    }
    
    if(lastdirection=='down'){
      $('#divtop').css('z-index','10');
      
      $('#afficheDoc').css('top','-435px');
      $('#afficheDoc').animate({            
        top: '0px'         
      }, 1000);
      
      $('#divtop').animate({            
        top: '433px'         
      }, 1000, function() {
        $('#divbottom').css('z-index','1');
        $('#divtop').css('z-index','2');
        extraitBarres();
        $('#ariane').fadeIn(600);
        
      });
    }  
    
        
  });
}

function showDoc(arbo, direction){
        
  lastdirection=direction;
  
  updateDoc(arbo, 'afficheDoc', root, function(){
  
    retracteBarres();
    $('#enter').fadeOut(500);
    $('#menugauche').slideUp(300, function(){$('#sousmenugauche').hide();});
    $('#menudroit').slideUp(500, function(){$('#sousmenudroit').hide();});    
    
    $('#flash').fadeOut(500);
    $('#logo').fadeOut(500, function(){
      
      if(lastdirection=='up'){
        cacheBarresBas(true);
        $('#divbottom').css('z-index','10');
        
        $('#afficheDoc').css('top','768px');
        $('#afficheDoc').animate({            
          top: '336px'         
        }, 1000);
        
        $('#divbottom').animate({            
          top: '0px'         
        }, 1000, function() {
          $('#divbottom').css('z-index','2');
          
          extraitBarres();
          $('#ariane').fadeIn(600);          
          
        });
      }
      
      if(lastdirection=='down'){
        $('#divtop').css('z-index','10');
        
        $('#afficheDoc').css('top','-435px');
        $('#afficheDoc').animate({            
          top: '0px'         
        }, 1000);
        
        $('#divtop').animate({            
          top: '433px'         
        }, 1000, function() {
          $('#divbottom').css('z-index','1');
          $('#divtop').css('z-index','2');
          extraitBarres();
          $('#ariane').fadeIn(600);
          
        });
      }  
      
          
    });
  
  });
}


function closeDoc(){
  
  $('#ariane').fadeOut(200); 
  retracteBarres(function(){
    if(lastdirection=='up'){
      $('#divbottom').css('z-index','10');                       
      
      $('#afficheDoc').animate({            
        top: '770px'         
      }, 1000);
    
      $('#divbottom').animate({            
        top: '433px'         
      }, 1000, function() {
        $('#divbottom').css('z-index','2');
        lastdirection='';
        cacheBarresBas(false);
        extraitBarres();
        $('#enter').fadeIn(500);
        
        $('#menugauche').slideDown(600);
        $('#menudroit').slideDown(900);
        $('#logo').fadeIn(800);
        $('#flash').fadeIn(800);
        
      });
    }
    
    if(lastdirection=='down'){
      $('#divtop').css('z-index','10');            
      
      $('#afficheDoc').animate({            
        top: '-435px'         
      }, 1000);
      
      $('#divtop').animate({            
        top: '0px'         
      }, 1000, function() {
        $('#divbottom').css('z-index','1');
        $('#divtop').css('z-index','2');
        lastdirection='';
        extraitBarres();
        $('#enter').fadeIn(500);
        
        $('#menugauche').slideDown(600);
        $('#menudroit').slideDown(900);
        $('#logo').fadeIn(800);
        $('#flash').fadeIn(800);
        
      });
    }      
  });  
}
