/*
 * Newsflash routines
 */

function playOrPause(el, imgid, api, isPlaying) {
    if (!isPlaying) {
        api.play();
        el.firstChild.src = '/App_Themes/Default/images/pause_newsflash.gif';
        el.onmouseover = "MM_swapImage('" + imgid + "','','/App_Themes/Default/images/pause2_newsflash.gif',1)";
        el.alt = 'Zaustavi';
    }
    else {
        api.pause();
        el.firstChild.src = '/App_Themes/Default/images/play.gif';
        el.onmouseover = "MM_swapImage('" + imgid + "','','/App_Themes/Default/images/play.gif',1)";
        el.alt = 'Pokreni';
    }
}
var topnews_isPlaying = true;
function topnews_playOrPause(el) {
    playOrPause(el, 'topnews_pause', topnews_api, topnews_isPlaying);
    topnews_isPlaying = !topnews_isPlaying;
}



function newsflash_doPlayOrPause(el, imgid, api, isPlaying) {
    if (!isPlaying) {
        newsflash_play();
        el.firstChild.src = '/App_Themes/Default/images/pause_newsflash.gif';
        el.onmouseover = "MM_swapImage('" + imgid + "','','/App_Themes/Default/images/pause2_newsflash.gif',1)";
        el.alt = 'Zaustavi';
    }
    else {
        newsflash_pause();
        el.firstChild.src = '/App_Themes/Default/images/play.gif';
        el.onmouseover = "MM_swapImage('" + imgid + "','','/App_Themes/Default/images/play.gif',1)";
        el.alt = 'Pokreni';
    }
}
var newsflash_isPlaying = true;
function newsflash_playOrPause(el) {
    newsflash_doPlayOrPause(el, 'vijesti_pause', null, newsflash_isPlaying);
    newsflash_isPlaying = !newsflash_isPlaying;
}
function newsflash_pause() { newsflash_isPlaying = false; clearTimeout(mqr[0].TO); }
function newsflash_play() { mqRotate(mqr); newsflash_isPlaying = true; }
function newsflash_mouseOut() { if (newsflash_isPlaying) mqRotate(mqr); }
function newsflash_mouseOver() { if (newsflash_isPlaying) clearTimeout(mqr[0].TO); }
function newsflash_next() {
    count = 0;
    clearTimeout(mqr[0].TO);
    mqRotateNext(mqr, false);
}
function newsflash_prev() {
    count = 0;
    clearTimeout(mqr[0].TO);
    mqRotateNext(mqr, true);
}

function objWidth(obj) {
    if (obj.offsetWidth)
        return obj.offsetWidth;
    if (obj.clip)
        return obj.clip.width;
    return 0;
}
function getOuterHTML(object) {
    var element;
    if (!object) return null;
    element = document.createElement("div");
    element.appendChild(object.cloneNode(true));
    return element.innerHTML;
}
var mqr = [];
var offset = 8;
function mq(id) {
    this.mqo = document.getElementById(id);
    var wid = objWidth(this.mqo.getElementsByTagName('span')[0]) + offset;
    var txt = getOuterHTML(this.mqo.getElementsByTagName('span')[0]);
    this.mqo.xes = [];
    for (var n = 1; n < this.mqo.getElementsByTagName('span').length; n++) {
        var obj = this.mqo.getElementsByTagName('span')[n];
        if (obj.parentNode == this.mqo) {
            var x = objWidth(obj) + offset;
            wid += x;
            this.mqo.xes.push(wid);
            txt += getOuterHTML(obj);
        }
    }
    //this.mqo.xes.push(wid);
    var fulwid = objWidth(this.mqo);
    this.mqo.innerHTML = '';
    var heit = this.mqo.style.height;
    this.mqo.onmouseout = function() { newsflash_mouseOut(); };
    this.mqo.onmouseover = function() { newsflash_mouseOver(); };
    this.mqo.ary = [];
    var maxw = Math.ceil(fulwid / wid) + 1;
    for (var i = 0; i < maxw; i++) {
        this.mqo.ary[i] = document.createElement('div');
        //this.mqo.ary[i].className = 'swrap';
        this.mqo.ary[i].innerHTML = txt;
        this.mqo.ary[i].style.position = 'absolute';
        this.mqo.ary[i].style.left = (wid * i) + 'px';
        this.mqo.ary[i].style.width = wid + 'px';
        this.mqo.ary[i].style.height = heit;
        this.mqo.appendChild(this.mqo.ary[i]);
    }
    mqr.push(this.mqo);
}
function mqRotate(mqr) {
    if (!mqr) return;
    newsflash_isPlaying = true;
    var j = 0;
    //for (var j = mqr.length - 1; j > -1; j--) 
    {
        maxa = mqr[j].ary.length;
        for (var i = 0; i < maxa; i++) {
            var x = mqr[j].ary[i].style;
            x.left = (parseInt(x.left, 10) - 1) + 'px';
        }
        var y = mqr[j].ary[0].style;
        if (parseInt(y.left, 10) + parseInt(y.width, 10) < 0) {
            var z = mqr[j].ary.shift();
            z.style.left = (parseInt(z.style.left) + parseInt(z.style.width) * maxa) + 'px'; mqr[j].ary.push(z);
        }
    }
    mqr[0].TO = setTimeout('mqRotate(mqr)', 100);
}

var count = 0;
var item = 0;
function mqRotateNext(mqr, right) {
    if (!mqr) return;
    newsflash_isPlaying = true;

    var j = 0;
    if (count == 0) {

        var x = mqr[j].ary[0].style;
        var w = Math.abs(parseInt(x.left, 10));
        for (var n = 0; n < mqr[j].xes.length; n++) {
            if (w < mqr[j].xes[n] - offset * n - 15) {
                item = n;
                break;
            }
        }
    }
    count++;

    maxa = mqr[j].ary.length;
    for (var i = 0; i < maxa; i++) {
        var x = mqr[j].ary[i].style;
        x.left = (parseInt(x.left, 10) - 10) + 'px';
    }
    var y = mqr[j].ary[0].style;
    if (parseInt(y.left, 10) + parseInt(y.width, 10) < 0) {
        var z = mqr[j].ary.shift();
        z.style.left = (parseInt(z.style.left) + parseInt(z.style.width) * maxa) + 'px'; mqr[j].ary.push(z);
    }

    var x = mqr[j].ary[0].style;
    var found = false;
    var newitem = item - 1;
    if (newitem < 0)
        newitem = mqr[j].xes.length - 1;
    var w = Math.abs(parseInt(x.left, 10));
    for (var n = 0; n < mqr[j].xes.length; n++) {
        if (w < mqr[j].xes[n] - offset * n - 15) {
            if (right) {
                if (n == newitem) {
                    mqr[0].TO = setTimeout('mqRotate(mqr)', 1500);
                    found = true;
                }
            }
            else {
                if (item != n) {
                    mqr[0].TO = setTimeout('mqRotate(mqr)', 1500);
                    found = true;
                }
            }
            break;
        }
    }
    if (!found) {
        if (right)
            mqr[0].TO = setTimeout('mqRotateNext(mqr,true)', 1);
        else
            mqr[0].TO = setTimeout('mqRotateNext(mqr,false)', 1);
    }
}

