/**
	@file
	completion.js <br/>
	入力補完ファイル <br/>
	<br/>
	DEPENDENCIES <br/>
	 - lib/prototype.js <br/>
	
	@breif completion
	@author m-okuda
	@version 1.0.0.0
	@since  18/02/2007
	$Revision: $
*/


var Completion = Class.create();

/**
	入力補完クラス
	@brief 入力補完クラス
*/
Completion.prototype = {

	/// コンストラクタ
	initialize: function(){},

	/**
		inputに入力補完機能を設定
		@brief inputに入力補完機能を設定
		@param input 対象テキストボックス
	*/
	setCompletion: function( input, clickButtonId ){
		input.__selectedIndex = -1;						//< 選択中のアイテムのインデックス
		input.__oldquery = encodeURI( input.value );	//< 現状のテキストボックス
		
		// 補完領域追加
		input.__completionitems = document.createElement( 'div' );
		// style指定
		input.__completionitems.style.display = "none";
		//input.__completionitems.style.border = "1px";
		//input.__completionitems.style.borderStyle = "solid";
		//input.__completionitems.style.borderColor = "#c0c0c0";
		//input.__completionitems.style.borderColor = "#777777";
		input.__completionitems.style.position = "absolute";
		input.__completionitems.style.fontSize = "13px";
		input.__completionitems.style.color = "#00008B";
		input.__completionitems.style.filter = "alpha(opacity=95)";
		input.__completionitems.style.opacity = "0.95";
		input.__completionitems.style.zIndex = 1;		
		input.__selectedDIV = null;

		document.body.appendChild( input.__completionitems );
		// method
		/**
			補完領域クリア
			@brief 補完領域クリア
		*/
		input.clearCompletionItems = function(){
			if ($("hideSelect") != null)
			{
				//this.__completionitems.removeChild($("hideSelect"));
				var currentElement = document.getElementById("hideSelect");
				currentElement.parentNode.removeChild(currentElement);

			}
			
			this.__completionitems.innerHTML = "";
			this.__completionitems.style.display = "none";
			this.__selectedDIV = null;

		};
		
		/**
			補完領域幅設定
			@brief 補完領域幅設定
			@param w 幅px
		*/
		input.setCompletionWidth = function( w ){
			this.__completionitems.__width = w;
		};
		
		/**
			補完領域表示
			@brief 補完領域表示
			@param ary 表示アイテム配列
			@param callback コールバック関数
		*/
		input.showCompletionItems = function( ary, callback ){
			var x = 0; var y = 0;
			for( var o = this ; o ; o = o.offsetParent ){
				x += ( o.offsetLeft ); 
				y += ( o.offsetTop );
			}
			this.clearCompletionItems();
			
			var ci = this.__completionitems;
			ci.style.width = ( ci.__width ? ci.__width : this.offsetWidth + "px" );
			ci.style.top = y + this.offsetHeight + "px";
			ci.style.left = x + "px";
			ci.style.display = "block";
			var input = this;
			
			/// アイテム追加
			function __addItem( n ){
				var div = document.createElement( "div" );
				div.style.cursor = "pointer";
				div.style.backgroundColor = "#FFFFFF";
				div.style.padding = "2px";
				div.__selected = false;
				div.style.textAlign = "left";
				/// クリック
				div.onclick = function(){
					callback( n );
				}
				/// マウスオーバー
				div.onmouseover = function(){
					if(input.__selectedDIV != null){
						input.__selectedDIV.style.backgroundColor = "#FFFFFF";
						input.__selectedDIV.style.color = "#00008B";
						input.__selectedDIV.__selected = false;
					}
					this.style.backgroundColor = "#36c";
					this.style.color = "#FFFFFF";
					this.__selected = true;
					
					input.__selectedDIV = this;
					input.__selectedIndex = n;
				}
				/// マウスアウト
				div.onmouseout = function(){
					this.style.backgroundColor = "#FFFFFF";
					this.style.color = "#00008B";
					this.__selected = false;
					input.__selectedIndex = -1;
					
					if(input.__selectedDIV != null){
						input.__selectedDIV.style.backgroundColor = "#FFFFFF";
						input.__selectedDIV.style.color = "#00008B";
						input.__selectedDIV.__selected = false;
					}
					input.__selectedDIV = null;
				}
				div.innerHTML = ary[ n ];
				ci.appendChild( div );
	    	}
			for( var i = 0 ; i < ary.length -1; ++i ){
				__addItem( i );
			}
	
			var item = this.__completionitems.childNodes[ 0 ];
					if( item != null && typeof(item) != "undefined"){
						item.style.backgroundColor = "#36c";
						item.style.color = "#FFFFFF";
						item.__selected = true;
						this.__selectedIndex = 0;
						this.__selectedDIV = item;
					}

			// IE 5.0 6.0バグの修正
			if (navigator.userAgent.indexOf("MSIE")>0) {
				if( 0< ary.length -1){
				
				var objFrame = document.createElement("iframe");   
				objFrame.setAttribute("id", "hideSelect");   
				
				objFrame.style.cssText = "z-index: -3;"+   
										"position: absolute;"+   
										"top: 0;"+   
										"left: 0;"+   
										"background-color:#fff;"+   
										"border: none;"+   
					"border-style:none; border-width:0px; border:0px"+
										"filter: DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"+   
										"height: "+ci.offsetHeight + ";"+    
										"width: "+ci.offsetWidth + ";";
				ci.appendChild(objFrame);
				
				}
			}
		};
		
		/**
			キーダウンイベント
			@brief キーダウンイベント
			@param evt イベントオブジェクト
		*/
		input.onkeydown = function( evt ){
			evt = ( evt ) ? evt : ( ( window.event ) ? window.event : null );
			if( evt == null ){
				return;
			}
			
			try{
			if( evt.keyCode == 38 ){
			// 上キー
				if( this.__selectedIndex > 0 ){
					var item1 = this.__completionitems.childNodes[ this.__selectedIndex ];
					if( item1 != null ){
						item1.style.backgroundColor = "#FFFFFF";
						item1.style.color = "#00008B";
						item1.__selected = false;
						--this.__selectedIndex;
						var item2 = item1.previousSibling;
						item2.style.backgroundColor = "#36c";
						item2.style.color = "#FFFFFF";
						item2.__selected = true;
						this.__selectedDIV = item2;
					}
				}
			}else if( evt.keyCode == 40 ){
			// 下キー
				if( this.__selectedIndex == -1 ){
					var item = this.__completionitems.childNodes[ 0 ];
					if( item != null ){
						item.style.backgroundColor = "#36c";
						item.style.color = "#FFFFFF";
						item.__selected = true;
						this.__selectedDIV = item;
						++this.__selectedIndex;
					}
				}else{
					var item1 = this.__completionitems.childNodes[ this.__selectedIndex ];
					var item2 = item1.nextSibling;
					if( item2.innerHTML ){
						item1.style.backgroundColor = "#FFFFFF";
						item1.style.color = "#00008B";
						item1.__selected = false;
						++this.__selectedIndex;
						item2.style.backgroundColor = "#36c";
						item2.style.color = "#FFFFFF";
						item2.__selected = true;
						this.__selectedDIV = item2;
					}
				}
			}else if( evt.keyCode == 37 || evt.keyCode == 39 || evt.keyCode == 13 ){
			// 横キー and Enterキー
					var item = this.__completionitems.childNodes[ 0 ];
				
				if((this.__selectedIndex == -1 || typeof(item) == "undefined") && evt.keyCode == 13){
									
					if(typeof(closeTipOnSearch) != "undefined") {
						closeTipOnSearch();
					}
					document.getElementById(clickButtonId).click();
				
				}
				if( this.__selectedIndex > -1 && typeof(item) != "undefined"){
					
					var item = this.__completionitems.childNodes[ this.__selectedIndex ];
					
					this.value = item.innerHTML;
				
					if(this.id == "dep_node"){
						if($("suggest_start_spot") != "undefined" && $("suggest_start_spot") != null){
							$("suggest_start_spot").value = this.value;
						}
					}else if(this.id == "arv_node"){
						if($("suggest_end_spot") != "undefined" && $("suggest_end_spot") != null){
							$("suggest_end_spot").value = this.value;
						}
					}
					
					this.__selectedIndex = -1;
					this.clearCompletionItems();
					this.__oldquery = encodeURI( this.value );					
				}
			}
				
				if(evt.keyCode == 13){
					event.returnValue = false;
				}
			}catch(e){}
		};
		
		/**
			フォーカスアウトイベント
			@brief フォーカスアウトイベント
		*/
		input.onblur = function(){
			if( this.__completionitems.childNodes.length <= 0 ){
				return;
			}
			var item = this.__completionitems.childNodes[ 0 ];
			for( i = 0 ; i < this.__completionitems.childNodes.length ; ++i ){
				if( item.__selected == true ){
					this.value = item.innerHTML;
					
					if(this.id == "dep_node"){
						if($("suggest_start_spot") != "undefined" && $("suggest_start_spot") != null){
							$("suggest_start_spot").value = this.value;
						}
					}else if(this.id == "arv_node"){
						if($("suggest_end_spot") != "undefined" && $("suggest_end_spot") != null){
							$("suggest_end_spot").value = this.value;
						}
					}
					
					break;
				}
				item = item.nextSibling;
			}
			this.clearCompletionItems();
			this.__oldquery = encodeURI( input.value );
		};
	}
};


/*
 * Copyright (c) 2007 by Hitachi Information Systems,Ltd. All Rights Reserved.
 * Consult your license regarding permissions and restrictions.
 */
