dojo.provide("epages.cartridges.de_epages.content.widget.SmartSearchInput");
dojo.require("epages.widget");
dojo.require("epages.io.json");
dojo.require('epages.cartridges.de_epages.content.widget.JsonTagEditor');

dojo.declare(
	"epages.cartridges.de_epages.content.widget.SmartSearchInput",
	[dijit._Widget, dijit._Templated],
	{
		/**
		 * public properties
		 */
		url                     : '?',  // url for the json action(s)
		ViewAction              : 'JSONSmartSearchObjects',
		inputName               : 'SmartSearch',
		inputStyle              : 'width: 200px',
		value                   : '',    // last input value (i.e. if formerror)
		objectId                : epages.vars.SiteID || '',

		/**
		 * private properties
		 */
		_lastInputValue         : null,  // the last search value
		_timeoutMarker          : null,
		_lastSeachResultCount   : null,
		_searchStartDelay       : 400,   // delay after which time no key was pressed, the search should be started. In msec.
		_minCharactersForSearch : 1,     // how many characters can a user enter before triggering the search

		/**
		 * dojo widget properties
		 */
		templatePath:       dojo.moduleUrl('epages.cartridges.de_epages.content.widget', 'templates/SmartSearchInput.html'),
		widgetsInTemplate:  true,

		postCreate: function() {
			this.inherited("postCreate",arguments);
			if(this.value) {
				this.SmartSearchBox.setValue(this.value);
			}
			dojo.subscribe(this.SmartSearchBox.id + '/onEvaluateInput', this, "searchBoxKeyUp");
		},

		searchBoxKeyUp: function(/*String*/textInput,/*Object?*/callbackArgs) {
			// fire keyup evt
			// REMOVED dojo.publish(this.id+'/keyup', [ evt ]);
			// check if value changed
			if(this._lastInputValue == textInput || textInput.length <= this._minCharactersForSearch) {
				return;
			}
			// check if last request returned 0 results and if entered value is not the same string plus more chars
			if(this._lastSeachResultCount !== null && this._lastSeachResultCount != '') {
				if(this._lastSeachResultCount === 0 &&
				   textInput.match(new RegExp('^' + this._lastInputValue))) {
				  this._lastInputValue = textInput;
					return;
				}
			}
			// update values
			this._lastInputValue = textInput;
			this.value = textInput;

			// delay requests if keys pressed too fast
			if(this._timeoutMarker) {
				window.clearTimeout(this._timeoutMarker);
			}
			var self = this;
			this._timeoutMarker = window.setTimeout(function(){
				// do search
				self.queryObjectAliases(callbackArgs);
			}, this._searchStartDelay);
		},

		getValue: function() {
			return this.SmartSearchBox.getValue();
		},

		queryObjectAliases: function(callbackArgs) {
			if(!this._lastInputValue) {
				return;
			}
			var inputValue = this._lastInputValue;
			var me = this;
			var json = new epages.io.Json();
			var result = json.loadAsync(this.url,
			function(result){
				if(result.error || !result.data) {
					return;
				}
				// update tageditor available tags ... result.data
				me._lastSeachResultCount = parseInt(result.data.NumResults);
				dojo.publish(me.SmartSearchBox.id + '/onNewSearchResults', [result.data.Results,callbackArgs]);
			},
			{
				'ObjectID' 			: this.objectId,
				'ViewAction'		: this.ViewAction,
				'SearchString' 	: inputValue
			});
		}
	}
);