/* ****************************************************

	@file		  global.js
	@description  Comportements globaux
	@author		  remi (ixmedia.com)
	@version	  20090504

***************************************************** */

(function($){

	$(function() {
	
/*
 * @section Javascript est activé
 ******************************************************************************/
		$('#wrap').removeClass('nojs');

/*
 * @section Carte du réseau
 ******************************************************************************/

	if (window.DetectFlashVer && DetectFlashVer(9,0,0)) {
		$('#contenu.carte').find('.entry-content').show();
		$('#contenu.carte').find('.axes, .textes').hide();
	}
 
	var carte_section = '';
	$('#carte-menu .menu-fermer a').css('opacity', 0);
	$('#contenu.carte .barre .tab').css('left', '-74px');
	$('#contenu.carte .barre a').click(function() {

		$('#carte-menu').css('top', ($('.entry-content').position().top)+'px' );
		var id = $(this).parent().attr('class').match(/icone-([0-9]+)/)[1];

		if (carte_section == id) { fermer_menu(); return false; }
		carte_section = id;

		$('#carte-menu .menu-inner').html( $('.texte-'+id).html() );

		$(this).find('.menu-contenu').css('overflow', 'hidden');
		$('#carte-menu').show().stop().animate({
			width: '287px'
		},{
			duration: 300,
			easing: 'easeOutQuart',
			complete: function() {
				$(this).find('.menu-contenu').css('overflow', 'auto');
				$('#carte-menu .menu-fermer a').show().animate({
					opacity: 1
				},{
					duration: 300
				});
			}
		});

		var barre_options = {
			top : ($(this).position().top - 4) + 'px'
		}

		if ($('#contenu.carte .barre .tab').css('left') != '0px') {
			barre_options.left = 0;
			$('#contenu.carte .barre .tab').css('top', barre_options.top);
		}
		$('#contenu.carte .barre .tab').show().stop().animate(barre_options,
		{
			duration: 300,
			easing: 'easeOutQuart'
		});

		return false;
		
	});

	$('#carte-menu .menu-fermer a').click(fermer_menu);
	
	function fermer_menu() {
		carte_section = '';
		$('#carte-menu .menu-fermer a').stop().animate({
			opacity: 0
		},{
			duration: 300
		});

		$('#contenu.carte .barre .tab').show().stop().animate({
			left : '-74px'
		},{
			duration: 300,
			easing: 'easeOutQuart'

		});

		$('#carte-menu').find('.menu-contenu').css('overflow', 'hidden');

		$('#carte-menu').stop().animate({
			width: '0'
		},{
			duration: 300,
			easing: 'easeOutQuart',
			complete: function() {
				$(this).hide();
			}
		});
	}


/*
 * @section Rotation des icones à l'accueil
 ******************************************************************************/
 
	function nextIcone() {
		var $actuel = $('#intro .icones li:visible');
		var $next = $actuel.next().length ? $actuel.next() : $('#intro .icones li:first');
		
		$.browser.msie6 = ( $.browser.msie && $.browser.version < 7 );
		
		if ( $.browser.msie6 ) {
		
			//$actuel.css('border', '2px solid red');
			$actuel.hide();
			$next.css('display', 'block');
		} else {
			$actuel.stop().animate({
				opacity: 0
			},{
				duration: 1200,
				complete: function() {
					$(this).hide();
				}
			});
			$next.stop().css('opacity', 0).show().animate({
				opacity: 1
			},{
				duration: 1200
			});
		}
	}
	$('#intro .icones li:first').siblings().css('opacity', 0).hide();
	setInterval(function() {
		nextIcone();
	}, 5000);
 
/*
 * @section Champ de recherche cliquable
 ******************************************************************************/
		$('#s:not(.plein)').focus(function() {
			if (this.value == this.defaultValue) {
				$(this).val('');
			}
		}).blur(function() {
			if (this.value == '') {
				$(this).val(this.defaultValue);
			}
		});

/*
 * @section Tooltips sur la carte
 ******************************************************************************/
		$('.tooltip').hide();
		$('.tooltip .tooltip-fermer a').click(function() {
			fermerTooltip();
			getFlashMovie("map").onFermerInfosTrajet();
		});
		
	});
})(jQuery);


	function fermerTooltip(callback) {
		$('.tooltip').animate({
			opacity : 0
		},{
			duration: 300,
			complete: function() {
				$(this).hide();
				if (callback) {
					callback.call();
				}
			}
		});
	}

/*
 * @section Tooltips sur la carte
 ******************************************************************************/
	function afficherTooltip(id) {
	
		var callback  = function(id) {
		
			var axe = $('.axe-'+id);
			$('.tooltip').attr('class', '').addClass('tooltip').addClass('tooltip-'+id);
			$('.tooltip .tooltip-contenu').html( axe.find('.axe-contenu').html() );
			$('.tooltip h1').html( axe.find('h1').html() );
			$('.tooltip').css('opacity', 0).show().animate({
				opacity: 1
			},{
				duration: 300
			});
		}
	
		if ($('.tooltip').is(':visible')) {
			fermerTooltip(function() { callback(id) });
		} else {
			callback(id);
		}
		
	}
