/*
 * $Id: gimmick.js,v 1.8 2006/03/10 09:47:35 y-uzumasa Exp $
 */

var _schedulers = new Array();

function newScheduler() {
    var scheduler = new Scheduler();
    _schedulers[_schedulers.length] = scheduler;
    return scheduler;
}
function getScheduler(id) {
    return _schedulers[id];
}

function Scheduler() {
    this.id = _schedulers.length;
    this.tasks = new Array();
    this.index = -1;
    this.add = function(task) {
        task.sid = this.id;
        this.tasks[this.tasks.length] = task;
    };
    this.start = function(interval) {
        if (this.hasNext()) {
            this.next().start();
        }
        if (interval > 0) {
            setTimeout("getScheduler(" + this.id + ").start()", interval);
        }
    };
    this.hasNext = function() {
        return (this.tasks.length > (this.index + 1));
    };
    this.next = function() {
        return this.tasks[++this.index];
    };
    this.current = function() {
        return this.tasks[this.index];
    };
}

/* 
 * <script language="javascript">
 * var s = newScheduler();
 * s.add(new WaitTask(1000));
 * s.add(new OtherTask());
 * s.start();
 * </script>
 */
function WaitTask(interval) {
    this.interval = interval;
    this.sid = 0;
    this.start = function() {
        if (getScheduler(this.sid).hasNext()) {
            setTimeout("getScheduler(" + this.sid + ").next().start()", this.interval);
        }
    };
}

/* 
 * <div id="xxx" style="width:100%; filter:alpha(Opacity=100); -moz-opacity:1;"></div>
 * <script language="javascript">
 * var s = newScheduler();
 * s.add(new FadeTask("xx", 100, FADE_IN_FAST, "fade in text"));
 * s.add(new FadeTask("xx", 100, FADE_OUT_SLOW));
 * s.start();
 * </script>
 */

var FADE_IN_SLOW = 5;
var FADE_OUT_FAST = -10;

function FadeTask(id, interval, opinc, html) {
    this.id = id;
    this.interval = interval;
    this.opacity = (opinc > 0) ? 0: 100;
    this.opinc = opinc;
    this.html = html;
    this.safeCounter = 100;
    this.sid = 0;
    this.start = function() {
        var elm = $(this.id);
        if (!elm || this.opacity < 0 || this.opacity > 100 || --this.safeCounter < 0) {
            if (getScheduler(this.sid).hasNext()) {
                getScheduler(this.sid).next().start();
            }
            return;
        }
        if (elm.filters && elm.filters.alpha) {
            elm.filters.alpha.Opacity = this.opacity;
        } else if (elm.style) {
            // PENDING: elm.style.MozOpacity に 1.0 を入れると何故かテキストがずれるので 0.99 入れとく
            elm.style.MozOpacity = (this.opacity == 100) ? 0.99: parseFloat(this.opacity / 100);
        }
        if (this.html && elm.innerHTML != this.html) {
            elm.innerHTML = this.html;
        }
        this.opacity += this.opinc;
        setTimeout("getScheduler(" + this.sid + ").current().start()", this.interval);
    };
}

/*
 * <div style="width:153px;height:80px;overflow-x:hidden;overflow-y:hidden;" id="xxx">
 * <div id="tip1" style="height:80px;">test 1</div>
 * <div id="tip2" style="height:80px;">test 2</div>
 * <div id="tip3" style="height:80px;">test 3</div>
 * </div>
 * <script language="javascript">
 * window.setInterval("scrollTips('xxx', 15)", 2000);
 * </script>
 */
function scrollTips(id, interval) {
    var tips = document.getElementById(id);
    removeWithoutElement(tips);
    var height = getPixcelValue(tips.style["height"]);
    var beforeLast = tips.childNodes.length - 1;
    var orgScrollTop = tips.scrollTop;
    if (orgScrollTop == (height * beforeLast)) {
        while (--beforeLast >= 0) {
            tips.appendChild(tips.removeChild(tips.firstChild));
        }
        orgScrollTop = 0;
        tips.scrollTop = 0;
    }
    var i = 1;
    var scrollDelay = window.setInterval(
        function() {
            tips.scrollTop = orgScrollTop + i;
            if (++i > height) {
                clearInterval(scrollDelay);
                scrollDelay = 0;
            }
        }
    , interval);
}

function getPixcelValue(pixcel) {
    var px = pixcel.indexOf("px");
    return (px == -1) ? pixcel: pixcel.substring(0, px);
}

/** @deprecated */
function getElementById(id) {
    return $(id);
}

function removeWithoutElement(node) {
    var child = node.firstChild;
    while (child) {
        var nextSibling = child.nextSibling;
        if (child.nodeType != 1) {
            node.removeChild(child);
        }
        child = nextSibling;
    }
}
