﻿function $(x) { return document.getElementById(x); }

function FindAnchors(x) {
    var el = $(x).getElementsByTagName('a');
    var output = Array();
    for (var j = 0; j < el.length; j++) {
        if (el[j].className == 'ITUScrollerItem') {
            output[output.length] = el[j];
        }
    }
    return output;
}

function scrollLoop(x) {
    var pnl = $(x);
    var nodes = FindAnchors(x);
    pnl.insertBefore(nodes[0], pnl.lastChild.nextSibling);
}


function getStyle(el, styleProp) {
    var x = document.getElementById(el);
    if (x.currentStyle)
        var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
    return y;
}

function PauseScroller(counter) {
    clearTimeout(ScrollerCounters[counter]);
}

function RestartScroller(counter) {
    ScrollerCounters[counter] = setTimeout("ScrollUp(" + counter.toString() + ");", 500);
}

function ScrollUp(counter) {
    clearTimeout(ScrollerCounters[counter]);
    var x = ScrollerCorpses[counter];
    var pnl = $(x);
    var nodes = FindAnchors(x);
    if (typeof (nodes[0].style.marginTop) == 'undefined') {

    }
    s = nodes[0].style.marginTop;
    if (s.indexOf('px')) {
        s = Math.round(s.substring(0, s.length - 2));
    } else {
        s = 0;
    }

    if (Math.abs(s - 3) > nodes[0].offsetHeight) {
        s = 0;
        scrollLoop(x);
        nodes[0].style.marginTop = '0px';
        ScrollerCounters[counter] = setTimeout("ScrollUp(" + counter.toString() + ");", 5000);
    } else {
        s = s - 3;
        nodes[0].style.marginTop = s + 'px';
        ScrollerCounters[counter] = setTimeout("ScrollUp(" + counter.toString() + ");", 33);
    }

}
var ScrollerCounters = Array();
var ScrollerCorpses = Array();

function StartScroller(x) {
    var nodes = FindAnchors(x);
    if (nodes.length < 1) return;
    var t = ScrollerCounters.length;
    ScrollerCorpses[t] = x;
    eval("var tmpFunction1 = function() {RestartScroller(" + t.toString() + ");}");
    eval("var tmpFunction2 = function() {PauseScroller(" + t.toString() + ");}");

    for (var j = 0; j < nodes.length; j++) {
        nodes[j].onmouseout = tmpFunction1;
        nodes[j].onmouseover = tmpFunction2;
    }

    ScrollerCounters[t] = setTimeout("ScrollUp(" + t + ");", 1500);
}


function AutoScroller() {
    var d = document.getElementsByTagName("div");
    var e = Array();
    for (var j = 0; j < d.length; j++) {
        if (d[j].className.indexOf('ITUScroller') > -1) {
            if (d[j].id != null)
                StartScroller(d[j].id);
        }
    }
}
