// ---------------------------------------------------------------- //
//  自動フリガナ入力フォーム - JavaScript
// ---------------------------------------------------------------- //
// http://d.hatena.ne.jp/kuwa/20051027/p1
// http://shiro.au-lait.net/test/autoruby/autoruby2.html
// 改造 Issei Ishii / 2006-06-01 > JKLを使用していた部分をDOMに変更
// 改造 Yuji Yamamoto / 2007-01-10 > prototype化(要prototype.js)
// 使い方
// new autoRuby( src_elem , dst_elem, no_convert_kana)
//
// src_elem : 入力元フォームのid
// dst_elem : ルビ先フォームのid
// no_convert_kana : かな変換しない
//
// 複数使うときはnewを繰り返せばOK
// ---------------------------------------------------------------- //

var autoRuby = Class.create();
autoRuby.prototype = {
	initialize : function (src_elem, dst_elem, no_convert_kana, interval_time){	
		this.prev_val = $F(src_elem);		
		this.src_elem = $(src_elem);
		this.dst_elem = $(dst_elem);
		this.no_convert_kana = no_convert_kana || true;
		new Form.Element.Observer( this.src_elem, 0.05, this.getRuby.bind(this) );
	},
	getRuby : function (){
		var new_val = $F(this.src_elem);
		if ( this.prev_val == new_val ) return;
		if ( new_val == "" ){
			this.dst_elem.value = '';
			this.prev_val = '';
			return;
		}
		var add_val = new_val;
		for( var i=this.prev_val.length; i>=0; i--){
			if ( new_val.substr(0,i) == this.prev_val.substr(0,i)){
				add_val = new_val.substr(i);
				break;
			}
		}
		this.prev_val=new_val;
		var addruby = add_val.replace( /[^ ぁあ-んァー]/g, "" );
		if ( addruby == "" ) {
			return;
		}else{
			this.dst_elem.value= this.dst_elem.value+addruby;
		}
	},
	hira2kana : function( s ){
		if (this.no_convert_kana) {
			return s;
	 	} else {
 			return String.fromCharCode( s.charCodeAt(0) + 0x60 );
 		}
	}
}
