// JavaScript Document

(function($)
{

	jQuery.fn.cercle = function(childIdent, debut, sens, parfait)
	{
		var childIdent = childIdent || ''; 	  // Sélecteur Jquery des enfants mis en cercle
		var debut 	   = debut 		|| 0; 	  // Position angulaire de départ en °
		var sens 	   = sens  		|| false; // Sens de rotation, horaire : true, horaire inverse : false
		var parfait    = parfait 	|| false; // Vrai cercle, parfait = true, sinon on conserve les dimensions du conteneur
		
		$(this).css('position','relative');

		this.each(function()
		{
			var pas   = Math.PI / ($(this).children(childIdent).length/2);
			var alpha = Math.PI*debut/180;
			
			if ( parfait 
				&& $(this).height()+$(this).children(childIdent).width() != $(this).width()
				&& $(this).width()+$(this).children(childIdent).height() !=  $(this).height())
			{
				if ($(this).height() > $(this).width())
					$(this).width( $(this).height()+$(this).children(childIdent).width() );
				else
					$(this).height( $(this).width()+$(this).children(childIdent).height() );
			}
			
			$(this).children(childIdent).each(function(id,item)
			{
				var radiusX = $(this).parent().width()/2-($(this).width()/2);
				var posX  	= radiusX + radiusX * Math.sin(alpha);
				var radiusY = $(this).parent().height()/2-$(this).height()/2;
				var posY  	= radiusY + radiusY * Math.cos(alpha);

				$(item).css('position','absolute')
					   .css('visibility','inherit')
					   .css('left',posX)
					   .css('top',posY);

				if (sens)
					alpha -= pas; // Sens horaire
				else
					alpha += pas;
			});
		});

		return this;
	};

})(jQuery);

