/**
 * Fichier comportement
 *
 * @author Antoine Rolland
 * @date 19/03/2009
 */

$(document).ready(function(){
    
    /**
     * Montrer/cacher les romes des resultats de la recherche grand public
     */
    $('a#cacherRome').click(function (){
	$('span#filtreRomes').hide();
	$('span#romesMontres').hide();
	$('span#romesCaches').show();
	return false;
    });
    
    $('a#montrerRome').click(function (){
	$('span#romesCaches').hide();
	$('span#romesMontres').show();
	$('span#filtreRomes').show();
	return false;
    });
    
  /**
   * tableaux à entetes fixes
   */

	$('.bigTableContainer table').each(function(){$(this).fixedHeader(10, 500)});
	$('table#workFlowAdmin, ' +
	  'table#recapitulatifWorkflow, ' +
	  'table#accesDossier, ' +
	  'table#affectationProfil, ' +
	  'table#statDetailEtatInstructeur, ' +
	  'table#statDetailRenouvellementN-1, ' +
	  'table#statDetailRenouvellementN, ' +
	  'table#gestionHabilitationModule').each(function(){$(this).fixedHeader(10, 500, 765)});
	
  /**
   * multi fieldset présenté sous forme d'onglet
   */
  $('dl.multiFieldset').dl2Onglet({
	  cssClass2Remove: ['multiFieldset'],
	  elemSelector2Remove: ['dd>a', 'ul.navigationInterne'],
	  defaultBlockIndex: 1
  });
  
  /**
   * Menu déroulant
   */
  $('div#navigationLaterale ul.navEspaceReserve > li > ul.closed').hide();
  
  $('div#navigationLaterale ul.navEspaceReserve > li > p').click(
      function (){
    	var isOpened = $(this).next('.opened').length>0;
    	if (isOpened) {
    		$(this).attr('class','closed');
    		$(this).next().attr('class','closed');
    	} else {
    		$(this).attr('class','opened');
    		$(this).next().attr('class','opened');
    	}
        $(this).next().toggle('slow');
      }
  );
  
//  if(!jQuery.support.leadingWhitespace){
//    $('div#navigationLaterale ul.navEspaceReserve li').css('background','none');
//    $('div#navigationLaterale ul.navEspaceReserve li ul.closed').css('background','transparent url(../images/puceOrange.gif) repeat-y scroll left top');
//  }
  
  /**
   * Confirmation
   */
  // ajout des containers
  $('body').append('<div class="confirmationContainer"></div>' +
					'  <div class="confirmation">' +
					'    <p class="message"></p>' +
					'    <p class="submit">' +
					'      <a class="confirmer">Confirmer</a>' +
					'      <a class="annuler">Annuler</a>' +
					'    </p>' +
					'  </div>');
  
  $.each($('a.confirmation'), function(i, val) {
    //Passage par le title
    $(this).attr('oldHref', $(this).attr('href'));
    $(this).attr('href', '#confirm');
  });
 
  $('a.confirmation').click(
      function (){
        $('div.confirmationContainer').css('width', $(window).width());
        $('div.confirmationContainer').css('height', $(window).height());
        $('div.confirmationContainer').css('opacity', '0.80');
        $('div.confirmationContainer').fadeIn('slow');
        
        var scrollTop = 0;
        if (!$.support.leadingWhitespace){
        	scrollTop = document.documentElement.scrollTop;
        }
        
        $('div.confirmation').css('top', (($(window).height()*0.85 - $('div.confirmation').height()) / 2 + scrollTop) + 'px');
        $('div.confirmation').css('left', (($(window).width() - $('div.confirmation').width()) / 2) + 'px');        
        $('div.confirmation').show();
        
        $('div.confirmation p.message').text('Etes vous sur de vouloir effectuer cette action ?');
        var destination = $(this).attr('oldHref');
        if (-1 == destination.indexOf('?')){
          destination += '?';
        }
        else{
          destination += '&';
        }
        $('div.confirmation a.confirmer').attr('href', destination + 'confirm=ok');
        if (!jQuery.support.leadingWhitespace){
        	$('select').fadeOut('slow');//css('visibility', 'hidden');
        }
        // annule l'action par defaut du lien
        return false;
      }
  );
  
  $('div.confirmation a.annuler').click(
      function (){
        $('div.confirmation').hide();  
        $('div.confirmationContainer').fadeOut('slow');
        if (!jQuery.support.leadingWhitespace){
        	$('select').fadeIn('slow');//css('visibility', 'visible');
        }
      }
  );  
  
  /**
   * Montrer archive
   */
  $('div.dossierArchive').hide().removeClass('dossierArchive').addClass('dossierArchiveDynamique');
  $('a.montrerArchive').attr('href', '#montrerArchive');
  
  $('a.montrerArchive').click(
      function (){
        $('div.dossierArchiveDynamique').slideToggle();
      }
  );
  
  /**
   * Bouton fermer
   */
  $('a.fermer').attr('href', '#fermer');
  $('a.fermer').click(
    function (){
      window.close();
    }
  );

  /**
   * Bouton imprimer
   */
  $('a.imprimer').attr('href', '#imprimer');
  $('a.imprimer').click(
    function (){
      window.print();
    }
  );  
  
  /**
   * Liste déroulante choix multiple
   */
  $('select#rechercheExperteFormFilter_regions, ' +
  	'select#rechercheAncienTitreFormFilter_regions').multiSelect({
    selectAll: true,
    selectAllText: 'Sélectionner toutes les régions',
    noneSelected: 'Choisir une région',
    oneOrMoreSelected: '% région(s) sélectionnée(s)'
  });
  $('span.multiSelectHelp').css('display', 'none');

  /**
   * Suppression des liens Haut/Bas de page pour les pages courtes
   */
  if ($('body').height()<1300){
	  $('a.goTop, a.goDown').remove();
  }
  
  /**
   * Gestion de l'affichage des liens de navigation récente
   */
  var navCt = $('div#navigation_conversation');
  var navItems = navCt.find('li');
  
  var maxVisibleItem = 3;
  if (navItems.length > maxVisibleItem) {
	  var seeMoreLess = $('<span class="seeLess"/>');
	  navCt.append(seeMoreLess);
	  $(seeMoreLess).click(function(){
			  var className = this.className;
			  if (className == 'seeMore'){
				  $('div#navigation_conversation li').each(function(){
					  $(this).css('display', 'block');
				  });
				  this.className = 'seeLess';
				  $(this).text('Voir moins d\'éléments de l\'historique...');
			  } else {
				  $('div#navigation_conversation li:gt('+ (maxVisibleItem - 1) +')').each(function(){
					  $(this).css('display', 'none');
				  });
				  this.className = 'seeMore';
				  $(this).text('Voir plus d\'éléments de l\'historique...');
			  }
	  });
	  $(seeMoreLess).click();
	  
			  
  }

  /**
   * gestion de la colorisation du tableau agenda
   */
  var lines = $('table#agendaDossier tbody tr');
  var levels = new Array();
  var lineLevel = new Array();
  $(lines).each(function(){//$.inArray('01.1.a', checkTest) != -1
	 var order = $(this).find('td input').attr('value');
	 if ($.inArray(order, levels) == -1){
		 levels.push(order);
	 }
	 if (lineLevel[order] == undefined){
		 lineLevel[order] = new Array();
	 }
	 lineLevel[order].push(this);
	 
  });
  levels = levels.sort();
  var colors = ['#FCC', '#FDF', '#CCF', '#DFF', '#CFC', '#FFD'];
  
  var cpt = 0;
  for (var i in levels){
	  if (cpt < colors.length){
		  $(lineLevel[levels[i]]).each(function(){
			  $(this).find('td').css('background-color',colors[cpt]);
		  });
	  }
	  cpt++;
  }
  
  /**
   * Correction bugs IE6
   */
  
  //PNG FIX - problème avec le background position
  //$(document).pngFix();
  
  //Radio/Check
  if (!jQuery.support.leadingWhitespace){
    $('input[type=radio]').css('border', 'none');
    $('input[type=radio]').css('background', 'transparent');
    $('input[type=radio]').css('margin', '0');
    $('input[type=radio]').css('padding', '0');
    $('input[type=checkbox]').css('border', 'none');
    $('input[type=checkbox]').css('background', 'transparent');
    $('input[type=checkbox]').css('margin', '0');
    $('input[type=checkbox]').css('padding', '0');
  }
  
  if (have_to_check_accessibility){
	  CheckAccessibility.run(null,'#zoneDeTravail');
	  CheckAccessibility.post('/cncpref/logCheck_dev.php');
	  gg = CheckAccessibility.getLog();
  }
});