/**
 * Search
 *
 * Auto-complete search word Class
 */
var SearchLB = Class.create();
SearchLB.prototype = {

	/**
	 * Creator
	 *
	 * @param input: Keyword input box
	 * @param panel: diplay panel for suggested words
	 */
	initialize: function(input, panel, dType, _site_cd, subPanel,isOmnitureSite) {

		this.input = input;
		this.panel = panel;
		this.dType = dType;			
		this.subPanel = subPanel ; 	
		this._site_cd = _site_cd ;
		this.isOmnitureSite = isOmnitureSite ;
	  	this.WORDLIST_URL = '/' + this._site_cd + '/support/search/SupportSearchLowb.do';
	 	
		Event.observe(input, 'keyup', this.doOnKeyUp.bindAsEventListener(this));
		Event.observe(input, 'keypress', this.doOnKeyPress.bindAsEventListener(this));
		Event.observe(panel, 'click', this.doOnClick.bindAsEventListener(this));
		Event.observe(subPanel, 'click', this.doOnClick.bindAsEventListener(this));		
	},

	setSearchWord: function(searchWord) {		
		this.input.value = searchWord;
		if(this.panel.visible()) {
			this.panel.hide();
			this.panel.innerHTML = '';
		}
	},

	doOnKeyUp: function(e) {		
		if ((e.keyCode == Event.KEY_RETURN) || (e.keyCode == Event.KEY_UP) || (e.keyCode == Event.KEY_DOWN) ||
			(e.keyCode == Event.KEY_LEFT) || (e.keyCode == Event.KEY_RIGHT) || (e.keyCode == Event.KEY_ESC) ||
			(e.keyCode == 229) || (e.keyCode == 32)) {
			return;
		}

		this.request(e);
	},

	items: null,
	currItem: -1,
	doOnKeyPress: function(e) {

		if(e.keyCode!=Event.KEY_RETURN && e.keyCode!=Event.KEY_ESC
			&& e.keyCode!=Event.KEY_UP && e.keyCode!=Event.KEY_DOWN
			&& e.keyCode!=Event.KEY_LEFT && e.keyCode!=Event.KEY_RIGHT) {
			if (this.subPanel.visible()){
				this.subPanel.hide();
			}
			return;
		}

		if(e.keyCode==Event.KEY_ESC || this.input.value=='') {

			this.currItem = -1;
			this.panel.hide();
			if (this.subPanel.visible()){
				this.subPanel.hide();
			}
			return false;
		}

		if(!this.items || this.items.length==0) {
			return;
		}

		switch(e.keyCode) {

		case Event.KEY_RETURN:
			
			if(this.currItem>=0 && this.items[this.currItem]) {
				this.setSearchWord(this.items[this.currItem].innerHTML.stripTags().strip().unescapeHTML());
			}
			// omniture 
			if (this.isOmnitureSite == "true") {			
				omnitureTagging('MODEL', null);
			}
			
			model_search();
			break;		

		case Event.KEY_DOWN:

			if(this.currItem>=0) {
				this.items[this.currItem].removeClassName('hover');
			}

			this.currItem = (this.currItem<this.items.length-1)? this.currItem + 1: 0;
			this.items[this.currItem].addClassName('hover');

			break;

		case Event.KEY_UP:

			if(this.currItem>=0) {
				this.items[this.currItem].removeClassName('hover');
			}

			this.currItem = (this.currItem>0)? this.currItem - 1: this.items.length - 1;
			this.items[this.currItem].addClassName('hover');
			break;

		case Event.KEY_LEFT:
			/*
			if(this.currItem>=0) {
				this.items[this.currItem].removeClassName('hover');
			}

			if(this.items.length>5) {
				this.currItem = (this.currItem>=5)? this.currItem - 5: (this.currItem - 5) + this.items.length;
			}
			else {
				this.currItem = (this.currItem>0)? this.currItem - 1: this.items.length - 1;
			}

			this.items[this.currItem].addClassName('hover');
			*/
			break;

		case Event.KEY_RIGHT:
			/*
			if(this.currItem>=0) {
				this.items[this.currItem].removeClassName('hover');
			}

			if(this.items.length>5) {
				this.currItem = (this.currItem<this.items.length - 5)? this.currItem + 5: this.currItem + 5 - this.items.length;
			}
			else {
				this.currItem = (this.currItem<this.items.length - 1)? this.currItem + 1: 0;
			}

			this.items[this.currItem].addClassName('hover');
			*/
			break;

		default:
			break;
		}		
		
		this.cancelBubble(e); // 2008-03-28
				
		return false;
	},

	cancelBubble: function(ev) {

		var e = ev;
		if (!e) {
			e = window.event;
		}

		if (e.stopPropagation) { // 2008-03-28
			e.stopPropagation();
		} else {
			e.cancelBubble = true;
		}
	},

	doOnClick: function(e) {

		var o = Event.element(e);
		
		var _SUBYN = o.id ;
		Element.extend(o);
		if(o.tagName.toLowerCase()=='span') {
			o = o.up();
		}
		else if(o.tagName.toLowerCase()!='li') {
			return false;
		}

		this.input.value = o.innerHTML.stripTags().strip().unescapeHTML();			

		if(_SUBYN == "Y"){//if subpanel have

			if(!this.subPanel.visible()) {
				this.subPanel.show();
				//this.subPanel.hide();
			}
			
			new Ajax.Request(this.WORDLIST_URL, {
		
					method: "get",
					parameters: {					
						//disp_nm : encodeURIComponent(this.input.value),				
						disp_nm : this.input.value,				
						dType    : this.dType.value,
						bandwidth: "B"
					},
					onSuccess: (function(transport) {											
						this.subPanel.innerHTML = transport.responseText;
						this.items = this.subPanel.getElementsBySelector('li');

						if(this.items.length < 6) {					

							this.subPanel.style.height = 'auto' ; 					
						}else{
							this.subPanel.style.height = '95px' ;
						}
						
						if(!this.items || this.items.length==0) {
							this.subPanel.hide();
						}
					}).bind(this),
					onFailure: function(transport) {
												alert("fail");
					//this.subPanel.hide();
					},
					onException: function(request, exception) {
												alert("exception");
					//this.subPanel.hide();
					}
				}
			);
		}else{			
			var frj = document.getElementsByName("suppForm")[0];
	
			var tempValue = this.input.value.split("|");
			
			frj.disp_nm.value = tempValue[0];
			if(frj.dType.value != "G")frj.dType.value="";	
			frj.mType.value = "";
			// omniture
			if (this.isOmnitureSite == "true") {			
				omnitureTagging('MODEL', null);
			}
			
			frj.action = "/" + this._site_cd + "/support/search/supportSearchResultView.do";
				
			frj.submit();
					
			
		}
	
	},
	
	request: function(e) {

		if(this.input.value=='') {
			this.currItem = -1;
			this.panel.hide();
			return;
		}

		if (this.input.value.length < 2 || this.input.value.replace(/^\s*/, "").replace(/\s*$/, "").length < 2 || this.input.value.replace(/^\s*/, "").replace(/\s*$/, "").length == 1 ) {		
			this.currItem = -1;
			this.panel.hide();
			return;
		}

		new Ajax.Request(this.WORDLIST_URL, {

				method: "get",
				parameters: {					
					disp_nm : this.input.value,				
					dType    : this.dType.value,
					bandwidth: "A"
				},
				onSuccess: (function(transport) {														
					//var temp = transport.responseText.replace(/\|N|\|Y/g,'');					
					
					this.panel.innerHTML = transport.responseText;													
					this.items = this.panel.getElementsBySelector('li');

					if(this.items.length < 6) {					
						this.panel.style.height = 'auto' ; 					
					}else{
						this.panel.style.height = '95px' ;
					}
					
					if(!this.items || this.items.length==0) {
						this.panel.hide();
					}else{
						this.panel.show();
					}
				}).bind(this),
				onFailure: function(transport) {
				//this.panel.hide();
				},
				onException: function(request, exception) {
				//this.panel.hide();
				}
			}
		);

	}
};
