$(document).ready(function() {
	
	// #### BILDPOSITION BEI NEWS ##################	
	$(".artikelcontent img:not([align])").not('.galerie img').each(function(){
		$(this).addClass('leftImg');
	});
	
	$("img[style*='left']").each(function(){
		$(this).removeAttr('style').addClass('leftImg');
	});
	
	$("img[align='left']").each(function(){
		$(this).removeAttr('align').addClass('leftImg');
	});
		
	$("img[style*='right']").each(function(){
		$(this).removeAttr('style').addClass('rightImg');
	});
	
	$("img[align='right']").each(function(){
		$(this).removeAttr('align').addClass('rightImg');
	});
	
	
	// #### BILDERGRÖSSE BEI FIRMENPORTRÄTS ##################	
	$(".firmenportraet img").each(function(){
		ImgWidth = $(this).width();
		if (ImgWidth >= 380) {
			$(this).attr('width', '580');
		}
	});
	
	// #### NEWS-ARCHIV ##################
	$('#newsarchiv a').hide();
	$('#newsarchiv .month').click(function(){
		$('#newsarchiv a').hide();
		$(this).nextUntil('.month').show();
	});
});


$(window).load(function() {
	
	// #### BILDERGRÖSSE BEI NEWS ##################	
	$(".artikel.content img").not(".galerie img").each(function(){
		ImgWidth = $(this).width();
		if (ImgWidth >= 380) {
			$(this).attr('width', '550');
			$(this).removeAttr('height');
		}
	});
	
	// #### BILDLEGENDEN ###################
	var imgsLeft = $("#main .artikel .leftImg[alt!='']").not('.galerie img').not('.vorstand img').not('.firmenportraet img');
	var imgsRight = $("#main .artikel .rightImg[alt]").not('.galerie img').not('.firmenportraet img');
	var numLeft = imgsLeft.length;
	var numRight = imgsRight.length;
	var i_left = 0;
	while (i_left < numLeft) {
		var imgWidth = $(imgsLeft[i_left]).width();
		var imgLeftsrc = $(imgsLeft[i_left]).attr("src");
		var imgLeftalt = $(imgsLeft[i_left]).attr("alt");
		$(imgsLeft[i_left]).replaceWith("<dl class='captionLeft' style='width: "+imgWidth+"px'><dt><img src='"+imgLeftsrc+"' alt='"+imgLeftalt+"' width='"+imgWidth+"'/></dt><dd>" + $(imgsLeft[i_left]).attr("alt") + "</dd></dl>");
		i_left++;
	}
	var i_right = 0;
	while (i_right < numRight) {
		var imgWidth = $(imgsRight[i_right]).width();
		var imgRightsrc = $(imgsRight[i_right]).attr("src");
		var imgRightalt = $(imgsRight[i_right]).attr("alt");
		$(imgsRight[i_right]).replaceWith("<dl class='captionRight' style='width: "+imgWidth+"px'><dt><img src='" + imgRightsrc + "' alt='" + imgRightalt + "'/></dt><dd>" + $(imgsRight[i_right]).attr("alt") + "</dd></dl>");
		i_right++;
	}
	
	// #### GALERIE #################
	$('.galerie').after('<br class="clear"/><div class="galerienav clearfix"><span class="counter"></span><a href="#" class="prev">prev</a><a href="#" class="next">next</a><span class="legende"></span></div>').cycle({
		timeout: 0,
		next:   '.galerienav .next',
		prev:   '.galerienav .prev',
		containerResize: 0,
		speed:  300,
		after:	function(curr,next,opts) {
			$('.galerienav .legende').html(this.alt);
			var count = (opts.currSlide + 1) + '/' + opts.slideCount;
			$('.galerienav .counter').html(count);
			$('.galerie').animate({height: this.height}, 300);
		}
	});
	
	// #### FIRMENLOGOS ZENTRIEREN #################
	$('.fplogo img').each(function(){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
	});

});
