	// Ajax-Request-Object for upadting the page contents
var PageLoader;
var LightBoxLoader;
	// variables to enforce reload
var forceId = 0;
var currForceId = -1;
	// Fetch the ajax-request-url for the entry page content
var originalPageContentsAjaxUrl = processUrlToAjax(document.location.href, true);
var currLoadedAjaxPage = '';
var lastWasGuestBook = 0;

var lbId= 0;

var ajaxPageRequestResponse = "";
var ajaxLightBoxRequestResponse = "";
var nextHashNavigationIsJustLightBoxClose = 0;

	// Add global onload-handler
$(document).ready(gloablOnloadFunction);

/*
	The global onload event-handler which will execute everything which should be done after the page-code is loaded
*/
function gloablOnloadFunction() {
		// Transform all page-links on this page to ajax-links
	if (!disableHashNav && checkForAjaxLinkCompatibility())
		processAllLinksForAjax();

		// Enable the fragment-change-event
	$.fragmentChange( true );

		// Add event-handler for fragment-change (hash change)
	$(document).bind( 'fragmentChange.nav', doHashNavigation );
  
		// Chek if current page already contains a hash-pointer and navigate to the requested page if requested
	if ($.fragment().ajaxcall || $.fragment().lb)
		doHashNavigation();


	if ($.fragment().lb)
		lbId = $.fragment().lb * 1;
	if ($.fragment().force)
		forceId = $.fragment().force * 1;

	checkGbErrorMessage();
	
	
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 7)
		setTimeout('IE7BugHack();', 500);
	
//showLightBox(500, 500);
}


function IE7BugHack() {
	var x = $("#menu1_bar").html();
	x = "<div style=\"width:100%; height:1px; background-color:#f28e00;\"></div>" + x;

	$("#menu1_bar").html(x);
}

/*
	Checks all links in a document and tries to convert them to ajax links
*/
function processAllLinksForAjax() {
	for (var i = 0; i < document.links.length; ++i) {
		if (document.links[i] && document.links[i].href)
			document.links[i].href = processUrlToAjax(document.links[i].href);
	}
}

/*
	Transforms an URL to an ajax-request-call (will skipp links with certain parameters)
*/
function processUrlToAjax(url, plainUrl) {
	var _AJAX_PARAMS_EXCLUDE = new Array("type");		// If link contains any of this parameters, it will not be converted to an ajax link
	var _AJAX_PARAMS_INCLUDE = new Array("id");		// ONLY if link contains any of this parameters, it will be converted to an ajax link
	var _AJAX_PAGETYPE = 40;			// Pagetype which is used for ajax request
	var _NavFunc = "hashNavigate";
	var exclude = false;
	var include = false;
	
	if (url.length < 11 || url.substring(0, 11).toLowerCase() != "javascript:") {
	
			// Check for specified exclude parameters
		for (var i = 0; i < _AJAX_PARAMS_EXCLUDE.length; ++i) {
			if (url.indexOf("&" + _AJAX_PARAMS_EXCLUDE[i] + "=") > -1 || url.indexOf("?" + _AJAX_PARAMS_EXCLUDE[i] + "=") > -1) {
				exclude = true;
				break;
			}
		}
			// Check for specified include parameters
		for (var i = 0; i < _AJAX_PARAMS_INCLUDE.length; ++i) {
			if (url.indexOf("&" + _AJAX_PARAMS_INCLUDE[i] + "=") > -1 || url.indexOf("?" + _AJAX_PARAMS_INCLUDE[i] + "=") > -1) {
				include = true;
				break;
			}
		}
		
		if (url.indexOf("type=40") != -1) {
			exclude = false;
		}
		
		if (url.indexOf("id=newssingle") != -1 || url.indexOf("id=singleevent") != -1 || url.indexOf("id=newgbentry") != -1) {
			_AJAX_PAGETYPE = 41;
			_NavFunc = "lightBoxNav";
		}
		
		if ((!exclude && include) || plainUrl)
				// generate ajax-call
			if (!plainUrl) {
			
					// Make sure ajax-calls and docuemnt url both contain www or both do not. (same origion problem)
				var www = document.location.href.indexOf("http://www.");
				if (www != url.indexOf("http://www.")) {
					if (!www)
						url = url.replace(/http:\/\//, "http://www.");
					else
						url = url.replace(/http:\/\/www./, "http://");
				}
			
				var hIndex = url.indexOf("#");
				if (hIndex == -1)
					return "javascript:" + _NavFunc + "('" + url + (url.indexOf("?") > -1 ? "&" : "?") + "type=" + _AJAX_PAGETYPE + "');";
				else
					return "javascript:" + _NavFunc + "('" + url.substring(0, hIndex) + (url.substring(0, hIndex).indexOf("?") > -1 ? "&" : "?") + "type=" + _AJAX_PAGETYPE + url.substring(hIndex) + "');";
			}
			else {
				// if plain url is requested: make sure it does not contain hash navigation because this would lead to an endless reload
			
				var hIndex = url.indexOf("#");
				if (hIndex != -1)
					url = url.substring(0, hIndex);
			
				return url + (url.indexOf("?") > -1 ? "&" : "?") + "type=" + _AJAX_PAGETYPE;
			}
		else
				// return original url
			return url;

	}
	else {
		return url;
	}
	
}

/*
	Initalizes the ajax request for new page content
*/
function ajaxLoadPage(url, suppressNewLocation, tp) {
		// Show progress bar
	showPageLoadingProgressBar();
	
		// Abort old page requests
	if (PageLoader) {
		PageLoader.abort();
		PageLoader = null;
	}
	
	if (url.indexOf("id=newgbentry") != -1)
		lastWasGuestBook = 1;
	else
		lastWasGuestBook = 0;
	
		// Start new ajax request
	PageLoader = $.ajax({
		type: (tp ? tp : "GET"),
		url: url,
		dataType: "html",
		success: PageLoader_onSuccess,
		failure: PageLoader_onFailure
	}); 
}


function ajaxLoadLightBox(url, suppressNewLocation) {
		// Show progress bar
	showPageLoadingProgressBar();
	
		// Abort old page requests
	if (LightBoxLoader) {
		LightBoxLoader.abort();
		LightBoxLoader = null;
	}
		// Start new ajax request
	LightBoxLoader = $.ajax({
		type: "GET",
		url: url,
		dataType: "html",
		success: LightBoxLoader_onSuccess,
		failure: LightBoxLoader_onFailure
	}); 
}

/*
	Abort the current page request (progress bar etc. is reset)
*/
function ajaxAboortLoadPage() {
		// abort old page requests
	if (PageLoader) {
		PageLoader.abort();
		PageLoader = null;
	}
	
	if (LightBoxLoader) {
		LightBoxLoader.abort();
		LightBoxLoader = null;
	}

		// hide progress bar
	hidePageLoadingProgressBar();
	$(document).bind( 'fragmentChange.nav', doHashNavigation );
}

/*
	Event-handler for a successful update of the page contents.
	
*/
function PageLoader_onSuccess(response) {
		// Save response
	ajaxPageRequestResponse = PageLoader.responseXML;
	
	// Check for a guest-book-reply
	if (!ajaxPageRequestResponse || lastWasGuestBook == 1) {
		if (response.indexOf("<ul id=\"ve_guestbook_formerror\">") != -1) {
			// Error in form
			LightBoxLoader = PageLoader;
			LightBoxLoader_onSuccess(response);
		}
		else {
			// Successfull - reload page
			$.setFragment({ ajaxcall: ($.fragment().ajaxcall ? $.fragment().ajaxcall : originalPageContentsAjaxUrl), force : ++forceId }, 2);
		}
		lastWasGuestBook = 0;
	}
	else {
		// Common replies
			// Destroy ajax-request-object
		PageLoader = null;
		
			// Fadeout the old content and pass fade-in-function as callback
		$("#contentpanel").hide(500, fadeOutStep2);
		$("#headerpanel").fadeOut(500);
	}
	
}


function LightBoxLoader_onSuccess(response) {
		// Save response
	ajaxLightBoxRequestResponse = LightBoxLoader.responseXML;
		// Destroy ajax-request-object
	LightBoxLoader = null;
	
		// show lightbox
	showLightBox(700,null, null, ajaxLightBoxRequestResponse.getElementsByTagName("content")[0].firstChild.data);
	
	// hide progress bar
	hidePageLoadingProgressBar();
	
}

function LightBoxLoader_onFailure(response) {
	LightBoxLoader = null;
	
		// show lightbox
	showLightBox(500,400, null, "Fehler beim Laden!");
	
	// hide progress bar
	hidePageLoadingProgressBar();
}

function fadeOutStep2() {
	$("#menu2panel").fadeOut(500, pageFadeInNewContent);
}

/*
	Displays the new content (stored in "ajaxPageRequestResponse") using a nice animation
	
*/
function pageFadeInNewContent() {
	var cp = $("#contentpanel");
	var hp = $("#headerpanel");
	var m1p = $("#menu1_itemcontainer");
	var m2p = $("#menu2panel");

		// Set the html-content
	cp.html(ajaxPageRequestResponse.getElementsByTagName("content")[0].firstChild.data);
	hp.html(ajaxPageRequestResponse.getElementsByTagName("headers")[0].firstChild.data);
	m1p.html(ajaxPageRequestResponse.getElementsByTagName("menu1")[0].firstChild.data + '<div style="clear:both;"></div>');
	m2p.html(ajaxPageRequestResponse.getElementsByTagName("menu2")[0].firstChild.data);
	
	document.title = "You-th.de :: " + ajaxPageRequestResponse.getElementsByTagName("pagetitle")[0].firstChild.data
	
		// Ensure that all page-links on this page are ajax-links
	processAllLinksForAjax();
	ajaxPageRequestResponse = null;
		// Fade-in the new content
	m2p.fadeIn(500, fadeInStep2);

	// hide progress bar
	hidePageLoadingProgressBar();
}

function fadeInStep2() {
	$("#contentpanel").show(500);
	$("#headerpanel").fadeIn(500);
}

/*
	Event-handler for a failed update of the page contents
*/
function PageLoader_onFailure(response) {
		// Display error-message
	$("#contentpanel").html('<p style="font-weight:bold;"><span style="color:#ff0000;">Fehler beim Laden Seite!</span><br /><br />Dr&uuml;cke F5 um die Seite erneut zu laden!</p><p style="font-style:italic;">' + PageLoader.status + '<br />' + PageLoader.statusText +'</p>');
	
		// Destroy ajax-request-object
	PageLoader = null;
	
		// hide progress bar
	hidePageLoadingProgressBar();
}
/*
	Performs a navigation by just setting the url-hash.
*/
function hashNavigate(hashUrl) {
	nextHashNavigationIsJustLightBoxClose = 0;
	$.setFragment({ ajaxcall: hashUrl}, 2);
}

function lightBoxNav(hashUrl) {
	nextHashNavigationIsJustLightBoxClose = 0;
	$.setFragment({ lbw : '' , lbh : '' , im : '', lb:++lbId, lbcall : hashUrl });
}

/*
	Executes the hash-navigation by starting the request for the page specified by the url-hash
*/
function doHashNavigation() {
	
	if (!nextHashNavigationIsJustLightBoxClose) {
			// Do a normal hash navigation
		var has = $.fragment().ajaxcall;
		var lbw = $.fragment().lbw;
		var lbh = $.fragment().lbh;
		var lb = $.fragment().lb;
		var lbcall = $.fragment().lbcall;
		var force = $.fragment().force;

		if ((has && has != '' && has != currLoadedAjaxPage) || (force && force != '' && currForceId != force)) {
			currForceId = force;
			ajaxLoadPage(has);
			currLoadedAjaxPage = has;
		}
		else if ((!has && currLoadedAjaxPage != '' && currLoadedAjaxPage != originalPageContentsAjaxUrl) || (force && force != '' && currForceId != force)) {
			currForceId = force;
			ajaxLoadPage(originalPageContentsAjaxUrl);
			currLoadedAjaxPage = originalPageContentsAjaxUrl;
		}
		
		if (lb && lb != "" && (!lbcall || lbcall == '')) {
			showLightBox((lbw != '' ? lbw : null), (lbh != '' ? lbh : null), $.fragment().im);
			
		}
		else {
				// Make sure lightbox is closed
			var box = $("#lightbox");
			if (box.css("visibility") != "hidden")
				doLightBoxHide();
				
		}
		
		if (lbcall && lbcall != '') {
			ajaxLoadLightBox(lbcall);
		}
	}
	else {
		doLightBoxHide();
	}
}

function doLightBoxHide() {
	// Close lightbox
	var bg = $("#lightboxbg");
	var box = $("#lightbox");
	
	bg.fadeTo(1000,0);
	box.hide(500);
	setTimeout('$("#lightboxbg").css("visibility","hidden"); $("#lightbox").css("visibility","hidden");',1050);
	
	nextHashNavigationIsJustLightBoxClose = 0;
}

function showPageLoadingProgressBar() {
	$("#ajaxLoading_requestUrl").text($.fragment().ajaxcall);
	var d = $("#ajaxloading");
	d.fadeTo(0, 0);
	if (d.css("visibility") == "hidden")
		d.css("visibility","");
	d.fadeTo(500, 0.85);
	
	
}

function hidePageLoadingProgressBar() {
	var d = $("#ajaxloading");
	d.fadeTo(500, 0);
	d.css("visibility","hidden");
}

/* do nothing - just avoid js error */
function blurLink(e) { }


function showLightBox(width, height, img, html) {
	
	
	var lb = $("#lightbox");
	var lbbg = $("#lightboxbg");
	
	
	var c = $("#lightboxcontent");
	var chtml = '';
	
	
	if (img)
		chtml = '<div style="width:100%; height:100%; text-align:center;"><a href="' + img + '" target="_blank" title="Bild anzeigen"><img id="d1" onload="" style="max-width:100%; max-height:450px;" src="' + img + '" alt="Bild" /></a></div>';
	else if (html) {
		
		if (width || height) {
			chtml = "<div style=\"";
			
			if (width)
				chtml += "width:" + width + "px;";
			if (height)
				chtml += "height:" + height + "px;";
				
			chtml += "\">";
		}
		
		chtml += html + '<div style="clear:both;"></div>';
		
		if (width || height)
			chtml += "</div>";
	}
	
	c.html(chtml);
	
	checkGbErrorMessage();
	
	lbbg.fadeTo(0,0);
	if (lbbg.css("visibility") == "hidden")
		lbbg.css("visibility","");
	lbbg.fadeTo(500, 0.91);
	
	
	lb.hide(0);
	if (lb.css("visibility") == "hidden")
		lb.css("visibility","");
	
	lb.show(1000);
}


function hideLightBox() {
	nextHashNavigationIsJustLightBoxClose = 1;

	$.setFragment({ im : '', lb : '', lbcall : '' });
}


function lightboximage(img) {
	$.setFragment({ lbw: "", lbh : "", im : img, lb : ++lbId, lbcall: '' });
}

function checkGbErrorMessage() {
	var e = $("#gb_formerrdesc");
	
	try {
		if (e.html() == '') {
			e.css("visibility", "hidden");
			e.css("visibility", "height:0px");
		}
		else {
			e.css("visibility", "");
			e.css("visibility", "");
			
			return 1;
		}
	}
	catch(ex) { }
	
	return -1;
}

function submitGuestBookEntry() {
	var inputs = $("#newentryform input");
	var textArea = $("#newentryform textarea");
	var url = "index.php?id=newgbentry&type=40";
	
	for (var i = 0; i < inputs.length; ++i) {
		if (inputs[i].type.toLowerCase() != "button") 
			url += "&" + inputs[i].name + "=" + encodeURI(inputs[i].value);
	}
	url += "&" + textArea[0].name + "=" + encodeURI(textArea[0].value);
	
	//hideLightBox();
	ajaxLoadPage(url);
}


function toggleDlItems(id) {
	var item = $("#" + id );
	var curr = item.next();
	var mode = 1;
	
	while (!curr.hasClass("sdb_song") && curr.hasClass("dlitem")) {
		if (curr.css("visibility") != "collapse") {
			curr.css("visibility", "collapse");
			mode = 1;
		}
		else {
			curr.css("visibility", "visible");
			mode = 0;
		}
		
		curr = curr.next();
	}
	
	if (mode == 0)
		item.css("list-style-image", "url(fileadmin/youth-v4/templates/style_files/intern/minus.gif)");
	else
		item.css("list-style-image", "url(fileadmin/youth-v4/templates/style_files/intern/plus.gif)");
}


function dirtypop()
{
  var generator=window.open('','name','height=400,width=500');
  
  generator.document.write('<object type="application/x-shockwave-flash" data="http://flash-mp3-player.net/medias/player_mp3_maxi.swf" width="200" height="20"><param name="movie" value="http://flash-mp3-player.net/medias/player_mp3_maxi.swf" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="mp3=http%3A//you-th.de/fileadmin/youth-v2/media/predigten/youth-service-2009-11-13_32kb.mp3&amp;autoplay=1&amp;autoload=1&amp;showstop=1&amp;showvolume=1" /></object>');

  generator.document.close();
}



function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-3);}




function checkForAjaxLinkCompatibility() {
	if (BrowserDetect.browser == "Firefox" && BrowserDetect.version >= 2)
		return true;
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 7)
		return true;
	if (BrowserDetect.browser == "Opera" && BrowserDetect.version >= 8)
		return true;
	if (BrowserDetect.browser == "Chrome")
		return true;
	if (BrowserDetect.browser == "Safari" && BrowserDetect.version >= 3)
		return true;
		
	return false;
}

/* Browser Detection */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
