var FST = {};

document.documentElement.className = 'js';

$(document).ready(function() {
	var h = $('#content').height();
	$('.left').css('height',h + 'px');
	if(h > 320) {
		$('#menu').css('height',h - 40 + 'px');
	}
	$('#trainings_bremen').each(function() {
		FST.init_tables();
	});
	$('.has_tooltip').Tooltip();
	$(".lightbox").lightbox();
	$('#submenu li:last-child').addClass('last');
//	$('.events tr:even').addClass('odd');
});

FST.init_tables = function() {
	var loc = readCookie('adac_wem_prefered_location') || 'bremen';
	if('aurich' == loc) {
		show_aurich();
	} else {
		show_bremen();
	}
	$('#opener_bremen').click(function() {
		show_bremen();
	});
	$('#opener_aurich').click(function() {
		show_aurich();
	});
};

function show_bremen() {
	$('#trainings_aurich').hide();
	$('#trainings_bremen').show();
	$('#opener_bremen').addClass('current');
	$('#opener_aurich').removeClass('current');
	createCookie('adac_wem_prefered_location', 'bremen',30);
};

function show_aurich() {
	$('#trainings_aurich').show();
	$('#trainings_bremen').hide();
	$('#opener_bremen').removeClass('current');
	$('#opener_aurich').addClass('current');
	createCookie('adac_wem_prefered_location', 'aurich',30);
};

$.fn.Tooltip = function() {
	this.mouseover(function(e) {
		if((!this.title && !this.alt) && !this.tooltip_is_set) return;
		var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
		var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
		mouseX += 16;
		mouseY += 10;
		if(!this.tooltipdiv) {
			var div = document.createElement("div");
			this.tooltipdiv = div;
			this.tooltipdiv.className = 'tooltip';
			$(div).html((this.title || this.alt));
			this.title = "";
			this.alt   = "";
			$("body").append(div);
			this.tooltip_is_set = true;
		}
		$(this.tooltipdiv).show().css({left: mouseX + "px", top: mouseY + "px"});
	}
	).mouseout(function() {
		if(this.tooltipdiv) {
			$(this.tooltipdiv).hide();
		}
	});
	return this;
};
	
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	var expires = "";
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	};
	document.cookie = name + "=" + value + expires + "; path=/";
};

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		};
	};
	return null;
};

function eraseCookie(name) {
	createCookie(name,"",-1);
};
