function App() { }

//App.domain = 'http://localhost/trunk/Web/basictext';
App.domain = 'http://staging.writinginbasic.co.uk';

App.adminUrl = App.domain + '/admin';
App.mediaItemContainerClass = 'mediaitem_attachments';
App.Field_embedUrl = 'embed_url';
App.ClassNames = {
	Popup: 'popup',
	Overlay: 'overlay',
	ConvertToHtmlEditor: 'html-editor',
	Loading: 'loading'
};
App.Ids = {
	MediaitemAttachments: 'media-item-attachments'
};

/** Suggestions **/
App.SuggestEndpoints = {
	Artist: App.domain + '/artists/suggestJson/',
	Label:  App.domain + '/labels/suggestJson/'
}
App.Suggestions = new Array();

App.suggest = function(input, url, object) {

	var searchTerm = input.val();
	
	if (searchTerm.length <= App.Admin.suggestAfter) {
		return;
	}

	var data = "searchTerm="+searchTerm;

	$.ajax({
		url: url,
		data: data,
		dataType: 'json',
		type: 'POST',
		success: function(results) {
			App.renderSuggestions(results, input, object);
		}
	});
	
}

App.renderSuggestions = function(results, input, object) {
	
	var ul = null;
	var fadeIn = false;
	
	if (typeof App.Suggestions[object] == 'undefined') {
		App.Suggestions[object] = new Object();
	}
	
	if (!App.Suggestions[object].Suggestions) {
		var ul = $(document.createElement('ul'));
		ul.hide();
		ul.addClass('suggestions');
		ul.insertAfter(input);
		App.Suggestions[object].Suggestions = ul;
		fadeIn = true;
	}
	else {
		console.log('Here');
		ul = App.Suggestions[object].Suggestions;
		ul.html('');
	}
	
	$.each(results, function() {
		
		var artist = this[object].name;

		var li = $(document.createElement('li'));
		li.text(this[object].name);
	
		li.bind('click', function() { 
			input.val(artist);
			App.Suggestions[object].Suggestions = null;
			ul.fadeOut(function() { 
				ul.remove();
			});
		});
		
		ul.append(li);
		
	});
	
	if (fadeIn) {
		ul.fadeIn();
	}
	
}
