/*
	if an element contains longer text, this script insert "zero width space" to prevent it expanding
*/
(function(){
	var REGEXP =/([0-9a-zA-Z０-９ａ-ｚＡ-Ｚ]{5})/g;
		  
	var if_old_ie=false;
	var iest = navigator.userAgent.indexOf("MSIE");
	if(iest != -1){
		var ieed = navigator.userAgent.indexOf(";",iest);
		var ienum = Number(navigator.userAgent.substring(iest+5,ieed));		
		var version=Number(ienum);
		if(version<7) if_old_ie=true;
	}

	var wbrReplace=Class.create();
	wbrReplace.prototype={
		initialize:function(listingFunction,regex,width){
			if(if_old_ie) return;
			this.listingFunction=listingFunction;
			this.regex=regex;
			this.width=width;
			Event.observe(window,"load",this.load.bind(this),false);
		},
		load:function(){
			var lists=this.listingFunction();
			
			if(lists.length>0)	lists.each(this.text_mod.bind(this),false);
		}
	}
	
	if( !if_old_ie ){
		
		var insertstring=String.fromCharCode(8203); // = zero width space at Unicode
		wbrReplace.prototype.text_mod=function(node,mod_width){
			if(this.width) node.style.width=this.width;
			
			if(node.hasChildNodes()){
				$A(node.childNodes).each(this.textReplace.bind(this));
				
			}
		}
		wbrReplace.prototype.textReplace=function(parent){
			if(parent.nodeType!=3) return false; // Only work for text node
			var string=parent.nodeValue;
			if(string){
				var string=string.replace(this.regex,
					function (s0,s1) {
						return s1+insertstring;
					}
				);
				parent.nodeValue=string;
			}
		}
	}
	
	new wbrReplace(function(){
		return $A($("left").getElementsByTagName("a"))		
	},REGEXP);
	new wbrReplace(function(){
		return $A($("main").getElementsByTagName("td"))		
	},REGEXP);
	new wbrReplace(function(){
		return $A($("main").getElementsByTagName("div"))		
	},REGEXP);
	new wbrReplace(function(){
		return $A($("main").getElementsByTagName("li"))		
	},REGEXP);
	new wbrReplace(function(){
		return $A($("main").getElementsByTagName("p"))		
	},REGEXP);
	new wbrReplace(function(){
		return $A($("main").getElementsByTagName("a"))		
	},REGEXP);
	
})()
