var DiscogsAPI = function() {
	var me = this;
	// Connection manager for getting data
	this.connection = new MCAjaxConnection();
	this.connection.delegate = function(method, properties) {
		switch (method) {
			case "didReceiveResponse":
				var artworkImages = [];
				for (var i = 0; i < properties.response.resp.search.searchresults.results.length; i++) {
					var result = properties.response.resp.search.searchresults.results[i];
					if (result.thumb != null && (result.type == "master" || result.type == "release")) {
						var imgUrl = result.thumb;
						imgUrl = imgUrl.replace("/R-150-", "/R-");
						artworkImages.push(imgUrl);
					};
				};
				try {me.delegate("didReceiveArtwork", {artwork:artworkImages})} catch(err) {}; // Delegate method
			break;
			case "didFailWithError":
				try {me.delegate("didReceiveNoResults")} catch(err) {}; // Delegate method
			break;
		};
	};
	
	this.getArtworkForQuery = function(query) {
		this.connection.makeRequest("http://api.discogs.com/search", {"q":query});
	};
};



var MCAjaxConnection = function() {
	
};
MCAjaxConnection.prototype.makeRequest = function(url, parameters) {
	var me = this;
	var loadSuccess = false;
	$.getJSON(url + "?callback=?", parameters, function(data) {
	    loadSuccess = true;
	    try {me.delegate("didReceiveResponse", {response:data})} catch(err) {}; // Delegate method
	});
	setTimeout(function() {
		if (!loadSuccess) {
			try {me.delegate("didFailWithError")} catch(err) {}; // Delegate method
		};
	}, 2000);
};
