jQuery cycle like gallery

jQuery cycle like gallery

hello
i am trying to build a cycle like gallery here is the code:


    $('.fadein img:gt(0)').hide();


    i=0;
    $("#next").click(function(){

$(".fadein img").eq(i).fadeOut().next('img').fadeIn()
       i++;
       if( i== $('.fadein img').length-1 ) {
     i=0;
 $(".fadein img").eq(0).fadeOut().next('img').fadeIn().end().appendTo('.fadein')
     }

    })














the html:



<a  href="javascript:;"  id="next" >next</a>


<div class="fadein">
<img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">
<img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">
<img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">
</div>







i mainly have a problem when   i== $('.fadein img').length-1 , meaning that the gallery has to restart(to fade in the first img element, anyone have an idea on how to accomplish this?
regards