// ==UserScript==
// @name           Twitter Change Style W
// @namespace      http://worstman.net/
// @include        http://twitter.com/home*
// @version        1.0.0
// ==/UserScript==

// original script : http://userscripts.org/scripts/show/10638
// by http://twitter.com/youpy

// original script : http://twitter.g.hatena.ne.jp/trickstar_os/20070716/1184579609
// by  os9(trickstar_os) ( http://d.hatena.ne.jp/trickstar_os/ )

// original script : http://tyoro.orz.ne.jp/exe/2007/07/twitter_change_s.html
// by  tyoro ( http://tyoro.orz.ne.jp/exe/ )

// author: worstman ( http://worstman.net/ )

(function(){

//////////////////ユーザー定義

//自身の名前が含まれる発言のスタイル（色を指定
var in_color1 = "background-color:#dde840;";
//var in_color1 = "変更後の背景のstyle";
var in_color2 = "color:#4c4cac;";
//var in_color2 = "変更後のリンクのstyle";

//自分の発言のスタイル
var my_color1 = "background-color:#000000;color:#ffffff;";
//var my_color1 = "変更後の背景のstyle";
var my_color2 = "color:#00ff00;";
//var my_color2 = "変更後のリンクのstyle";

//送信者一致
var from_array = new Array();
from_array.push( new Array( "yusukebe" , "background-color:#f45bb7;color:#000000;" , "" ) );
//from_array.push( new Array( "username" , "変更後の背景のstyle" , "変更後のリンクのstyle" ) );

//宛先一致
var to_array = new Array();
//to_array.push( new Array( "username" , "変更後の背景のstyle" , "変更後のリンクのstyle" ) );

//テキスト一致
var text_array = new Array();
//text_array.push( new Array( "色を変えたい文言" , "background-color:#444444;" ) );
//text_array.push( new Array( "消したい文言" , "display:none;" ) );


/////////////////以下プログラムソース（触らない方がよさげ

var username;
username= -1;
var i = 0;
var style_str = "";

var foreach = function(arr,callback) {
	for(;i<arr.length;i++) callback(arr[i]);
};

var h = -300;
function colorchange(){
	if((h+300) < document.body.offsetHeight) {
		if(username === -1)
			username =  $x('//div[@id="side"]//div[@class="section-header"]//a')[0].href.replace(/.*\//,'');
		var updates = $x('//tr[@class="hentry"]');
		if(!!updates) {
			foreach(updates, function(e) {
				if(!e.addcolor) {
					var fromname = $x('descendant::td[@class="content"]/strong/a',e)[0].href.replace(/.*\//,'');
					if( fromname == username ){
						style_str += "#"+e.id+"{"+my_color1+"} #"+e.id+" a {"+my_color2+"} ";
						e.addcolor = true;
						return;
					}

					var content_text = $x('descendant::span[@class="entry-title entry-content"]',e)[0].textContent;
					var me_flag = content_text.match(username);
					if(me_flag){
						style_str += "#"+e.id+"{"+in_color1+"} #"+e.id+" a {"+in_color2+"} ";
						e.addcolor = true;
						return;
					}

					for( u in from_array){
						if(from_array[u][0] == fromname ){
							style_str += "#"+e.id+"{"+from_array[u][1]+"} #"+e.id+" a {"+from_array[u][2]+"} ";
							e.addcolor = true;
							return;
						}
					}

					var meta_text = $x('descendant::span[@class="meta entry-meta"]/a[starts-with(text(),"in reply to")]',e);
					if(meta_text != null){
						var to_text = meta_text[0].textContent.replace(/[\s\w]* /,'');
						if(to_text != null){
							for( u in to_array ){
								if(to_array[u][0] == to_text ){
									style_str += "#"+e.id+"{"+to_array[u][1]+"} #"+e.id+" a {"+to_array[u][2]+"} ";
									e.addcolor = true;
									return;
								}
							}
						}
					}

					for( u in text_array){
						if(content_text.match(text_array[u][0])){
							style_str += "#"+e.id+"{"+text_array[u][1]+"} ";
							break;
						}
					}

					e.addcolor = true;
				}
			});
			h = document.body.offsetHeight;
			if( style_str.length != 0 )
				GM_addStyle(style_str);
		} else {
			clearInterval(timer);
		}
	}
}


if (typeof(GM_addStyle) != 'function') {
	function GM_addStyle(css) {
		var head = document.getElementsByTagName('head');
		if (!!head) {
			var style = document.createElement('style');
			style.type = 'text/css';
			style.innerHTML = css;
			head[0].appendChild(style);
		}
	}
}

var timer = setInterval(colorchange,4500);
colorchange();

// cho45 - http://lowreal.net/
function $x(exp, context) {
  if (!context) context = document;
    var resolver = function (prefix) {
      var o = document.createNSResolver(context)(prefix);
      return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml";
    }
  var exp = document.createExpression(exp, resolver);
  
  var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
  switch (result.resultType) {
    case XPathResult.STRING_TYPE : return result.stringValue;
    case XPathResult.NUMBER_TYPE : return result.numberValue;
    case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
    case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
      result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var ret = [];
      for (var i = 0, len = result.snapshotLength; i < len ; i++) {
        ret.push(result.snapshotItem(i));
      }
			return len != 0 ? ret : null;
    }
  }
  return null;
}
})();