var _featuredArtistTimer = 0;
var _featuredArtworkTimer = 0;
var _featuredExhibitionTimer = 0;
var _currentFeaturedExhibition = 0;
var _currentFeaturedArtwork = 0;
var _currentFeaturedArtist = 0;

$(document).ready(function() {

	_featuredExhibitionTimer = setInterval(function() { fireFeaturedExhibition() }, 5000);

});

function fireFeaturedExhibition()
{
	$.ajax({
		type: 'GET',
		contentType: 'application/json; charset=utf-8',
		url: '/getrandom.ashx?id=' + _currentFeaturedExhibition + "&type=exhibition",
		dataType: 'json',
		timeout: 2000,
		async: false,
		error: function(xhr, desc, exceptionobj, request, error) {
			
		},
		success: function(data) {
			
			clearTimeout(_featuredExhibitionTimer); 
			
			/// Fade in the new image and link
			$("a#v_PageBody_exhibition_lnk").children("img").fadeOut("slow", function() { 
				
				$(this).parent("a").attr("href",data[0].link);
				//$(this).attr("src",data[0].image).fadeIn("slow"); 
				
				// Insert preloaded image after it finishes loading
				$(this)
				    .attr('src', data[0].image)
				    .load(function(){
				        $(this).fadeIn("slow"); 
				        // Your other custom code
				    });
				
				 
			});
			
			/// Fade in the new text and link
			$("div#v_PageBody_exhibition_text").children("div").children("a").fadeOut("slow", function() { 
				
				$(this).attr("href", data[0].link);
				$(this).html("<span>Featured exhibition</span>" + data[0].text);
				$(this).fadeIn("slow"); 
			
			});
			
			/// Set the current id
			_currentFeaturedExhibition = data[0].id;
			
			/// Reset the slideshow
			//_featuredExhibitionTimer = setInterval(function() { fireFeaturedExhibition() }, 10000);
			_featuredArtworkTimer = setInterval(function() { fireFeaturedArtwork() }, 5000);
			
		}
	});
}

function fireFeaturedArtwork()
{
	$.ajax({
		type: 'GET',
		contentType: 'application/json; charset=utf-8',
		url: '/getrandom.ashx?id=' + _currentFeaturedArtwork + "&type=artwork",
		dataType: 'json',
		timeout: 2000,
		async: false,
		error: function(xhr, desc, exceptionobj, request, error) {
			
		},
		success: function(data) {
			
			clearTimeout(_featuredArtworkTimer); 
			
			/// Fade in the new image and link
			$("a#v_PageBody_artwork_lnk").children("img").fadeOut("slow", function() { 
				
				$(this).parent("a").attr("href",data[0].link);
				//$(this).attr("src",data[0].image).fadeIn("slow"); 
				
				// Insert preloaded image after it finishes loading
				$(this)
				    .attr('src', data[0].image)
				    .load(function(){
				        $(this).fadeIn("slow"); 
				        // Your other custom code
				    });
				 
			});
			
			/// Fade in the new text and link
			$("div#v_PageBody_artwork_text").children("div").children("a").fadeOut("slow", function() { 
				
				$(this).attr("href", data[0].link);
				$(this).html("<span>Featured artwork</span>" + data[0].text);
				$(this).fadeIn("slow"); 
			
			});
			
			/// Set the current id
			_currentFeaturedArtwork = data[0].id;
			
			/// Reset the slideshow
			/// _featuredArtworkTimer = setInterval(function() { fireFeaturedArtwork() }, 10000);
			_featuredArtistTimer = setInterval(function() { fireFeaturedArtist() }, 5000);
		}
	});
}

function fireFeaturedArtist()
{
	$.ajax({
		type: 'GET',
		contentType: 'application/json; charset=utf-8',
		url: '/getrandom.ashx?id=' + _currentFeaturedArtist + "&type=artist",
		dataType: 'json',
		timeout: 2000,
		async: false,
		error: function(xhr, desc, exceptionobj, request, error) {
			
		},
		success: function(data) {
			
			clearTimeout(_featuredArtistTimer); 
			
			/// Fade in the new image and link
			$("a#v_PageBody_artist_lnk").children("img").fadeOut("slow", function() { 
				
				$(this).parent("a").attr("href",data[0].link);
				//$(this).attr("src",data[0].image).fadeIn("slow"); 
				
				// Insert preloaded image after it finishes loading
				$(this)
				    .attr('src', data[0].image)
				    .load(function(){
				        $(this).fadeIn("slow"); 
				        // Your other custom code
				    });
				 
			});
			
			/// Fade in the new text and link
			$("div#v_PageBody_artist_text").children("div").children("a").fadeOut("slow", function() { 
				
				$(this).attr("href", data[0].link);
				$(this).html("<span>Featured artist</span>" + data[0].text);
				$(this).fadeIn("slow"); 
			
			});
			
			/// Set the current id
			_currentFeaturedArtist = data[0].id;
			
			/// Reset the slideshow
			///_featuredArtistTimer = setInterval(function() { fireFeaturedArtist() }, 10000);
			_featuredExhibitionTimer = setInterval(function() { fireFeaturedExhibition() }, 5000);
		}
	});
}
