jQuery(document).ready(function () {
	var $ = jQuery,
	visibleElmnt = 3,
	oneElmntHeight = 50,
	size = 0,
	tList = $(".widget .twitter"),
	tElmnt = $(".widget .twitter .twitter-item"),
	groupShown = 0;
	
	var tmpPos = 13;
	var i = 0;
	var maxSize = 0, tmpMaxSize = 0;
	tElmnt.each(function(){
		tmpMaxSize += $(this).height() + 18;
		
		if (i == visibleElmnt - 1) {
			if (maxSize < tmpMaxSize){
				maxSize = tmpMaxSize;
			}
			i = 0;
			tmpMaxSize = 0;
		}
		i++;
		
		if (oneElmntHeight < $(this).height()){
			oneElmntHeight = $(this).height();
		}
	});
	function increaseTwitterSize(){
		/*tElmnt.slice( 0, visibleElmnt ).each(function(){
			size += $(this).height();
		});*/
		
		tList.animate( {height: /*((oneElmntHeight)*visibleElmnt)*/maxSize + "px"} , {duration:1000, complete: showTwitts});
	}
	
	function showTwitts(){
		tElmnt.slice( 0, visibleElmnt )
			.css("top", function(){
				var pos = tmpPos;
				tmpPos += $(this).height() + 20;
				return pos +"px";
			}).show();
		var numBlocks = Math.ceil(tElmnt.length / visibleElmnt);

		for (var i = 1 ; i < numBlocks ; i++){
			tmpPos = 13;
			tElmnt.slice( visibleElmnt * i, visibleElmnt * (i +  visibleElmnt))
			.css("top", function(){
				var pos = tmpPos;
				tmpPos += $(this).height() + 20;
				return pos +"px";
			}).css("left", (tList.width()*i + 7)+"px").show();
		}
		if (tElmnt.length >= visibleElmnt){
			var next = $("<a id='prevTwit' >pr&eacute;c&eacute;dent</a>").css("cursor", "pointer").click(function(){
				tElmnt.animate({left:"-="+tList.width()});
				groupShown++;
				if (groupShown >= numBlocks -1){
					$(this).hide();
				}
				previous.show();
			});
			var previous = $("<a id='nextTwit' style='display:none;'>suivant</a>").css("cursor", "pointer").click(function(){
				tElmnt.animate({left:"+="+tList.width()});
				groupShown--;
				if (groupShown == 0){
					$(this).hide();
				}
				next.show();
			});

			tList.parent().append($("<div style='text-align:right; margin:6px 0; height:16px;'>").append(previous).append(next));
		}
	};
	
	increaseTwitterSize();

	
	
});
