!function($){
    $.extend($.fn, {
        TweenScroll:function(options){
        
            var defaults = {
                speed:300,
                step:30,
                pause:5000,
                num:80,
                fill:false,
                dictionary:['up','right','down','left'],
                direction : 'down',
                btnPrev:null,
                btnNext:null,
                count:4,
                sumCount:8,
                autoplay:true
            }
            
            var externalTimer = null,
                movelock = false,
                opt = $.extend(defaults, options),
                pageHeight = this.height(),
                pageWidth = this.width(),
                self = this;
                        
            if(typeof opt.content == 'undefined') return;
            
            var content = $(opt.content),copyelem = content.clone(true);
            
            if(opt.direction == "left" || opt.direction == "right"){
                
                content.before(
                                "<table id='wrap' cellpadding='0' cellspacing='0' border='0'>"
                                +"<tr>"
                                +"<td></td>"
                                +"<td></td>"
                                +"</tr>"
                                +"</table>"
                              );
                $("#wrap").find("td:eq(0)").append(content);
                
                $("#wrap").find("td:eq(1)").append(copyelem);
                
                var w = Math.ceil(opt.sumCount / opt.count) * pageWidth;
                
                content.width(w);
                
                copyelem.width(w);
                
                $("#wrap").width(content.width() * 2);
                
                if($.browser.msie){
                    $(".ul > li").css("textIndent","10px");
                }else{
                    $(".ul > li").css("paddingLeft","10px");
                }
                
            }else{
                    var h = Math.ceil(opt.sumCount / opt.count) * pageHeight;
                    content.height(h);
                    copyelem.height(h);
                    content.parent().append(copyelem);
                
            }
            
            
            if(opt.btnNext){
                if(opt.direction == "left" || opt.direction == "right"){
                    $(opt.btnNext).mousedown(function(){
                        if(!movelock){
                            clearInterval(externalTimer);
                            s_right();
                            if(opt.autoplay){
                                AutoPlay(opt.direction);
                            }
                            movelock = true;
                        }
                    });
                }else{
                    $(opt.btnNext).click(function(){
                        clearInterval(externalTimer);
                        if(!movelock){
                            s_down();
                            if(opt.autoplay){
                                AutoPlay(opt.direction);
                            }
                            movelock = true;
                        }
                    });
                }
            }
            if(opt.btnPrev){
                if(opt.direction == "left" || opt.direction == "right"){
                    $(opt.btnPrev).click(function(){
                        if(!movelock){
                            clearInterval(externalTimer);
                            s_left();
                            if(opt.autoplay){
                                AutoPlay(opt.direction);
                            }
                            movelock = true;
                        }
                    });
                }else{
                    $(opt.btnPrev).click(function(){
                        if(!movelock){
                            clearInterval(externalTimer);
                            s_up();
                            if(opt.autoplay){
                                AutoPlay(opt.direction);
                            }
                            movelock = true;
                        }
                    });
                }
            }
            
            function AutoPlay(dir){
                switch(dir){
                    case "up":
                            externalTimer = setInterval(s_up,opt.pause);
                            break;
                    case "right":
                            //s_right();
                            externalTimer = setInterval(s_right,opt.pause);
                            break;
                    case "down":
                            externalTimer = setInterval(s_down,opt.pause);
                            break;
                    case "left":
                            externalTimer = setInterval(s_left,opt.pause);
                            break;
                    default:
                          alert('No match is found, the direction of');
                }
            }
            if(opt.autoplay){
                AutoPlay(opt.direction);
                self.hover(
                    function(){clearInterval(externalTimer)},
                    function(){AutoPlay(opt.direction)}
                );
            }
            
            function s_up(){
                self.stop();
                if(content.offset().top - self.scrollTop()>0){
                    self.scrollTop(self.scrollTop() + content.height());
                }
                self.animate({
                    scrollTop:self.scrollTop() - (opt.fill ? pageHeight : opt.num)
                },{duration: opt.speed,complete :function(){
                    movelock = false;
                }});
            }
            function s_down(){
                self.stop();
                if(copyelem.height() - self.scrollTop()<=0){
                    self.scrollTop(0);
                }
                self.animate({
                    scrollTop:self.scrollTop() + (opt.fill ? pageHeight : opt.num)
                },{duration: opt.speed,complete :function(){
                    movelock = false;
                }});
            }
            function s_left(){
                self.stop();
                if(self.scrollLeft()<=0){
                    self.scrollLeft(self.scrollLeft() + content.width());
                }
                self.animate({
                    scrollLeft:self.scrollLeft() - (opt.fill ? pageWidth : opt.num)
                },{duration: opt.speed,complete :function(){
                    movelock = false;
                }});
            }
            function s_right(){
                self.stop();
                if(content.width() - self.scrollLeft()<=0){
                    self.scrollLeft(0);
                }
                self.animate({
                    scrollLeft:self.scrollLeft() + (opt.fill ? pageWidth : opt.num)
                },{duration: opt.speed,complete :function(){
                    movelock = false;
                }});
            }
        }
    });
}(jQuery);

