var accordionned = 0;
var tl; // timeline
var offsetNer,offsetRef;
var refsAreVisible = 0;

var menus = {
"menuapproche"  : { "name":"notre approche","zone":["ner","une"]},
"menuactivites" : { "name":"activités","zone":["expertises"]},
"menublogs"     : { "name":"blogs / actualités","zone":["cause"]},
"menurefs"      : { "name":"références","zone":["refs"]},
"menucontacts"  : { "name":"contacts","zone":["contacts","correspond","pied"]}
}

function doMenu() {
    jQuery("span.menu").each(function(){
	    $(this).html(menus[$(this).attr("id")]["name"]);
	    $(this).after("<span id='ball"+$(this).attr("id")+"' class='menuball'></span>");
	    $(this).parent().find(".menuball").hide();
	}
	);
    //.addClass('menu-unselected');
    $("#navmenu li span[id=menuapproche]").removeClass('menu-unselected').addClass('menu-selected');
    jQuery("span.menu").hover(
			      function(){$(this).addClass('menu-hover');},
			      function(){$(this).removeClass('menu-hover');}
			      );
    jQuery("span.menu.menuoutside").click(function () {
        var elt = $(this).text();
        var id  = $(this).attr("id");
	if (id == "menucontacts") {
	    $(".slideme[title=Contact]").click();
	    // don't propagate, or else this will click
	    // more than once and close the slider !
	    return false;
	}
    });
    jQuery("span.menu.menuinside").click(function () {
        var elt = $(this).text();
        var id  = $(this).attr("id");

	var idball = "#ball"+id;
	var idtarget = "#"+menus[id]["zone"][0];
	$(idball).show();
	var offset = $(idball).offset();
	var bg = $(idball).css("background");
	var offset2 = $(idtarget).offset();
	$(idball).animate({
		left: "+="+(offset2.left-offset.left+$(idtarget).width()/2)+"px",
		    top: "+="+(offset2.top-offset.top+$(idtarget).height()/2)+"px"
		    }, 1500, 'linear', function() {
		$(this).css({"top":"0px","left":"0px","background":"transparent"})
		    .hide("slow", function() {$(this).css({"background":bg})});
	    } );

	jQuery("#footer").css({"background":"orange"});
        for (var z in menus[id]["zone"])  {
//jQuery("#debug").html("debug: ["+menus[id]["zone"][z]+"]");
//          jQuery("#"+menus[id]["zone"][z]).addClass('menuhilit');
	    jQuery("#"+menus[id]["zone"][z]).fadeOut(2000).fadeIn(300);
	    if (menus[id]["zone"][z] == "une") {showCol2("une");}
        }
	jQuery("#footer").css({"background":"#a1bddc"});
    });

}

///////////  grayscale images ///////// http://webdesignerwall.com/demo/html5-grayscale/

// Grayscale w canvas method
function grayscale(src){
    var canvas = document.createElement('canvas');
    var url=src,width=200;

    if (canvas.getContext){  
	var ctx = canvas.getContext('2d');  

	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	/*
	  img.onload = function() {
	  ctx.drawImage( img, .... );
	  };
	*/
	/* Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1, getImageData */
	
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
	    for(var x = 0; x < imgPixels.width; x++){
		var i = (y * 4) * imgPixels.width + x * 4;
		//	    var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
		var brigthness = 0.34*imgPixels.data[i] + 0.5*imgPixels.data[i + 1] + 0.16*imgPixels.data[i + 2];
		imgPixels.data[i] = brigthness; 
		imgPixels.data[i + 1] = brigthness; 
		imgPixels.data[i + 2] = brigthness;
	    }
	}
	//	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	ctx.putImageData(imgPixels, 0, 0, 0, 0, 64, imgPixels.height);
	url   = canvas.toDataURL();
	width = canvas.width;
    } 
    return {url : url, width : width};
}


function doGrayscale() {
    $("img.gs").animate({opacity:1},500);
    
    // clone image
    $('img.gs').each(function(){
	    var el = $(this);
	    var realwidth = $(this).width();
	    el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
		    var el = $(this);
		    el.parent().css({"width":this.width,"height":this.height});
		    el.dequeue();
		});
	    
	    var res = grayscale(this.src);
	    this.src = res.url;
	    //res.width=64;
	    $(this).parent().find('img:nth-child(2)').removeClass('gs').addClass('gs2');
	    $(this).parent().find('img:nth-child(1)').attr("realwidth",res.width).css({"width":"48px"});
	    //$(this).parent().find('img:nth-child(1)').attr("realwidth",200).css({"width":"48px"});
	});
    /* très bizarre, webkit n'a pas pris en compte le passage de img_wrapper à 48x48 dans la
       zone queue ci-dessus, et je suis obligé de le forcer maintenant */
    $(".img_wrapper").css({"width":"48px","height":"48px"});
    // all original images are 64px height, and mapped to 48px height, hence : 3/4
    $('img.gs').mouseover(function(){
	    if (refsAreVisible) {
		$(this).parent().find('img:first').stop()
		    .animate({opacity:1,width:Math.round($(this).attr("realwidth")*3/4)}, 1000);
	    }
	});
    $('.img_grayscale').mouseout(function(){
	    if (refsAreVisible) {
		$(this).stop().animate({opacity:0,width:"48px"}, 1000);
	    }
	});
    
}		

//////////////////////

jQuery.fn.sbTooltip = function() {
	return this.each(function(){
		jQuery(this).attr({'data-sbtooltip': jQuery(this).attr("title")}).removeAttr("title");
	});
};


//// COMPUTE TAGS FROM blogs /////////////////////////////////

var computedTags = new Object();

// for every articles
for (var article in arrayTags) {
    aTags = arrayTags[article];
    for (var i=0;i<aTags.length;i++) {
	// for every tag, compute an empty array if non existent
	if (!computedTags[aTags[i]]) {
	    computedTags[aTags[i]] = new Array();
	}
	// for every tag, add associated tags in this article
	computedTags[aTags[i]] = computedTags[aTags[i]].union(aTags);
    }
}
// computedTags is now an object where each property is a tag
// pointing ot an array of tags found to be associated with this tag,
// including itself. We now remove every tag from its own list of
// associated tags.
var allTags = new Array();
for (var t in computedTags) {
    allTags.push(t);
    var l = computedTags[t].length;
    var n = 0;
    for(var i=0; i<l; i++)
	computedTags[t][i] == t ? n++ : computedTags[t][i-n] = computedTags[t][i];
    computedTags[t].length = computedTags[t].length - n;
}
// computedTags is now a list of arrays where every unique tag t
// gives an array computedTags[t] of every tags associated with t


//// /compute tags ///////////////////////////


//////////////////////////////////////////////
function svg_object(str) {
  var attrs = {};
  str.split(/;/).
    forEach(function(pair) {
      var kv = pair.split(/:/);
      attrs[kv[0]] = kv[1];
    });
  return attrs;
}

//////// DO PAGE SLIDES //////////////////////
var psw; // will contain a special div for pageslides mechanism : #pageslide-slide-wrap


var dPS=0;
var slided=0;
function doPageSlides() {
// this must be done only once
// WHY ??????  I have to comment this for timeline call to work
    if (dPS) return; dPS=1;
//  $('.slideme').pageSlide({width:$('body').width()});

    $('.slideme[target="ext"]').addClass('slidemeext').removeAttr('target');

    
    $('.slideme').each(function() {
	    var width = parseInt($(this).css("width")) > 0 ? $(this).css("width") : "90%";
	    $(this).pageSlide({
		    width: width,
			start: function() {
			elm = this._identifier;
			if ($(elm).hasClass('slidemeext') &&
			    !$(elm).hasClass('slidemeextstartok')) {
			    //alert("ok class=slidemeext "+$(elm).html());
			    $(elm).data('targext',$(elm).attr('href'));
			    $(elm).data('descext',$(elm).attr('desc'));
			    $(elm).attr('href','holder-slideext.html');
			    // we dont want to be initialised again and have
			    // targext above set to this href holder-slideext.html !
			    $(elm).addClass('slidemeextstartok');
			}
		    },
			addclosebutton:function() {
			
			if (slided==0)
			    {
				// automagically add a Close button
				$('#pageslide-content')
				    .prepend('<div  class="closeslide" style="position:relative;left:-24px"><div class="pageslide-close"></div>')
				    .css({"margin-left": "10px"});
				$('#pageslide-slide-wrap')
				    .css({background: "black"});
				//.prepend('<div style="background:white;" class="closeslide"><div class="pageslide-close">Close me NOW!</div> ::'+slided+'::</div>');
			    }
		    },
			complete:function(){
			// TODO: do this only for sliding content
			
			if (slided==0)
			    {
				slided=1;
				elm = this._identifier;
				var color = $("#pageslide-content h1 span").html($(elm).attr('title')).css("color");
				
				if ($(elm).hasClass('slidemeext')) {
				    $("#pageslide-content p#description").html($(elm).data('descext'))
					.css({"color": color});
				    //alert("TARGEXT: "+$(elm).data('targext'));
				    $("#pageslide-content iframe").attr('src',$(elm).data('targext'))
					.css({width: "100%", height: "500px","background":"orange"});
				} else if ($(elm).hasClass('slideme')) {
				    $("#pageslide-content")
					.css({"color": color});
				}
				Cufon.replace('.Ftallys', { fontFamily: 'Tallys' });
			    } else {
			    slided=0;
			    elm = this._identifier;
			    $('.closeslide').remove();
			    $("#pageslide-content iframe").attr('src',"")
				.css({width: "0%", height: "0%"});
			    $(window).resize();
			    //$(elm).attr('href',$(elm).data('targext'));
			    //dimutil('reset');
			    //dimutil('bigrow');
			    //dimutil('contentright');
			    //dimutil('ner');
			    //dimutil('supfooter');
			}
			return false; //04112010  found really useful
		    }
		});
	});
    
    
$('body').prepend('<div id="dummyabs2" class="vertical" style="position:absolute;width:0px;top:0px;left:0px;"></div>');
};
///////// /doPageSlides ///////////////////////////




/////////  ABSDRAW ////////////////////////////////
function absdraw() {
    // absolute positionning must be done after flexify
    offsetRef = $('#refs').offset();
    offsetNer = $('#ner').offset();
    /*$('#galet').css({"z-index":10,left: (offsetRef.left-($('#galet').width()/2))+"px", 
      top: (offsetRef.top-($('#galet').height()/2))+"px"});*/
    /*$("#listOfSelectedTags").css({"width":($("#refs").width()*2-100)+"px",
      "margin-left":(offsetRef.left+30)+"px"});*/


    /*
	$("#listOfSelectedTags").css({"position":"absolute",
		"width":($("#refs").width()*2-100)+"px",
		"top":(offsetNer.top+$("#ner").height())+"px",
		"left":(offsetRef.left+30)+"px"});
    */
    
    /*
    $("#listOfRefs").css({"width":($("#ner").width()-40)+"px",
		"margin-left":(offsetNer.left)+"px"});



    //    $("#ner").css({"width":"80%"});
    
    
    */
    //setTimeout("doPageSlides()",2000);
    drawTimeline();
};
/////////// /ABSDRAW /////////////////////////


/////////// SHOW SELECTED ARTICLES FROM TAGS //////////////
var selectedTags = new Array();
var subTags = new Array();
var selectedArticles = new Array();
function selectTag(elem) {
    elem.css("background","#00448f");
    var t = elem.attr("title"); //the tag
    selectedTags.push(t);
}
function selectTag2(node) {
    var t = node.label //the tag
    selectedTags.push(t);
}
function unselectTag(elem) {
    elem.css("background","");
    var t = elem.attr("title"); //the tag
    var l = selectedTags.length;
    var n = 0;
    for(var i=0; i<l; i++)
	selectedTags[i] == t ? n++ : selectedTags[i-n] = selectedTags[i];
    selectedTags.length = selectedTags.length - n;
}
function unselectTag2(node) {
    var t = node.label; //the tag
    var l = selectedTags.length;
    var n = 0;
    for(var i=0; i<l; i++)
	selectedTags[i] == t ? n++ : selectedTags[i-n] = selectedTags[i];
    selectedTags.length = selectedTags.length - n;
}
function showSubTags() {
    selectedTags.length != 0 ? subTags = computedTags[selectedTags[0]] : subTags = allTags;
    for(var i=1; i<selectedTags.length; i++)
	{
	    subTags = subTags.intersect(computedTags[selectedTags[i]]);
	}
    for(var t in computedTags) {
	//$('*[title="'+t+'"]').hide();
	$('*[title="'+t+'"]').addClass("unchecked");
    }
    var offset = $("#refs").offset();
    $("#listOfSelectedTags").show();
    $("#lOSTchoice").show();
    $("#lOSTbackToList").hide();
    $("#lOSThelp").hide();
    //$("#listOfSelectedTags").show().css({"width":($("#refs").width()*2-100)+"px","margin-left":(offset.left+30)+"px"});
    $("#lOSTselected").empty();
    $("#lOSTmore").empty();
    var losts = "";
    for (var i=0; i<selectedTags.length; i++) {
	$('*[title="'+selectedTags[i]+'"]').removeClass("unchecked").show();
	losts += "<b>"+selectedTags[i]+"</b>";
	if (i != selectedTags.length - 1) { losts += ", "}
    }
    $("#lOSTselected").append(losts);
    var lostm = "";
    for (var i=0; i<subTags.length; i++) {
	$('*[title="'+subTags[i]+'"]').removeClass("unchecked").show();
	lostm += "<i>"+subTags[i]+"</i>";
	if (i != subTags.length - 1) {lostm += ", "}
    }
    if (lostm != "") {lostm = ", à préciser éventuellement avec&nbsp;: "+lostm+",";}
    $("#lOSTmore").append(lostm);
    if (selectedTags.length == 0) {
	// no selected Tags anymore, so empty the following div
	$("#listOfSelectedTags").hide();
    }
}
function showSubTags2() {
    selectedTags.length != 0 ? subTags = computedTags[selectedTags[0]] : subTags = allTags;
    for(var i=1; i<selectedTags.length; i++)
	{
	    subTags = subTags.intersect(computedTags[selectedTags[i]]);
	}

    for(var id in gTag.nodes) {
	//alert("hidding "+id);
	gTag.nodes[id].hide();
    }

    var offset = $("#refs").offset();
    $("#listOfSelectedTags").show();
    $("#lOSTchoice").show();
    $("#lOSThelp").hide();
    $("#lOSTbackToList").hide();
    //$("#listOfSelectedTags").show().css({"width":($("#refs").width()*2-100)+"px","margin-left":(offset.left+30)+"px"});
    $("#lOSTselected").empty();
    $("#lOSTmore").empty();
    var losts = "";
    for (var i=0; i<selectedTags.length; i++) {
	losts += "<b>"+selectedTags[i]+"</b>";
	if (i != selectedTags.length - 1) { losts += ", "}
    }
    $("#lOSTselected").append(losts);
    var lostm = "";
    for (var i=0; i<subTags.length; i++) {
	lostm += "<i>"+subTags[i]+"</i>";
	if (i < subTags.length - 2) {lostm += ", "}
	else if (i == subTags.length - 2) {lostm += " ou "}
    }
    $("#lOSTmore").append(lostm);
    for(var id in gTag.nodes) {
	for (var i=0; i<selectedTags.length; i++) {
	    gTag.nodes[selectedTags[i]].show();
	}
	for (var i=0; i<subTags.length; i++) {
	    gTag.nodes[subTags[i]].show();
	}
    }
}
function selectedArticlesFromSelectedTags() {
    selectedArticles = [];
    for (var p in arrayTags) {
	if (selectedTags.diff(arrayTags[p]).length == 0) {
	    selectedArticles.push(p);
	}
    }
}
function showSelectedArticles() {


    $(".selectedArticles").unbind('click');
    $("#selectedArticles").empty();
    for (var i=0;i<selectedArticles.length;i++) {
	$("#selectedArticles").append("<span class='selectedArticles' style='cursor:pointer; background:#ddd;padding:2px;' title='"+selectedArticles[i]+"'>"+selectedArticles[i]+"</span> ");
    }

    $(".selectedArticles")
	.click(
	       function() {
		   //		   alert("click on "+$(this).attr('title'));
		   showArticleInCol2($(this).attr('title'));
		   myScrollArt.refresh();

		   $("#lOSTchoice").hide();
		   $("#lOSThelp").hide();
		   $("#lOSTbackToList").show();

		   /* $(this).attr('title') contains a non utf-8 strings. for instance, Ã© is now é .
		      $(this).attr('title') is thus no longer a correct index for arrayTags !
		      arrayTags[$(this).attr('title')] can be null ! So we must compare index
		      in another, tricky, way */

		   var P = "";
		   for (var p in arrayTags) {
		       $("#selectedArticles").after("<span id='temp' title='"+p+"'></span>");
		       var temp = $("#selectedArticles").parent().find("#temp");
		       if ($(this).attr('title') == temp.attr('title')) 
			   {P=p;temp.remove();break;}
		       temp.remove();
		   }

		   var lestags = "";
		   var aTags = arrayTags[P];
		   for (var i=0;i<aTags.length;i++) {
		       lestags += "<li>"+aTags[i];
		   }

		   if ($("#lestags")) {$("#lestags").remove();}
		   $("#lOSTbackToList").append("<div id='lestags'><h5>Tags associés à cet article...</h5><ul>"+lestags+"</ul></div>");
	       }
	       );
    $(".selectedArticles")
	.hover(function() {$(this).css({"background":"#aaa"});},
	       function() {$(this).css({"background":"#ddd"});});





}

function showArticleInCol2(article) {
    hideAllInCol2();
    $("#articleHolder").show();
    $("#articleHolder div.csb").show();
    /* bizarre, z-index dans les CSS ne suffit pas */
    $("#articleHolder div.fl").show().css({"z-index":-1});
    $("#articleHolder div[title='"+article+"']").show()
	.css({width:"96%",height:$('#bigrow').height()-20});
    
    //    $("#mcsaH_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);

}

function hideArticleInCol2() {
    $("#articleHolder div").hide();
    $("#articleHolder").hide();
}

//////////// / show selected articles from tags ////////////////

/////////////////////////////////////////////
////////////  MAIN //////////////////////////
/////////////////////////////////////////////
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

var myScrollNer,myScroll2,myScrollRef,myScrollExp,myScrollCau,myScrollArt,myScrollUne;

$(function () {


	Cufon.replace('.Ffirstorder', { fontFamily: 'First Order' });
	$("#mybodywrapinner").show().click(function(){$("#mybodywrap").hide();});
	Cufon.replace('.Ftallys', { fontFamily: 'Tallys' });
	
	myScrollNer = new iScroll('nercontainer', { scrollbarClass: 'myScrollbar' });
	//myScroll2 = new iScroll('wrapper', { scrollbarClass: 'myScrollbar' });
	myScrollExp = new iScroll('expcontainer', { scrollbarClass: 'myScrollbar2' });
	myScrollRef = new iScroll('refcontainer', { scrollbarClass: 'myScrollbar' });
	myScrollCau = new iScroll('caucontainer', { scrollbarClass: 'myScrollbar' });
	myScrollArt = new iScroll('artcontainer', { scrollbarClass: 'myScrollbar' });
	//myScrollUne = new iScroll('unecontainer', { scrollbarClass: 'myScrollbar' });


	$(".sbtooltip").sbTooltip();
	//	setTimeout("doGrayscale()",5000);
	var imgP = [];
	$("img.gs").each(function(index) {
		imgP.push($(this).attr('src'));
	    });
	$.imgpreload(imgP,
		     {
			 all: function()
			     {
				 doGrayscale();
			     }
		     });
	//doGrayscale();

	$("a.tooltip").hover( 
			      function(){
				  $(this).after(" <span class='ttmore'>"+$(this).attr("comment")+"</span>");
			      },
			      function(){
				  $(this).parent().find(".ttmore").remove();
				  $(this).after(" ");
			      }
			       );


	/*$("#mcsay_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);*/
	/*$("#mcsaH_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$("#mcsExp_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$("#mcsCau_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$("#mcsRef_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);*/

	///////////

	// ***
	// Scrolling background
	// ***
	var offsetBigBackground;	
	var backgroundheight = 4000;
	/*	
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	*/

	var hour = 12;
	var minute = 0;
	
	var hourpercent = hour / 24 * 100;
	var minutepercent = minute / 60 / 24 * 100;
	var percentofday = Math.round(hourpercent + minutepercent);

	

 	var offset = backgroundheight / 100 * percentofday;

	var offset = offset - (backgroundheight / 4);

	function scrollbackground() {
   		offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
		offsetBigBackground = offset;
   		$('body').css("background-position", "50% " + offset + "px");

		/*$("#mcsay_container .dragger").css({"background-position": "50% " + offset + "px"});*/
   		/* setTimeout(function() {
			scrollbackground();
			}, 40
			); */
   	}
    
	scrollbackground();
	////////


	var i=0;
	for (var p in arrayTags) {
	    i++;
	    //$("#arrayTags").append("<li>"+p+"\n");
	}
	//$("#arrayTagsLength").html(i);
	/*
	  for (var p in computedTags) {
	  $("#arrayTags").append("<span style='cursor:pointer;font-size:12pt;padding:4px;' class='taggable' title='"+p+"'><b>"+p+"</b></span> ");
	  for (var i=0; i<computedTags[p].length;i++) {
	  $("#arrayTags").append(computedTags[p][i]+", ");
	  }
	  $("#arrayTags").append("<br>\n");
	  }
	*/
	
	$("#tagCloudInner").tagCloud(countTags);
	$(".tagcloudlink").each(function(index) {
		$(this).attr("title",$(this).text());
	    });
	
	
	$('.tagcloudlink').toggle(
				  function() {
				      if ($(this).hasClass("unchecked")) {return}
				      selectTag($(this));
				      showSubTags();
				      selectedArticlesFromSelectedTags();
				      showSelectedArticles();
				  },
				  function() {
				      unselectTag($(this));
				      showSubTags();
				      selectedArticlesFromSelectedTags();
				      showSelectedArticles();
				  }
				  );
	
	// populate articleHolder
	for (var t in arrayTagsData) {
	    $("#articleHolder .content").append("<div class='fromblog' style='display:none;font-size:12pt;padding:10px;overflow:auto;position:relative' title='"+t+"'><h2 style='font-size:1.3em'>"+t+"</h2><p style='float:right; font-style:italic'>Publié sur <a href='http://blog.nereys.fr/' target='ext'>blog.nereys.fr</a> le "+arrayTagsDate[t]+"</p><br clear='all'>"+arrayTagsData[t]+"</div>\n");
	} 

	//$(".fromblog p:eq(1)").addClass("fp"); // first (real) paragraph
	$(".fromblog").each(function(){
		$(this).find("p:eq(1)").addClass("fp");
		$(this).find("p:gt(0)").addClass("inp");
	    });
	//$(".fromblog p:gt(0)").addClass("inp"); // first (real) paragraph
	$(".fromblog img").unwrap(); 
	// lettrine, sauf si webkit 
	if (!$.browser.webkit) {
	    $(".fromblog").each(function(){
		    var pfp = $(this).find("p.fp");
		    pfp.prepend("<div class='fl'>"+pfp
				.text().substring(0,1)+"</div>");
		}); 
	}

	$("#lOSTback").click(function() {
		hideArticleInCol2();
		showCol2("tagcloud");
		$("#lOSTchoice").show();
		$("#lOSTbackToList").hide();
		//$('#une').show();
		//showTagNodes();
	    });
	
	


	
	var style="font-size:64px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#4477bb;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:First Order";
	var d="M 0.0017568286,9.9324431 C -0.052363171,12.938843 1.1423669,16.153143 3.7957974,17.780843 C 6.5126374,19.507643 9.9432174,19.478843 12.975637,18.741843 C 14.862407,18.467043 16.643807,17.681543 18.249217,16.677543 C 19.281267,15.923943 20.132847,14.901143 20.692597,13.754443 C 21.117547,12.708143 21.293847,11.514143 20.984207,10.412943 C 21.131527,9.4574431 20.223727,8.8169431 19.824697,8.0295431 C 18.260057,5.7964431 16.735147,3.4739431 14.664307,1.6710431 C 13.577257,0.88324284 12.243617,0.36584284 10.893407,0.32214284 C 10.034177,0.0066428429 9.0658474,0.20734284 8.1717174,0.29224284 C 4.5361374,0.78464284 1.3282569,3.5526431 0.37408683,7.1046431 C 0.11657683,8.0234431 0.0012968286,8.9791431 0.0017468286,9.9324431";
	var style2="font-size:64px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#004488;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:First Order";
	var d2 = "M 9.8114674,3.6703431 C 6.4218174,3.3900431 3.1456064,6.3870431 3.4765564,9.8488431 C 3.2095764,12.565743 5.0894074,15.532543 8.0446774,15.440543 C 10.990627,15.605043 14.164627,15.088643 16.550157,13.261743 C 18.895617,11.324543 16.174957,9.4395431 14.981977,7.3850431 C 13.415697,5.4245431 12.459247,3.7259431 9.9678874,3.6703431 L 9.8114674,3.6703431 z";
	
	
	
	
	/*
	  (function (local_raphael) {*/
	    
	    var paper = Raphael("galet", 70, 70);
	    
	    var a = paper.path(d).attr(svg_object(style));
	    a.scale(3,3,0,0);
	    var b = paper.path(d2).attr(svg_object(style2));
	    b.scale(3,3,0,0);
	    paper.setSize(a.getBBox().width+4,a.getBBox().height+4);
	    

	    //var offsetSlideShow = $("#slideshow").offset();
	    var finistereH = Math.floor(0.9*$("#une").height())-10;
	    var finistere     = Raphael("finistere", Math.floor(finistereH*4/3), finistereH);
	    finistere.path(svgFinistere).attr(svg_object(styleFinistere)).scale(0.7,0.7,0,0).toBack();
	    
	    //$('#H').html($('#bigrow').height());
	    //$('.Raphaël').remove();
	    
	    /*})(Raphael.ninja());
	*/
	
	
	$('.hover1').hover(
			   function() {
			       $(this).hide();

			       $(this).next('.hover2').css({cursor:"pointer",width:($(this).width()-40)+"px"}).show();
			       $('#footer div.container').css({width:($(this).width()-40)+"px"});
			       $('#footer div.content').css({width:($(this).width()-40)+"px"});
			       myScrollExp.refresh();
			       myScrollRef.refresh();
			       myScrollCau.refresh();
			       /*
			       $("#mcsExp_container").css({height:$("#expertises").height()-$("#expertises div.footer1").css("padding-top").replace(/([\d.]+)(px|pt|em|%)/,'$1')})
				   .mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
				   $("div.csbexp").show();
			       $("#mcsRef_container").css({height:$("#refs").height()-$("#refs div.footer2").css("padding-top").replace(/([\d.]+)(px|pt|em|%)/,'$1')})
				   .mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
				   $("div.csbref").show();
			       $("#mcsCau_container").css({height:$("#cause").height()-$("#cause div.footer3").css("padding-top").replace(/([\d.]+)(px|pt|em|%)/,'$1')})
				   .mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
				   $("div.csbcau").show();
			       */

				   },
			   function() {}
			   );
	$('.hover2').hover(
			   function() {},
			   function() {
			       $(this).hide();
			       $(this).prev('.hover1').css({cursor:"default"}).show()
				   }
			   );

	$('.footer1').click(
			    function() {
				$("#navmenu li span.menu")
				    .addClass('menu-unselected')
				    .removeClass('menu-selected');
				$("#navmenu li span[id=menuactivites]")
				    .addClass('menu-selected')
				    .removeClass('menu-unselected');

				showCol2("tagcloud");
				//showCol2("tagcloud2");
			    }
			    );
	$('.footer2').click(
			    function() {
				$("#navmenu li span.menu")
				    .addClass('menu-unselected')
				    .removeClass('menu-selected');
				$("#navmenu li span[id=menurefs]")
				    .addClass('menu-selected')
				    .removeClass('menu-unselected');
				showCol2("timeline");
			    }
			    );
	$('#trigrefs').click(
			     function() {
				 $("#lORbutton").click();
				 return false;
			     });
	$('.footer3').click(
			    function() {
				$("#navmenu li span.menu")
				    .addClass('menu-unselected')
				    .removeClass('menu-selected');
				$("#navmenu li span[id=menublogs]")
				    .addClass('menu-selected')
				    .removeClass('menu-unselected');
				showCol2("blogs");	
			    }
			    );
	
	

	var urls = {
	    sel  : '/js/jfeed/proxy.php?url=http://www.nereys.fr/sel/feed/rss2',
	    ier  : '/js/jfeed/proxy.php?url=http://www.lactuier.info/feed/',
	    //trek : '/js/jfeed/proxy.php?url=http://www.nereys.fr/trek/feed/rss2',
	    nereys : '/js/jfeed/proxy.php?url=http://blog.nereys.fr/feed/' //,
	    //twit : '/js/jfeed/proxy.php?url=http://twitter.com/statuses/user_timeline/11048002.rss'
	}
	var nfeeds = {
	    sel  : 5,
	    ier  : 5,
	    //trek : 5,
	    nereys : 5 //,
	    //twit : 1
	}
	var feeds = {
	    'Sel Marin'           : 'sel',
	    "L'actuier"           : 'ier',
	    //'Trek Telecom'        : 'trek',
	    'Lettres à Nereÿs'              : 'nereys' //,
	    //'Twitter / AymericPM' : 'twit'
	}
	
	for (var b in urls) {

	    
	    jQuery.getFeed({
		    //        url: urls[b],
		    url: urls[b],
			success: function(feed) {
			
			var leblog = feeds[feed.title];

			/*            jQuery('#'+leblog+'visuel').html('');
				      jQuery('#'+leblog+'visuel').append('<h'+'2>'
				      + '<a href="'
				      + feed.link
				      + '">'
				      + feed.title 
				      + '</a>'
				      + '</h2>');*/
			if (feed.description.length < 80) {jQuery('#'+leblog+'desc').html(feed.description);}
			
			var html = '';

			jQuery('#'+leblog+'rss').html('');
			for(var i = 0; i < feed.items.length && i < nfeeds[feeds[feed.title]]; i++) {
			    
			    var item = feed.items[i];
			    
			    if (feeds[feed.title] != 'twit') {
				
				html += '<h'+'3>'
				    + '<a href="'
				    + item.link
				    + '" ta'+'rget="blogs">'
				    + item.title
				    + '</a>'
				    + '</h3>';
			    }
			    
			    html += '<d'+'iv class="updated"><i>'
				+ item.updated
				+ '</i></div><br clear="all">';
			    
			    
			    html += '<d'+'iv style="margin-top:2px;margin-bottom:8px">'
				+ item.description
				+ '</div>';
			    
			}
			
			jQuery('#'+leblog+'rss').append(html);
		    }
		});
	}
	
	var slideshow;
	var toggle = $('#toggleSlideShow').click(function() {
		var paused = slideshow.is(':paused');
		slideshow.cycle(paused ? 'resume' : 'pause', true);
	    });
	
	/**** add article to the slideshow ****/
	/*
	for (var t in arrayTagsData) {
	    // Cufon mange les ' ici parce qu'il reçoit &#8217;
	    $("#slideshow").append("<div title='"+t+"'><h2 class='Ftallys' style='font-size:1.3em'>"+t.replace("&#8217;","&#39;")+"</h2>"+arrayTagsData[t]+"</div>\n");
	} 
	*/

	slideshow = $('#slideshow').css({"height":$("#une").height()-24,
	    "width":$("#contentright").width()}).show()
	    .cycle({
		    speed:    500,
		    timeout:  8000,
		    easing:  'easeInBack',
		    fx: 'scrollLeft,scrollDown,scrollRight,scrollUp',
		    pager:  '#nav',
		    pagerEvent: 'mouseover',
		    pauseOnPagerHover: true ,
		    randomizeEffects: false,
		    //		    after: function() {myScroll2.refresh();},
		    paused: function(cont, opts, byHover) {
			/* passage par html('') obligatoire pour safari sinon bug graphique */
			!byHover && toggle.html('').html('Play');
		    },
		    resumed: function(cont, opts, byHover) {
			!byHover && toggle.html('').html('Pause');
		    },
		    pagerAnchorBuilder: function(idx, slide) { 
			return '<a href="#">' + slide.title + '</a>'; 
		    } 
		});

	
	//showTagNodes();
	$('#timelineInner').css({"height":$("#une").height(),
		    "width":$("#contentright").width()});
	makeTimeline();
	absdraw();
	doMenu();
	
	//    setTimeout("doPageSlides()",2000);

	doPageSlides();
		
	$(".clickmenuactivites").click(function() {$("#menuactivites").click()});
	$(".clickmenurefs").click(function() {$("#menurefs").click()});
	
	// recalcul en relative pour supfooter
	$("#supfooter").css({"position":"relative",
		    "top":($("#ner").height()+10)+"px"});

	$("div#makeMeScrollable").smoothDivScroll({ 
		autoScroll: "onstart" , 
		    autoScrollDirection: "endlessloopright", 
		    autoScrollStep: 1, 
		    autoScrollInterval: 60,	
		    startAtElementId: "startAtMe", 
		    visibleHotSpots: "always"
		    });
	


	var lorAnim=0;

	$("#lORbutton").toggle(
			       function(){
				   if(!lorAnim){
				       $("div.scrollingHotSpotLeft").animate({top:"-=60"},1000);
				       $("div.scrollingHotSpotRight").animate({top:"-=60"},1000);
				       $("#listOfRefs").animate({top:"-=40"},1000,
								function(){
								    $(this).find(".lORbarrowup")
									.removeClass("lORbarrowup")
									.addClass("lORbarrowdown");
								    refsAreVisible=1;
								});
				       lorAnim=1;}
			       },
			       function(){
				   if(lorAnim){
				       refsAreVisible=0;
				       $("div.scrollingHotSpotLeft").animate({top:"+=60"},1000);
				       $("div.scrollingHotSpotRight").animate({top:"+=60"},1000);
				       $("#listOfRefs").animate({top:"+=40"},1000,
								function(){
								    $(this).find(".lORbarrowdown")
									.removeClass("lORbarrowdown")
									.addClass("lORbarrowup");
								});
				       lorAnim=0;}
			       }
			       );

	var offsetlOR = $("#listOfRefs").offset();
	offsetRef = $('#refs').offset();
	var lORanim = Math.floor(offsetRef.top-offsetlOR.top-30);
	$("#listOfRefs").animate({top:"+="+lORanim},2000)
	    .hover(function(){
		    $("#makeMeScrollable").smoothDivScroll("stopAutoScroll");
		    /*if(!lorAnim){
			$("div.scrollingHotSpotLeft").animate({top:"-=60"},1000);
			$("div.scrollingHotSpotRight").animate({top:"-=60"},1000);
			$(this).animate({top:"-=40"},1000);
			lorAnim=1;}*/
		    },
		function(){
		    /*		    if(lorAnim){
			$("div.scrollingHotSpotLeft").animate({top:"+=60"},1000);
			$("div.scrollingHotSpotRight").animate({top:"+=60"},1000);
			$(this).animate({top:"+=40"},1000);
			lorAnim=0;}*/

		});

		$("#navmenu").css({"top":(offsetNer.top-$("#navmenu").height())+"px"});
	var oa = $("#navmenu").offset();
	var ob = $("#refs").offset();
	var ossf = $("#supfooter").offset();
	//	$("#debug").append("width: "+$(document).width()+" height: "+$(document).height()+
	//   " navmenu: "+oa.top+" ner: "+offsetNer.top+ " nh:"+$("#navmenu").height()
	//	   +" otop:"+ossf.top
	//	 ).show();
	var oldwidth  = $(document).width();
	var oldheight = $(window).height();
	$(window).resize(function() {
		var diffwidth  = oldwidth-$(document).width();
		var diffheight = oldheight-$(window).height();
		oldwidth  = $(document).width();
		oldheight = $(window).height();

		offsetNer = $('#ner').offset();
		

		/*$('#accordionme').width($("#blogsInner").width());
		$('#accordionme').height($("#blogsInner").height());
		$('#accordionme dl').width($("#blogsInner").width());
		$('#accordionme dl').height($("#blogsInner").height());
		$('#accordionme').setVariables();
		$('#accordionme').findActiveSlide();
		$('#accordionme').calculateSlidePos();
		$('#accordionme').find('dt').not('active').click(function(){		
			jQuery(this).activateSlide();
		    });
		*/

		$("#listOfSelectedTags")
		    .css({"position":"absolute","top":(offsetNer.top+20)+"px",
				"left":(offsetNer.left+20)+"px",
				"width":$('#ner').width()-80,
				"height":$('#ner').height()-80});

		$("#ner").css({"height":($("#ner").height()-diffheight)+"px"});
		$("#contentright").css({"height":($("#contentright").height()-diffheight)+"px"});
		ossf = $("#supfooter").offset();
		//$("#debug").html("OTOP:"+ossf.top+" DESC:"+(ossf.top+$("#supfooter").height()+diffheight));
		$("#supfooter").css({"position":"relative",
			    "top":($("#ner").height()+10)+"px"});
		
		$("#navmenu").css({"top":(offsetNer.top-$("#navmenu").height())+"px"});
		
		$('#slideshow').css({"height":$("#une").height()-24,
			    "width":.9*$("#contentright").width()});

		//tl.layout();
		//    drawTimeline();
		$("#timelineInner").css({"width":$("#timeline").width(),
			    "height":$("#timeline").height()});

		$("#pageslide-body-wrap").css({"height":($("#pageslide-body-wrap").height()-diffheight)+"px"});

		/* refreshing scrolls */
		myScrollNer.refresh();

		/*$("#debug").html(" width: "+$(document).width()+" height: "+$(window).height()+
		  " diffh:"+diffheight+" otop:"+ossf.top).show();*/
	    });

	//myScroll.refresh();
	
	//prepareTagNodes();

	$("#mybodywrap").fadeOut(1000);
	
    });

function hideAllInCol2() {
	$('#tagcloud').hide();
	$('#blogs').hide();
	$('#timeline').hide();
	$('#une').hide();
}

function showCol2(what) {
    hideArticleInCol2();
    $("#listOfSelectedTags").hide();
    if (what == "une") {
	$('#tagcloud').hide();
	$('#blogs').hide();
	$('#timeline').hide();
	$('#une').show();
    } else if (what == "tagcloud") {
	offsetNer = $('#ner').offset();


	$("#listOfSelectedTags").show();
	var ol = $("#listOfSelectedTags").offset();


	$("#listOfSelectedTags")
	    .css({"position":"absolute","top":(offsetNer.top+20)+"px",
			"left":(offsetNer.left+20)+"px",
			"width":$('#ner').width()-80,
			"height":$('#ner').height()-80});
	$("#lOSTchoice").hide();
	$("#lOSTbackToList").hide();
	$("#lOSThelp").show();
	$("#slideshow").hide();
	$('#une').hide();
	$('#blogs').hide();
	$('#timeline').hide();
	//	$('#tagcloud').show("slow").css({width:"100%",height:$('#bigrow').height()});
	$('#tagcloud').show("slow");
	absdraw();
    } else if (what == "tagcloud2") {
	hideAllInCol2();
	$("#listOfSelectedTags").show();
	$("#lOSTchoice").hide();
	$("#lOSTbackToList").hide();
	$("#lOSThelp").show();
	$("#slideshow").hide();
	$('#une').show();
	showTagNodes();
    } else if (what == "timeline") {
	var offdeb = $(".timeline-band-input").offset();
	$('#une').hide();
	$('#tagcloud').hide();
	$('#blogs').hide();
	//	$('#timeline').show("slow").css({width:"100%",height:$('#bigrow').height()});
	$('#timeline').show("slow");
	absdraw();
    } else if (what == "blogs") {
	$('#une').hide();
	$('#tagcloud').hide();
	$('#timeline').hide();
	$('#blogs').css({width:"100%",height:($('#bigrow').height()-2)+"px"})
	    .show(); //immediate show, or hrzAccordion will compute
	// a 1px width for the hrzContainer3 wrapping element !
	//alert("width: "+$('#blogs').width());

	$('#accordionme').width($("#blogsInner").width());
	$('#accordionme').height($("#blogsInner").height());
	$('#accordionme dl').width($("#blogsInner").width());
	$('#accordionme dl').height($("#blogsInner").height());
	$('#accordionme').easyAccordion();
	
	absdraw();
	
    } 
}


function dimutil(name) {
    if (name == 'reset') {$('#debug').html(''); return;}
    elem = $('#'+name);
    var offset = elem.offset();
    $('#debug').append(name+": top:"+offset.top+" left:"+offset.left+" WxH:"+elem.width()+"x"+elem.height()+"<br>");
}
