function getSetImages( setID )
{
	$.ajax({
		url: "http://"+window.location.hostname+"/flickrapi/getSetImages.php",
		dataType: "json",
		data: { photoset_id: setID },
		success: function (data) {
			eval( data );
		},
		async: true
	});
}

function jsonFlickrApi( rsp )
{
	if( rsp.stat == "ok" )
	{
		//Request was good. Results have been returned.
		for(var i=0; i < rsp.photoset.photo.length; i++)
		{
			var img = document.createElement('img');
			img.src = "http://farm" + rsp.photoset.photo[i].farm + ".static.flickr.com/" + rsp.photoset.photo[i].server + "/" + rsp.photoset.photo[i].id + "_" + rsp.photoset.photo[i].secret + ".jpg";
			
			$( img ).data( 'title', rsp.photoset.photo[i].title ).data( 'owner', rsp.photoset.owner ).data( 'imgID', rsp.photoset.photo[i].id );
			
			$("div.image-gallery").append( img );
		}
		
		$("div.image-gallery img").css('opacity', '0');
		$("div.image-gallery img:first").addClass("current").css('opacity', "1");
		
		$("#image-gallery-overlay").css('opacity', '0').mouseover( overlayFadeIn ).mouseout( overlayFadeOut );
		$("#overlay-title").html('<a target="_blank" href="http://www.flickr.com/photos/' + $("div.image-gallery img:first").data('owner') + '/">' + $("div.image-gallery img:first").data('title') + '</a>').css('opacity', '0').mouseover( overlayFadeIn ).mouseout( overlayFadeOut );
		
		//$("#overlay-title").html( $("div.image-gallery img:first").data('title') ).css('opacity', '0').mouseover( overlayFadeIn ).mouseout( overlayFadeOut );
		
		cycleNextImage();
	}
}

function setImagesResults( rsp )
{
	
	alert(rsp.photoset.photo);
	if( rsp.stat == "ok" )
	{
		//Request was good. Results have been returned.
		for(var i=0; i < rsp.photoset.photo.length; i++)
		{
			var img = document.createElement('img');
			img.src = "http://farm" + rsp.photoset.photo[i].farm + ".static.flickr.com/" + rsp.photoset.photo[i].server + "/" + rsp.photoset.photo[i].id + "_" + rsp.photoset.photo[i].secret + ".jpg";
			
			$( img ).data( 'title', rsp.photoset.photo[i].title ).data( 'owner', rsp.photoset.owner ).data( 'imgID', rsp.photoset.photo[i].id );
			
			$("div.image-gallery").append( img );
		}
		
		$("div.image-gallery img").css('opacity', '0');
		$("div.image-gallery img:first").addClass("current").css('opacity', "1");
		
		$("#image-gallery-overlay").css('opacity', '0.85');
		$("#overlay-title").html('<a target="_blank" href="http://www.flickr.com/photos/' + $("div.image-gallery img:first").data('owner') + '/' + $("div.image-gallery img:first").data('imgID') + '/">' + $("div.image-gallery img:first").data('title') + '</a>');
		
		cycleNextImage();
	}
}

function cycleNextImage()
{
	setTimeout(galleryFadeOut, 5000);
}

function galleryFadeOut()
{
	$("div.image-gallery img.current").animate({opacity: "0"}, 1000, galleryFadeIn);
}

function galleryFadeIn()
{
	if($("div.image-gallery img.current").next().size() == 0)
	{
		$("div.image-gallery img.current").removeClass("current");
		$("div.image-gallery img:first").addClass("current").animate({opacity: "1"}, 1000, cycleNextImage);
	}
	else
	{
		$("div.image-gallery img.current").removeClass("current").next().addClass("current").animate({opacity: "1"}, 1000, cycleNextImage);
	}
	
	$("#overlay-title").html('<a target="_blank" href="http://www.flickr.com/photos/' + $("div.image-gallery img.current").data('owner') + '/">' + $("div.image-gallery img.current").data('title') + '</a>');
	//$("#overlay-title").html('<a target="_blank" href="http://www.flickr.com/photos/' + $("div.image-gallery img.current").data('owner') + '/' + $("div.image-gallery img.current").data('imgID') + '/">' + $("div.image-gallery img.current").data('title') + '</a>');
}

function overlayFadeOut()
{
	$("div.image-gallery #image-gallery-overlay").animate({opacity: "0"}, {duration: 500, queue: false});
	$("div.image-gallery #overlay-title").animate({opacity: "0"}, {duration: 500, queue: false});
}

function overlayFadeIn()
{
	$("div.image-gallery #image-gallery-overlay").animate({opacity: ".75"}, {duration: 500, queue: false});
	$("div.image-gallery #overlay-title").animate({opacity: "1"}, {duration: 500, queue: false});
}

$(document).ready( function() {
	if( $("span#set-id").size() > 0 )
	{
		getSetImages( $("span#set-id").html() );
	}
	else
	{
		getSetImages( "72157624047244918" );
	}
});
