/*
var DraggableList=Class.create();
DraggableList.prototype={
	initialize : function(id,chks){
		this.root=$(id);
		switch($(id).nodeName){
			case "TABLE":
				this.list=$A($(id).getElementsByTagName("tr"));
			break;
			
			case "UL":
			case "OL":
				this.list=$A($(id).getElementsByTagName("li"));
			break;
			
			default:
				return ;
			break;
		}
		
		if(!chks){
			this.checks=$A(this.root.getElementsByTagName("input"));
		}else{
			this.checks=chks;
		}
		
		
		this.list.each(function(v){
			var chk=v.getElementsByTagName("input")[0];
			if(chk.checked){
				
			}
			
			Event.observe(v,"mouseover",this.changeColor.bind(this));
			Event.observe(v,"mouseover",function(){
				v.style.cursor="pointer";
			}.bind(this));
		});
	}
	
	,changeColor : function(node){
		if(node.nodeName == "TR"){
		
		}else{
		}
	}
}
*/
/*

		var chk=$A($("money").getElementsByTagName("input"));
		list_TR($("money").getElementsByTagName("li"));
		list_check(chk);
*/


var colors={
	def : "#fff"
	,hover : "#dde"
	,checked : "#ffa"
};

TRStatus=null;

function changeColor(tr,color){
	if(tr.nodeName == "TR"){
		for(var i=0; i < tr.childNodes.length ; i++){
			tr.childNodes[i].style.backgroundColor=color;
		}
	}else{
		tr.style.backgroundColor=color;
	}
}
		
function list_TR(tr){
		$A(tr).findAll(function(e){
			return (e.getElementsByTagName("input").length > 0) ? true : false;
		}).each(function(e){
			var chk=e.getElementsByTagName("input")[0];
			
			if(chk.checked){
				changeColor(e,colors.checked);
			}
			
			e.onmouseover=function(){
				e.style.cursor="pointer";
				switch(TRStatus){
					case true:
						changeColor(e,colors.checked);
						chk.checked=true;
					break;
					
					case false:
						changeColor(e,colors.def);
						chk.checked=false;
					break;
					
					default:
						if(chk.checked == false){
							changeColor(e,colors.hover);
						}
					break;
				}
			}
			
			
			e.onmouseout=function(){
				e.style.cursor="default";
				if(chk.checked == false){
					changeColor(e,colors.def);
				}
			}
			
			Event.observe(window,"mouseup",function(){
				TRStatus=null;
			});
			
			e.onmouseup=function(){
				TRStatus=null;
			}
			
			e.onmousedown=function(){
				if(chk.checked){
					TRStatus=false;
					changeColor(e,colors.def);
					chk.checked=false;
				}else{
					changeColor(e,colors.checked);
					chk.checked=true;
					TRStatus=true;
				}
				return false;
			}
			
			e.onselectstart=function(){
				return false;
			}
		});
	}

function list_check(chk){
	chk.findAll(function(e){
		return e.checked;
	}).each(function(e){
		changeColor(e.parentNode.parentNode,colors.checked);
	});
	
	chk.each(function(e){
		e.onclick=function(){
			if(this.checked){
				changeColor(this.parentNode.parentNode,colors.def);
				this.checked=false;
			}else{
				changeColor(this.parentNode.parentNode,colors.checked);
				this.checked=true;
			}
		}
	});
}

function list_navigation(navid,chk){
	var sp=span_create();
	sp.onmousedown=function(){
		chk.each(function(e){
			changeColor(e.parentNode.parentNode,colors.checked);
			e.checked=true;
		});
		return false;
	}
	sp.innerHTML="すべてチェック";
	sp.title='全部にチェックをつけます';
	$(navid).appendChild(sp);
	
	/* ------------------- */
	
	var sp=span_create();
	sp.onmousedown=function(){
		chk.each(function(e){
			changeColor(e.parentNode.parentNode,colors.def);
			e.checked=false;
		});
		return false;
	}
	sp.innerHTML="すべてのチェックを外す";
	sp.title='全てのチェックを解除します。';
	$(navid).appendChild(sp);
	
	/* ------------------- */
	
	var sp=span_create();
	sp.onmousedown=function(){
		chk.each(function(e){
			if(e.checked){
				changeColor(e.parentNode.parentNode,colors.def);
				e.checked=false;
			}else{
				changeColor(e.parentNode.parentNode,colors.checked);
				e.checked=true;
			}
		});
		return false;
	}
	sp.innerHTML="反転";
	sp.title='チェックを反転します。';
	$(navid).appendChild(sp);
	
}

Spans=[];

function span_create(){
	var sp=document.createElement("span");
	sp.style.cursor="pointer";
	sp.style.margin="0px 4px";
	sp.style.padding="3px";
	sp.style.textDecoration="underline";
	sp.style.background="#ddf";
	sp.style.fontSize="small";
	Spans.push(sp);
	return sp;
}

window.onunload=function(){
	for(var i in Spans){
//		Spans[i].removeEventListener("onmousedown",Spans[i].onmousedown);
		Spans[i].onmousedown=null;
	}
}
