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 = '<a href="http://twitter.com/chinacky" style="text-decoration:none;"><div id="' + twittickerId + '" style="overflow: hidden;position: relative;width :' + twittickerWidth + 'px;height :' + twittickerHeight + 'px;padding :25px 0px 0px 10px;font-size:13px;margin:5px 0px 0px 5px;text-decoration:none;color :#' + twittickerColor + ';background :url(' + twittickerBackground + ') no-repeat;" onmouseover="javascript:clearTimeout(twitticker)" onmouseout="setTimer()"></div></a>';

setTimer();
