﻿function ToMarquee(margueeid) {
    var $marqueeUl = $('div#' + margueeid + ' ul'),
        $marqueeli = $marqueeUl.append($marqueeUl.html()).children(),
        _height = $('div#' + margueeid).height() * -1,
        scrollSpeed = 600,
        timer,
        speed = 3000 + scrollSpeed;

    // 把 $marqueeUl 的 top 設為顯示到第二組內容的第一筆 
    $marqueeUl.css('top', $marqueeli.length / 2 * _height);

    // 幫左邊 $marqueeli 加上 hover 事件 
    // 當滑鼠移入時停止計時器；反之則啟動 
    $marqueeli.hover(function () {
        clearTimeout(timer);
    }, function () {
        timer = setTimeout(showad, speed);
    });

    // 控制跑馬燈移動的處理函式 
    function showad() {
        var _now = $marqueeUl.position().top / _height;
        _now = (_now - 1 + $marqueeli.length) % $marqueeli.length;

        // $marqueeUl 移動 
        $marqueeUl.animate({
            top: _now * _height
        }, scrollSpeed, function () {
            // 如果已經移動到第一組時...則馬上把 top 設為顯示到第二組內容的第一筆 
            // 藉此產生不間斷的輪播 
            if (_now == 0) {
                $marqueeUl.css('top', $marqueeli.length / 2 * _height);
            }
        });

        // 再啟動計時器 
        timer = setTimeout(showad, speed);
    }

    // 啟動計時器 
    timer = setTimeout(showad, speed);

    $('a').focus(function () {
        this.blur();
    });
}
