/* ****************************************************

	@file		  global.js
	@description  Comportements globaux
	@version	  20091126

***************************************************** */
(function($){

	$(function() {

/*
 * @section Liens externes PDF dans une nouvelle fenêtre
******************************************************************************/
		$('a[href$=\.pdf]').attr('target', '_blank').addClass('lien-pdf').parent().addClass('item-pdf');
		$('a[href^=http://]').not('a[href*='+window.location.host+']').attr('target', '_blank');

/*
 * @section Soumission automatique des formulaires
******************************************************************************/
		$('body.template-nouvelles #categorie').change(function() {
			$(this).parents('form').each(function() {
				this.submit();
			});
		});

		$('.documentation-filtre select').change(function() {
			$(this).parents('form').each(function() {
				this.submit();
			});
		});

/*
 * @section Tooltips
******************************************************************************/

		$('a.tooltip').tooltip({
			delay : 100,
			track : true,
			showURL : false
		});

/*
 * @section Premiers H2 et H3
******************************************************************************/

		$('h2:first-child, h3:first-child').addClass('first-child');

/*
 * @section Afficher/cacher le formulaire d’ajout de document
******************************************************************************/

		$('#documentation-directory-page .ajout .form').hide().prev().find('a').click(function() {
			$(this).parent().next().slideToggle(200);
			return false;
		}).parents('.ajout.visible').find('.form').show();

/* @section Accueil {{{
******************************************************************************/

/* Class Crossfade {{{
******************************************************************************/

		var Crossfade = function(opts) {

			opts = opts || {};
			
			var _this = this;
			this.$panels = $(opts.panels);
			this.panelCount = this.$panels.length;
			this.currentPanel = 0;
			this.interval = opts.interval || 2500;
			this.fadeTimer;
			
			this.$panels.each(function(){
				$this = $(this);
				if($this.css('display') == 'none') {
					console.log($this);
					$this.css({'opacity':'0','display':'block'});
				}
			});

			this.startAutoFade();
				
		};
		Crossfade.prototype = {
			startAutoFade: function() {
				var _this = this;
				this.fadeTimer = setInterval(function() {
					_this.fadeTo();
				}, this.interval);
			},
			fadeTo: function(panelID) {
				var fadeToPanel = (typeof panelID != "undefined") ? panelID : this.currentPanel + 1;
				fadeToPanel = (fadeToPanel == this.panelCount) ? 0 : fadeToPanel;
				
				$panel = $(this.$panels).eq(this.currentPanel);
				this.currentPanel = fadeToPanel;

				$newPanel = $(this.$panels).eq(this.currentPanel);
				$newPanel.css('z-index', 1000);
			
				$panel.css('z-index', 1);
			
				$panel.animate({
					opacity: 0
				}, 'slow', function() {
					$newPanel.animate({
						opacity: 1
					}, 'slow');
				});
			}
		};

/*}}}*/

		if($('.section-accueil').length) {

			$innerText = $('#crossfadeText .inner');

			if ($innerText.length > 0) {

				new Crossfade({
					panels: '#crossfadeText .inner',
					interval: 10000
				});

			}

		}

/*}}}*/


	});

})(jQuery);




