﻿function applyShadowBox (elemArr)
{
	for (var i = 0; i < elemArr.length; ++i)
	{
		var elem = elemArr [i];
		
		$("" + elem + " img").each (
			function ()
			{
				var aTag = $(this).parent ();
				var link = aTag.attr ("href");
				
				var onclickAttr = "" + aTag.attr ("onclick");
				
				var aDomObj = aTag[0];
				aDomObj.setAttribute("onclick", "javascript:void(0);");
				
				//aTag.removeAttr ("onclick");
				aTag.removeAttr ("target");
				
				var substr = "" + onclickAttr.match (/[^&]width.*/);
				
				substr = "" + substr.match (/width=.*height=[0-9]*/, "");
				
				if (substr != null)
				{
					var valArr = substr.split (",");
					
					var width = ("" + valArr[0]).split ("=") [1];
					var height = ("" + valArr[1]).split ("=") [1];
					
					if (width > 0 && height > 0)
					{
						var attr = "shadowbox[page_pics];width=" + width + ";height=" + height + ";"
						aTag.attr ("rel", attr);
					}
				}
			}
		);
	}
}

function shadowBoxActivation ()
{
	Shadowbox.init({
		displayNav: true,
		continuous: true,
		overlayOpacity:0.2,
		players:  ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv']
	});
}





function alignFooter ()
{
	var leftHeight = $('#containerContentLeft').height ();
	var mainHeight = $('#containerContentMain').height ();// - $('#containerFooter').height ();
	var diff = leftHeight - mainHeight;
	if (diff > 0 && mainHeight != null)
	{
		$('#containerFooter').css ("top", diff);
	}
}



$.fn.flash.update = function(htmlOptions) 
{
	var url = String(location).split('?');
	url.splice(1,0,'?hasFlash=true&');
	url = url.join('');
	var msg = '<div class="flashWarning"><p>F&uuml;r die Darstellung dieses Inhaltes wird der Flash-Player 10 ben&ouml;tigt.</p>';
	msg += '<p>Sie k&ouml;nnen diesen hier downloaden: <a href="http://www.adobe.com/go/getflashplayer" target="_blank">Download Flash Player</a>.</p>';
	msg += '<p>Sie haben den Flash Player 10 bereits installiert? <a href="'+url+'">Hier klicken.</a></p></div>';
	this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
	jQuery(this)
		.addClass('flash-update')
		.prepend(msg);
};



function getCurrImageID (li)
{
	return $(li).prev ().find ("img").attr ("name");
	//return $("li:hover a img").attr("name");
}



var minicalTimeoutID = 0;
var eventTimeoutID = 0;
var eventPopup = null;
var maxEventsPerDay = 10;

function initCalendar ()
{
	// minical popups
	$(".cal-mini .cal-event").parent().hover (showMinicalPopup, hideMinicalPopup);
	$('#minicalPopup').hover (stopMinicalTimeout, hideMinicalPopup);
	$(".cal-mini-head").click (showMonthView);
	$(".cal-mini-bottom").click (showMonthView);	
	
	// disable links in minical days
	$(".cal-mini .bold a").each (function ()
	{
		$(this).attr("onclick", "return false;"); // => keep link url for popup
		$(this).attr("title", "");
	});
	
	// large cal popups
	$(".cal-large .cal-event").hover (showEventPopup, hideEventPopup);
	$(".cal-large .cal-event-popup").hover (stopEventPopupTimeout, hideEventPopup);
	
	// show multiple days only once
	var events = new Array();
	$("#containerContentHolder .eventDay .cal-event").each (function ()
	{
		var e = $(this).find("a").attr("href");
		if (events.indexOf (e) != -1)
		{
			$(this).hide();
		}
		else
		{
			events.push (e);
		}
	});
	
	// limit max number of events per day
	$("#containerContentHolder .eventDay").each (function ()
	{
		var e = $(this).find(".cal-event:visible");
		if (e.length > maxEventsPerDay)
		{
			var indexes = new Array ();
			for (var i = 0; i < e.length; ++i)
			{
				indexes.push(i);
			}
			indexes.sort(function () { return 0.5 - Math.random() });
			for (var i = 0; i < e.length - maxEventsPerDay; ++i)
			{
				$(e[indexes[i]]).hide();
			}
		}
	});
	
}

function showEventPopup (evtObj)
{
	if (eventPopup != null)
	{
		stopEventPopupTimeout ();
		eventPopup.hide ();
	}
	eventPopup = $(this).next ();
	eventPopup.show();
	
	var pos = $(this).position ();
	pos.top -= 28;
	pos.left += 20;
	eventPopup.css ("top", pos.top);
	eventPopup.css ("left", pos.left);

	// align on screen	
	var diff = eventPopup.offset().top + eventPopup.height () - $(window).height() - $(window).scrollTop();
	if (diff > 0)
	{
		eventPopup.css("top", pos.top - diff);
	}

	diff = pos.left + eventPopup.width () - 700;
	if (diff > 0)
	{
		eventPopup.css("left", pos.left - diff);
	}
}

function hideEventPopup (evtObj)
{
	eventTimeoutID = setTimeout (doHideEventPopup, 100);
	//$(this).find(".cal-event-popup").hide();
}

function doHideEventPopup ()
{
	eventPopup.hide ();
}

function stopEventPopupTimeout ()
{
	clearTimeout (eventTimeoutID);
}

function showMinicalPopup (evtObj)
{
	clearTimeout (minicalTimeoutID);
	var popup = $('#minicalPopup');
	
	// find popup content
	var events = $(this).find('.cal-event-popup-content');
	var content = "";
	for (var i = 0; i < events.length; ++i)
	{
		content += events[i].innerHTML;
	}
	popup.find('.cal-event-popup-content').html(content);
	
	var url = $(this).find('.bold a').attr("href").split("/");
	var s = (url.length < 8) ? url.length - 3 : url.length - 4;
	var date = url[s] + "." + url[s+1] + "." + url[s+2].substr(0, 4);
	popup.find('.cal-event-popup-top').text(date);
	
	// set position
	var x = $(this).position().left + 20;
	var y = $(this).position().top - 28;
	popup.css("left", x);
	popup.css("top", y);
	popup.show();
}

function hideMinicalPopup (evtObj)
{
	minicalTimeoutID = setTimeout (doHideMinicalPopup, 100);
}

function doHideMinicalPopup ()
{
	$('#minicalPopup').hide ();
}

function stopMinicalTimeout ()
{
	clearTimeout (minicalTimeoutID);
}

function showMonthView ()
{
	window.open ("kalender/alle_termine.html", "_self");
}


function hideRecipeDB ()
{
	var liPoint = $('#containerMenu a:contains(Rezeptdatenbank )').parent ("li");
	liPoint.hide ();
}


function toggleWidgetText ()
{
	var thisTag = $(this).parent ("p");
	var isShortTag = thisTag.hasClass ("shortText")
	
	var selector = "";
	
	if (isShortTag)
	{
		selector = "p[class=longText]";
	}
	else
	{
		selector = "p[class=shortText]";
	}
	
	var parentDiv = thisTag.parent ('div');
	
	thisTag.hide ();
	parentDiv.children (selector).show ();	
}

$(document).ready (
	function ()
	{
		// Menu		
		/*
		$('ul.sf-menu').superfish({
		    speed:       'fast',
            autoArrows:  false,
			dropShadows: false,
			delay:       0
		});
		*/
		
		$('.sf-menu li ul').hover (
			function ()
			{
				over (getCurrImageID (this) );
			},
			function ()
			{
				out (getCurrImageID (this) );
			}
		);	
				
		// Spam Mail Filter
		$(".spmMailAt").html ("&#064;");
		
		// Shadowbox
		applyShadowBox (new Array (".csc-textpic", ".news-single-img"));
		shadowBoxActivation();

		// Search Form
		$(".tx-indexedsearch-rules p").hide ();
		$(".tx-indexedsearch-rules h2").replaceWith ("<a id='search-rules-hide' href='javascript:void(0);'>Suchoptionen anzeigen</a>");
		$("#search-rules-hide").click (
			function ()
			{
				
				if ($(".tx-indexedsearch-rules p").css ("display") == "none")
				{
					$(".tx-indexedsearch-rules p").slideDown ();
					$("#search-rules-hide").html ("Suchoptionen verstecken");
				}
				else
				{
					$(".tx-indexedsearch-rules p").slideUp ();
					$("#search-rules-hide").html ("Suchoptionen anzeigen");
				}
			}
		);
		
		hideRecipeDB ();
		
		// Calendar
		initCalendar ();
		
		// Small Login Box verschieben, da sie nach Maincontent gerendert werden muss
		$('.tx-stsmallloginform-pi1').appendTo("#containerCommunityHolder");
		
		// Footer
		alignFooter ();
		
		// News Widget More Button		
		$('.newsWidgetMoreButton').click (toggleWidgetText);

	}
);


function switchBannerCategory (str)
{
	
}

