/*
Tweet Me!

The MIT License
Copyright (c) 2008 Masahiko Tachizono <mshk@otolog.jp>

Based on Yuichiro Masui's Hatebu Anywhere (http://blog.masuidrive.jp/index.php/2008/04/17/released-hatena-bookmark-anywhere/).
 */

var tweet_me_style; // falseを設定するとCSSでスタイルの指定が可能になる
var tweet_me_limit; // 表示件数
var tweet_me_url; // 表示するURL 未指定の場合、このページ

function __tweet_me_anywhere_receiver(json) {

    try {
	var html = "";

	var escapeHTML = function(str) {
	    str = str.replace("&","&amp;");
	    str = str.replace("\"","&quot;");
	    str = str.replace("'","&#039;");
	    str = str.replace("<","&lt;");
	    str = str.replace(">","&gt;");
	    return str;
	}

	// http://juce6ox.blogspot.com/2007/11/cssdom.html
	// by latchet
	var addCSSRule = (document.createStyleSheet)
	    ? (function(sheet){
		    return function(selector, declaration){
			sheet.addRule(selector, declaration);
		    };
		})(document.createStyleSheet())
	    : (function(sheet){
		    return function(selector, declaration){
			sheet.insertRule(selector + '{' + declaration + '}', sheet.cssRules.length);
		    };
		})((function(e){
			    e.appendChild(document.createTextNode(''));
			    (document.getElementsByTagName('head')[0] || (function(h){
				    document.documentElement.insertBefore(h, this.firstChild);
				    return h;
				})(document.createElement('head'))).appendChild(e);
			    return e.sheet;
			})(document.createElement('style')))

	if(tweet_me_style!==false) {
	    addCSSRule(".tweet_me_box", "background-color: #9ae4e8; margin: 2px; padding: 15px;");        
	    addCSSRule(".tweet_me_avator", "float: left; margin: 5px;");
        
	    addCSSRule(".tweet_me_result", "list-style-type: none; padding: 2px; margin: 0px; background-color: #fff;");
	    addCSSRule(".tweet_me_result li", "border-bottom: 1px dotted; list-style-type: none; padding: 3px;");
	    addCSSRule(".tweet_me_avator", "float: left; margin: 5px;");
	    addCSSRule(".tweet_me_info", "color: #777;");
   	    addCSSRule(".tweet_me_powered_by", "text-align: right;");
	    addCSSRule(".tweet_me_clearer", "clear: both;");
	    addCSSRule(".tweet_me_url", "color: #aaa;");                
	}
	if(json==null) {
	    html += "このエントリーへのつぶやき<br/>";
        html += "<a href=\"http://twitter.com/home?status=" + escapeHTML(tweet_me_url) + "\">つぶやく</a>";
	    html += "<div class=\"tweet_me_box\">";                
	}
	else {
	    if((typeof tweet_me_limit)!="number") tweet_me_limit = 100;
	
	    html += "このエントリーへのつぶやき <a href=\"http://search.twitter.com/search?q="+tweet_me_url+"\" target=\"_blank\">もっと見る</a>";
        html += " - <a href=\"http://twitter.com/home?status=" + escapeHTML(tweet_me_url) + "\" target=\"_blank\">つぶやく</a>";
	    html += "<div class=\"tweet_me_box\">";                        
	    html += "<ul class=\"tweet_me_result\">";
	    for(var i=0; i<json.results.length&&tweet_me_limit>0; ++i) {
		    var tweet = json.results[i];
//            tweet.text.replace(tweet_me_url, "<span class=\"tweet_me_url\">" + tweet_me_url + "</span>");
		var t = tweet.created_at.split(" ")[0].split("/");
		var tags = [];
		if(tweet.text!='') {
            html += "<li class=\"tweet_me_result\">";
            html += "<div class=\"tweet_me_avator\">";            
            html += "<img src=\"" + tweet.profile_image_url + "\">";
            html += "</div>";
            html += "<div class=\"tweet_me_msg\">";
            html += "<a href=\"http://twitter.com/" + tweet.from_user + "\">" + tweet.from_user + "</a>: ";            
            html += tweet.text;
            html += "</div>";
            html += "<div class=\"tweet_me_info\">";
            html += tweet.created_at;
            html += " - <a href=\"http://twitter.com/home?status=@" + tweet.from_user + "\" target=\"_blank\">リプライ</a> - ";
            html += "<a href=\"http://twitter.com/" + tweet.from_user + "/statuses/" + tweet.id + "\" target=\"_blank\">このつぶやきを見る</a>";                                    
            html += "</div>";
            html += "<p class=\"tweet_me_clearer\"></p>";
            
            html += "</li>";

		    tweet_me_limit--;
		}
	    }
	    html += "</ul></div>";
        html += "<div class=\"tweet_me_powered_by\">";        
        html += "<a href=\"http://search.twitter.com/api\">Powered by Twitter</a>";
	    html += "</div>";        
	}

	var wrap = document.createElement("div");
	wrap.innerHTML = html;
	document.getElementById("tweet_me_here").appendChild(wrap);
    } catch(e) { }
}

function __tweet_me_anywhere_load() {
    try {
	if((typeof document.getElementById("tweet_me_anywhere"))!="undefined") {
	    var script = document.createElement("script");
	    script.setAttribute("type","text/javascript");
	    if((typeof tweet_me_url)=="undefined") tweet_me_url = location.href.replace(/#.*/,"");
	    script.setAttribute("src","http://search.twitter.com/search.json?q="+tweet_me_url+"&callback=__tweet_me_anywhere_receiver");
	    document.body.appendChild(script);
	}
    } catch(e) { }
}

try {
    if(window.addEventListener) {
	window.addEventListener("load", __tweet_me_anywhere_load, false);
    }
    else {
	window.attachEvent("onload", __tweet_me_anywhere_load);
    }
} catch(e) {  }
