
// WebTicker by Mioplanet
// www.mioplanet.com

window.onload=ticker_start;

function ticker_start() {
    TICKER_CONTENT = document.getElementById("TICKER").innerHTML;
    $("#TICKER").show();

    TICKER_RIGHTTOLEFT = false;
    TICKER_FIRST_RUN=true;
    TICKER_OVERFLOW=0;
    TICKER_SLEEP_TIME = 5000;
    //    TICKER_MAX_TIME = 60000;
    TICKER_SPEED = 3;
    TICKER_STYLE = "font-family:Arial; font-size:12px; color:#444444";
    TICKER_PAUSED = false;
    var tickerSupported = false;
    TICKER_WIDTH = $("#TICKER").css("width");
    
    // Firefox
    if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Opera")!=-1) {
        document.getElementById("TICKER").innerHTML = "<TABLE  cellspacing='0' cellpadding='0'><TR><TD nowrap='nowrap'><SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' >&nbsp;</SPAN></TD></TR></TABLE>";
        tickerSupported = true;
    }
    // IE
    if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
        document.getElementById("TICKER").innerHTML = "<DIV nowrap='nowrap' ><SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY'></SPAN></DIV>";
        tickerSupported = true;
    }
    if(!tickerSupported) document.getElementById("TICKER").outerHTML = ""; else {
        var sLeft = TICKER_RIGHTTOLEFT ? $("#TICKER").attr("scrollWidth") - $("#TICKER")[0].offsetWidth : 0;
        $("#TICKER").scrollLeft(sLeft);
        $("#TICKER_BODY").html(TICKER_CONTENT);        
        var last_span = $("#TICKER_BODY span.original:last")
        TICKER_OVERFLOW = last_span[0].offsetLeft + last_span.width();

        if(($("#TICKER").attr("scrollWidth") - $("#TICKER").width()) < TICKER_OVERFLOW){ //ja nepietiek lai veiktu pilnu apli noklone visu htmlu
            var spans = $("#TICKER_BODY span[class!=original]")
            var html = "";
            spans.each(function(){
                html += $(this).html()
            })
            $("#TICKER_BODY").append(html);
        }
        window.setTimeout("TICKER_tick()", 15000);
    }
}

function TICKER_tick() {
    var timeout = 30;
    if(!TICKER_PAUSED){
        var sLeft = $("#TICKER").scrollLeft()
        $("#TICKER").scrollLeft(sLeft+TICKER_SPEED * (TICKER_RIGHTTOLEFT ? -1 : 1))
    }
    if(TICKER_RIGHTTOLEFT && $("#TICKER").scrollLeft() <= 0){
        var sWidth = $("#TICKER").attr("scrollWidth") - $("#TICKER")[0].offsetWidth;
        $("#TICKER").scrollLeft(sWidth);
    }
    var current = get_current()
    if($("#TICKER").scrollLeft() >= current.offsetLeft){
        index = $("#TICKER_BODY span").index(current)+1;
        if(!TICKER_FIRST_RUN)
            timeout = TICKER_SLEEP_TIME;
        else
            TICKER_FIRST_RUN = false
    }
    if(!TICKER_RIGHTTOLEFT && $("#TICKER").scrollLeft() >= TICKER_OVERFLOW){
        $("#TICKER").scrollLeft(0);
        index=0;
    }
    //TICKER_MAX_TIME -= 30
    window.setTimeout("TICKER_tick()", timeout);
}

function get_current(){
    if(typeof index != "number" || $("#TICKER_BODY span").get(index).length==0){
        index=0;
    }
    return $("#TICKER_BODY span").get(index)
}
function set_current(i){
    index = i
}