$(document).ready(function(){
	
/* Fancybox **************************************/

	$('.fancybox').fancybox({
		'overlayColor' 	 : 'black',
		'overlayOpacity' : 0.65,
		'showNavArrows'  : true,
		'showCloseButton': true,
		'titleShow'		 : true,
		'cyclic'		 : true
	});

	
/* Header ****************************************/
	kopfbereich($('#header'));

/* Registerkarten ********************************/

	$('#sportarten .csc-default').each(function(){
		pageUrl  = document.location.toString();
		reglink  = $(this).find('a')[0].href;
			
		pageUrl = pageUrl.split('/');
		reglink = reglink.split('/');
			
		if(reglink[3] == pageUrl[3]) {
			$(this).find('img').css('margin-left','0');
			$(this).find('img').addClass('active');
		} else {
			$(this).find('img').addClass('notActive');
		}
	});
	
	catHover($('#sportarten .csc-default img.notActive'));
/* 	catHoverExt($('#startseite img'),'alt',$('#sportarten .csc-default img.notActive')); */
	catHoverExt($('#mainNavi li a'),'',$('#sportarten .csc-default img.notActive'));

/* News ******************************************/

	$('.cat img').each(function(){
		var zack = $(this).attr('src').split(' ');
		$(this).attr('src',zack[0]+zack[1]);
	});
	
	$('.simpleNewsListImage img').each(function(){
		var zack = $(this).attr('src').split(' ');
		$(this).attr('src',zack[0]+zack[1]);
	});
	
	$('.newsContent .L').each(function(){
		var img = $('.newsContent .R').find('img').parent();
		$(this).html($(this).html().replace('###bild###',img.html()));
		img.hide();
		$(this).find('img').parent().addClass('inlinePic');
	});

/* Startseite ************************************/

	$('#teaser .csc-default').each(function(){
		$(this).find('.csc-textpic-text').wrapInner('<div class="text">');
		$(this).find('h2').prependTo($(this).find('.csc-textpic-text'));
	});
	
	$('.eventsTermine').each(function(){
		/* Hockey-Liste */
		$(this).find('li.Hockey').wrapAll('<ul class="catList hockey"></ul>');
		$(this).find('.catList.hockey').wrap('<li></li>').before().prepend('<li class="catHeader hockey">Hockey Events:</li>');
		/* Tennis-Liste */
		$(this).find('li.Tennis').wrapAll('<ul class="catList tennis"></ul>');
		$(this).find('.catList.tennis').wrap('<li></li>').before().prepend('<li class="catHeader tennis">Tennis Events:</li>');
	});
	
	$('.catList li').each(function(){
		$(this).click(function(){
			document.location = $(this).find('a')[0].href;
		});
	});
	
	
/* News-Hover ************************************/

	$('.simpleNewsList li').hover(function(){
		$(this).addClass('active');
	},function(){
		$(this).removeClass('active');
	});
	
	$('.simpleNewsList li').click(function(){
		document.location = $(this).find('.simpleNewsListTitle a')[0].href;
	});

/* Accordion *************************************/

	$('.accordion h3').each(function(){
		$(this).siblings().wrapAll('<div></div>');
	});
	$('.accordion:first').before('<div class="theAccordion"></div>');
	$('.accordion').appendTo('.theAccordion');
	$('.theAccordion h3').unwrap();
	$(".theAccordion").accordion({header: 'h3', autoHeight: false, active: 'h3:first', collapsible: true});
	
	
/* Navi ******************************************/
	
	$('#mainNavi').find('li').find('.dropdown').hide();
	$('#mainNavi').find('li').hover(function(){
		$(this).find('.dropdown').css('opacity','0').show().animate({
			opacity: 1
		},200);
	},function() {
		$(this).find('.dropdown').animate({
			opacity: 0
		},0, function(){
			$(this).hide()
		});
	});
	$('#mainNavi li:last').css('border-right-width','0');


/* MailForm ***************************************/
	
	$('#mailForm').find('input, textarea, select').focus(function() {
		$(this).attr('style', '');
	});

/* Download-Box 
   ----------------------------------------------
	auch auf anderen Websites verwendbar
	<a href="datei.pdf">Beschreibung</a> (Größe)
   ----------------------------------------------*/
	
	$('.csc-downloadBox .fileDesc').each(function(){
		/* Reihenfolge des DOM festlegen */
		$(this).prev('br').remove();
		var prevSpan = $(this).prev('span');
		prevSpan.find('a').hide();
		$(this).wrapInner('<a href="' + prevSpan.find('a')[0].href + '"></a>');
		prevSpan.appendTo($(this));
		
		/* Hover (über Datei) */
		$(this).hover(function(){
			$(this).addClass('active');
		},function(){
			$(this).removeClass('active');
		});
		
		/* Click-Link */
		$(this).click(function(){
			document.location = $(this).find('a')[0].href;
		});
	});
	
/* Event-Listen (News) ***************************/

	$('.eventListe li').each(function(){
		$(this).hover(function(){
			$(this).addClass('active');
		},function(){
			$(this).removeClass('active');
		});
		
		$(this).click(function(){
			document.location = $(this).find('span.eventName a')[0].href;
		});
		
		if($(this).find('span.eventKategorie').text() == "Tennis Events") {
			$(this).addClass('tennis');
		}
		
		if($(this).find('span.eventKategorie').text() == "Hockey Events") {
			$(this).addClass('hockey');
		}
	});
	
	
/* Bilder-Größen anpassen
**************************************************/

	/* Allgemein */
	$('#contentLeft .textboxAboveLeft img').each(function(){
		if($(this).width() > 458) {
			$(this).height('');
			$(this).width('458px');
			$(this).css('height','auto');
		}
	});


	/* Gallerie Vorschaubilder */
	$('#content .galPrev img').each(function(){
		if($(this).width() > 195) {
			$(this).height('');
			$(this).width('195px');
			$(this).css('height','auto');
		}
	});
	
/*	Footer
**************************************************/

	$('#footer .socials img').each(function(){
		$(this).wrap('<a href="'+$(this).attr('longdesc')+'" target="_blank"></a>');
	});
	
});


function kopfbereich(element) {
	var headerBg  = element.find('#headerBg img');
	var headerTxt = element.find('#headerTxt .csc-default');
	var headerLg  = element.find('#headerLg img');
	
	element.css('background','url(' + headerBg.attr('src') + ') left top no-repeat');
	headerBg.hide();
}

function catHover(element) {
	element.hover(function() {
		$(this).animate({
	    	'margin-left': '0px'
	    },200);
	},function(){
	    $(this).animate({
	    	'margin-left': '-40px'
	    },200);
	});
}


function catHoverExt(hoverElement,hoverElementDefinition,actionPic) {
	hoverElement.each(function(){
		if(hoverElement[0].nodeName == "IMG") { 
			var imgTitle   = $(this).attr(hoverElementDefinition);
		} else if (hoverElement[0].nodeName == "A") {
			var imgTitle   = $(this).text();
		}
		var imgElement = $(this);
		actionPic.each(function(){
			var catTitle   = $(this).attr('alt');
			var catElement = $(this);
			if(catTitle == imgTitle) {
				imgElement.hover(function(){
					catElement.animate({
					    'margin-left': '0px'
					},200);
				},function(){
					catElement.animate({
						'margin-left': '-40px'
					},200);
				});
			}
		});
	});
}
