$(function() { 
    var ua = navigator.userAgent.toLowerCase(); 
    var myBrowsers = { 
        // Déjà détectés en natif par jQuery 
        mozilla: $.browser.mozilla, 
        safari:  $.browser.safari, 
        opera:   $.browser.opera, 
        ie:      $.browser.msie, 
        // Quelques améliorations et ajouts 
        ie6:     $.browser.msie && ($.browser.version < 7), 
        ie7:     $.browser.msie && ($.browser.version == 7), 
        ie8:     $.browser.msie && ($.browser.version > 7), 
        iphone:  /iphone/.test(ua), 
        chrome:  /chrome/.test(ua), 
        firefox: /firefox/.test(ua), 
        webkit:  /webkit/.test(ua), 
        // Détection de plateformes 
        osx:     /mac os x/.test(ua), 
        win:     /win/.test(ua), 
        linux:   /linux/.test(ua) 
    }; 
    $.each(myBrowsers, function(a, b) { 
        if (b)
		{
			$('html').addClass(a);
		}
    }); 
}); 



$(document).ready(function()
{
	//actus - centrage des pages
	var padding = Math.floor(($('.a_la_une').width() - $('.a_la_une .pages').width()) / 2);
	$('.a_la_une .pages').css({'left':padding+'px'});
	
	
	
	//menu
	$('.projet div.menu ul li').each(function(i)
	{
		var slideTimer = null;
		var slideTime = 20;
		var direction = 0;
		var item = null;
		
		$(this).css({'margin-left':'-115px'});
		
		$(this).children('a').mouseenter(function ()
		{
			direction = 1;
			item = $(this).parent();
			
			if (slideTimer) clearInterval(slideTimer);
			slideTimer = setInterval(slideMenu, slideTime);
		});
		
		$(this).children('a').mouseleave(function ()
		{
			direction = -1;
			item = $(this).parent();
			
			if (slideTimer) clearInterval(slideTimer);
			slideTimer = setInterval(slideMenu, slideTime);
		});
		
		function slideMenu()
		{
			var marginLeft = parseInt($(item).css('margin-left')) + (5 * direction);
			
			if (marginLeft <= 0 && marginLeft >= -115)
			{
				$(item).css({'margin-left':marginLeft+'px'});
			}
			else
			{
				clearInterval(slideTimer);
			}
		};
	});
	
	
	
	//background
	$('div.gpv').css({'background':'url('+$('.gpv .contenu .gpv_bg').html()+') no-repeat 0px 20px'});
	
	
	
	//bloc "à la une"
	var itemActu = 1;
	var actusTimer = null;
	
	$('.a_la_une .pages ul li a').click(function ()
	{
		$('.a_la_une .texte ul li').hide();
		$('.a_la_une .texte ul li.item_' + $(this).html()).show();
		
		if (actusTimer) clearInterval(actusTimer);
		actusTimer = setInterval(slideActus, actusTime);
		itemActu = $(this).html();
	});
	
	$('.a_la_une .pages').each(function (i)
	{
		if (actusTimer) clearInterval(actusTimer);
		actusTimer = setInterval(slideActus, actusTime);
	});
	
	function slideActus()
	{
		$('.a_la_une .texte ul li').hide();
		$('.a_la_une .texte ul li.item_' + itemActu).show();
		itemActu++;
		
		if (itemActu > nbPages) itemActu = 1;
	}
	
	
	
	//onglets sous-rubriques
	var largeur = 0;
	
	$('.projet .contenu .page .texte .sous-rubriques ul li').each(function(i)
	{
		largeur += $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
	});
	
	largeur = $('.projet .contenu .page .texte .sous-rubriques').width() - largeur - 1;
	$('.projet .contenu .page .texte .sous-rubriques ul li.separator').css({'width':largeur+'px'});
	
	
	
	//google maps
	$('.projet .contenu .page .texte .sous-rubriques ul li.map a').click(function ()
	{
		$('.projet .contenu .page .texte .content').toggle();
		$('.projet .contenu .page .texte .content_left').toggle();
		$('.projet .contenu .page .texte .content_right').toggle();
		$('.projet .contenu .page .texte .google_maps').toggle();
		$('.projet .contenu .page .texte .jScrollPaneContainer').toggle();
		$('.projet .contenu .page .texte .jScrollPaneContainer .content').show();
		$('.projet .contenu .page .texte .jScrollPaneContainer .content_left').show();
		
		if ($('.projet .contenu .page .texte .google_maps').css('display') == "block") showGmap();
		
		return(false);
	});
	
	
	
	//colonne droite des rubriques et sous-rubriques (centrage vertical)
	var paddingTopTexteDroite = Math.floor((323 - $('.projet .contenu .page .texte .content_right').height()) / 2);
	var heightTexteDroite = 323 - paddingTopTexteDroite;
	
	$('.projet .contenu .page .texte .content_right').css({'padding-top':paddingTopTexteDroite+'px'});
	$('.projet .contenu .page .texte .content_right').css({'height':heightTexteDroite+'px'});
	
	$('.projet.espace_pro .contenu .page .texte .content_right').css({'padding-top':'0px'});
	$('.projet.espace_pro .contenu .page .texte .content_right').css({'height':'323px'});
	
	
	
	//scrollbars
	$('.contenu .page.transverse .texte .content').jScrollPane();
	$('.projet .contenu .page .texte .content').jScrollPane();
	$('.projet .contenu .page .texte .content_left').jScrollPane();
	
	if ($('.projet .contenu .page .texte .content_left').height() > $('.projet .contenu .page .texte .jScrollPaneContainer').height())
	{
		$('.projet .contenu .page .texte .content_right').css({'width':'140px','border-left':'0px'});
	}
	else if ($('.projet .contenu .page .texte .content').height() <= $('.projet .contenu .page .texte .jScrollPaneContainer').height())
	{
		//alert('pas scroll');
		$('.projet .contenu .page .texte .content').css({'width':'690px'});
	}
	
	
	
	//padding-right pour les vidéos
	$('.projet .contenu .page .texte .jScrollPaneContainer .jScrollPaneTrack').parent().children('.content').children('.video').css('padding-right','0px');
	$('.contenu .page.transverse .texte .jScrollPaneContainer .jScrollPaneTrack').parent().children('.content').children('.video').css('padding-right','0px');
	
	
	
	//agrandissement des images de contenu
	$('.projet .contenu .page .texte .class-image .attribute-image').each(function(i)
	{
		var src = $(this).children('img').attr("src");
		src = src.replace('_medium.','.');
		
		$(this).html("<a href='"+src+"' class='thickbox' rel='gpv_content'>" + trim($(this).html()) + "</a>");
	});
	
	function trim (myString)
	{
		return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
	}
});
