﻿/****************************
OTHERPAGES
****************************/
function otherPageWait(href, position)
{
	$(document).ready(function ()
	{
		$.fancybox.showActivity();
		var navRight = $('div.otherPagesLinkList').html();
		var posLink = $('#containerOtherPagesLinkList').offset();
		$("#fancybox-wrap").addClass("otherPagesFancyBoxWrap");
		$("#fancybox-content").addClass("otherPagesFancyBoxContent")
		$("#fancybox-content").children().addClass("otherPagesFancyBoxContentChild");

		$.ajax(
	  {
	  	type: "get",
	  	url: href + "?requestlayout=otherpage",
	  	dataType: "html",
	  	async: false,
	  	success: function (html)
	  	{
	  		$('div.otherPagesFirstColDiv').html(html);
	  		$('div.otherPagesSecondColDiv').html(navRight);
	  		var resultContent = $('div.otherPagesView').html();

	  		$.fancybox(resultContent, {
	  			'overlayColor': '#000000',
	  			'autoScale': false,
	  			'autoDimensions': false,
	  			'transitionIn': 'elastic',
	  			'transitionOut': 'elastic',
	  			'speedIn': 1,
	  			'speedOut': 1,
	  			'orig': $("div.page_margins"),
	  			'onStart': function ()
	  			{
	  				$('body').css('cursor', 'wait');
	  			},
	  			'onComplete': function ()
	  			{

	  				var scrollPosTop = $(document).scrollTop();
	  				$("#fancybox-wrap").removeClass("otherPagesFancyBoxWrap").addClass("otherPagesFancyBoxWrapVisible");
	  				$("#fancybox-content").addClass("otherPagesFancyBoxContent otherPagesBoxHeight otherPagesBoxContentWidth");
	  				$("#fancybox-close").css({ 'display': 'none' });

	  				var contentWidth = $("#fancybox-content").width();
	  				var contentHeight = $("#fancybox-content").height();
	  				var startWidth = 200;
	  				var startMargin = contentWidth - startWidth;

	  				$("#fancybox-content").children().css({ 'width': startWidth + 'px', 'height': contentHeight + 'px', 'overflow': 'visible' });

	  				$("#fancybox-wrap").css({ 'width': startWidth + 'px', 'margin-left': startMargin + 'px', 'height': contentHeight + 'px', 'top': posLink.top });
	  				$("#fancybox-wrap").animate({ top: posLink.top + "px" }, { queue: false, duration: 800, complete: function () { } });


	  				$("#fancybox-wrap").animate({ width: contentWidth + "px", marginLeft: "0px" }, { queue: false, duration: 1500, complete: function () { $('body').css('cursor', 'default'); $("#fancybox-close").css({ 'display': 'inline' }); } });
	  				$("#fancybox-content").children().animate({ width: contentWidth + "px" }, { queue: false, duration: 1500,
	  					complete: function ()
	  					{
	  						if ($("#fancybox-wrap").width() < contentWidth)
	  						{
	  							$("#fancybox-wrap").animate({ top: posLink.top + "px" }, { queue: false, duration: 1000, complete: function () { } });
	  							$("#fancybox-wrap").animate({ width: contentWidth + "px", marginLeft: "0px" }, { queue: false, duration: 1000, complete: function () { $('body').css('cursor', 'default'); $("#fancybox-close").css({ 'display': 'inline' }); } });
	  						}
	  					}
	  				});

	  			},
	  			'onClosed': function ()
	  			{
	  				$("#fancybox-wrap").removeClass("otherPagesFancyBoxWrap otherPagesFancyBoxWrapVisible");
	  				$("#fancybox-content").removeClass("otherPagesFancyBoxContent otherPagesBoxHeight otherPagesBoxContentWidth");
	  				$("#fancybox-content").children().removeClass("otherPagesFancyBoxContentChild");
	  			}

	  		});
	  	},
	  	complete: function ()
	  	{
	  		$("td.otherPagesSecondCol a").removeClass("button_otherPages_active").addClass("NButton otherPagesp");
	  		$('a.otherbutton_' + position).removeClass("NButton otherPagesp").addClass("button_otherPages_active");
	  	}

	  });
	});
}
function showOtherPage(href, position, isNotCalledFromThisPage)
{
	$('body').css('cursor', 'wait');
	if (isNotCalledFromThisPage == 1)
		window.setTimeout(function () { otherPageWait(href, position) }, 3000);
	else
		otherPageWait(href, position);
}

function showOtherPageInner(href, obj)
{
	if (obj != null)
	{
		$("td.otherPagesSecondCol a").removeClass("button_otherPages_active").addClass("NButton otherPagesp");
		$(obj).removeClass("NButton otherPagesp").addClass("button_otherPages_active");
	}
	$.ajax(
	{
		type: "get",
		url: href + "?requestlayout=otherpage",
		dataType: "html",
		async: false,
		success: function (html)
		{
			$('div.otherPagesFirstColDiv').html(html);
		}
	});
}


//*** every <infoarea type="accordion"> calls infoAreaAccordionJQuery and registers the events and so code will be used n times. This variable prevents this. ***
var infoAreaAccordionAlreadyInitialized = false;

/****************************
<INFOAREALIST TYPE="ACCORDION">
<INFOAREA></INFOAREA>
</INFOAREALIST>
****************************/
function infoAreaAccordionHide(context, slide)
{
	context = $(context);
	context.data("open", false);
	if (slide)
		$(".infoarea_accordion_content", context).slideUp();
	else
		$(".infoarea_accordion_content", context).hide();
	$(".infoarea_accordion_teaser", context).addClass("infoarea_accordion_teaser_inactive").removeClass("infoarea_accordion_teaser_active  gradient_faircolor");
	infoAreaAccordionChgImage(context, plusImage);
	$(".infoarea_accordion_teaser", context).css("cursor", "pointer");
}

function infoAreaAccordionShow(context, slide)
{
	context = $(context);
	context.data("open", true);
	if (slide)
		$(".infoarea_accordion_content", context).slideDown();
	else
		$(".infoarea_accordion_content", context).show();
	$(".infoarea_accordion_teaser", context).addClass("infoarea_accordion_teaser_active gradient_faircolor").removeClass("infoarea_accordion_teaser_inactive");
	infoAreaAccordionChgImage(context, minusImage);
	$(".infoarea_accordion_teaser", context).css("cursor", "pointer");
}

function infoAreaAccordionHoverIn(context)
{
	context = $(context);
	$(".infoarea_accordion_teaser", context).addClass("infoarea_accordion_teaser_active gradient_faircolor").removeClass("infoarea_accordion_teaser_inactive");
	infoAreaAccordionChgImage(context, plusImage);
}

function infoAreaAccordionHoverOut(context)
{
	context = $(context);
	$(".infoarea_accordion_teaser", context).addClass("infoarea_accordion_teaser_inactive").removeClass("infoarea_accordion_teaser_active gradient_faircolor");
	infoAreaAccordionChgImage(context, plusImage);
}

function infoAreaAccordionChgImage(context, newimage)
{
	$(".infoarea_accordion_teaser img", context).attr("src", newimage);
}

function infoAreaAccordionCalculateHeight(context)
{ //context = list
	context = $(context);
	var maxHeight = 0;
	$(".infoarea_accordion", context).each(function (index)
	{
		var height = $(this).height();
		if (maxHeight < height)
		{
			maxHeight = height;
		}
	});
	context.height(maxHeight + (($(".infoarea_accordion_teaser", context).length - 1) * $(".infoarea_accordion_teaser", context).height()));
}

function infoAreaAccordionIsOpen(context)
{
	context = $(context);
	if (context.data("open") == true)
		return true;
	else
		return false;
}

function infoAreaAccordionJQuery()
{
	if (!infoAreaAccordionAlreadyInitialized)
	{
		infoAreaAccordionAlreadyInitialized = true;

		$(document).ready(function ()
		{
			$(".infoarea_accordion_list").each(function (index)
			{
				var list = $(this);
				var items = $(".infoarea_accordion", list);

				//infoAreaAccordionCalculateHeight(list);
				infoAreaAccordionHide(items);
				infoAreaAccordionShow($(".accordion-isopen", list), false);

				items.mouseenter(function ()
				{
					var context = $(this);
					if (!infoAreaAccordionIsOpen(context))
					{
						infoAreaAccordionHoverIn(context);
					}
				});

				items.mouseleave(function ()
				{
					var context = $(this);
					if (!infoAreaAccordionIsOpen(context))
					{
						infoAreaAccordionHoverOut(context);
					}
				});

				items.find(".infoarea_accordion_teaser").click(function ()
				{
					var context = $(this).parents(".infoarea_accordion");
					if (!infoAreaAccordionIsOpen(context))
					{
						//infoAreaAccordionHide(items, true);
						infoAreaAccordionShow(context, true);
					} else
					{
						infoAreaAccordionHide(context, true);
					}
				});
			});
		});
	}
}


/****************************
FILELANGUAGEINFO (FILE FOR LANGUAGES WITH NO COMPLETE WEBSITE)
****************************/
function filelistLanguageJQuery()
{
	$(document).ready(function ()
	{
		var shortInfoLanguageContent = $(".shortinfofilelanguage");

		shortInfoLanguageContent.each(function ()
		{
			var item = $(this);
			item.attr('href', item.data('href'));
		});

		//*** ajax reload info for language and display (to shorten the sourcecode for 99,9% of the people who do not want to read this) ***
		$(shortInfoLanguageContent).click(function ()
		{
			var language = $(this).data("language");

			$.ajax(
			{
				type: "get",
				url: "/FileLanguageInfo.ashx?language=" + escape(language),
				dataType: "html",
				async: false,
				success: function (html)
				{
					$("#filelistlanguageInner").html(html);
				}
			});
		});
	});
}

/****************************
SHORTINFO
****************************/
function fairInfoJQuery()
{
	$(".Fairinfo .fairinfo_button .expandbutton").show();

	$(window).load(function ()
	{
		var finfo = $(".Fairinfobox");
		var speed = 500;
		var height = 0;

		// Klickbox einblenden, Höhe berechnen und wieder ausblenden
		finfo.css("height", "auto");
		height = parseInt(finfo.height());
		finfo.css("height", "0px");


		var toggle = function ()
		{
			if (parseInt(finfo.height()) > 0)
			{
				hide();
			} else
			{
				show();
			}
		};

		var show = function ()
		{
			finfo.animate({
				height: height + "px"
			}, speed, function ()
			{
				$("#shortinfobuttonclose", finfo).parent("div").fadeIn("fast");
			});

			$(".Fairinfo .fairinfo_button .expandbutton #shortinfobuttonswitch").attr("src", upImage);
		};

		var hide = function ()
		{
			$("#shortinfobuttonclose", finfo).parent("div").fadeOut("fast");

			finfo.animate({
				height: "0px"
			}, speed);

			$(".Fairinfo .fairinfo_button .expandbutton #shortinfobuttonswitch").attr("src", downImage);
		};

		$(".Fairinfo .fairinfo_button .expandbutton").click(toggle);
		$("#shortinfobuttonclose", finfo).click(toggle);
	});
}

/***************************
PAGEHERO
****************************/
(function ($)
{
	$.fn.nxPageHero = function ()
	{
		return this.each(function ()
		{
			var $this = $(this);
			if ($this.data("pageherocontrol") == null)
			{
				$this.data("pageherocontrol", new PageHeroControl($this, { arrowSlideOnly: false }));
			}
		});
	};

	var PageHeroControl = function (element, settings)
	{
		var control = this;
		control.element = element;
		control.settings = settings;
		control.teasers = new Array();
		control.movelocked = false;

		control.fillTeasers = function ()
		{
			control.teasers = new Array();
			control.element.children(".teaser").each(function ()
			{
				var teaser = $(this);
				if (!teaser.hasClass("colored"))
				{
					control.teasers.push(teaser);
				}
			});
			control.teasers = $(control.teasers);
		};

		control.teaserClose = function (teasers, animate)
		{
			$(teasers).each(function ()
			{
				var teaser = $(this);
				if (!animate)
				{
					teaser.removeClass("stateOpened stateClosed").addClass("stateClosed");
					teaser.children(".arrow").css("backgroundImage", "url('" + upImageSolid + "')");
				} else
				{
					teaser.animate({ //TODO: Konfigurierbar
						height: "138px",
						marginTop: "-138px"
					}, "slow", function ()
					{
						teaser.removeClass("stateOpened stateClosed").addClass("stateClosed");
						teaser.children(".arrow").css("backgroundImage", "url('" + upImageSolid + "')");
					});
				}

				teaser.find(".teaserContent").animate({
					height: "106px"
				}, "slow");
			});
		};

		control.teaserOpen = function (teasers, animate)
		{
			$(teasers).each(function ()
			{
				var teaser = $(this);
				if (!animate)
				{
					teaser.removeClass("stateOpened stateClosed").addClass("stateOpened");
					teaser.children(".arrow").css("backgroundImage", "url('" + downImageSolid + "')");
				} else
				{
					if (teaser.data("slideInProgress") == null || teaser.data("slideInProgress") == false)
					{
						teaser.data("slideInProgress", true);
						teaser.stop(true).animate({ //TODO: Konfigurierbar
							height: "412px",
							marginTop: "-412px"
						}, "slow", function ()
						{
							teaser.removeClass("stateOpened stateClosed").addClass("stateOpened");
							teaser.children(".arrow").css("backgroundImage", "url('" + downImageSolid + "')");
							teaser.data("slideInProgress", false);
						});

						teaser.find(".teaserContent").stop(true).animate({
							height: "380px"
						}, "slow");
					}
				}
			});
		};

		control.registerEvents = function ()
		{
			control.teasers.each(function ()
			{
				var teaser = $(this);
				if (control.settings.arrowSlideOnly)
				{
					teaser.children(".arrow").bind("mouseover", function () { control.teaserOpen(teaser, true); });
					teaser.bind("mouseleave", function () { control.teaserClose(teaser, true); });
				} else
				{
					teaser.bind("mouseover", function () { control.teaserOpen(teaser, true); });
					teaser.bind("mouseleave", function () { control.teaserClose(teaser, true); });
				}
			});
		};

		//Logic
		control.fillTeasers();
		control.teaserClose(control.teasers, false);
		control.registerEvents();
	};
})(jQuery);


/***************************
Search Type dropdown
****************************/
function improveAskSearch()
{
	$("#search_ask").change(function ()
	{
		var target = $(this).val() == "ask" ? "_blank" : "_self";

		$("#websitesearch-small").attr("target", target);
	});
}


/***************************
Fancy Flash
****************************/

function enableFancyFlash(selector)
{
	$(selector).fancybox({
		'overlayColor': '#000000',
		'centerOnScroll': true,
		'height': 'auto',
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'speedIn': 400,
		'speedOut': 400,
		'padding': 0
	});
}

$(document).ready(function ()
{
	enableFancyFlash('a.fancyFlash');
});

function enableFancyFlashFullwidthGray(selector)
{
	$(selector).fancybox({
		'overlayColor': '#000000',
		'centerOnScroll': true,
		'height': 'auto',
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'speedIn': 400,
		'speedOut': 400,
		'padding': 0,
		'onStart': function ()
		{
			$("#fancybox-wrap").addClass("fancyBoxWrapFullWidthGray");
			$('body').css('cursor', 'wait');
		},
		'onComplete': function ()
		{
			$("#fancybox-wrap").removeClass("fancyBoxWrapFullWidthGray").addClass("fancyBoxWrapFullWidthGrayVisible");
			$("#fancybox-content").addClass("fancyBoxWrapFullWidthGrayContent fancyBoxWrapFullWidthGrayHeight fancyBoxWrapFullWidthGrayWidth");
			$("#fancybox-content").children().addClass("fancyBoxWrapFullWidthGrayChild");
			$('body').css('cursor', 'default');
		},
		'onClosed': function ()
		{
			$("#fancybox-wrap").removeClass("fancyBoxWrapFullWidthGray fancyBoxWrapFullWidthGrayVisible");
			$("#fancybox-content").removeClass("fancyBoxWrapFullWidthGrayContent fancyBoxWrapFullWidthGrayHeight fancyBoxWrapFullWidthGrayWidth");
			$("#fancybox-content").children().removeClass("fancyBoxWrapFullWidthGrayChild");
		}
	});
}

$(document).ready(function ()
{
	enableFancyFlashFullwidthGray('a.fancyFlashFullwidthGray');
});

/***************************
Quickstart / Fairtext
****************************/

function enableQuickstart()
{
	$(document).ready(function ()
	{
		var overviewSwitch = $("#overviewswitch");

		$('#overviewswitch_icons', overviewSwitch).show();
		$('#fairtext', overviewSwitch).hide();

		$('#overviewswitch_icon_quickstart', overviewSwitch).click(function ()
		{
			$('#quickstart', overviewSwitch).show();
			$('#fairtext', overviewSwitch).hide();

			$("#overviewswitch_icon_quickstart > img", overviewSwitch).attr("src", overviewImage_Active);
			$("#overviewswitch_icon_fairtext > img", overviewSwitch).attr("src", fairtextImage_Inactive);
		});

		$('#overviewswitch_icon_fairtext', overviewSwitch).click(function ()
		{
			$('#quickstart', overviewSwitch).hide();
			$('#fairtext', overviewSwitch).show();

			$("#overviewswitch_icon_quickstart > img", overviewSwitch).attr("src", overviewImage_Inactive);
			$("#overviewswitch_icon_fairtext > img", overviewSwitch).attr("src", fairtextImage_Active);
		});
	});
}


/***************************
Nachträgliches Highlighting
z.B. bei Navi, Languagepicker
****************************/

$(function ()
{
	$(".mainnav ul").hover(function ()
	{
		$(this).find(".mainnav_firstentry > a").addClass("hover");
	}, function ()
	{
		$(this).find(".mainnav_firstentry > a").removeClass("hover");
	});

	$("#chooselanguageteaser").hover(function ()
	{
		$(this).find(".chooselanguage").addClass("hover");
	}, function ()
	{
		$(this).find(".chooselanguage").removeClass("hover");
	});
});
