
$(function(){
//CLUSTER
	var clusterWidth=0;
	$('.cluster li').each( function(){ clusterWidth += $(this).width() + 10; });
	$('.cluster').width( clusterWidth );
	var clusterVisibleWidth = $(".cluster-visible").width();
	var clusterVisibleLeft = 20;
	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();
			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 ($(this).hasClass("arrow-right")) {
				if (clusterLeft-40>clusterContainmentLeft) {
					$(".cluster").css({
						"left":"-="+40	
					});
				}else{
					$(".cluster").css({
						"left":clusterContainmentLeft
					});
				}
		
			}else if($(this).hasClass("arrow-left")){
			
				if (clusterRight+40<clusterContainmentRight) {
					$(".cluster").css({
						"left":"+="+40	
					});
				}else{
					$(".cluster").css({
						"left":0
					});
				}
			}
		});
	
		//DRAGGABLE
		$(".cluster").draggable({ axis: "x",containment: ".cluster-containment"  });
	};
//WIDGET
	//HEIGHT
	var contentRightHeight = $(".content-right").height();
	var contentLeftHeight = $(".content-left").height();
	if(contentRightHeight>contentLeftHeight){
		$(".content-left .widget-wrap").css({
			"height":"+="+(contentRightHeight-contentLeftHeight)
		});
	}
	//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();
			  $(".actu img").each(function() 
				{
					
					var img = $(this),
						div = $(this).parent();
						
					
						var iw = $(this).width(),
							ih = $(this).height(),
							dw = $(this).parent().width(),
							dh = $(this).parent().height(),
							scale = Math.max(dw/iw, dh/ih),
							niw = iw * scale,
							nih = ih * scale;
						img.css({
							width: niw,
							height: nih,
							"margin-left": (dw - niw) / 2,
							"margin-top": (dh - nih) / 3
						});
				});
		}
		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({
		minDate: new Date(2007, 7 - 1, 01),
        maxDate: new Date(2012,12 - 1 , 31),
		onSelect: function(selectedDate) {
			$("#pickeddatebegin").val(selectedDate);
			$("#ajaxfiltretreemap").submit();
			$( "#pickeddateend" ).datepicker( "option", "minDate", selectedDate );
        }
	});
	$(".dateEnd").datepicker({
		minDate: new Date(2007, 7 - 1, 01),
        maxDate: new Date(2012, 12 - 1, 31),
		onSelect: function( selectedDate ) {
			$("#pickeddateend").val(selectedDate);
        	$("#ajaxfiltretreemap").submit();
			$( "#pickeddatebegin" ).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();
		}
	});
*/
	//$("div.header p.dossier-button").bind("click",function(){alert('tutu';});
	$("div.header p.dossier-button, div.header div.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);
	});
//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 ];
		}
	});


	
});//jQuery
