//for closure compiler to ensure these both get exported
window['loadIcons'] = loadIcons;
window['loadHTML'] = loadHTML;


function loadIcons(dir, assets)
{
	dir = dir || './';
	
	//delete existing icons
	$('#svg-icons').remove();
	
	//add new icon holder
	$svg = $('<svg id="svg-icons" xmlns="http://www.w3.org/2000/svg" style="display: none"></svg>');
	
	$svg.appendTo('body');
	
	
	//add returned icon to svg icons container
	
	if(typeof assets === 'string')
	{
		return $.ajax(dir + assets).done(function(data, status, xhr)
		{
			$(data).each(function(item, index)
			{
				var id = $(this).attr('id');
				
				if(!id)
					return;

				$(this)
					.attr('id', 'icon-' + id)
					.appendTo($svg);
			});
		});
	}

	
	var addIcon = function(icon)
	{
		return function(data, status, xhr)
		{
			$(data)
				.find('svg')
				.attr('id', 'icon-' + icon)
				.appendTo($svg);
		}
	};

	
	var promises = [];

	for(var i = 0, len = assets.length; i < len; i++)
		promises.push($.ajax(dir + assets[i] + '.svg').done(addIcon(assets[i])));
	
	return $.when(promises);
}


function loadHTML(dir, assets, destination)
{
	dir = dir || './';
	
	//add returned icon to svg icons container
	var addHTML = function(full_id)
	{
		return function(data, status, xhr)
		{
			$(data).appendTo(destination || 'body');
		}
	};

	
	if(typeof assets === 'string')
		assets = [assets];
	
	var promises = [];

	for(var i = 0, len = assets.length; i < len; i++)
	{
		var id = assets[i];
		var index = id.indexOf('-');
		
		if(index !== -1)
			url = dir + id.substr(0, index) + '/' + id.substr(index+1) + '.html';
		else
			url = dir + id + '.html';
		
		promises.push($.ajax(url).then(addHTML(assets[i])));
	}
	
	//return a promise for all the assets
	return $.when.apply($, promises);
}