/*
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.8.0r4
*/

YAHOO.widget.AutoComplete.prototype._onTextboxKeyDown = function(v,oSelf) {
	var nKeyCode = v.keyCode;

	// Clear timeout
	if(oSelf._nTypeAheadDelayID != -1) {
		clearTimeout(oSelf._nTypeAheadDelayID);
	}
	
	switch (nKeyCode) {
		case 9: // tab
			if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
				// select an item or clear out
				if(oSelf._elCurListItem) {
					if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
						if(oSelf._bContainerOpen) {
							YAHOO.util.Event.stopEvent(v);
						}
					}
					oSelf._selectItem(oSelf._elCurListItem);
				}
				else {
					oSelf._toggleContainer(false);
				}
			}
			break;
		case 13: // enter
			if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
				if(oSelf._elCurListItem) {
					if(oSelf._nKeyCode != nKeyCode) {
						if(oSelf._bContainerOpen) {
							YAHOO.util.Event.stopEvent(v);
						}
					}
					oSelf._selectItem(oSelf._elCurListItem, 'enter');
				}
				else {
					oSelf._toggleContainer(false);
				}
			}
			break;
		case 27: // esc
			oSelf._toggleContainer(false);
			return;
		case 39: // right
			oSelf._jumpSelection();
			break;
		case 38: // up
			if(oSelf._bContainerOpen) {
				YAHOO.util.Event.stopEvent(v);
				oSelf._moveSelection(nKeyCode);
			}
			break;
		case 40: // down
			if(oSelf._bContainerOpen) {
				YAHOO.util.Event.stopEvent(v);
				oSelf._moveSelection(nKeyCode);
			}
			break;
		default: 
			oSelf._bItemSelected = false;
			oSelf._toggleHighlight(oSelf._elCurListItem, "from");

			oSelf.textboxKeyEvent.fire(oSelf, nKeyCode);
			YAHOO.log("Textbox keyed", "info", oSelf.toString());
			break;
	}

	if(nKeyCode === 18){
		oSelf._enableIntervalDetection();
	}    
	oSelf._nKeyCode = nKeyCode;
};

YAHOO.widget.AutoComplete.prototype._onTextboxKeyPress = function(v,oSelf) {
	var nKeyCode = v.keyCode;

		// Expose only to non SF3 (bug 1978549) Mac browsers (bug 790337) and  Opera browsers (bug 583531),
		// where stopEvent is ineffective on keydown events 
		if(YAHOO.env.ua.opera || (navigator.userAgent.toLowerCase().indexOf("mac") != -1) && (YAHOO.env.ua.webkit < 420)) {
			switch (nKeyCode) {
			case 9: // tab
				// select an item or clear out
				if(oSelf._bContainerOpen) {
					if(oSelf.delimChar) {
						YAHOO.util.Event.stopEvent(v);
					}
					if(oSelf._elCurListItem) {
						oSelf._selectItem(oSelf._elCurListItem);
					}
					else {
						oSelf._toggleContainer(false);
					}
				}
				break;
			case 13: // enter
				if(oSelf._bContainerOpen) {
					YAHOO.util.Event.stopEvent(v);
					if(oSelf._elCurListItem) {
						oSelf._selectItem(oSelf._elCurListItem, 'enter');
					}
					else {
						oSelf._toggleContainer(false);
					}
				}
				break;
			default:
				break;
			}
		}

		//TODO: (?) limit only to non-IE, non-Mac-FF for Korean IME support (bug 811948)
		// Korean IME detected
		else if(nKeyCode == 229) {
			oSelf._enableIntervalDetection();
		}
};

YAHOO.widget.AutoComplete.prototype._onContainerClick = function(v,oSelf) {
	var elTarget = YAHOO.util.Event.getTarget(v);
	var elTag = elTarget.nodeName.toLowerCase();
	while(elTarget && (elTag != "table")) {
		switch(elTag) {
			case "body":
				return;
			case "li":
				// In case item has not been moused over
				oSelf._toggleHighlight(elTarget,"to");
				oSelf._selectItem(elTarget, 'click');
				return;
			default:
				break;
		}

		elTarget = elTarget.parentNode;
		if(elTarget) {
			elTag = elTarget.nodeName.toLowerCase();
		}
	}    
};

YAHOO.widget.AutoComplete.prototype._selectItem = function(elListItem, selectType) {
	this._bItemSelected = true;
	this._updateValue(elListItem);
	this._sPastSelections = this._elTextbox.value;
	this._clearInterval();
	this.itemSelectEvent.fire(this, elListItem, elListItem._oResultData, selectType);
	YAHOO.log("Item selected: " + YAHOO.lang.dump(elListItem._oResultData), "info", this.toString());
	this._toggleContainer(false);
};

YAHOO.namespace("videoscom.autocomplete");

YAHOO.videoscom.autocomplete = function () {	
	return {
		init: function (url, limit) {
			var oDS = new YAHOO.util.XHRDataSource(url);
			oDS.responseType = YAHOO.util.XHRDataSource.TYPE_JSARRAY;
			
			var oAC = new YAHOO.widget.AutoComplete("searchtext", "search_autocomplete", oDS, {
				autoHighlight: false,
				autoSnapContainer: false,
				useIFrame: true,
				maxResultsDisplayed: limit
			});
			oAC.generateRequest = function(sQuery) {
				return sQuery+"/"+Verify();
			};
			oAC.formatResult = function(oResultData, sQuery, sResultMatch) {
				var result = sResultMatch;
				if (result.indexOf(sQuery) == 0) {
					var remainder = result.substr(sQuery.length);
					return sQuery + "<b>" +  remainder + "</b>";
				}
				else {
					return sResultMatch;
				}
			};
			oAC.itemSelectEvent.subscribe(function(sType, aArgs) {
				if (aArgs[3] == 'click' || aArgs[3] == 'enter') {
					YAHOO.util.Dom.get("mainform").submit();
				}
			});
			oAC.itemArrowToEvent.subscribe(function(sType, aArgs) {
				this._updateValue(aArgs[1]);
			});
		}
	}
}();
