$(window).load(function() {

	initLayer();
	initSlideshow();
	initFontsize();

});

$(document).ready(function() {

	setOriginalElementDimensions();
	initLayout();
	$('nav.sub').find('li:contains("Club")').addClass('special');

});

$(window).resize(function(){

	initLayout();

});

function initLayout(){
	var i = $(window).width();
	var j = $('#content-left-col').height()+20;
	if (i < 724) {

		resizeContentElements(0.45);
		
		//$("#content img").attr("width", 600*factor).attr("height", 400*factor).parent().parent().removeAttr("style");
		//$("#content embed").attr("width", 600*factor).attr("height", 400*factor);
		
		$('#content-right-col').appendTo('#col-right');
		$('#content-left-col').insertAfter('#content-right-col');
	} else if (i <= 1024) {
		
		resizeContentElements(0.652);

		$('#content-right-col').appendTo('#col-left');
		$('#content-left-col').appendTo('#col-left');
		$('#col-left').css('paddingBottom','0');
	} else {

		resizeContentElements(0.999);
		
		$('#content-left-col').appendTo('#col-left');
		$('#content-right-col').appendTo('#col-right');
		$('#col-left').css('paddingBottom',j+'px');
	}
}

function resizeContentElements(factor) {
	var $content = $('#content');
	$('img', $content).each(function() {
		var $currentImg = $(this);
		$currentImg.attr("width", $currentImg.attr("org-width")*factor).attr("height", $currentImg.attr("org-height")*factor).parents('.csc-textpic-imagewrap, .csc-textpic-image').removeAttr("style");
	});
	
	$('embed', $content).each(function() {
		var $currentEmbed = $(this);
		$currentEmbed.attr("width", $currentEmbed.attr("org-width")*factor).attr("height", $currentEmbed.attr("org-height")*factor);
	});

	$('video', $content).each(function() {
		var $currentVid = $(this);
		$currentVid.attr("width", $currentVid.attr("org-width")*factor).attr("height", $currentVid.attr("org-height")*factor).removeAttr("style").parents('.video-js-box').removeAttr("style");
	});

	// $('.tx-wecmap-map', $content).each(function() {
	// 	var $currentMap = $(this);
	// 	$currentMap.css("width", $currentMap.attr("org-width")*factor).css("height", $currentMap.attr("org-height")*factor);
	// });

}

/**
 * Vergibt bestimmten Content Elementen die original Breite u. Höhe
 */
function setOriginalElementDimensions() {
	
	$('.csc-textpic img, embed, video', $('#content')).each(function() {
		
		var $currentElement = $(this);
		
		$currentElement
			.attr('org-width', $currentElement.attr('width'))
			.attr('org-height', $currentElement.attr('height'));
	});
}

function initSlideshow(){
	$('.slideshow').each(function(){
		$(this).append('<ul class="slides"></ul>');
		$(this).find('img').each(function(){
			if ($(this).parent('a').length!=0) {
				$(this).parent('a').wrap('<li></li>');
			} else {
				$(this).wrap('<li></li>');
			}
			if ($(this).attr('title')) {
				$('<p class="flex-caption">'+$(this).attr('title')+"</p>").insertAfter(this);
			}
		});
		$('.slideshow').find('li').appendTo('.slides', this);
		$(this).find('li').removeAttr('class').removeAttr('style').appendTo('.slides', this);
		$(this).find('img').removeAttr('width').removeAttr('height');
		$(this).find('div:not(.csc-header)').remove();
		$(this).toggleClass('slideshow flexslider');
		$('#content .flexslider').flexslider({animation:'slide',controlNav:false});
		$('#content-right-col .flexslider').flexslider({animation:'fade',controlNav:false,directionNav:false});
	});
}

function initLayer() {
	if(!isMobileDevive()){
		var LayerContent = $('#layer .csc-default').length;
		if($.cookie('layer')!='hide' && LayerContent!=0) {
			var i = $(window).width();
			var j = $(window).width();
			$('<div id="layer_overlay" style="width:'+i+'px;height:'+j+'px"></div>').insertAfter('#layer');
			$('<img id="layer_close" src="/fileadmin/tmpl/img/layer_close.png">').appendTo('#layer');
			$('body').css('overflow','hidden');
			var k = $('#layer').width();
			var l = $('#layer').height();
			$('#layer').css('marginLeft','-'+(k/2)+'px');
			$('#layer').css('marginTop','-'+(l/2)+'px');
			$('#layer').fadeIn();
			$('#layer_close, #layer_overlay').click(hideLayer);
			$.cookie('layer','hide',{expires:1,path:'/'});
			$(document).keyup(function(e) {
				if ((e.keyCode ? e.keyCode : e.which) == 27) {
					hideLayer();
				}
			});
		}
	}
}

function hideLayer() {
	$('#layer_overlay').fadeOut();
	$('#layer').fadeOut();
	$('body').css('overflow','auto');
}

function initFontsize() {
	$('#fnt_smaller').click(function(){setFontSize('smaller')});
	$('#fnt_default').click(function(){setFontSize('default')});
	$('#fnt_bigger').click(function(){setFontSize('bigger')});
	setFontSize('init');
}

function setFontSize(toSize) {
	var min=8;
	var max=20;
	var def=14;
	var step=1;
	if ($.cookie('fontsize')==null) {
		var size=14;
	} else {
		var size=parseInt($.cookie('fontsize').replace(/[^0-9]/g,""));
	}
	if( size < min || size > max) {
		size = def;
	}
	switch(toSize) {
		case 'smaller':
			size -= step;
			if(size < min) {
				size = min;
			}
		break;

		case 'bigger':
			size += step;
			if(size > max) {
				size = max;
			}
		break;

		case 'default':
			size = def;
		break;
	}
	$('body').css('font-size', size+"px");
	$.cookie('fontsize', size, { path: '/' });
}

/**
 * Prüft ob es sich bei dem Besucher um ein mobile OS handelt
 */
function isMobileDevive() {
	return /android|iphone|ipod|ipad|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent);
}

