$(document).ready(function() {
	(function() {
		$('<div id="featured-photos-loading"><img src="/cs/RedBull/images/nascar/activity_indicator.gif" alt="loading" /></div>').appendTo('.photo-content');

		var buildDom = function(data) {
			var list = data.list,
				galleryThumb,
				galleryUrl,
				galleryDateTime,
				galleryDate,
				newImageUrl,
				node,
				images,
				template;

			$.each(list, function(i, object) {
					images = object.images[0],
					galleryUrl = decodeURIComponent(object.url),
					galleryTitle = object.title,
					galleryThumb = (decodeURIComponent(images.imageurl)).split('/'),
					galleryDateTime = (images.date).split(' '),
					galleryDateChunks = galleryDateTime[0].split('-'),
					galleryDate = galleryDateChunks[1] + '-' + galleryDateChunks[2] + '-' + galleryDateChunks[0],
					newImageUrl = galleryThumb.splice(10, 1, 'photo260x146'),
					template = '<div class="featured-photo-item">' +
									'<a href="' + galleryUrl + '" class="photo">' +
										'<img class="" src="' + galleryThumb.join('/') + '" alt="" title="">' +
									'</a>' +
									'<h5>' +
										'<a href="' + galleryUrl + '">' + galleryTitle + '</a>' +
									'</h5>' +
									'<dl class="metadata">' +
										'<dt class="date">Date</dt>' +
										'<dd class="date">' + galleryDate + '</dd>' +
									'</dl>' +
								'</div>';
					$(template).appendTo('.photo-content');
			});
		};

		$.ajax({
			url:'/cs/Satellite?pagename=RedBull/AssetInterface/AssetDetailList&locale=en_US&parentid=1242928521485&type=gallery&start=0&stop=2&callback=',
			dataType: 'json',
			success: function(data) {
				$('#featured-photos-loading').fadeOut();
				 buildDom(data);
			}
		});
	})();	
});
