/*
if (typeof(Prototype) == "undefined" || typeof(domFunction) == "undefined" || typeof(Autocomplete) == "undefined") {
	alert("whatSearch : Required scripts are missing");
}
*/
if (typeof(Prototype) == "undefined"){ 
	alert("whatSearch : Required scripts are missing - PROTOTYPE");
}
if (typeof(domFunction) == "undefined"){
	alert("whatSearch : Required scripts are missing - Dom Function");
} 
if (typeof(Autocomplete) == "undefined") {
	alert("whatSearch : Required scripts are missing - Autocomplete");
}

if(typeof(close_label)=="undefined")
{
	close_label = '';
}

var WhatSearch = Class.create({  

	initialize: function(name,options){		
		WaSe = this;
		
		this.thO = Object.extend({
			debug : debug, // false
			suffix : '',
			SearchWhat : 'SearchWhat',
			selectStringWhat : 'selectStringWhat', 
			AutoSuggest : {
				callerscript : this,
				script: "/index.cfm?event=DashBoard.getAutosuggest_what&",
				serviceUrl:'/index.cfm?event=DashBoard.getAutosuggest_what&',
				varname: "key",
				minchars: 3,
				timeout: 50000,
				maxentries: 15,
				cache: false,
				shownoresults: false,
				json: true,
				callback: this.callbackWhat,
				onSelect: this.callbackWhat,
				closeLabel: close_label,
				suffix : ''
			}
		},options || {});
		
		var suffix = this.thO.suffix;

		window['WaSe'+suffix] = this;

		this.name = name;
		name = this;
		
		this.thD = this.thO.debug;
		
		//this.log("looking for: SearchWhat: " + this.thO.SearchWhat + suffix);
		//this.log("looking for: selectStringWhat: " + this.thO.selectStringWhat + suffix);
		
		if(typeof this.thO.AutoSuggest.callback == 'undefined'){
			this.thO.AutoSuggest.callback = this.callbackWhat;
		}
		
		
		this.SW = this.SearchWhat = $(this.thO.SearchWhat + suffix);
		this.sS = this.selectStringWhat = $(this.thO.selectStringWhat + suffix);	
		
		if(this.SW == null){
			this.log("this.SW == null: return false");
			return false;
		}
		
		if(this.sS == null){
			this.log("this.sS == null: return false");
			return false;
		} 
		
		this.bindBoxWhat(this.SearchWhat);		
	
	},
	
	log: function(msg) {
		if (typeof this == 'object' && debug && typeof dump == 'function' && typeof msg != 'undefined'){
			dump(this.name + " : " + msg);
		}
	},

	bindBoxWhat: function(el){
		var options = this.thO.AutoSuggest;
		window['WaSe'+this.thO.suffix].log("bindBox active on " + this.SearchWhat.id);
		if(typeof Autocomplete != 'undefined') {
			try {
				asWhat = new Autocomplete(el, options);
			} 
			catch (err) {
				window['WaSe'+this.thO.suffix].log("Error: Autocomplete: " + err.message);
			}
		}
	},
	
	// WaSe2[suffix] needs to be specified
	callbackWhat: function(value){
		var obj, value;
		obj = new Object();
		obj.value = value;
		this.suffix = this.options.suffix;
		
		if(typeof obj.value != 'undefined'){
			window['WaSe'+this.suffix].selectStringWhat.value = window['WaSe'+this.suffix].SearchWhat.value = obj.value.stripTags();
			window['WaSe'+this.suffix].SW.removeClassName("loading");
			
		}
		
	}

});

document.observe("dom:loaded", function() {
  var startWhatSearch = new domFunction(function(){
     var DoWhatSearch = new WhatSearch('WhatSearch');
  },{
	'SearchWhat': 'id'
  }); 
});
