/* -------------------------------------------------*/
/* = Parseur d'url ---------------------------------*/
/* -------------------------------------------------*/
jQuery.parseUrl = {
  fileOnly : ((window.location.pathname).split("/"))[(window.location.pathname).split("/").length-1],
  filePath :   window.location.pathname,
  parent   : ((window.location.pathname).split("/"))[(window.location.pathname).split("/").length-2],
  parent2  : ((window.location.pathname).split("/"))[(window.location.pathname).split("/").length-3]
}

$(document).ready(function(){

/* --------------------------------------------------------*/
/* = Conteneur d'image de la largeur de l'image -----------*/
/* --------------------------------------------------------*/

	if($('.image').length > 0){
		var imgPreload = new Image();
		imgPreload.onload = function() {
			var imgWidth = $('.image img').attr('width');
			$('.image').css('width',imgWidth+'px');
		}
		imgPreload.src = $('.image img').attr('src');

	}

/* --------------------------------------------------------*/
/* = Slider --------------------------------*/
/* --------------------------------------------------------*/
	if($('#accueil').length > 0){
		 $('#s3slider').s3Slider({
		  timeOut: 4000
	   });
	}

/* --------------------------------------------------------*/
/* = Parseur d'url [Suite] --------------------------------*/
/* --------------------------------------------------------*/

  var nom_Section = $('#colonne-gauche h3:first').text();
  var lien_Section = ($.parseUrl.fileOnly)
  var nom_sousSection = $('#contenu h2:first').text();
  var lien_sousSection = ($.parseUrl.parent)
  var lien_sousSection2 = ($.parseUrl.parent2)
  var lien_niveau3 = '/'+lien_sousSection2+'/'+lien_sousSection+'.php'

/* -------------------------------------------------------------*/
/* = Sélection pour le menu d'entete ---------------------------*/
/* -------------------------------------------------------------*/

  select_entete ()

  function select_entete (){

    $('#entete ul.ixfont li a').each(function(){

      var current_href = $(this).attr("href");
	  var filePath = $.parseUrl.filePath;
	  filePath = filePath.match(/^(.+)\??.?/)[1];

      if (filePath == current_href) {
        $(this).addClass("choisi");
      }

    });
  }

/* -------------------------------------------------------------*/
/* = Ajout de style pour les H3 ---------------------------*/
/* -------------------------------------------------------------*/

 $('body#accueil h3').not('h3:first').css('margin-top','25px');


/* -----------------------------------------------*/
/* = Menu en accordéon ---------------------------*/
/* -----------------------------------------------*/

  //Ajout de class titre pour les titre ayant un sous-menu
  $('#menu > ul > li:has(ul)').children('a').addClass('titre');

   $('#menu > ul').accordion({
      header:'a.titre',
      alwaysOpen: false,
      autoheight: false,
      active: '.selected',
      navigation: true
    });

  $('a.current').addClass('selected');

  $('#menu > ul > li > a').click(function (){
	  $('#menu > ul > li > .current').removeClass('selected')
  })

  $('#menu a').each(function(){
    var current_left = $(this).attr("href");
	var filePath = $.parseUrl.filePath;
	filePath = filePath.match(/^(.+)\??.?/)[1];

    if (filePath == current_left) {
          $(this).addClass("current").parent().parent().show().parent().addClass('selected').children('a').addClass('selected');
    }
  });


/* -----------------------------------------------------------*/
/* = Ajustement de la taille de font -------------------------*/
/* -----------------------------------------------------------*/

  // si un cookie existe, on l'applique
  if ($.cookie('ixfont_size')) {
    $('body').addClass($.cookie('ixfont_size'));
  } else {
    var defaultfont = $('.ixfont li.lien_vert a[@rel=default-font]').attr("class") || '';
    $('body').addClass(defaultfont);
  }

  // on bâtit un array de tailles disponibles
  var ixfont_tailles = new Array();
  $('.ixfont li.lien_vert a').each(function() {
    ixfont_tailles.push($(this).attr("class"));
  });

  $('.ixfont li.lien_vert a').click(function() {

    // on enlève les classes existantes
    var newclass = $(this).attr("class");
    $.each(ixfont_tailles,function(i, n) {
      $('body').removeClass(n);
    });

    // on applique la nouvelle classe
    $('body').addClass(newclass);

    // on place le cookie
    $.cookie('ixfont_size', newclass, { expires: 365 });

    return false

    ;
  });

/* ----------------------------------------------------*/
/* = Création du fil d'ariane -------------------------*/
/* ----------------------------------------------------*/

  //	1er niveau
  var fil = '<ul id="fil"><li><a href="/default.php">Accueil</a>&nbsp;&nbsp;&gt;&nbsp;</li><li>'+ nom_Section +'</li></ul>'
  // 2e et 3e niveau
  if (lien_sousSection.length > 0 ){
    // 3e Niveau
    if (lien_sousSection2.length > 0){
      
    	 
		if($.parseUrl.filePath == '/intervention-sectorielle/ressources-naturelles-territoire/default.php'){
			$('#menu a[href="'+$.parseUrl.filePath+'"]').parent().parent().prev().addClass('current selected');
		} else {
		 $('#menu > ul > li:has(ul) a:contains('+nom_sousSection+')').addClass('current selected');
		}
      
//      console.log($.parseUrl.filePath);
      
      var sousSection3 = $('a.current').parents('ul').siblings('.titre');
      var nom_sousSection2 = $(sousSection3).text();
      $(sousSection3).addClass('selected current').parent('li').addClass('selected');
      $(sousSection3).next('ul').show()

      $('.titre').click(function (){
        $(sousSection3).next('ul').slideUp()
      });

      fil = '<ul id="fil"><li><a href="/default.php">Accueil</a>&nbsp;&nbsp;&gt;&nbsp;</li><li><a href="/'+lien_sousSection2+'/default.php">'+nom_sousSection2+'</a>&nbsp;&nbsp;&gt;&nbsp;</li><li><a href="'+lien_niveau3+'">'+ nom_sousSection +'</a>&nbsp;&nbsp;&gt;&nbsp;</li><li>'+ nom_Section +'</li></ul>'
    }
    // 2e niveau
    else {
      $('#menu > ul > li').not(':has(ul)').children('a[href*="'+lien_sousSection+'"]').addClass('selected')

      if($.parseUrl.filePath != $('#menu > ul > li').not(':has(ul)').children('a.current').attr("href")){
        fil ='<ul id="fil"><li><a href="/default.php">Accueil</a>&nbsp;&nbsp;&gt;&nbsp;</li><li><a href="/'+ lien_sousSection +'/default.php">'+ nom_sousSection +'</a>&nbsp;&nbsp;&gt;&nbsp;</li><li>'+ nom_Section +'</li></ul>'
	  }

	  // Execption pour le calendrier
	  if($.parseUrl.filePath == '/calendrier/default.php'){
		fil = '<ul id="fil"><li><a href="/default.php">Accueil</a>&nbsp;&nbsp;&gt;&nbsp;</li><li>'+ nom_Section +'</li></ul>'
	}

    }
  }
  if($('body#accueil').length == 0){
  	$('h2').after(fil);
  }

/* -------------------------------------------------------------*/
/* = Ajout de couleurs alternative aux tableaux de contenu -----*/
/* -------------------------------------------------------------*/

  $("#colonne-gauche table tr:even").addClass("alt");
  $("#colonne-gauche table th:last").addClass("derniereColonne");

/* --------------------------------------------------------------------*/
/* = Ajout d'un icone pour les liens externes -------------------------*/
/* --------------------------------------------------------------------*/

	fichiersInclus = /.+\.(pdf|ppt|pps|zip|xls|zip|csv|doc|txt|odt)$/;

	$("a[@href^=http]:not([@href*="+window.location.host+"])").attr('target', '_blank');

	$("a")
	.each(function() {
		if ($(this).attr('href').match(fichiersInclus)) {
			$(this).attr('target', '_blank');
		}
	 });


/* --------------------------------------------------------------------*/
/* = Hide le dernier filet à l'accueil section dans notre mire --------*/
/* --------------------------------------------------------------------*/

  $('body#accueil div.conteneur hr.espace:last').hide();


/* -------------------------------------------------------------*/
/* = Ajout de couleurs au calendrier ---------------------------*/
/* -------------------------------------------------------------*/

  $('#calendrier table tr th:first-child, #calendrier table tr td:first-child, #calendrier table tr th:last-child, #calendrier table tr td:last-child').addClass('weekend');

});


