(function($,win,doc){
        $.fn.iconScroll = function (options){
            var speed = 15 // 1:max 30:min
            var timer = 10;
            $(this).each(function(){
                var $scrolled = $(this)
                var $content  = $(".scroll_content",$scrolled)
                var $dragger  = $(".scroll_drager",$scrolled)
                var posY      = 0;
                var cposY     = 0;

                $scrolled.installscroll = function(){
                    if($content.length+$dragger.length>0){
                        $scrolled.removescroll()
                        $content  = $(".scroll_content",$scrolled)
                        $dragger  = $(".scroll_drager",$scrolled)
                    }
                    if(!$content.length){
                        $content = $("<div />").addClass("scroll_content")
                        $content.append($scrolled.html())
                        $content.appendTo($scrolled.empty())
                    }
                    if(!$dragger.length){
                        $dragger = $("<div unselectable='on' />").addClass("scroll_drager")
                        $dragger[0].onselectstart=function(){return false}
                        $dragger .appendTo($scrolled)
                    }
                    var conttop = 0
                    var dragtop = 0
                    if($scrolled.scrollTop()){
                        conttop = -$scrolled.scrollTop()
                        dragtop = $scrolled.scrollTop()/($content.height()-$scrolled.height())
                    }
                    $scrolled.css("overflow","hidden")
                    if($scrolled.height()>$content.height()-16){
                        $dragger.hide();
                        return false;
                    }
                    cposY = conttop;
                    $scrolled.css("position","relative")
                    $scrolled.scrollTop(0)
                    $content.css({position:'absolute',left:0,top:conttop})
                    $dragger.css({position:'absolute',right:0,top:0})
                    if($dragger.height()==0){
                        
                        $dragger.css({height:($scrolled.height()/$content.height())*$scrolled.height()})
                    }
                    if($dragger.height()<60){
                        $dragger.height(60)
                    }
                    if($dragger.width()==0){
                       $dragger.width(10) 
                    }
                    posY = dragtop*($scrolled.height()-$dragger.height())
                    $dragger.css({top:posY})
                    $content.css({width:$scrolled.width()-$dragger.width()})
                    
                    $dragger.empty()
                    $dragger.append($("<div class='drag_button' />").addClass("upbutton"))
                    $dragger.append($("<div class='drag_button' />").addClass("downbutton"))
                    $(".downbutton",$dragger).css({position:'absolute',bottom:0})
                    $(".downbutton",$dragger).click(buttondown)
                    $(".upbutton",$dragger).click(buttonup)
                    $dragger.mousedown($scrolled.dragstart)
                    $dragger.mouseup($scrolled.dragstop)
                    $content.mouseup($scrolled.dragstop)
                    
                    $scrolled.bind("mousewheel",function(e,d){
                        if(d<0){
                            buttondown()
                        }
                        if(d>0){
                            buttonup()
                        }
                    })
                }
                var mouse_down_pos = [0,0]
                var drag_down_pos  = [0,0]
                var move_start = false;
                $scrolled.dragstart =function (e){
                    mouse_down_pos = [e.clientX,e.clientY]
                    drag_down_pos  = [$dragger.position().left,posY]
                    move_start = true
                    $(doc).unbind("mousemove")
                    $(doc).bind("mousemove",$scrolled.mover)
                    $(doc).bind("mouseup",$scrolled.dragstop)
                    if(mover_interval)clearInterval(mover_interval)
                    $scrolled.scrolling();
                    mover_interval = setInterval($scrolled.scrolling,timer)
                    return false;
                }

                var mover_interval = null;
                var mouse_move_pos = [0,0]
                $scrolled.mover = function(e){
                    if(move_start){
                        mouse_move_pos = [e.clientX,e.clientY]
                        var fark = mouse_move_pos[1]-mouse_down_pos[1]
                        var newp = drag_down_pos[1]+fark
                            newp = newp<0?0:newp>$scrolled.height()-$dragger.height()?$scrolled.height()-$dragger.height():newp;
                            posY = newp 
                        $dragger.css("top",posY)
                        if(!mover_interval){
                            mover_interval = setInterval(function(){$scrolled.scrolling()},timer)
                        }
                   }
                } 
                $scrolled.scrolling =function(np){
                    var contenth = $content.height();
                    var ratio    = posY / ($scrolled.height()-$dragger.height())
                    if(np != undefined)
                        ratio    = np / ($scrolled.height()-$dragger.height())
                    
                    var oldpos   = cposY 
                    var newpos   = -(contenth-$scrolled.height())*ratio
                    var fark = (oldpos-newpos)/speed
                    var ptop = oldpos - fark
                    if(Math.floor(fark*10) == 0){
                        if(mover_interval)clearInterval(mover_interval)
                        mover_interval = null
                    }
                    cposY = ptop;
                    $content.css({top:cposY})
                }
                $scrolled.dragstop =function (e){
                   move_start = false
                   $(doc).unbind("mousemove")
                   $(doc).unbind("mouseup",$scrolled.dragstop)
                   return false;
                }
                
                function buttondown(){
                    var newp = posY+20;
                        newp = newp<0?0:newp>$scrolled.height()-$dragger.height()?$scrolled.height()-$dragger.height():newp;
                        posY = newp
                    $dragger.stop().animate({top:newp},200,"easeOutQuad")
                    if(mover_interval)clearInterval(mover_interval)
                    $scrolled.scrolling(newp);
                    mover_interval = setInterval(function(){$scrolled.scrolling(newp)},timer)
                }
                
                function buttonup(){
                    var newp = posY-20;
                        newp = newp<0?0:newp>$scrolled.height()-$dragger.height()?$scrolled.height()-$dragger.height():newp;
                        posY = newp 
                    $dragger.stop().animate({top:newp},200,"easeOutQuad")
                    if(mover_interval)clearInterval(mover_interval)
                    $scrolled.scrolling(newp);
                    mover_interval = setInterval(function(){$scrolled.scrolling(newp)},20)
                }

                $scrolled.removescroll = function(){
                    $scrolled.unbind("mousewheel")
                    $(doc).unbind("mousemove")
                    $dragger.unbind("mousedown")
                    $dragger.unbind("mouseup")
                    $content.unbind("mouseup")
                    $scrolled.css("overflow","")
                    $scrolled.css("position","")
                    if($content.length){
                        var contenth = $content.height();
                        var ratio    = $dragger.position().top / ($scrolled.height()-$dragger.height())
                        var newpos   = -(contenth-$scrolled.height()-32)*ratio
                        $scrolled.html($content.html())
                        $scrolled.scrollTop(-newpos)
                    }
                }
                if(options == "remove"){
                    $scrolled.removescroll()
                }
                else 
                    $scrolled.installscroll()
            })
        }
    })(jQuery,window,document)
