$(document).ready(function() {
	var i18nTexts = new Array();
	i18nTexts['de'] = {
		closeText: 'Schließen'
	};
	i18nTexts['en'] = {
		closeText: 'Close'
	};
	i18nTexts['fr'] = {
		closeText: 'Fermer'
	};
	i18nTexts['sp'] = {
		closeText: 'Cerrar'
	};
	i18nTexts['it'] = {
		closeText: 'Chiudi'
	};
	var lang = $("html").attr("xml:lang");
	
	$("#content a[href^='http://']").attr("target", "_blank").addClass("external");

	$("#content a.lightbox").attr("rel", "lightbox");
	
	$('a.emailLink').mailto();
	
	if($('div#flashMessage').length) {
		$('div#wrapper').click(function(){
			$('div#flashMessage').fadeOut('slow', function(){ $(this).remove() });
		});
	}
	if($('div#wrapper div.message').length) {
		$('div#wrapper').click(function(){
			$('div.message').fadeOut('slow', function(){ $(this).remove() });
		});
	}
	$('div#marginal').find('a.noOutline img').each(function(){
	    $(this).mouseenter(function(){
	        $(this).parent().next('h2').children('a').addClass("hover");
	    }).mouseleave(function(){
	        $(this).parent().next('h2').children('a').removeClass("hover");
	    });
	});

/* Custom page logic */
	
	if($('div#b2b').length) {
		$('#b2bInfolink').mouseenter(function(){
			$("img", this).attr('src', $('#b2bMouseOver').text());
	    }).mouseleave(function(){
	    	$("img", this).attr('src', $('#b2bMouse').text());
	    });
	}
	
	if($('div#visit').length) {
		var img_backup;
		$('.visitLink').mouseenter(function(){
			$(this).attr('rel', $("img", this).attr('src'));
			$("img", this).attr('src', $('#'+$(this).attr('id')+'MouseOver').text());
		}).mouseleave(function(){
			if(!$(this).hasClass('active'))
				$("img", this).attr('src', $(this).attr('rel'));
		});
	}
	
	if($('div#teacher').length) {
		var img_backup;
		$('.teacherLink').mouseenter(function(){
			$(this).addClass('zIndex').attr('rel', $("img", this).attr('src'));
			$("img", this).attr('src', $('#'+$(this).attr('id')+'MouseOver').text());
		}).mouseleave(function(){
			$(this).removeClass('zIndex');
			if(!$(this).hasClass('active'))
				$("img", this).attr('src', $(this).attr('rel'));
		});
	}
	
	if($('div#subPages').length) {
		var img_backup;
		$('.subPagesLink').mouseenter(function(){
			$(this).addClass('zIndex').attr('rel', $("img", this).attr('src'));
			$("img", this).attr('src', $('#'+$(this).attr('id')+'MouseOver').text());
		}).mouseleave(function(){
			$(this).removeClass('zIndex');
			if(!$(this).hasClass('active'))
				$("img", this).attr('src', $(this).attr('rel'));
		});
	}
	
	// Scroll Bar
	if($('div#formInset div.scroll-pane').length) {
		$.getScript('/js/libs/jquery.jscrollpane.min.js', function() {
			$('.scroll-pane').jScrollPane();
		});
	}

	$("a.ajaxPage").click(function(e) {
		e.preventDefault();
		$.ajax({
			url: $(this).attr('href'),
			success: function(data) {
				$('#formWrapper').fadeOut(500, function() {
					if(!$('#formWrapperBackup').length) {	//nur einmal als Backup verpacken
						$(this).attr('id', 'formWrapperBackup');
					}
					if($('#ajaxPageContent').length) {
						$(this).replaceWith(data);
					} else {
						$('#menuActive div.pageContent').prepend('<div id="ajaxPageContent">'+data+'</div>');
					}
					$('#ajaxPageContent #formWrapper #formInset').append('<a id="closeAjaxPageContent" href="#">'+i18nTexts[lang].closeText+'</a>');
					if($.slimbox)
						$("#ajaxPageContent a.lightbox").slimbox();
					$("#ajaxPageContent p a.cycleTarget").cyclePic();
					$("#ajaxPageContent a[href^='http://']").attr("target", "_blank").addClass("external");
					$("a#closeAjaxPageContent").click(function(e) {
						e.preventDefault();
						$('#ajaxPageContent').fadeOut(500, function(){
							$(this).remove();
							$('#formWrapperBackup').attr('id', 'formWrapper').fadeIn(500);
						});
					});
				});
			}
		});
	});

	// Newsticker
	if($('ul#newsList').length) {
		var options = {
		    newsList: "#newsList",
		    startDelay: 10,
		    controls: false,
		    stopOnHover: false,
		    placeHolder1: "_"
		}
		$().newsTicker(options);
	}

});

// Homepage Animation
$(function($) {
	var owidth = 1200;
	var oheight = 1080;
	var nwidth = 750;
	var nheight = 650;
	var top = ((oheight - nheight) / 2);
	var left = ((owidth - nwidth) / 2);

	$('img#growImage').delay(2000).animate({
		'height' : oheight+'px',
		'width' : owidth+'px',
		'left' : '-'+top+'px',
		'top' : '-'+left+'px'
	}, 1000);
});

$.fn.mailto = function() {
	return this.each(function(){
		var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
		$(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
	});
};

$.fn.cyclePic = function() {
	if($(this).hasClass('cycleTarget')) {
		$(this).parents('p').addClass('rPadding').css('width', $('img', this).width()).css('height', $('img', this).height());
		$(this).append('<img class="hide" src="' + $(this).attr('href') + '" alt="fade to" />').removeAttr('href');
		if(!$.cycle) {
			$.getScript('/js/libs/jquery.cycle.lite.js', function() {
				$('a.cycleTarget').cycle({
					speed: 1750,
					timeout: 7000
				});
			});
		} else {
			$('a.cycleTarget').cycle({
				speed: 1750,
				timeout: 7000
			});
		}
	}	
};

