﻿
/*
*作者:天书(根据karry的修改)
*图片滚动 Version 1.0
*日期:2010-05.28
*jquery1.4
*感谢karry的作品,转载使用请保留karry的名字
*/

(function($) {
    var flagRoll = true;
    $.fn.imageScroll = function(options) {
            var opts = $.extend({}, $.fn.imageScroll.defaults, options);
                        
            $(this).each(function() {
                var $myThis = $(this);
                 $myThis.css("position","absolute");
                 var width =  $myThis.width();
                 $myThis.css("width","3000");
                 $myThis.css("top",0);
                    $myThis.css("left",0);
                       $myThis.css("text-align","left");
                 var wrapId = "imageScroll"+$myThis.attr("id");
                 $myThis.wrap("<div id='"+wrapId+"'></div>");
                 $("#"+wrapId).css("width",width+"px");
                 $("#"+wrapId).css("height",$myThis.height()+"px");
                 $("#"+wrapId).css("position","relative");
                 $("#"+wrapId).css("overflow","hidden");

                if(opts.scrollUnit==0)
                {
                    opts.scrollUnit = $myThis.children(":first-child").width();  
                }
                 if(opts.scrollSize==0)
                 {
                    opts.scrollSize =  $myThis.children().size();
                 }
                var html =   $myThis.html();
                  $myThis.append(html);

                if(opts.manualControl)
                {
//                      $(opts.leftControlId).bind("click",function(){bindEvent($myThis,true);});
//                      $(opts.rightControlId).bind("click",function(){bindEvent($myThis,false);});
                        $("div[id=imageContainer]").bind("mouseover",function(){bindEvent($myThis,false);});
                        $("div[id=imageContainer]").bind("mouseout",function(){bindEvent($myThis,true);});
                        $("div[id=imageContainer]").bind("click",function(){bindclickEvent($myThis);});
                }
//              else
//              {
                 if(opts.direction=="left")
                 {     
                       $("div[id=scrollnewstitle] ul li").text($("div[id="+opts.idstr+"] ul li:nth-child(1)").text());
                                
                       setInterval(function(){
                       if(flagRoll){
                           var left =  getScrollValue($myThis,$myThis.css("left"),true);
                           $myThis.animate({"left":(left+"px")},opts.scrollSpeed);
                           }
                       },opts.timeUnit);

                 }
                 else if (opts.direction == "right")
                 {
                     setInterval(function() {
                         var left = getScrollValue($myThis, $myThis.css("left"),false);
                         $myThis.animate({"left":(left + "px")}, opts.scrollSpeed);
                     }, opts.timeUnit);
                 }
//               }
            });
            
//        function bindEvent($myThis,isLeft) {
//            if (!flagRoll)
//            {
//                flagRoll = true;
//                var left = getScrollValue($myThis, $myThis.css("left"), isLeft);
//                $myThis.animate({"left":(left + "px")}, opts.scrollSpeed, function() {
//                    flagRoll = false;
//                });
//            }
//        }

            function bindEvent($myThis,isflag) {
                    if (!isflag)
                    {
                        flagRoll =false;
                    }
                    else
                    {
                        flagRoll=true;
                    }
            }
            
            function bindclickEvent($myThis,isflag) {
                window.open($("div[id="+opts.idstr+"] ul li:nth-child("+opts.flag+")").attr("title"));
            }
        
        //获取移动后的left值
        //isLeft--是否向右滚动
        function getScrollValue($obj,leftValue,isLeft)
        {

            var reg = /-?\d+/;
            leftValue =Number(leftValue.match(reg));
            if(isLeft)
            {leftValue -= opts.scrollUnit+6; }
            else {leftValue += opts.scrollUnit-10;}

            opts.flag++;
                        
            if (Math.abs(leftValue) > (opts.scrollSize) * opts.scrollUnit+30)
            {
                leftValue = 0;
                opts.flag=1;
            }

            if(leftValue>0)
            {
                leftValue = (opts.scrollSize) * opts.scrollUnit*(-1);
            }
            
            $("div[id=scrollnewstitle] ul li").text($("div[id="+opts.idstr+"] ul li:nth-child("+opts.flag+")").text());
                                    
            return leftValue;
        }
    }
    
    $.fn.imageScroll.defaults = {
        direction:"left",
        manualControl:false, 
        leftControlId:".btnToLeft",
        rightControlId:".btnToRight",
        scrollUnit:0,
        scrollSize:0,
        timeUnit:3000,
        flag:1,
        idstr:"newstitle",
        scrollSpeed:1000
    }
})(jQuery);


