// Author:              Matt Rossi
// Website:             ifohdesigns.com
// Article Source:      http://ifohdesigns.com/blog/tutorials/
// Last Modified:       August 26, 2008


// IF YOU WISH TO USE THIS SCRIPT I WOULD APPRECIATE A BACKLINK

$(document).ready(function() {
	$("a.iframe").fancybox({
		'width': 450,
		'height': 90
	}).trigger('click');
	//$(this).find('.dropdown')
	$('#navigation ul li').hoverIntent({
		over: function() {$(this).find('.navigation_sublist').fadeIn(100);},
		out: function() {$(this).find('.navigation_sublist').fadeOut(100);}
	});

	$('#zeigLigen').hoverIntent({
		over: function() {$(this).find('.alleLigen').fadeIn(100);},
		out: function() {$(this).find('.alleLigen').fadeOut(100);}
	});
	$('#zeigeSpieltage').hoverIntent({
		over: function() {$(this).find('.alleSpieltage').fadeIn(100);},
		out: function() {$(this).find('.alleSpieltage').fadeOut(100);}
	});

	$("a#tabelle_ajax").live("click",function(){
		var toLoad = $(this).attr('href');  
		$('#LigaTabelleAjax').fadeOut(100).load(toLoad).fadeIn(400);
		return false;  
	});  
	$("a#team_ajax").live("click",function(){
		var toLoad = $(this).attr('href');  
		$('#LigaTeamAjax').fadeOut(100).load(toLoad).fadeIn(400);
		return false;  
	});
	/*

	$("a#kalender_next").live("click",function(){
		var toLoad = $(this).attr('href');  

		$('#bulli_kalender').fadeTo(50,0,loadContent);

		function loadContent() {
		$('#kalender_load').load(toLoad,showNewContent);
		}
		function showNewContent() {
		$('#bulli_kalender').fadeTo('fast',100);
		}
		return false;  
	});  
	$("a#kalender_prev").live("click",function(){
		var toLoad = $(this).attr('href');  

		$('#bulli_kalender').fadeTo(50,0,loadContent);

		function loadContent() {
		$('#kalender_load').load(toLoad,showNewContent);
		}
		function showNewContent() {
		$('#bulli_kalender').fadeTo('fast',100);
		}
		return false;  
	});*/
	
	$("#txtInput").autoGrow();
	
   	$("a.bildlink").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over'
	});
	
	$("a.bildgalerie").attr('rel', 'gallery').fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Bild ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	
	$("#head_news").hide();

	var a = $('<a id="head_control_btn"></a>').attr('href','#').addClass("btn-slide");
	$('#news_control').append(a);


	$(".btn-slide").click(function(){

	  if ($("#head_news").is(":hidden")) {
		$("#head_news").fadeIn("slow");
		$("#content_mid_layer").animate({ top: "455px"}, 400 );
		$.cookie('showTop', 'collapsed',{ expires: 14 });
		$("#content").removeClass("content_active");
		$("#content").addClass("content_normal");
		$(this).addClass("active");

	return false;
	} else {
		$("#head_news").fadeOut("slow");
		$(this).removeClass("active");
		$(this).addClass("");
		$("#content_mid_layer").animate({ top:"151px"}, 400 );
		$("#content").removeClass("content_normal");
		$("#content").addClass("content_active");
		$.cookie('showTop', 'expanded',{ expires: 14 });
	return false;
	  }

	});

	// COOKIES
	// Header State
	var showTop = $.cookie('showTop');

	// Set the user's selection for the Header State
	if (showTop == 'expanded') {
		//geschlossen
		$("#content_mid_layer").css("top","151px");
		//$("#head_news").fadeOut(200);
		$(".btn-slide").removeClass("active");
		$("#content").removeClass("content_normal");
		$("#content").addClass("content_active");
	}
	else{
		//geoeffnet
		$("#content_mid_layer").css("top","455px");
		$("#head_news").fadeIn(0);
		$(".btn-slide").addClass("active");
	}

	
	
	
	
	
	/*Fuer den Kalender die Animation*/
	
	$('.date_has_event').each(function () {
		// options
		var distance = 10;
		var time = 200;
		var hideDelay = 200;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $(this);
		var popup = $('.events ul', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					bottom: 20,
					left: -90,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
});
