$(function(){
//CLUSTER
	var clusterWidth=0;
	$('.cluster li').each( function(){ clusterWidth += $(this).width() + 10; });
	$('.cluster').width( clusterWidth );
	var clusterVisibleWidth = $(".cluster-visible").width();
	var clusterVisibleLeft = 20;
	$(".arrow-left").addClass('bute');
	if (clusterWidth>clusterVisibleWidth) {
		$(".cluster-containment").css({
			"left": -(clusterWidth-clusterVisibleWidth),
			"width":clusterWidth+(clusterWidth-clusterVisibleWidth+clusterVisibleLeft)
		});
		$(".cluster-visible").css({"left": (clusterWidth-912+clusterVisibleLeft)});
		
		$("a.cluster-arrow").bind("click",function(e){
			e.preventDefault();
			$(".cluster").stop();
			moveCluster(this, 5);
		});


		$("a.cluster-arrow").hover(
			function(){
				moveCluster(this, 20);
			},
			function(){
				$(".cluster").stop();
			}
		);

		function moveCluster(arrow, speed){
			var clusterContainmentLeft = parseInt($(".cluster-containment").css("left"));
			var clusterContainmentRight = parseInt($(".cluster-containment").css("left")) + $(".cluster-containment").width();
			var clusterLeft = parseInt($(".cluster").css("left"));
			var clusterRight = parseInt($(".cluster").css("left"))+$(".cluster").width();

			if ($(arrow).hasClass("arrow-right")) {
				$(".arrow-left").removeClass('bute');
				vitesse = (clusterLeft-clusterContainmentLeft)*speed;
				$(".cluster").animate({
					"left":clusterContainmentLeft
				},vitesse,'linear',function(){
					$(arrow).addClass('bute');
				});
			}else{
				$(".arrow-right").removeClass('bute');
				vitesse = (clusterContainmentRight-clusterRight)*speed;
				$(".cluster").animate({
					"left":0
				},vitesse,'linear',function(){
					$(arrow).addClass('bute');
				});
			}
		}

		//DRAGGABLE
		$(".cluster").draggable({ axis: "x",containment: ".cluster-containment"  });
	};
//WIDGET
	//HEIGHT
	function resizeWidgetSidebar(){
		var contentRightHeight = $(".content-right").height();
		var contentLeftHeight = $(".content-left").height();
		if(contentRightHeight>contentLeftHeight){
			$(".content-left .widget-wrap").css({
				"height":"+="+(contentRightHeight-contentLeftHeight)
			});
		}
	}
	resizeWidgetSidebar();

	//VOLET
	//Tout fermer par défaut
	//$("div.content-left div.widget-wrap div.widget div.widget-inner").hide().removeClass("open");
	//$("div.dossier-similaire div.widget div.widget-inner").hide().removeClass("open");

	$("div.content-left div.widget-wrap div.widget h3 a, div.dossier-similaire div.widget h3 a").bind("click",function(e){
		e.preventDefault();
		var widgetInner = $(this).parents("h3").siblings("div.widget-inner");
		widgetInner.stop();
		if ($(this).hasClass("open")) {
			widgetInner.slideUp();
			$(this).removeClass("open");
		}else{
			widgetInner.slideDown();
			$(this).addClass("open");
		}
	});
	//AFFICHAGE
	$('.widget.affichage li a').bind('click',function(e){
		e.preventDefault();
		if ($(this).hasClass("treemap") && !$(this).hasClass("active")) {
			$('.widget.affichage li a.liste').removeClass("active");
			$(this).addClass('active');
			$('#liste').hide();$('#treemap').show();
		}else if($(this).hasClass("liste") && !$(this).hasClass("active")){
			$('.widget.affichage li a.treemap').removeClass("active");
			$(this).addClass('active');
			$('#liste').show();$('#treemap').hide();
		}
		resizeWidgetSidebar();
	});
	//DATE PICKER
	$(".dateBegin").datepicker({
		onSelect: function( selectedDate ) {
        	$( ".dateEnd" ).datepicker( "option", "minDate", selectedDate );
        }
	});
	$(".dateEnd").datepicker({
		onSelect: function( selectedDate ) {
        	$( ".dateBegin" ).datepicker( "option", "maxDate", selectedDate );
        }
	});
//VOLET DOCUMENTAIRE
	$("div.dossier-documentaire").hide();
	/*
	$("div.header a.dossier-button").bind("click",function(e){
		e.preventDefault();
		$("div.dossier-documentaire").stop()
		if($("div.dossier-documentaire").is(":visible")){
			$("div.dossier-documentaire").slideUp();
		}else{
			$("div.dossier-documentaire").slideDown();
		}
	});
*/
	$(".header .dossier-button").bind("click",function(e){e.preventDefault();});
	$(".header .dossier-button, .header .dossier-documentaire").hover(
		function(){
			$("div.dossier-documentaire").stop().slideDown();
		},
		function(){
			$("div.dossier-documentaire").stop().slideUp();
		}
	);
//ARTICLES
	$(".article-visuel-wrap").each(function(){
		var contentHeight = $(this).parents(".article-content").height();
		//$(this).height(contentHeight);
	});
	$(".article-annotations a").hover(
		function(){
			var x = $(this).offset().left + $(this).width()/2;
			var y = $(this).offset().top;
			var text = $(this).attr('title');
			showTooltip(x,y,text);
		},
		function(){
			var tip = $(".tooltip");
			tip.hide();
		}
	);
//FRISE
	//valeurs init du slider à définir
	var valSlider1 = 75;
	var valSlider2 = 300;
	var diffSlide = valSlider2-valSlider1;
	var widthFrise = $('.frise').width();

	$( "#slider-range" ).dragslider({
		range: true,
		rangeDrag: true,
		min: 0,
		max: widthFrise,
		values: [ valSlider1, valSlider2 ],
		slide: function( event, ui ) {
            if (window.streamgraph) {
                streamgraph.slidevalues(ui.values[0], ui.values[1]);
            }
		},
		change: function( event, ui ){
			//ici on récup les valeurs après un slide
			var value1 = ui.values[ 0 ];
			var value2 = ui.values[ 1 ];
		}
	});

//TOOLTIP
	function showTooltip(x,y, text) {
		//white_arrow_long.png
		var tip = $(".tooltip");
		tip.show();
	    tip.css({
	        "left" : Math.floor(x - tip.outerWidth() / 2) + "px",
	        "top" : Math.floor(y - tip.outerHeight()) + "px"
	    }).text(text);
	}

//HOME TREEMAP
	var lastActuBind = false;
	$('#treemap').on('mouseover', '.actu', function(){
		if(this == lastActuBind) return;
		lastActuBind = this;

		console.log('mouseover', $(this));

	});


});//jQuery
