/*
 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.com/yui/license.html
 version: 3.3.0
 build: 3167
 */
YUI.add('autocomplete-sources',function(Y){var Lang=Y.Lang,_SOURCE_SUCCESS='_sourceSuccess',MAX_RESULTS='maxResults',REQUEST_TEMPLATE='requestTemplate',RESULT_LIST_LOCATOR='resultListLocator';function ACSources(){}
ACSources.prototype={_YQL_SOURCE_REGEX:/^(?:select|set|use)\s+/i,_createIOSource:function(source){var cache={},ioSource={},that=this,ioRequest,lastRequest,loading;ioSource.sendRequest=function(request){var _sendRequest=function(request){var query=request.request,maxResults,requestTemplate,url;if(cache[query]){that[_SOURCE_SUCCESS](cache[query],request);}else{maxResults=that.get(MAX_RESULTS);requestTemplate=that.get(REQUEST_TEMPLATE);url=source;if(requestTemplate){url+=requestTemplate(query);}
url=Lang.sub(url,{maxResults:maxResults>0?maxResults:1000,query:encodeURIComponent(query)});if(ioRequest&&ioRequest.isInProgress()){ioRequest.abort();}
ioRequest=Y.io(url,{on:{success:function(tid,response){var data;try{data=Y.JSON.parse(response.responseText);}catch(ex){Y.error('JSON parse error',ex);}
if(data){cache[query]=data;that[_SOURCE_SUCCESS](data,request);}}}});}};lastRequest=request;if(!loading){loading=true;Y.use('io-base','json-parse',function(){ioSource.sendRequest=_sendRequest;_sendRequest(lastRequest);});}};return ioSource;},_createJSONPSource:function(source){var cache={},jsonpSource={},that=this,lastRequest,loading;jsonpSource.sendRequest=function(request){var _sendRequest=function(request){var query=request.request;if(cache[query]){that[_SOURCE_SUCCESS](cache[query],request);}else{source._config.on.success=function(data){cache[query]=data;that[_SOURCE_SUCCESS](data,request);};source.send(query);}};lastRequest=request;if(!loading){loading=true;Y.use('jsonp',function(){if(!(source instanceof Y.JSONPRequest)){source=new Y.JSONPRequest(source,{format:Y.bind(that._jsonpFormatter,that)});}
jsonpSource.sendRequest=_sendRequest;_sendRequest(lastRequest);});}};return jsonpSource;},_createStringSource:function(source){if(this._YQL_SOURCE_REGEX.test(source)){return this._createYQLSource(source);}else if(source.indexOf('{callback}')!==-1){return this._createJSONPSource(source);}else{return this._createIOSource(source);}},_createYQLSource:function(source){var cache={},yqlSource={},that=this,lastRequest,loading;if(!this.get(RESULT_LIST_LOCATOR)){this.set(RESULT_LIST_LOCATOR,this._defaultYQLLocator);}
yqlSource.sendRequest=function(request){var yqlRequest,_sendRequest=function(request){var query=request.request,callback,env,maxResults,opts,yqlQuery;if(cache[query]){that[_SOURCE_SUCCESS](cache[query],request);}else{callback=function(data){cache[query]=data;that[_SOURCE_SUCCESS](data,request);};env=that.get('yqlEnv');maxResults=that.get(MAX_RESULTS);opts={proto:that.get('yqlProtocol')};yqlQuery=Lang.sub(source,{maxResults:maxResults>0?maxResults:1000,query:query});if(yqlRequest){yqlRequest._callback=callback;yqlRequest._opts=opts;yqlRequest._params.q=yqlQuery;if(env){yqlRequest._params.env=env;}}else{yqlRequest=new Y.YQLRequest(yqlQuery,{on:{success:callback},allowCache:false},env?{env:env}:null,opts);}
yqlRequest.send();}};lastRequest=request;if(!loading){loading=true;Y.use('yql',function(){yqlSource.sendRequest=_sendRequest;_sendRequest(lastRequest);});}};return yqlSource;},_defaultYQLLocator:function(response){var results=response&&response.query&&response.query.results,values;if(results&&Lang.isObject(results)){values=Y.Object.values(results)||[];results=values.length===1?values[0]:values;if(!Lang.isArray(results)){results=[results];}}else{results=[];}
return results;},_jsonpFormatter:function(url,proxy,query){var maxResults=this.get(MAX_RESULTS),requestTemplate=this.get(REQUEST_TEMPLATE);if(requestTemplate){url+=requestTemplate(query);}
return Lang.sub(url,{callback:proxy,maxResults:maxResults>0?maxResults:1000,query:encodeURIComponent(query)});}};ACSources.ATTRS={yqlEnv:{value:null},yqlProtocol:{value:'http'}};Y.Base.mix(Y.AutoCompleteBase,[ACSources]);},'3.3.0',{optional:['io-base','json-parse','jsonp','yql'],requires:['autocomplete-base']});
