$(function () {

	var flickr_set = 'www.immobilienpreise24.de';
	var _loadDivId = 'loadnote';
	var _imgContainerId = 'container';
	var _imageLoadClassName = 'loading';
	var images = $('ul#'+_imgContainerId+' li img');
	var max = images.length;

	$('ul#'+_imgContainerId+' li').remove();

	var loadDiv = null;

	if(max>0){
		loadDiv = $('<div id="'+_loadDivId+'" style="display:none"></div>').appendTo($('body'));
		LoadImage(0,max);
	}

	function LoadImage(index,max)
	{
		if(index<max)
		{
			// loading div update
			$('#'+_loadDivId).html("Loading "+(index+1)+" of "+max).fadeIn("fast");
			// add list to ul
			var list = $('<li id="_img'+index+'"></li>').attr('class',_imageLoadClassName).appendTo('ul#'+_imgContainerId);
			// new image object
			var img = new Image();
			// current image
			var curr = $("li#_img"+index);
			// load current image
			$(img).load(function ()
			{
				// hide it first + .hide() failed in safari
				$(this).css('display','none');
				// remove loading class from li and insert the image into it
				$(curr).removeClass(_imageLoadClassName).append(this);
				// optional wrap with link
				$(this).wrap('<a href="'+flickr_set+'"></a>');
				// fade it in
				$(this).fadeIn('slow',function()
				{
					if(index == (max-1))
						{
							// remove loading div after all images loaded
							$(loadDiv).remove();
						}
					else
						{
						  // we are inform loading next item
						  $(loadDiv).html('Wait Loading Next Item ...');
						  // delay 2 secons and load next item *uses jquery timer plugin*
						  $(this).oneTime("2s", function() { LoadImage(index+1,max); });
						}
				});
			}).error(function () {
				// if loading error remove li
				$(curr).remove();
				// try to load next item
				LoadImage(index+1,max);
			}).attr('src', $(images[index]).attr('src'));
		}
	}
});

