var scrollCount = 0;
var stopX = 10;
var stopCount = 0;
var stopTime = 200;
var tweetCount = 0;
var tweetList = new Array();

var twittickerId = "twit-ticker" + (+new Date);
var twitticker;

function timeline(timeline) {
    for (var i = 0; i < timeline.results.length; i++) {
        tweetList[i] = textFormat(timeline.results[i].text);
    }
}

function textFormat(text) {
    text = text.replace(/(https?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, "<a href='$1' target='_blank'>$1</a>");
    text = text.replace(/@([a-zA-Z0-9_]+)/gi, "<a href=\"http://twitter.com/$1\" target=\"_blank\">@$1</a>");
    text = text.replace(/#([a-zA-Z0-9_]+)/gi, "<a href='http://twitter.com/search?q=%23$1' target='_blank'>#$1</a>");
    return text;
}

function scrollMessage() {
    if (!tweetList[tweetCount]) {
        setTimer();
        return;
    }
    var posX = twittickerWidth - scrollCount * twittickerScrollSpeed;

    if (posX < stopX) {
        posX = stopX;

        stopCount++;
        if (stopCount > stopTime) {
            stopCount = 0;
            scrollCount = 0;
            tweetCount++;
        }
        if (tweetCount == tweetList.length) {
            tweetCount = 0;
        }
    }

    if (stopCount < 1) {
        document.getElementById(twittickerId).innerHTML = '<nobr><div style="position:absolute;left:' + posX + 'px;">' + tweetList[tweetCount] + '</div></nobr>';
    } else if (stopCount == 1) {
        document.getElementById(twittickerId).innerHTML = '<div style="position:absolute;left:' + posX + 'px;">' + tweetList[tweetCount] + '</div>';
    }

    scrollCount++;

    setTimer();
}

function setTimer() {
    twitticker = setTimeout("scrollMessage()", 20);
}

//document.write('<scr' + 'ipt type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?id=' + twittickerAccount + '&count=' + twittickerCount + '&callback=timeline"></scr' + 'ipt>');
document.write('<scr' + 'ipt type="text/javascript" src="http://search.twitter.com/search.json?q=from:' + twittickerAccount + '&rpp=' + twittickerCount + '&callback=timeline"></scr' + 'ipt>');
document.getElementById("twit-ticker").innerHTML = '<div id="' + twittickerId + '" style="overflow: hidden;position: relative;width :' + twittickerWidth + 'px;height :' + twittickerHeight + 'px;padding :5 0 0 10;font-size:13px;color :#' + twittickerColor + ';background-color :#' + twittickerBackground + ';" onmouseover="javascript:clearTimeout(twitticker)" onmouseout="setTimer()"></div>';

setTimer();


