
$(document).ready(function() {
	initPageBg();
	initHeaderBg();
	initHomeMap();
	initFGR();
	initFontSizer();
	initAccordions();
});


function initPageBg() {

	if ($('#pageBg').length > 0) {
		if ($('#pageBg').attr('value') != '') {
			$('body').css('background-image', 'url(' + $('#pageBg').attr('value') + ')');
		}
	}
}

var slideshow;
var activeimg = 0;
var numberimg = 0;
function initHeaderBg() {

	if ($('#headerBg').length > 0) {
		if ($('#headerBg').attr('value') != '') {

			var arImg = $('#headerBg').attr('value').split(',');
			var iNbImg = arImg.length;
			
			if (iNbImg < 2) {
				$('#header').css('background-image', 'url(uploads/pics/' + arImg[0] + ')');
			}
			else {
				$('#slogan').after('<div id="slideshow"></div>');
				$('#header > h1').css({
					'position': 'absolute',
					'z-index': 9999
				});

				numberimg = iNbImg;

				for (var i = 0; i < iNbImg; i++) {
					$('#slideshow').append('<img src="uploads/pics/' + arImg[i] + '" />');
				}

				$('#slideshow img:eq(0)').css('display', 'block');

				var slideshow = window.setInterval("doTheSlideshow()", 7500);
			}
		}
	}
}
function doTheSlideshow () {

	$('#slideshow img:eq(' + activeimg + ')').fadeOut();

	activeimg++;
	if (activeimg >= numberimg) {
		activeimg = 0;
	}

	$('#slideshow img:eq(' + activeimg + ')').fadeIn();
}

function initHomeMap() {

	if ($('#homemap').length > 0) {

		$('#contentWrap img').each(function() {

			var src = $(this).attr('src');
			var res = src.search(/fachbereich/);

			if (res != -1) {
				$(this).attr('id', 'homemapimg');
				$(this).attr('usemap', '#homemap');
				$(this).parent().css('position', 'relative');
			}
		});

		$('#homemap area').each(function() {

			var arCoords = $('#' + $(this).attr('id') + 'Btn').val().split(',');
			$('#homemapimg').after('<img id="' + $(this).attr('id') + 'RoundBtn" src="fileadmin/sportklinik/img/btn-round-blue.png" style="position: absolute; top: ' + arCoords[1] + 'px; left: ' + arCoords[0] + 'px; cursor: pointer;" />');

			var objId = $(this).attr('id');

			$('#' + $(this).attr('id') + 'RoundBtn').click(function() {
				document.location.href = $('#' + objId).attr('href');
			});
			$('#' + $(this).attr('id') + 'RoundBtn, #' + $(this).attr('id')).mouseover(function() {
				$('#' + objId + 'RoundBtn').attr('src', 'fileadmin/sportklinik/img/btn-round-green.png');
			});
			$('#' + $(this).attr('id') + 'RoundBtn, #' + $(this).attr('id')).mouseout(function() {
				$('#' + objId + 'RoundBtn').attr('src', 'fileadmin/sportklinik/img/btn-round-blue.png');
			});
		});
	}
}

function initFGR() {

	if ($('#fivegoodreasonsmap').length > 0) {

		$('#contentWrap img').each(function() {

			var src = $(this).attr('src');
			var res = src.search(/gruende/);

			if (res != -1) {
				$(this).attr('id', 'fivegoodreasonsimg');
				$(this).attr('usemap', '#fivegoodreasonsmap');
			}
		});

		$('#fivegoodreasonsmap area').each(function() {

			$(this).click(function(e) {

				e.preventDefault();

				var arIdx = $(this).attr('href').split('#');
				var api = $('#accordion1').data('tabs');

				api.click(parseInt(arIdx[1]));
			});
		});
	}
}

function initFontSizer() {

	var container = '#contentWrap, #contentWrap a';
	var containerHeadlines = '#contentWrap h2';

	if ($.cookie('stgtskFontSize') == 15) {
		$('.fontMiddle').css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 15);
		$(containerHeadlines).css('font-size', 23);		
	}
	else if ($.cookie('stgtskFontSize') == 18) {
		$('.fontBig').css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 18);
		$(containerHeadlines).css('font-size', 26);
	}
	else {
		$('.fontStandard').css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 12);
		$(containerHeadlines).css('font-size', 20);
	}

	$('.fontStandard').click(function() {
		$('.fontsizerBtn').css('border-bottom', 'none');
		$(this).css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 12);
		$(containerHeadlines).css('font-size', 20);
		$.cookie('stgtskFontSize', '12', { expires: 7, path: '/' });
	});

	$('.fontMiddle').click(function() {
		$('.fontsizerBtn').css('border-bottom', 'none');
		$(this).css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 15);
		$(containerHeadlines).css('font-size', 23);
		$.cookie('stgtskFontSize', '15', { expires: 7, path: '/' });
	});

	$('.fontBig').click(function() {
		$('.fontsizerBtn').css('border-bottom', 'none');
		$(this).css('border-bottom', '1px #fff solid');
		$(container).css('font-size', 18);
		$(containerHeadlines).css('font-size', 26);
		$.cookie('stgtskFontSize', '18', { expires: 7, path: '/' });
	});
}

var iAccCnt = 1;
var arAccEl = new Array();
var iCntSubAcc = 0;
var iIdxCorrect = 0;
function initAccordions() {

	if ($('#contentWrap div.accordion').length > 0) {

		$('#contentWrap > div').each(function(idx) {

			if ($(this).hasClass('accordion')) {
				arAccEl[iCntSubAcc] = idx;
				iCntSubAcc++;
			}
			else {

				buildSingleAccordion();

				arAccEl = new Array();
				iCntSubAcc = 0;
			}
		});

		buildSingleAccordion();
	}
}

function buildSingleAccordion() {

	if (arAccEl.length > 0) {

		var iNbEl = arAccEl.length;
		var arSelektor = new Array;
		for (var i = 0; i < iNbEl; i++) {
			arSelektor[i] = '#contentWrap > div:eq(' + (arAccEl[i] - iIdxCorrect) + ')';
		}

		iIdxCorrect = iIdxCorrect + (iNbEl - 1);
		$(arSelektor.join(',')).wrapAll('<div id="accordion' + iAccCnt + '"></div>');

		$('#accordion' + iAccCnt).tabs('#accordion' + iAccCnt + ' div.accPanel', {
			tabs: 'h3',
			effect: 'slide',
			initialIndex: 0
		});
		
		iAccCnt++;
	}
}

