(function($){
$.fn.shrink = function() {
/* OPTIONS */
	var width = $(this).width();
	var height = $(this).height();
	var _loadDivId = 'loadnote';
	var _imgContainerId = 'stift';
	var _imgLoadClassName = 'loading';
	var images = $('div.stift img');
	var max = images.length;

/* END */
	var loadDiv = null;
    $('#newsoverlay').fadeOut(100);
    $('div .stift').fadeIn(1000);

    

	if(max>0)
		{
        $('#news a.weiterlesen').css({'display':'none'});
		var totalsize = 0;
		loadDiv = $('<div id="'+_loadDivId+'" style="display:none"></div>').appendTo('.stift');
		LoadImage(0,max);
		var title = $('#news h2:first').text();

	}
	function LoadImage(index,max)
		{
		if(index<max)
			{
			$('<span id="_img'+index+'"></span>').attr('class',_imgLoadClassName).appendTo('div.'+_imgContainerId);
			$('#'+_loadDivId).html("Lade Bild "+(index+1)+" von "+max).fadeIn("fast");
			var img = new Image();
			var curr = $("#_img"+index);
			$(img).load(function ()
			{
			$(curr).removeClass('loading').append(this);
			if (max>1)
			{
					var total = 0;
					$('.stift span:even').css({'margin':'5px 10px 5px 0','display':'block','float':'left'}).addClass('even');
					$('.stift span:odd').css({'margin':'5px 0 5px 0','display':'block','float':'left'}).addClass('odd');
					var width = $(this).width();
					var height = $(this).height();
					var proportion = height / width;
					var proportion = Math.round(proportion*100)/100;
					if ($(this).width() > 304) { $(this).width(304); };
					var width = $(this).width();
					var newHeight = proportion*width;
					var height = Math.round(newHeight);
					$(this).height(height);
					var newheight = height + 10;
					var newwidth = width + 10;
					var imageheight = height - 5;
					var imagewidth = width - 4;
					$(this).parent('span').css({'width':''+newwidth+'px','height':''+newheight+'px'});
					if ($(this).parent('span:first').is('.even')) {
					$('.stift').animate({height: '+='+ newheight});
					if(index == (max-1)) 
					{
					containerheight = newheight + 5;
					totalsize += containerheight;	
					}
					}
					else if ($(this).parent('span:first').is('.odd')) {
					oddheight = $(this).parent('span:first').height();
					evenheight = $(this).parent('span:first').prev('span.even:first').height();
					if (evenheight > oddheight) {
					containerheight = evenheight + 5;
					}
					else if (oddheight > evenheight) {
					var newheight = oddheight - evenheight;
					$('.stift').animate({height: '+='+ newheight});
					containerheight = oddheight + 5;
					}
					else if (oddheight == evenheight) {
					containerheight = oddheight + 5;
					}
					totalsize += containerheight;	
					}
					var parentheight = $(this).parent('span:first').height();
					if($(this).parent("span").parent().is('a')) {
						$(this).parent('span').parent('a').addClass('image').attr('title',title);
					}

					else {
					var src = $(this).attr('src');
					$(this).before('<a title="' + title + '" href="'+ src +'" class="image"></a>');
					$(this).prevAll('.image:first').css({'display':'block','margin':'8px 0px 0 8px','background':'url(http://www.adassjisroel.de/images/aj07.gif) top right no-repeat','position':'absolute','z-index':'31','height':''+ imageheight +'px','width':''+ imagewidth +'px'}).attr('rel', 'shadowbox').shadowbox().fadeIn();				
					}
					$('.image').hover(
					function () {
						$(this).css({'background':'url(http://www.adassjisroel.de/images/open.png) center center no-repeat'});
					},
					function () {
						$(this).css({'background':'url(http://www.adassjisroel.de/images/aj07.gif) top right no-repeat'});
					}
				);
					if ($(this).parent('span:first').is('.odd')) {
					var newheight = $(this).parent('span:first').height();
					var newheight = newheight + 10;
					$('#'+_loadDivId).animate({marginTop: "+=" + newheight +"px"}, "fast");
					}
			}
			if (max<2)
			{
				var total = 1;
				if($(this).parent('span').prevAll('p').children().is('a')) {
					var grund = $(this).parent("span").prevAll('p').children('a').attr('href');
					grund = grund.substr((grund.lastIndexOf("./")+1)); 
					grund = 'http://www.adassjisroel.de' + grund;
					$(this).parent('span').prevAll('p').children('a:first').remove();
					if($(this).parent("span").prevAll('p').children('a').attr('href').indexOf('pdf') != -1) {

						$(this).wrap('<a class="image" href="'+ grund +'"></a>');
						$('.image').css({'display':'block','position':'relative','top':'0px'});
						$(this).before('<img id="pdfsymbol" />');
						$('#pdfsymbol').attr('href',''+ src +'').attr('src','http://www.adassjisroel.de/images/pdf-big.png').css({'top':'10px','right':'6px','display':'block','position':'absolute','z-index':'31'});
					}
					else {
					$(this).wrap('<a rel="shadowbox" class="image" href="'+ grund +'"></a>').show().parent('a').shadowbox();

					}
				}
					else {
					var grund = $(this).attr('src');
					grund = grund.substr((grund.lastIndexOf("./")+1)); 
					grund = 'http://www.adassjisroel.de' + grund;
					$(this).parent('span').wrap('<a style="display:block;" title="'+ title +'" class="image" href="'+ grund +'"></a>');
					$('.image').attr('rel','shadowbox').shadowbox();
					}
					var width = $(this).width();
					var height = $(this).height();
					var newheight = height + 16;
					var newwidth = width + 16;
					var padding = 639 - newwidth;
					var padding = padding/2;
					var padding = Math.round(padding);
					$('.image').css({'margin-top':'10px','margin-bottom':'10px','margin-right':+padding+'px','margin-left':+padding+'px','display':'block','height':''+ newheight +'px','width':+newwidth+'px'});

			}
				$(this).fadeIn(900,function()
				{
					if(index == (max-1)) 
						{
							$(loadDiv).remove();
							if (jQuery.support.cssFloat == true) {
								$(".stift span").wrapAll("<div class=\"wrap\"></div>");
								var leadingwrapheight = $('.wrap').prevAll('p:first').height();
								$(".stift .wrap").append("<div class=\"clearer\"></div>");
								var wrapheight = $('.wrap').height();
								var fixheight = wrapheight + leadingwrapheight;
									if (total== 0) {
									$('.stift').animate({height: ''+ fixheight},200);
									}			
							}
							
						}
					else 
						{		
						  $(loadDiv).html('Nächstes Bild wird geladen');
						  $(this).oneTime("30ms", function() { LoadImage(index+1,max); });
						}
				});
			}).error(function () {
				$(curr).remove();
				LoadImage(index+1,max);
			}).attr('src', $(images[index]).attr('src'));

		}
            $('.weiterlesen').fadeIn(700);

	}

};

})(jQuery);
