/*
 * twitface
 * https://github.com/markdalgleish/twitface
 *
 * Copyright (c) 2012 Mark Dalgleish
 * Licensed under the MIT license.
 */

var q = require('q'),
	request = require('request'),
	cache = require('./cache');

var defaultSize = 'normal';
var sizes = {
	'original': 'original',
	'mini': 'mini',
	'normal': 'normal',
	'bigger': 'bigger',
	'reasonably_small': 'reasonably_small' // Huh?
};

exports.setExpiryDuration = cache.setExpiryDuration;
exports.getExpiryDuration = cache.getExpiryDuration;

exports.setDefaultSize = function(size) {
	if (sizes[defaultSize]) {
		defaultSize = size;
	}
};

exports.getDefaultSize = function(defaultSize) {
	return defaultSize;
};

exports.load = function(username, size, callback) {
	callback = typeof size === 'function' ? size : callback;
	size = typeof size === 'string' ? sizes[size] || sizes[defaultSize] : sizes[defaultSize];

	var cachedPromise = cache.get(username, size);

	if (cachedPromise) {
		// Fire callback when cached promise is resolved
		q.when(cachedPromise).then(function(cachedUrl) {
			callback(null, cachedUrl);
		}, function(err) {
			callback(err, null);
		});
	} else {
		var deferred = q.defer();

		// Place promise in cache
		cache.set(username, size, deferred.promise);

		request('http://api.twitter.com/1/users/profile_image/' + username + '?size=' + size, function(err, response, body) {
			if (err) {
				deferred.reject(err);
				callback(err, null);
				return;
			}

			var url = response.request.uri.href;

			// Resolve cached promise
			deferred.resolve(url);

			callback(err, url);
		});
	}
};
@@ -697,20 +697,16 @@
 unction(
-user
 name, si
@@ -721,16 +721,70 @@
 back) %7B%0A
+%09var usernames = Array.isArray(name) ? name : %5Bname%5D;%0A
 %09callbac
@@ -925,16 +925,78 @@
 Size%5D;%0A%0A
+%09var promises = %5B%5D;%0A%0A%09usernames.forEach(function(username) %7B%0A%09
 %09var cac
@@ -1036,16 +1036,17 @@
 size);%0A%0A
+%09
 %09if (cac
@@ -1065,189 +1065,40 @@
 %7B%0A%09%09
-// Fire callback when cached promise is resolved%0A%09%09q.when(cachedPromise).then(function(cachedUrl) %7B%0A%09%09%09callback(null, cachedUrl);%0A%09%09%7D, function(err) %7B%0A%09%09%09callback(err, null);%0A%09%09%7D);%0A
+%09promises.push(cachedPromise);%0A%09
 %09%7D e
@@ -1103,16 +1103,17 @@
  else %7B%0A
+%09
 %09%09var de
@@ -1130,19 +1130,52 @@
 .defer()
+,%0A%09%09%09%09promise = deferred.promise
 ;%0A%0A
+%09
 %09%09// Pla
@@ -1196,16 +1196,17 @@
 cache%0A%09%09
+%09
 cache.se
@@ -1223,25 +1223,44 @@
 , size, 
-deferred.
+promise);%0A%0A%09%09%09promises.push(
 promise)
@@ -1262,16 +1262,17 @@
 mise);%0A%0A
+%09
 %09%09reques
@@ -1386,16 +1386,17 @@
 y) %7B%0A%09%09%09
+%09
 if (err)
@@ -1394,24 +1394,25 @@
 %09if (err) %7B%0A
+%09
 %09%09%09%09deferred
@@ -1430,32 +1430,8 @@
 );%0A%09
-%09%09%09callback(err, null);%0A
 %09%09%09%09
@@ -1445,11 +1445,13 @@
 %0A%09%09%09
+%09
 %7D%0A%0A
+%09
 %09%09%09v
@@ -1490,16 +1490,17 @@
 ef;%0A%0A%09%09%09
+%09
 // Resol
@@ -1517,16 +1517,17 @@
 promise%0A
+%09
 %09%09%09defer
@@ -1548,39 +1548,148 @@
 l);%0A
-%0A%09%09%09callback(err, url);%0A%09%09%7D
+%09%09%09%7D);%0A%09%09%7D%0A%09%7D);%0A%0A%09q.when(promises%5B0%5D).then(function(cachedUrl) %7B%0A%09%09callback(null, cachedUrl);%0A%09%7D, function(err) %7B%0A%09%09callback(err, null
 );%0A%09%7D
+);
 %0A%7D;

Use promises internally to handle callback