var interval = 30000, fadetime = 1000,
	dynamic_sidebar_tags_container_selector = '.widget_listcategorypostswidget';

function dynamic_sidebar_tags(html) {
	if (jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').text() == jQuery(html).text()) return;
	//prompt('', jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').text());
	//prompt('', jQuery(html).text());
	if (jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').length)
		jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').fadeOut(fadetime, function () {
			jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').remove();
			dynamic_sidebar_tags2(html);
		});
	else dynamic_sidebar_tags2(html);
};

function dynamic_sidebar_tags2(html) {
	if (html) {
		jQuery(dynamic_sidebar_tags_container_selector).html('<ul class="related_posts_thumbnails">' + html + '</ul>');
		jQuery(dynamic_sidebar_tags_container_selector + ' ul.related_posts_thumbnails').hide().fadeIn(fadetime);
	}
};

function gallery_swap(a) {
	var img = jQuery('body.single-post .entry-content > .gallery img.preview');
	jQuery('body.single-post .entry-content > .gallery, body.single-post .entry-content > .gallery .gallery a').css('cursor', 'wait');
	jQuery('body.single-post .entry-content > .gallery').append('<img class="preview" src="' + jQuery(a).attr('href') + '" style="display: none;" alt="' + (jQuery(a).attr('title') || 'Bild') + '" title="' + (jQuery(a).attr('title') || 'Bild') + '"/>');
	jQuery('body.single-post .entry-content > .gallery img.preview').css('cursor', 'pointer').click(function () {
		jQuery.slimbox(this.src, 0, {});
	});
	jQuery('body.single-post .entry-content > .gallery img.preview:last-child').load(function () {
		jQuery(this).fadeIn(500, function () {
			img.remove();
			jQuery('body.single-post .entry-content > .gallery').css('cursor', 'auto');
			jQuery('body.single-post .entry-content > .gallery .gallery a').css('cursor', 'pointer');
			if (jQuery('body').hasClass('category-mediathek') || jQuery('body').hasClass('category-talente-ans-mikrofon-mediathek') || jQuery('body').hasClass('category-sieger-und-ergebnisse')) {
				jQuery('body.single-post .entry-content > .gallery').append('<p class="image-title">' + (jQuery(a).attr('title') || jQuery(a).attr('title') || 'Bild') + '</p>');
				jQuery('body.single-post .entry-content > .gallery p.image-title').fadeIn(250);
				jQuery('body.single-post .entry-content > p').fadeIn(250);
			}
		});
	});
};

function get_related_posts_html(query, maxamount) {
	var related_posts_html = [], count = 0;
	if (!maxamount) maxamount = 10;
	query.find('ul.related_posts_thumbnails li').each(function (i, element) {
		var html = '<li>' + jQuery(this).html() + '</li>';
		if (jQuery.inArray(html, related_posts_html) < 0) {
			if (++count > maxamount) return false;
			related_posts_html.push(html);
		}
	});
	//jQuery('.debug div').html('DEBUG: ' + '<textarea cols="100" rows="50">' + related_posts_html.join('') + '</textarea>');
	return related_posts_html.join('');
};

function content_fader(selector) {
	if (!jQuery(selector).length) return;
	jQuery(selector + ' #content ul.lcp_catlist li.lcp_entry').hide();
	jQuery(selector + ' #content ul.lcp_catlist li.lcp_entry:first-child').fadeIn(fadetime);
	jQuery('.widget_anual_archive').before('<li class="widget-container widget_listcategorypostswidget"></li>');
	//dynamic_sidebar_tags(jQuery(selector + ' #content ul.lcp_catlist li.lcp_entry:first-child .lcp_content ul.related_posts_thumbnails').html());
	dynamic_sidebar_tags(get_related_posts_html(jQuery(selector + ' #content .lcp_box:first-child ul.lcp_catlist li.lcp_entry .lcp_content')));
	jQuery(selector + ' #content ul.lcp_catlist li.lcp_entry').each(function (idx, el) {
		var html = jQuery(el).html();
		/* IE 7, 8: <UL class=related_posts_thumbnails> */
		//var related_posts = html.match(/<ul class=["']?related_posts_thumbnails["']?>[\s\S]+?<\/ul>/i);
		var related_posts = jQuery(el).find('ul.related_posts_thumbnails').html();
		html = html.split('<' + '!--more--' + '>');
		//if (related_posts) html[0] += related_posts[0];
		if (related_posts) html[0] += '<ul class="related_posts_thumbnails">' + related_posts + '</ul>';
		//if (html.length > 1) 
			//jQuery('.debug div').html('DEBUG: ' + '<textarea cols="100" rows="50">' + html[1] + '</textarea>');
		//else
			//jQuery('.debug div').html('DEBUG: ' + '<textarea cols="100" rows="50">' + html[0] + '</textarea>');
		var href = jQuery(el).find('.lcp_entry-title-thumb a').attr('href');
		jQuery(el).html(html[0] + (html.length > 1 ? '<a href="' + href + '">(weiterlesen...)</a>' : ''));
		jQuery(el).hover(function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		});
	});
	var lcp_catlists = jQuery(selector + ' #content ul.lcp_catlist')
	if (true) lcp_catlists.each(function (idx, el) {
		var lcp_catlist = jQuery(this),
			lcp_entries = lcp_catlist.find('li.lcp_entry');
		jQuery(this).css('min-height', lcp_entries.innerHeight() + 'px');
		setTimeout(function () {
			setInterval(function () {
				var current = lcp_entries.filter(':visible'),
					next = current.next();
				if (!current.hasClass('hover')) current.fadeOut(fadetime, function () {
					if (next.length) current = next;
					else current = lcp_entries.first();
					current.fadeIn(fadetime, function () {
						dynamic_sidebar_tags(get_related_posts_html(lcp_catlist.find('li.lcp_entry .lcp_content')));
					});
				});
			}, interval);
		}, (interval / lcp_catlists.length) * idx);
	});
};

var ajax_cache = {};

function ajax_show_img(uri, show) {
	jQuery('body, a').css('cursor', 'wait');
	jQuery.get('/lageplan', function(data) {
		ajax_cache[uri] = jQuery(data).find('.entry-content img').attr('src');
		if (show) jQuery.slimbox(ajax_cache[uri], 0, {});
		jQuery('body').css('cursor', 'auto');
		jQuery('a').css('cursor', 'pointer');
	});
};

jQuery(function () {
	/* Content-Fader Startseite */
	content_fader('body.home');
	content_fader('body.page-talente-ans-mikrofon');
	
	/* Lageplan-Popup */
	jQuery('#menu-item-385 a').click(function () {
		if (!ajax_cache[this.href]) ajax_show_img(this.href, true);
		else jQuery.slimbox(ajax_cache[this.href], 0, {});
		return false;
	});
	
	/* Archiv vor 2011 */
	jQuery('.widget_anual_archive ul').append('<li><a href="http://archiv.schowo.de/review/">2010 und älter</a></li>');
});

jQuery(window).load(function () {
	/* Galerie */
	jQuery('body.single-post .entry-content > .gallery').wrap('<div class="gallery" />');
	jQuery('body.single-post .entry-content > .gallery .gallery .gallery-item a').click(function () {
		var a = this,
			w = jQuery('body.single-post .entry-content > .gallery').width(),
			h = jQuery('body.single-post .entry-content > .gallery').height(),
			scroll_h = jQuery('body.single-post .entry-content > .gallery .gallery').height();
		if (jQuery('body').hasClass('category-mediathek') || jQuery('body').hasClass('category-talente-ans-mikrofon-mediathek') || jQuery('body').hasClass('category-sieger-und-ergebnisse')) {
			jQuery('body.single-post .entry-content').css('min-height', jQuery('body.single-post .entry-content').height());
			jQuery('body.single-post .entry-content > .gallery p.image-title').fadeOut(250, function () {
				jQuery('body.single-post .entry-content > .gallery p.image-title').remove();
			});
			if (jQuery('body.single-post .entry-content > p').length) {
				jQuery('body.single-post .entry-content > p').fadeOut(250, function () {
					jQuery('body.single-post .entry-content > p').remove();
					jQuery('body.single-post .entry-content').append(jQuery(a).parent().parent().find('.gallery-caption').html());
					gallery_swap(a);
				});
			}
			else {
				jQuery('body.single-post .entry-content').append(jQuery(a).parent().parent().find('.gallery-caption').html());
				gallery_swap(a);
			}
		}
		else gallery_swap(a);
		return false;
	});
	/* Scroll Galerie */
	var imgs = jQuery('body.single-post .entry-content > .gallery .gallery img');
	/* Mediathek Bildliste 2-spaltig */
	var is_doublerow = imgs.length * imgs.width() > jQuery('body.single-post .entry-content > .gallery').width() && (jQuery('body').hasClass('category-mediathek') || jQuery('body').hasClass('category-talente-ans-mikrofon-mediathek') || jQuery('body').hasClass('category-sieger-und-ergebnisse'));
	if (is_doublerow) {
		jQuery('body.single-post .entry-content > .gallery').height(jQuery('body.single-post .entry-content > .gallery').height() + imgs[0].offsetHeight);
		jQuery('body.single-post .entry-content > .gallery .gallery').height(imgs[0].offsetHeight * 2);
	}
	jQuery('body.single-post .entry-content > .gallery .gallery').width(imgs.width() * (imgs.length / (is_doublerow ? 2 : 1)));
	jQuery('body.single-post .entry-content > .gallery').jScrollPane({showArrows: true});
	/* Erstes Bild laden */
	jQuery('body.single-post .entry-content > .gallery .gallery .gallery-item:first-child a').click();
});

