$(document).ready(function()
{
	
	$('.col_1_1 .wc_artikelliste img').wrap('<div class="clip_first" />');
	
	$('.wc_comments .inner .mid').replaceWith($('.article_comments'));
	$('.wc_comments').closest('.area').addClass('wc_comments_area');
	
	// FORMAT AUTHOR IN BLOCKQUOTES
	$('.wc_artikel blockquote p br').after('<span class="blockquote_name">');
	$('.wc_artikel blockquote p span').each(function() {
	   var $this = $(this);
	   var textEl = this.nextSibling;
	   var spanEl = $('<span />').text(textEl.data);
	   $this.append(spanEl);
	   $(textEl).remove();
	});
	
	// ADD LOAD NEW CONTENT FUNTIONALITY TO .wc_galerieliste_horizontal
	init_horizontal_gallery();
	
	// MAKE IMAGES CLICKABLE
	init_clickable_images();
	
	// ADD POS_1 CLASS TO MENU ITEM IF SUBMENU
	if(!$('#menu').find('a.menu_1').length){
		$('#menu').find('a.menu_0').each(function(){
			var menu_0 = $(this);
			var menu_0_path= menu_0.attr('href').replace(/\//g, '');
			var cur_path = document.location.pathname.replace(/\//g, '');
			var pattern = new RegExp('^' + menu_0_path);
			if(cur_path != '' && menu_0_path != '' && cur_path.match(pattern)){
				menu_0.removeClass('menu_0').addClass('menu_1');
				return false;
			}
		});
	}
	
	$('.col_1_2_eq .article_pagination').find('.next_page a').html('»');
	$('.col_1_2_eq .article_pagination').find('.prev_page a').html('«');
});

function article_rotation_loaded(module_pos){
	if($('#' + module_pos).find('.article_rotation').length){
		$('#' + module_pos).find('.article_rotation .article').each(function(){
			var article = $(this);
			if(!$(article).find('.deck').length){
				if($(article).parent().find('.slide_topic').length){
					var deck_txt = $(article).parent().find('.slide_topic').html();
					var deck = '<h4 class="deck">' + deck_txt + '</h4>';
					$(deck).insertBefore($(article).find('h2'));
				}
			}
		});
	}
}

function init_clickable_images(){
	$('.article').each(function() {
		if($(this).find('h2 a').length){
			var img_link = $(this).find('h2 a').attr('href');
			$(this).find('.article_photo img').wrap('<a href="' + img_link + '"></a>');
		}
	});
}

function init_horizontal_gallery(){
	if($('.wc_galerieliste_horizontal').length){
		$('.wc_galerieliste_horizontal').each(function(){
			var gal_bar = $(this);
			if(gal_bar.find('.article_pagination').length){
				gal_bar.find('.article_pagination a').each(function(){
					var a = $(this);
					var href = a.attr('href');
					
					if(a.parent().hasClass('next_page')) a.html("&raquo;");
					if(a.parent().hasClass('prev_page')) a.html("&laquo;");
					
					
					var params = {};
					var e,
							add = /\+/g,  // Regex for replacing addition symbol with a space
							r = /([^&=]+)=?([^&]*)/g,
							d = function (s) { return decodeURIComponent(s.replace(add, " ")); },
							query = href.substring(href.lastIndexOf('?') + 1);
					
			    while(e = r.exec(query)){
						params[d(e[1])] = d(e[2]);
					}
					
					if(params['p'] !== undefined && params['mid'] !== undefined){
						a.attr('href', '#');
						a.click(function(event){
							event.preventDefault();
							
							$.get('/index.php?ax=1&articles_multi=1&mid=' + params['mid'] +  '&p=' + params['p'], function(data){
								gal_bar.find('.mid').html(data);
								init_horizontal_gallery();
								init_clickable_images();
							});
							return false;
						});
					}
				});
			}
		})
	}
}
