$(document).ready(function(){
	// ajoute u séparateur pour le menu du pied de page
	$('#footer #nav-secondaire li').slice(0, -1).append('<span>|</span>');
  
  /* ajoute un target _blank au lien avec une classe external */
	$("a[rel='external'],a[rel='document'], a[href^='http://'], a[href$='.pdf'], a[href$='.doc'], a[href$='.xls'], a[href$='.txt']").each(function(){
    if($(this).attr('href').indexOf('http://pref-vienne.demarchesenligne.fr') < 0) {
      $(this).attr({ title: ($(this).attr("title"))?$(this).attr("title"):$(this).attr("href")+" (nouvelle fenêtre)" });
      $(this).click(function() {window.open($(this).attr('href'));return false;});
    }
	});
	/*--------------------*/

  // flashs
  var flashvars = {};
  var params = {};
  var attributes = {};
  params.scale = "noscale";
  params.wmode = "transparent",
  params.allowFullScreen = "true",
  params.allowScriptAccess = "sameDomain",
  params.bgcolor = "ffffff";
  
  $('#home #header div.hr').attr('id', 'flash-home');
  if($('#flash-home').length != 0) {
    if(typeof(swfobject) != 'undefined'){
      swfobject.embedSWF("/flash/home.swf", "flash-home", "463", "320", "8.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
    }
  }

  if($('#reate-anim').length != 0) {
    if(typeof(swfobject) != 'undefined'){
      swfobject.embedSWF("/flash/" + $('#reate-anim').attr('class') + ".swf", "reate-anim", "448", "500", "8.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
    }
  }


    /* gestion pseudo classe last-child sur main-nav */
  $('#main-nav li:last-child a').attr('class', 'dafi86');
  /*----------------------------------------------------------*/

	/* Recherche */
	$("#search").css('height','30px');
	$("#search p:not(.submit) input").each(function(){
		$(this).attr('value',$(this).prev().html());
		$(this).prev().css('display','none');
		$(this).click(function() {
	      if ($(this).val() == $(this).prev().html()){
	        $(this).val('');
	      }
	    });
	    $(this).blur(function() {
	      if ($(this).val() == '')
	      {
	        $(this).val($(this).prev().html());
	      }
	    });
	});
	/*--------------------*/

	/* navigation accès directs */
	$('#direct-access').css('display','none');
	$('#direct-access').before('<p id="js-direct-access-link"><a title="accès directs" href="#" class="open">accès directs</a></p>');
	/* mouse events */
	$('#js-direct-access-link').click(function(){
		if($('#js-direct-access-link a').attr('class')=='open')
		{
			$('#direct-access').fadeIn(200);
			$('#js-direct-access-link a').attr('class', 'close');
		} else {
			$('#direct-access').fadeOut(200);
			$('#js-direct-access-link a').attr('class', 'open');
		}

	})

	/* gestion message d'alerte */
	if(document.getElementById('alerte')){
		$('#alerte').attr('class', 'js-enabled');
		$('#alerte').append('<p id="js-close-alert"><a href="#">Fermer</a></p>');
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		  $("body","html").css({height: "100%", width: "100%"});
		  $("html").css("overflow","hidden");
		  if (document.getElementById("HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='HideSelect'></iframe><div id='trame'></div>");
		  }
		}else{//all others
		  if(document.getElementById("TB_overlay") === null){
			$("body").append("<div id='trame'></div>");
		  }
		}
		$('#js-close-alert a').click(function(){
			if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
				$("html").css("overflow","auto");
				$('#HideSelect').hide();
			}
			$('#alerte').fadeOut(500);
			$('#trame').fadeOut(500);

		});
	}
	/*--------------------------*/

	/* tooltip */
	if(document.getElementById('services-en-ligne'))$('#services-en-ligne a').tooltip({ track: true, showURL: false });

  $(".picture-left a").each(function(){
    if($(this).children("img").attr('alt')!="Image associée à l'article")
      $(this).attr('title', $(this).children("img").attr('alt'));
   });

  $(".picture-right a").each(function(){
  if($(this).children("img").attr('alt')!="Image associée à l'article")
    $(this).attr('title', $(this).children("img").attr('alt'))
  });

  $(".picture-left a").tooltip({ track: false, showURL: false, extraClass: 'descr' });
  $(".picture-right a").tooltip({ track: false, showURL: false, extraClass: 'descr' });

	/* fancy zoom */
	$('p.picture-left a').fancybox();
	$('p.picture-right a').fancybox();

	/* séparation entre les items  */
	if ($('.pagination')) {
	  $('.pagination li[@class!="shorthands"]').each( function(index, el) {
	    if (index < $('.pagination li[@class!="shorthands"]').size() - 1) {
	      $(el).append('<span>&nbsp;|&nbsp;</span>');
	    }
	  });
	}

	/*init sliding div*/
	$('ul.faq').before('<p id="js-collapse" class="uncollapse"><a href="#" class="btn">Tout déplier</a></p>');
	$(".answer").prev().each(function(){
		$(".answer").css("display","none");
		$(".answer").prev().css('cursor','pointer');
		$(".answer").prev().css('background','transparent url(/css/img/icons/expand.gif) 0 6px no-repeat');
	});

	if($(".expand")[0]!=undefined){
		//alert($(".expand")[0]);
		$("h4").after('<a href="#" class="toggle_all">afficher/masquer tout</a>');
	}

	/*sliding actions*/
	$("p#js-collapse").toggle(
		function(){$(".answer").slideDown(300);$("#js-collapse a").html('Tout replier');$(".answer").prev().css('background-position','0 -1006px')},
		function(){$(".answer").slideUp(300);$("#js-collapse a").html('Tout déplier');$(".answer").prev().css('background-position','0 6px')}
	);
	$(".answer").prev().toggle(
		function(){$(this).next().slideDown(300);$(this).css('background-position','0 -1006px')},
		function(){$(this).next().slideUp(300);$(this).css('background-position','0 6px')}
	);

	if(document.getElementById("liste-cantons")){
		$("#liste-cantons").before('<div id="illustration-cantons"></div>');
		var flashvars = {
			activeCanton: 'nop'
		};
		var params = {
			swliveconnect: true,
      wmode: 'transparent'
		};
		swfobject.embedSWF("/flash/cantons.swf", "illustration-cantons", "405", "450", "8.0.0", "flash/expressInstall.swf", flashvars, params);
	};

});