
/*----------------------------------------*/
/* 設定                                   */
/*----------------------------------------*/
var twittickerAccount = "celldivisionjp";
var twittickerCount = 10;
var twittickerWidth = 250;
var twittickerHeight = 20;
var twittickerScrollSpeed = 18;
var twittickerColor = "000000";
var twittickerBackground = "ffffff";

/*----------------------------------------*/

var scrollCount = 0;
var stopX;
var fontSize = 11;
var stopCount = 0;
var stopTime = 0;
var tweetCount = 0;
var tweetList = new Array();
var strCnt = 0;

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;    
    
    /* 表示されている文字数のみをカウントする(<aタグにはさまれている部分は半角として0.5文字とカウント) */
    if(strCnt==0){
	    var target = tweetList[tweetCount];
	    var strAry = target.split("</a>");
	    if(target.indexOf("</a>")>0){
			for(i=0;i<strAry.length+1;i++){
				if(strAry[i]){
				strAry[i] = strAry[i].replace("</a>","");
				strCnt = strCnt + (strAry[i].indexOf("<a href="))+((strAry[i].length - strAry[i].lastIndexOf(">"))/2);
				}
			}
	    }else{
	    	strCnt = target.length;
	    }
	    
		/* 流れる文字列の停止位置を決定(文字数×フォントサイズ) */	
		stopX = -1 * strCnt * fontSize; 

    }

	if(tweetList[tweetCount]==""){
        setTimer();
        return;	
	}

    if (posX < stopX) {
        //posX = stopX;
        posX = -10000;
        stopCount++;
        if (stopCount > stopTime) {
            stopCount = 0;
            scrollCount = 0;
            tweetCount++;
            strCnt = 0;        
        }
        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()", 300);
}



//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:'+ fontSize +'px;color :#' + twittickerColor + ';background-color :#' + twittickerBackground + ';" onmouseover="javascript:clearTimeout(twitticker)" onmouseout="setTimer()"></div>';

setTimer();
