﻿var _this,lineW,width;
var speed=500;//卷动速度，数值越大，速度越慢（毫秒）
var timer=3000;//滚动的时间间隔（毫秒）
var AutoTimer=2000;//自动滚动的时间间隔
var ShowItems=1;//一次能显示几个

$(document).ready(function(){
_this=$("#ScrollItem");
lineW=723; //获取宽度
SetItemWidth();
width=$("#ScrollItem").width();
SetScroll();
$("#ScrollPanel").mouseover(function(){StopScroll=true;}).mouseout(function(){StopScroll=false;});
setTimeout(AutoScroll,AutoTimer);
});
var run=false;
var leftWidth=0;
function ScrollLeft()
{
    var oneWidth=0-lineW;
    
    leftWidth=parseInt(_this.css("marginLeft"),10)+oneWidth;
    Current=parseInt(_this.css("marginLeft"),10);
    
    if (width>lineW && Current-(lineW*(ShowItems+1))+width>0 && leftWidth%lineW==0)
        _this.animate({marginLeft:leftWidth},speed,SetScroll);
}

var RightWidth=0;
function ScrollRight()
{
    var oneWidth=lineW;
    RightWidth=parseInt(_this.css("marginLeft"),10)+oneWidth;
        
    if (width>lineW && RightWidth<=0 && RightWidth%lineW==0)
        _this.animate({marginLeft:RightWidth},speed,SetScroll);
}

function SetScroll()
{
    var Current=parseInt(_this.css("marginLeft"),10);

    if (width>lineW)
    {
        if (Current+lineW<=0)
            $("#ScrollRightImg").attr("src","images/Left_Scroll_button_Hover.gif");
        else
            $("#ScrollRightImg").attr("src","images/Left_Scroll_button.gif");

        if (Current-(lineW*(ShowItems+1))+width>0)
            $("#ScrollLeftImg").attr("src","images/Right_Scroll_button_Hover.gif");
        else
            $("#ScrollLeftImg").attr("src","images/Right_Scroll_button.gif");
    }
    else
    {
        $("#ScrollRightImg").attr("src","images/Left_Scroll_button.gif");
        $("#ScrollLeftImg").attr("src","images/Right_Scroll_button.gif");
    }
}

var ScrollToRight=true;
var StopScroll=false;
function AutoScroll()
{
    var Current=parseInt(_this.css("marginLeft"),10);
    
    if (width>lineW)
    {
        if (!StopScroll)
        {
            if (Current+lineW<=0&&ScrollToRight)
            {
                ScrollToRight=true;
                ScrollRight();
            }
            else
            {
                if (Current-(lineW*(ShowItems+1))+width>0)
                {
                    ScrollToRight=false;
                    ScrollLeft();
                }
                else
                    ScrollToRight=true;
            }
        }
        setTimeout(AutoScroll,AutoTimer);
    }
}

function SetItemWidth()
{   
    var i=1;
    $("#ScrollItem li").each(function(){i++;});
    $("#ScrollItem").width(lineW*i);
}
