jQuery(function ($) {
	$('a').bind('click', function () { $(this).blur(); });
	// layout
	  // first and last elements and titles
			$('#content').each(function () {
				$(this).children('div[id^=cmsContentElement_]:first').addClass('first').end()
				       .children('div[id^=cmsContentElement_]:last').addClass('last');
			});
			$('div[class*=cms__contentElements__layout__columns_col]').each(function () {
				$(this).children('div[id^=cmsContentElement_]:first').addClass('first').end()
			         .children('div[id^=cmsContentElement_]:last').addClass('last');
			});
			$('.cms__contentElements__html > *:first-child').filter('h1, h2, h3, h4, h5, h6, h7').addClass('first');
			$('form:not(:first-child) > .cmsForm:first-child').addClass('firstCmsFormElement');
		// menu
			$('ul#headerMetaMenu li:last').addClass('last');
			// icfMenuAddons
			  $('#mainMenu li').each(function () {
			    var me = $(this);
			    var icfMenuAddon = me.find('.icfMenuAddon');
			    if (!icfMenuAddon.length) return;
					$(this).bind('mouseenter', function () {
				    icfMenuAddon.show();
					}).bind('mouseleave', function () {
						icfMenuAddon.hide();
					}).bind('mousemove', function (e) {
					  icfMenuAddon.css({
					    left: (e.pageX - me.offset().left + -27) + "px",
					    top: (e.pageY - me.offset().top + -80) + "px"
						});
					});
				});
		// content columns
		  // top image
				$('#content > .cms__contentElements__layout__columns > .cms__contentElements__layout__columns_col2 > .cms__contentElements__image:first-child').addClass('rightTopImage').each(function () {
					var me = $(this);
					var src = me.find('img:first').attr('src');
					if (src.search(/_w[0-9]+/) == -1) {
					  if (src.lastIndexOf(".") > 0) src = src.substr(0, src.lastIndexOf(".")) + "_w585." + src.substr(src.lastIndexOf(".")+1);
					  else src += "_w585";
					}
					me.html('<img src="' + src + '" />');
				}).filter('.first').each(function () {
				  $(this).removeClass('first').next('div[id^=cmsContentElement_]').addClass('first');
				});
			// separators
			  $('#page').prepend('<a name="top"></a>')
			  $('#content > .cms__contentElements__layout__columns:not(.first)')
					.addClass('cmsContentElementsLayoutColumnsWithSeparator')
					.before('<div class="cmsContentElementsLayoutColumnsSeparator"><a href="#top"><img src="' + cmsTemplatePath + 'images/toTop.gif" alt="Nach oben" title="Nach oben" /></a></div>');
				// ie7 bugfix: separators were not always displayed correct - force browser to re-render
					if ($.browser.msie && $.browser.version < 8 && window.location.href.indexOf("?login") < 0 && $('.cmsContentElementsLayoutColumnsWithSeparator').length) {
					  $('#content').html($('#content').html());
					  $(window).bind('load', function () {
					    $('#content').html($('#content').html());
						});
					}
});

// FILE OR FOLDER
jQuery(function () {
	jQuery('.cms__contentElements__fileOrFolder:first').css('margin-top', '-4px');
	jQuery('.cmsFolderInfo img, .cmsFolderInfo span').bind('click', function () {
	  var me = jQuery(this).parents('li:first');
	  if (me.data('childrenShown')) me.children("ul").hide();
	  else me.children("ul").show();
	  me.data('childrenShown', !me.data('childrenShown'));
	  return false;
	});
	jQuery('.cmsFolderInfo').bind('mouseover', function () {
	  jQuery(this).find('.cmsFolderZip').show();
	}).data('childrenShown', false).bind('mouseout', function () {
	  jQuery(this).find('.cmsFolderZip').hide();
	});
});

// CWX GALLERY
jQuery(function ($) {
  $('.cwxContentGalleryThumbs').each(function () {
    var thumbs = $(this);
		var elements = thumbs.html();
		thumbs.find('.cwxContentGalleryThumbsLoader').bind('click', function () {
      var me = $(this);
      me.unbind('click').css('color', '#000').html("lade Bilder ... <span>0</span>%").blur();
      var precentSpan = me.find("span");
      var div = $('<div />').hide().append(elements.substring(elements.indexOf("<!--")+4, elements.indexOf("-->")));
      var divImgLoaded = 0;
      var divImgTotal = div.find('img').bind('load', function () {
        divImgLoaded++;
        if (divImgLoaded == divImgTotal) {
		      me.hide(250, function () {
						div.show(250, function () {
				      me.remove();
				      Shadowbox.setup();
						});
					});
				} else precentSpan.html(Math.round(100/divImgTotal*divImgLoaded));
			}).length;
      div.insertAfter(me);
		});
	});
});

// SHRINK 2
jQuery(function ($) {
	$('.shrink2').each(function () {
		var me = $(this);
		me.data('fullContent', me.html());
		var txt = me.text();
		var maxH = me.html('row1<br />row2').height();
		me.html(txt);
		for (var i=txt.length; me.height()>maxH; i--) me.html(txt.substr(0, i) + '... <a href="javascript:;">mehr</a>');
		me.find('a').bind('click', function () { me.html(me.data('fullContent')); });
	});
});
