﻿// JavaScript Document
/**
*@fucntion:无缝滚动
*@author:wangsir
*@date:2008-10-10
*@use:直接将文件加入到页面中。设置滚动的容器属性
*@---------------------scroll="true":设置为滚动
*@---------------------scrollTime="100"：滚动的速度。
*/

function Marquee(_speed, _container, _contentDiv) {
    this.speed = _speed ? _speed : 15;
    this.container = _container;
    this.contentDiv = _contentDiv;
    this.Content1 = $(this.contentDiv).get(0);
    this.Content2 = $(this.Content1).clone().get(0);
    this.Container = $(this.container).get(0);
    $(this.Content2).attr("id", $(this.contentDiv).attr("id") + "___2");
    $(this.Content2).insertAfter($(this.Content1)); //克隆demo1为demo2 
    var obj = this;
    var bo = true;
    this.startMarquee = function() {

        var userAgent = navigator.userAgent.toLowerCase();

        if (/mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)) {
            if (obj.Content2.offsetTop - obj.Container.offsetTop - obj.Container.scrollTop <= 0) {
                obj.Container.scrollTop -= obj.Content1.offsetHeight//demo跳到最顶端
            }
            else {

                obj.Container.scrollTop++; //如果是横向的 将 所有的 height top 改成 width left
            }
        }
        else {

            if (obj.Content2.offsetHeight - obj.Container.scrollTop <= 0)//当滚动至demo1与demo2交界时 
                obj.Container.scrollTop -= obj.Content1.offsetHeight//demo跳到最顶端 
            else {
                //alert(obj.Content2.offsetHeight);
                //alert(obj.Container.scrollTop);
                obj.Container.scrollTop++ //如果是横向的 将 所有的 height top 改成 width left 
            }
        }
    }
    var MyMar = setInterval(obj.startMarquee, obj.speed)//设置定时器 
    this.Container.onmouseover = function() { clearInterval(MyMar); } //鼠标移上时清除定时器达到滚动停止的目的 
    this.Container.onmouseout = function() { MyMar = setInterval(obj.startMarquee, obj.speed); } //鼠标移开时重设定时器 
}

/**
*--页面初始化 ------------------------
*-----------------------------------*/

$(document).ready(function() {
    $("div[@scroll=true]").each(function() {
        var control = this;
        var controlContent = $(this).children().get(0);
        var speed = $(this).attr("scrollTime");
        new Marquee(speed, control, controlContent);
    });

    $("div[@picScroll=true]").each(function() {

        var control = this;
        var controlContent = $(this).children().get(0);
        var speed = $(this).attr("scrollTime");
        var count = $(this).attr("scrollCount");
        new PicMarquee(control, speed, count);
    });

});

/**
*@****************************图片间隔滚动************************************
*-----------------_control：滚动的容器 _speed：滚动速度 _count每次滚动的数量
*****************************************************************************/

function PicMarquee(_control, _speed, _count) {
    this.control = $(_control);
    this.child = this.control.children();
    this.count = _count ? _count : 1;
    this.speed = _speed ? _speed : 1000;
    var obj = this;
    this.picSplit = function() {
        obj.child = obj.control.children();
        for (var l = 0; l < parseInt(obj.count); l++) {
            //alert(obj.count);
            $(obj.child.get(0)).insertAfter($(obj.child.get(obj.child.length - 1)));
            //obj.child.push(obj.child=obj.child.shift()); 
        }
    }

    var MyMar = setInterval(this.picSplit, this.speed)//设置定时器 

    for (var l = 0; l < this.child.length; l++) {
        this.child[l].onmouseover = function() { clearInterval(MyMar); } //鼠标移上时清除定时器达到滚动停止的目的 
        this.child[l].onmouseout = function() { MyMar = setInterval(obj.picSplit, obj.speed); } //鼠标移开时重设定
    }
}
