$(function () {
	// style for biz forms
	$(".FormPanel tr:nth-child(even) td").css({ "background-color": "#f7f7f7" });

	// downloads: init viewAll-buttons
	$(".DownloadBigMain .downloadsProducts .viewAll").click(function (ev) {
		ev.preventDefault();
		$(this).parents(".product").find(".productInner");
	});

	// draw background of right part of headline if right container is empty
	if ($(".ZoneMainControlRight").length == 1 && $.trim($(".ZoneMainControlRight").html()) == "") {
		$(".ZoneMainControlRight").html('<span class="fake_right_headline" />');
	}

	// vertical align of text in these buttons (news-page); french = 2 lines of text
	$(".dvNewsRight .ControlNewsButtons a span").each(function () {
		$(this).css({ "top": 19 - ($(this).height() / 2) });
	});
});

function showHideChangePassword() {
	showHideOverlay("dvChangePassword", "dvChangePasswordBG");
}

function showHideTrainingSelect() {
	showHideOverlay("dvTrainingSelect", "dvTrainingSelectBG");
}

function showDownloadSendEmailSelect() {

	var ov = $("#DownloadSendEmailSelect");
	var bg = $(".DownloadSendEmailSelectBG");

	ov.show();
	bg.show();
}

function hideDownloadSendEmailSelect() {

	var ov = $("#DownloadSendEmailSelect");
	var bg = $(".DownloadSendEmailSelectBG");


	ov.hide();
	bg.hide();
}


function showHideExamFeedback() {
	showHideOverlay("dvExamFeedback", "dvExamFeedbackBG");
}

function showHideOverlay(overlay, background) {
	var ov = $("." + overlay);
	var bg = $("." + background);

	if (ov.css("display") == "none") {
		ov.show();
		bg.show();
	} else {
		ov.hide();
		bg.hide();
	}
}

function openTNbedingungen() {
	try {
		var popTN = window.open("/Special-Pages/Terms.aspx", "TNbedingungen", "width=920,height=520,scrollbars");
		popTN.focus();
	} catch (ex) {
		alert("Dieser Link öffnet ein Popup.\nBitte schalten Sie Ihren Popup-Blocker aus und versuchen Sie es erneut.")
	}
}

// available trainings, profile

(function ($) {
	$(window).load(function () {
		// make some of the thumbs grayscale
		if (!$.browser.msie && !!document.createElement('canvas').getContext) {
			$(".trainingsThumbs ul li a.gray img").each(function () {
				grayscale(this, true);
				$(this).hide();
			});
		}
	});
})(jQuery);

$(function () {
	$(".availableTrainings")
		.delegate(".trainingsThumbs ul li a", "click", function (e) {
			// trainingsInfo
			e.preventDefault();
			var index = $(this).parents("li").index(),
				opened_element = $(this).parents(".availableTrainings").find(".trainingsInfo li:visible").index(),
				availableTrainingsContainer = $(this).parents(".availableTrainings");
			if (index === opened_element) {
				$(availableTrainingsContainer).find(".trainingsInfo li:visible").slideUp(300);
			} else {
				if ($(availableTrainingsContainer).find(".trainingsInfo li:visible").length > 0) {
					$(availableTrainingsContainer).find(".trainingsInfo li:visible").slideUp(300, function () { $(this).parent().find("li").eq(index).slideDown(300); });
				} else {
					$(availableTrainingsContainer).find(".trainingsInfo").find("li").eq(index).slideDown(300);
				}
			}
		})
		.delegate(".trainingsInfo li a.btnClose", "click", function (e) {
			e.preventDefault();
			var availableTrainingsContainer = $(this).parents(".availableTrainings");
			$(availableTrainingsContainer).find(".trainingsInfo li:visible").slideUp(300)
		});

	// change password button
	$(".profileForm .displayPasswordForm").click(function (e) {
		e.preventDefault();
		var contentBoxInnerText_top = $(".contentBoxInnerText").offset().top,
			button_top = $(this).offset().top;
		$(".dvChangePassword").css("top", (button_top - contentBoxInnerText_top - 100)).show();
		$(".dvChangePasswordBG").css("top", (button_top - contentBoxInnerText_top - 200)).show();
	});
	$(".dvChangePasswordBG, .dvChangePassword h3 a").click(function (e) {
		e.preventDefault();
		$(".dvChangePassword").hide();
		$(".dvChangePasswordBG").hide();
	});
});


$(function () {
	var handler = function (e) {
		var p = jQuery(this).parents('.product');
		p.find('.extended').slideToggle();
		p.find('a.viewAll').toggle();
		p.find('a.hideAll').toggle();

		e.preventDefault();
	};

	$('a.viewAll').click(handler);
	$('a.hideAll').click(handler);

	$('.productInner .extended').attr('style', 'display:none;');
});


// FORUM SCRIPTS
$(function () {
	if ($(".ZoneForum").length < 1) {
		return;
	}

	// if .Header is before .Info - swap position of these
	if ($(".ZoneForum .Forum .Table .Header").next().hasClass("Info")) {
		$(".ZoneForum .Forum .Table .Info").insertBefore($(".ZoneForum .Forum .Table .Header"));
	}

	// every even line has a darker background color
	$(".ZoneForum .Forum .Table .Forum:even, .ZoneForum .Forum .Table .Thread:even, .ForumFlat .Table .PostsTable .Post:even").addClass("ForumDark");

	// if no table row .Actions, then span in .Info becomes higher
	if ($(".ZoneForum .Forum .Table .Actions").length < 1) {
		$(".ZoneForum .Forum .Table .Info .GroupName").css("margin-bottom", 37);
	}

	// span in this td have no classes
	$(".ZoneForum .ForumFlat .Table .ForumContent .Thread .LastPost").each(function () {
		$("span:even", $(this)).addClass("PostUser");
		$("span:odd", $(this)).addClass("PostTime");
	});

	// hide paging if only one page
	if ($(".ZoneForum .Table .Pager td a").length < 1) {
		$(".ZoneForum .Table .Pager").hide();
	}

	// hide first table row of post (is empty)
	$(".ForumContent .Posts .Post table").find("tr:eq(0)").hide();

	// reply form
	$(".ForumContent .ForumNewPost .Title:eq(0)").addClass("ActionTitle");
	$("table.PostImage td:eq(0)").css({ "width": 10, "padding": 0 });
	$(".FormPadding .PostForm").attr("cellspacing", "0");
	$(".FormPadding .PostForm tr:eq(0), .FormPadding .PostForm tr:eq(2)").addClass("even");
	$(".FormPadding .PostForm tr:eq(1)").addClass("odd");
	$(".FormPadding .PostForm tr:eq(0) td").css("line-height", "25px");
	$(".PostForm .TextboxItem, .PostForm .TextboxItemShort").wrap("<div class='TextboxItemContainer' />");
	$(".PostForm .TextareaItem").wrap("<div class='TextareaItemContainer' />");
	$(".PostForm .SubmitButton:eq(0)").addClass("btn_ok").parents("td").css({ "width": 383, "padding-right": 261, "overflow": "hidden" });
	$(".PostForm .SubmitButton:eq(1)").addClass("btn_cancel");
	$(".PostForm .SubmitButton:eq(2)").addClass("btn_preview");
});
