function Search(quickSearchDiv){
	this.quickSearchDiv = $('#' + quickSearchDiv);
	
	this.init();
}
Search.prototype = {
	init: function(){
		var self = this;
		ajax(HTMLPATH + '/search/list/ajax/true', null, function(obj){
			self.initResponse(obj);
		});
	},
	
	initResponse: function(obj){
		try {
			var obj = obj.json;
			var self = this;
			this.quickSearchDiv.empty();
			
			if (obj.edit) 
				this.quickSearch = new QuickSearch('quickSearchDiv', 'Look for anything', function(name) {
					self.add(name);
				});
			else 
				this.quickSearch = new QuickSearch('quickSearchDiv', 'Look for anything');
			
			this.quickSearch.setApproveFunction(function(id, name, data){
				self.approve(id, name, data);
			});
			
			this.initResponseHelper('brokerage_id', 'company_name', obj.brokerage, 'Brokerage');
			this.initResponseHelper('institution_id', 'name', obj.institution, 'Institution');
			this.initResponseHelper('investor_id', 'name', obj.investor, 'Investor');
			this.initResponseHelper('plant_id', 'name', obj.plant, 'Plant');
			this.initResponseHelper('investment_id', 'registeredname', obj.investment, 'Registered Name');
			this.initResponseHelper('representative_id', 'name', obj.representative, 'Representative');
			this.initResponseHelper('series_id', 'seriesname', obj.series, 'Series');
		} 
		catch (e) {
			alert("Search.initResponse error: " + e.message);
		}
	},
	
	initResponseHelper: function(id, name, array, category) {
		for (var i = 0; i < array.length; i++)
			if (category == 'Registered Name')
				this.quickSearch.addOption(array[i][id], array[i][name] + ' ' + category, this.investmentTemplate(array[i][name], array[i].seriesname), category);
			else this.quickSearch.addOption(array[i][id], array[i][name] + ' ' + category, QuickSearchTemplate(array[i][name], category), category);
	},
	
	investmentTemplate: function (name, series) {
		var container = document.createElement('div');
		var nameDiv = document.createElement('div');
		var categoryDiv = document.createElement('div');
	
		nameDiv.innerHTML = name;
		categoryDiv.className = 'category';
		categoryDiv.innerHTML = 'Registered Name (' + series + ')';
		
		container.appendChild(nameDiv);
		container.appendChild(categoryDiv);
			
		return container;
	},
	
	approve: function(id, name, category) {
		var category = (category == 'Registered Name') ? 'Investment' : category;
		new Profiles(id, category).get();
	},
	
	add: function(name){
			var self = this;
			var div = $('#quickSearchAdd');
			div.html('<h5>What would you like to add?</h5>');
			var floatDiv = $(document.createElement('div'));
			floatDiv.addClass('container');
			var options = ['Brokerage', 'Institution', 'Investment', 'Investor', 'Plant', 'Series', 'Representative'];
			for (var i = 0; i < options.length; i++) {
				this.addHelper(floatDiv, name, options[i]);
			}
			var tag = new Tag('', '', 'Cancel');
			tag.disableClose();
			tag.container.onclick = function(){
				self.hideAddContainer();
			}
			floatDiv.append(tag.getContainer());
			
			div.append(floatDiv);
			div.append('<div class="clearBoth"></div>');
			this.addContainer = div;
		div = this.addContainer;
		div.show('normal');
	},

	addHelper: function(container, name, profile) {
		var self = this;
		var tag = new Tag('', '', (profile == 'Investment') ? 'Registered Name' : profile);
			tag.disableClose();
			tag.container.onclick = function() {
				self.hideAddContainer();
				new Profiles('new', profile).get(name);
			};
		container.append(tag.getContainer());
	},

	get: function(){
		return this.quickSearch;
	},
	
	hideAddContainer: function(){
		this.addContainer.hide('normal');
		this.addContainer.empty();
	}
};

