var SupportModelSuggest = {
	intervalMS : 100,
	suggestMax : 15,
	outClass : "suggestOut",
	overClass : "suggestOver",
	wordDivID : "wordDiv_",
	suggestDivID : "suggest",
	inputID : "searchText",
	seriesID : "series",
	makerID : "maker",
	deleteList : new Array( "-" ),
	replaceList : new Array(),
	spanID :   "spanIDforIE6",
	iframeID : "iframeForIE6",
	ieVer : 0,
	safariVer : 0,
	inputRef : null,
	inputText : "",
	suggestDivRef : null,
	textList : null,
	resultList : null,
	overPos : -1,
	intervalID : null,
	//-----------------------------------------------
	//                   initialize
	//-----------------------------------------------
	init : function( modelList ){
		if( Prototype.Browser.IE ){
			this.ieVersion();
			if( this.ieVer == 6 ){
				Element.insert( this.spanID, '<iframe class="suggestIframe"  id="' + this.iframeID + '" frameborder="0"></iframe>' );
			}

		}else if( Prototype.Browser.WebKit ){
			this.safariVersion();
		}
		this.inputRef = $( this.inputID );
		this.inputRef.blur();
		this.inputRef.value = "";
		this.suggestDivRef = $( this.suggestDivID );
		this.clearSuggest();
		this.prepareModel( modelList );

		Event.observe( document,      "mouseup",this.onClickDocument.bindAsEventListener( this ));
		Event.observe( this.inputRef, "focus",  this.onInputFocus.bindAsEventListener( this ));
		Event.observe( this.inputRef, "blur",   this.onInputBlur.bindAsEventListener( this ));
		Event.observe( this.inputRef, "keydown",this.onKeyDown.bindAsEventListener( this ));
		
		var href = location.href;
		if( href.indexOf( "support/trouble/manual/download/product" ) != -1 ) this.inputRef.focus();
	},
	
	prepareModel : function( modelLIst ){
		this.textList = new Array();
		for( var i = 0; i < modelLIst.length; i++ ){
			var words = modelLIst[i].split("|");
			var add = new Array();
			add.push( this.toLowerCase( words[0] ));
			for( var j = 0; j < words.length; j++ ){
				var word;
				if( j == 0 ) word = add[0];
				else{
					word = this.toLowerCase( words[j] );
					words[j] = word;
				}
				word = this.deleteWord( word );
				word = this.replaceWord( word );
				if( words[j] != word ){
					add.push( word );
				}
			}
			words = words.concat( add );
			this.textList[i] = words;
		}
	},
	
	deleteWord : function( word ){
		var len = this.deleteList.length;
		if( len == 0 ) return( word );
		for( var i = 0; i < len; i++ ){
			if( word.indexOf( this.deleteList[i] ) != -1 ) word = word.replace( this.deleteList[i], "" );
		}
		return( word );
	},

	replaceWord : function( word ){
		var len = this.replaceList.length;
		if( len == 0 ) return( word );
		for( var i = 0; i < len; i++ ){
			if( word.indexOf( this.replaceList[i][0] ) != -1 ) word = word.replace( this.replaceList[i][0], this.replaceList[i][1] );
		}
		return( word );
	},
	
	//-----------------------------------------------
	//                    Event
	//-----------------------------------------------
	onClickDocument : function(evtObj){
		var target = Event.element( evtObj );
		if( target.id.indexOf( this.wordDivID ) != 0 ){
			this.hideSuggest();
		}
	},
	
	onInputFocus : function(){
		this.intervalID = setInterval( this.onCheckInput.bind( this ), this.intervalMS );
	},

	onInputBlur : function(){
		if( this.intervalID != null ){
			clearInterval( this.intervalID );
			this.intervalID = null;
		}
	},
	
	onCheckInput : function(){
		var t = this.toLowerCase( this.inputRef.getValue() );

		if( this.inputText == t ) return;
		this.clearSuggest();
		this.inputText = t;
		if( this.inputText == "" )	return;

		this.matchWords();
		this.createSuggest();
	},
	
	onMouseOver : function( evtObj ){
		this.clearOver();
		var target = Event.element( evtObj );
		
		this.changeClass( target, this.outClass, this.overClass );
		this.overPos = parseInt( target.id.split( this.wordDivID )[1] );
	},
	
	onMouseOut : function(){
		this.clearOver();
		this.overPos = -1;
	},
	
	onClick : function( evtObj ){
		var target = Event.element( evtObj );
		var text = target.innerHTML;
		this.enterSuggest( text );
	},
	
	onKeyDown : function( evtObj ){
		if( this.resultList == undefined || this.resultList == null || this.resultList.length == 0 ) return;
		switch( evtObj.keyCode ){
			case 13: this.enter();	break;
			case 38: this.up();		break;
			case 40: this.down();
		}
	},

	setSuggestWordEvent : function(){
		var len = this.resultList.length;
		for( var key = 0; key < len; key++ ){
			var ref = $( this.wordDivID + key );
			Event.observe( ref, "mouseover", 	this.onMouseOver.bindAsEventListener( this ));
			Event.observe( ref, "mouseout",  	this.onMouseOut.bindAsEventListener( this ));
			Event.observe( ref, "mouseup",  		this.onClick.bindAsEventListener( this ));
		}
	},

	enter : function(){
		if( this.overPos == -1 ){
			if( this.inputText == "" ) this.onCheckInput();
//			else if( this.inputRef.value != "" ) alert("submit value="+ this.inputRef.getValue());
		}else{
			var text = $( this.wordDivID + this.overPos ).innerHTML;
			this.enterSuggest( text );
		}
	},
	
	up : function(){
		
		var nextPos = this.overPos - 1;
		if( nextPos < 0 ) nextPos = this.resultList.length - 1;
		this.keyUpDown( nextPos );
	},
	
	down : function(){
		var nextPos = this.overPos + 1;
		if( nextPos == this.resultList.length ) nextPos = 0;
		this.keyUpDown( nextPos );
	},
	
	keyUpDown : function( nextPos ){
		if( this.overPos != -1 ) this.clearOver();
		
		this.changeClass( $( this.wordDivID + nextPos ), this.outClass, this.overClass );
		this.overPos = nextPos;
	},

	//-----------------------------------------------
	//                    Search
	//-----------------------------------------------

	matchWords : function(){
		this.resultList = new Array();
		var len = this.textList.length;
		for( var key = 0; key < len; key++ ){
			var words = this.textList[key];
			var wlen = words.length;
			for( var i = 0; i < wlen; i++ ){
				if( words[i].indexOf( this.inputText ) == 0 ){
					this.resultList.push( key );
					break;
				}
			}
		}
	},

	toLowerCase : function( text ){
		var conv = "";
		var input = text;
		for( var i = 0; i < input.length; i++ ){
			var c = input.charAt( i );
			var code = c.charCodeAt( 0 );
			if( 0x3000 <= code ){
				if( 0xff10 <= code ){
					if( code <= 0xff19 ){
						c = String.fromCharCode( 0x30 + ( code - 0xff10 ));
					}else if( 0xff21 <= code ){
						if( code <= 0xff3a ){
							c = String.fromCharCode( 0x61 + ( code - 0xff21 ));
						}else if( 0xff41 <= code && code <= 0xff5a ){
							c = String.fromCharCode( 0x61 + ( code - 0xff41 ));
						}
					}
				}else{
					if( code == 0x3000 ){
						c = "";
					}else if( 0x3041 <= code && code <= 0x3093 ){
						c = String.fromCharCode( 0x60 + code );
					}else if( code == 0xff0d ){
						c = "";
					}else if( code == 0xff08 ){
						c = "(";
					}else if( code == 0xff09 ){
						c = ")";
					}else if( code == 0x30fc ){
						c = "";
					}
				}
			}else{
				if( code == 0x20 || code == 0x2d || code == 0x2212 ){
						//" " "-"
						c = "";
				}
			}
			conv += c;
		}
		conv = conv.toLowerCase();
		return( conv );
	},

	//-----------------------------------------------
	//                    view
	//-----------------------------------------------
	hideSuggest : function(){
		$( this.suggestDivRef ).hide();
		if( this.ieVer == 6 ){
//			Element.setStyle( this.seriesID,  { 'visibility':'visible' });
//			Element.setStyle( this.makerID,   { 'visibility':'visible' });
			Element.setStyle( this.iframeID,  { 'display':'none' });
		}
	},
	
	clearSuggest : function(){
		Element.update( this.suggestDivRef, "" );
		this.hideSuggest();
		this.overPos = -1;
	},

	createSuggest : function(){
		if( this.resultList.length == 0 ){
			this.clearSuggest();
			return;
		}
		Element.update( this.suggestDivRef, this.createHtml() ); //1.5
		this.setSuggestWordEvent();
		if( this.ieVer == 6 ){
			this.intervalID = setInterval( this.setIframeForIE6.bind( this ), 10 );
//			Element.setStyle( this.seriesID,  { 'visibility':'hidden' });
//			Element.setStyle( this.makerID,   { 'visibility':'hidden' });
		}
		$( this.suggestDivRef ).show();
	},
	
	setIframeForIE6 : function(){
		clearInterval( this.intervalID );
		var h = Element.getHeight( this.suggestDivID );
		var w = Element.getWidth( this.suggestDivID );
		Element.setStyle( this.iframeID,  { 'display':'block', height: h, width: w});
	},

	createHtml : function(){
		var len = this.resultList.length;
		var html = "";
		for( var key = 0; key < len; key++ ){
			if( key < this.suggestMax ){
				html += '<div id="' + this.wordDivID + key + '" class="' + this.outClass  + '">'+ this.textList[this.resultList[key]][0] +"</div>";
			}else{
				this.resultList = this.resultList.slice( 0, key );
				html += '<div class="suggestMessage">';
				html += SupportSearchWord.NOTE_SUGGEST;
				html += '</div>';
				break;
			}
		}
		return( html );

	},

	changeClass : function( ref, remove, add ){
		ref.removeClassName( remove );
		ref.addClassName( add );
	},

	enterSuggest : function( text ){
		this.inputRef.setValue( text );
		this.inputText = this.toLowerCase( text );
		this.clearSuggest();
		Form.Element.focus( this.inputRef );
		this.hideSuggest();
	},

	clearOver : function(){
		if( this.overPos < 0 ) return;
		this.changeClass( $( this.wordDivID + this.overPos ), this.overClass, this.outClass );
	},

	ieVersion : function(){
		if( this.ieVer == 0 ){
			this.ieVer = navigator.userAgent.match(new RegExp("MSIE [0-9]{1,2}\.[0-9]{1,3}"));
			this.ieVer = ( this.ieVer == null )  ? -1 : parseFloat(String(this.ieVer).replace("MSIE ",""));
		}
		return( this.ieVer );
	},
	
	safariVersion : function(){
		if( this.safariVer == 0 ){
			this.safariVer = navigator.userAgent.match(new RegExp("Safari/[0-9]{1,4}\.[0-9]{1,2}"));
			this.safariVer = ( this.safariVer == null )  ? -1 : parseFloat(String(this.safariVer).replace("Safari/",""));
		}
		return( this.safariVer );
	}
};

