(function($){
    $.fn.jslideshow=function(opts){
        var o={
            action:"click",
            duration:100,
            items:3,
            gotoEnd:false,
            slideAt:$.noop
        };
        this.slideAt=function(i){
            o.slideAt(i);
        }
        return this.each(function(){
            if(opts){
                $.extend(o,opts);
            }
            var $this=$(this),
            $ul=$this.children("ul"),
            $lis=$ul.children("li"),
            x=0,
            w=$lis.outerWidth(),
            h=$lis.outerHeight(),
            vW=w*o.items,
            dur=o.duration*(o.gotoEnd?$lis.length:o.items),
            oDur=dur;
            var tW=w*$lis.length;
            $this.css("overflow","hidden").css("position","relative").css("width",(vW)+"px").css("height",h+"px");
            $ul.css("padding","0px").css("position","absolute").css("width",tW+"px").css("left","0px");
            if(o.next){
                $(o.next).bind(o.action,next);
                if(o.action=="mouseover" && o.gotoEnd){
                    $(o.next).bind("mouseout",sAnim);
                }
            }
            if(o.prev){
                $(o.prev).bind(o.action,prev);
                if(o.action=="mouseover" && o.gotoEnd){
                    $(o.prev).bind("mouseout",sAnim);
                }
            }
            function sAnim(){
                $ul.stop();
                x=parseInt($ul.css("left"));
            }
            function next(e){
                var tmp=x;
                x=o.gotoEnd?-(tW-vW):Math.max(x-vW,-(tW-vW));
                if(o.gotoEnd){
                    dur=(oDur-(parseInt($ul.css("left"))/-(tW-vW))*oDur);
                }
                if(tmp!=x){
                    scrollIt();
                }else{
                    dur=oDur;
                }
            }
            function prev(e){
                var tmp=x;
                x=o.gotoEnd?0:Math.min(x+(vW),0);
                if(o.gotoEnd){
                    dur=(parseInt($ul.css("left"))/-(tW-vW))*oDur;
                }
                if(tmp!=x){
                    scrollIt();
                }else{
                    dur=oDur;
                }
            }
            function scrollIt(){
                $ul.animate({
                    left:x
                },dur,function(){
                    dur=oDur;
                });
            }
            o.slideAt=function(i){                
                if(i<$lis.length){
                    if(i<(-x/w)){
                        prev();
                    }else if(i>((-x/w)+o.items-1)){
                        next();
                    }
                }
            };
            o.show=function(i){
                
            };
        });
    };
})(jQuery);
