google.load("feeds", "1");

var url_match = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)|((@|#)[\w]+)/g; 

function appendChildren(div, text) {
    var nodes = new Array();
    var urls = text.match(url_match);
    text = stripTweetSource(div, text);
    extractNodes(text, urls, nodes);
    writeNodes(div, nodes);
}

function stripTweetSource(div, text) {
	var pos = text.indexOf(":");
	if (pos >= 0) {
		//writeLinkNode(div,text.substring(0,pos)+":","http://twitter.com/"+ text.substring(0,pos));
		return text.substring(pos + 1); 
	}
	return text;
}

function extractNodes(text, urls, nodes) {
    var startPos = 0;
    if (urls) {
        for (i = 0; i < urls.length; i++) {
            var currentUrl = urls[i];
            var pos = text.indexOf(currentUrl, startPos);
            if (pos != -1) {
                if (pos > startPos) {
                    nodes.push(text.substring(startPos, pos));
                    nodes.push(currentUrl);

                } else {
                    nodes.push(currentUrl);
                }
                startPos = pos + currentUrl.length + 1;
            }
        }
        nodes.push(text.substring(startPos));
    } else {
        nodes.push(text.substring(startPos));
    }
}

function writeNodes(div, nodes) {
    for (i = 0; i < nodes.length; i++) {
        if (url_match.test(nodes[i])) {
            writeLinkNode(div, nodes[i], nodes[i]);
        } else {
			var firstChar = nodes[i].charAt(0);
			if (firstChar == '@' || firstChar == '#') {
				writeLinkNode(div, nodes[i], nodes[i]);
			} else {
				writeTextNode(div, nodes[i]);
			}
        }
    }
}

function writeTextNode(div, text) {
    div.appendChild(document.createTextNode(text));
}

function writeLinkNode(div, text, url) {
    var aNode = document.createElement("a");
    var firstChar = text.charAt(0);
    var newUrl = url;
	switch(firstChar) {
		case '@' :
		    newUrl = "http://twitter.com/"+text.substring(1); 
		    break; 
		case '#' :
		    newUrl = "http://twitter.com/search?q=%23"+text.substring(1);
		    break;
	}
	aNode.setAttribute("href", newUrl);
    aNode.appendChild(document.createTextNode(text));
    div.appendChild(aNode);
	writeTextNode(div, " ")
}


function initialize() {
    var feed = new google.feeds.Feed("http://twitter.com/statuses/user_timeline/17745080.rss");
    feed.load(function(result) {
        if (!result.error) {
            var container = document.getElementById("tweets");
            var br = document.createElement("br");
            var h1 = document.createElement("h1");
            h1.appendChild(document.createTextNode("Latest Tweets"));
            container.appendChild(br);
            container.appendChild(h1);
            for (var i = 0; i < result.feed.entries.length; i++) {
                if (i < 3) {
                    var entry = result.feed.entries[i];
                    var attribute = "title";
                    var div = document.createElement("div");
                    div.setAttribute("class", "newsItems");

                    appendChildren(div, entry[attribute]);
                    container.appendChild(div);
                }
            }
        }
    });
}

google.setOnLoadCallback(initialize);

