$(document).ready(function(){
	// Hauptnavigation
	$("#hauptnavigation li.navigationshover").add("#wechselnavigation .wechselnavigation").hover(function() {
		var rel = $(this).attr("rel");
		$("#hauptnavigation  li.navigationshover").removeClass("aktiv");
		$("#hauptnavigation  li.navigationshover[rel="+rel+"]").addClass("aktiv");
		$("#wechselnavigation ul.wechselnavigation").removeClass("sichtbar");
		$("#wechselnavigation ul.wechselnavigation").addClass("versteckt");
		$("#wechselnavigation ul.wechselnavigation.navigation-"+rel).addClass("sichtbar");
	}, function() {
		var rel = $(this).attr("rel");
		$("#hauptnavigation li.navigationshover").not(".eigentlichAktiv").removeClass("aktiv");
		$("#hauptnavigation li.navigationshover.eigentlichAktiv").addClass("aktiv");
		$("#wechselnavigation ul.wechselnavigation").removeClass("versteckt");
		$("#wechselnavigation ul.wechselnavigation.navigation-"+rel).removeClass("sichtbar");
	});

	// Suche und Login
	$(".interaktion input[type=text]").add(".interaktion input[type=password]").add(".interaktion textarea").add(".interaktion select").each(function(i) {
		var feldText = $(this).attr("value");
		$(this).click(function() {
			if($(this).attr("value") == feldText) {
				$(this).attr("value","");
			}
			$(this).addClass("aktiv");
			$(this).addClass("eingabe");
		});
		$(this).blur(function() {
			if($(this).attr("value") == "") {
				$(this).attr("value",feldText);
				$(this).removeClass("eingabe");
			}
			$(this).removeClass("aktiv");
		});
	});
	
	// Administration Uploadfeld
	$(".uploadfeldDiv .uploadfeldLoeschen").show();
	$(".uploadfeldDiv:first .uploadfeldLoeschen").hide();
	$(".uploadfeldDiv input").attr("id","");
	$(".uploadfeldDiv:first input").attr("id","neuFotos");
	$(".uploadfeldDiv .uploadfeldDuplizieren").click(function() {
		$(".uploadfeldDiv:first").clone(true).insertAfter(".uploadfeldDiv:last");
		$(".uploadfeldDiv .uploadfeldLoeschen").show();
		$(".uploadfeldDiv:first .uploadfeldLoeschen").hide();
		$(".uploadfeldDiv input").attr("id","");
		$(".uploadfeldDiv:first input").attr("id","neuFotos");
		return false;
	});
	$(".uploadfeldDiv .uploadfeldLoeschen").click(function() {
		$(this).parent().remove();
		return false;
	});
	
	// Administration Bild Löschen Hover
	$("a.bildLoeschen").hover(function() {
		$(this).find("div").show();
	}, function() {
		$(this).find("div").hide();
	});

	// Abweichende Rechnungsanschrift
	if($("#__anm_rechnung_rechnungsanschrift:checked").val() != null) {
		$("#abweichendeRechnungsanschrift").show();
	}
	$("#__anm_rechnung_rechnungsanschrift").change(function() {
		if($("#__anm_rechnung_rechnungsanschrift:checked").val() == null) {
			$("#abweichendeRechnungsanschrift").hide();
		} else {
			$("#abweichendeRechnungsanschrift").show();
		}
	});
	if($("#neuRechnungsanschrift_rechnungsanschrift:checked").val() != null) {
		$("#abweichendeRechnungsanschrift").show();
	}
	$("#neuRechnungsanschrift_rechnungsanschrift").change(function() {
		if($("#__anm_rechnung_rechnungsanschrift:checked").val() == null) {
			$("#abweichendeRechnungsanschrift").hide();
		} else {
			$("#abweichendeRechnungsanschrift").show();
		}
	});

	// Abweichender Ansprechpartner
	if($("#__anm_ansprechpartner_ansprechpartner:checked").val() != null) {
		$("#abweichenderAnsprechpartner").show();
	}
	$("#__anm_ansprechpartner_ansprechpartner").change(function() {
		if($("#__anm_ansprechpartner_ansprechpartner:checked").val() == null) {
			$("#abweichenderAnsprechpartner").hide();
		} else {
			$("#abweichenderAnsprechpartner").show();
		}		
	});

	// Tooltip
	$(".tooltip[title]").tooltip({
		track: true,
		showURL: false,
		fade: 250,
		left: -8,
		top: 20
	});
	
	// Teilnehmerstimmen Studiengang
	var fadeZeit = 600;
	$('#teilnehmerstimmen').prepend('<ul class="teilnehmerstimmenauswahl"></ul>');
	$('#teilnehmerstimmen .teilnehmerstimme').each(function(i) {
		if(i == 0) {
			var klasse = "erstesElement";
		} else {
			var klasse = "";
		}
		$(".teilnehmerstimmenauswahl").append('<li><a href="#teilnehmerstimme-'+(i+1)+'" class="'+klasse+'">'+(i+1)+'</a></li>');
		var breite = (i+1)*29; // Breite berechnen für mittige Platzierung
		$("#teilnehmerstimmen .teilnehmerstimmenauswahl").css({"width":breite+"px"});
	});
	$('#teilnehmerstimmen .teilnehmerstimmenauswahl a.erstesElement').addClass("aktiv");
	$('#teilnehmerstimmen .teilnehmerstimme').not('#teilnehmerstimme-1').hide();
	$("#teilnehmerstimmen .teilnehmerstimmenauswahl a").click(function() {
		var href = $(this).attr("href");
		var hoehe = $("#teilnehmerstimmen "+href).height();
		$('#teilnehmerstimmen').css({"height":(hoehe+190)+"px"});
		$("#teilnehmerstimmen .teilnehmerstimmenauswahl a").removeClass("aktiv");
		$('#teilnehmerstimmen .teilnehmerstimme').fadeOut(fadeZeit);
		$('#teilnehmerstimmen .abschluss').fadeOut(fadeZeit);
		$(this).addClass("aktiv");
		var fadeIn = setTimeout(function() {
			$("#teilnehmerstimmen "+href).fadeIn(fadeZeit);
			$('#teilnehmerstimmen .abschluss').fadeIn(fadeZeit);
		}, fadeZeit);
		return false;
	});
	
	// Tabs
	//$(".tabs").tabs();
	
	// Tabs
	if ($(".tabs").length > 0){
		tabID = $(".tabs").attr("id").replace("selected-","");
		$(".tabs").tabs({ selected: tabID });
	}
	
	// Datepicker
	$.datepicker.regional['de'] = {
		closeText: 'schließen',
		prevText: '&#x3c;zurück',
		nextText: 'Vor&#x3e;',
		currentText: 'heute',
		monthNames: ['Januar','Februar','März','April','Mai','Juni',
		'Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
		'Jul','Aug','Sep','Okt','Nov','Dez'],
		dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		weekHeader: 'Wo',
		dateFormat: 'dd.mm.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['de']);
	$('.datepicker').datepicker({
		inline: true
	});

	// Externe Links
	$("a[href][rel*=external]").attr("target","_blank").addClass("extern");

	// png Fix
	$(document).pngFix();
	
	// Werbung Flash
	werbungAnimationsdauer = 2; // Sekunden
	werbungAngezeigt = false;
	werbungClicked = false;
	$("#werbungFlash").hover(function() {
		werbungAngezeigt = true;
		if(werbungClicked == false) {
			$("#werbung").css({"z-index":"999"});
			$("#werbungFlash").css({"width":"1111px"});
			$("#werbungFlashElement").css({"margin-left":"0px"});
		}
		
	});
	$("#werbungFlash").click(function() {
		if(werbungAngezeigt == true) {
			werbungClicked = true;
			var animation = setTimeout(function() {
				$("#werbung").css({"z-index":"0"});
				$("#werbungFlash").css({"width":"147px"});
				$("#werbungFlashElement").css({"margin-left":"-964px"});
			}, werbungAnimationsdauer*1000);
			var zeitverzoegerung = setTimeout(function() {
				werbungClicked = false;
			}, (werbungAnimationsdauer*1000)+500);
		}
	});
	
	
	/*clicked = false;
	$("#werbungFlash").hover(function() {
		if(clicked == false) {
			$("#werbung").css({"z-index":"999"});
		}
	});
	
	$("#werbungFlash").click(function() {
		clicked = true;
		var animation = setTimeout(function() {
			$("#werbung").css({"z-index":"0"});
			clicked = false;
		}, 2000);		
	});*/
	
	// Fancybox
	$("a.iframe").fancybox({
		'titleShow' 	: false,
		'width'			: 600,
		'height'		: 500,
		'autoScale'		: false,
		'type'			: 'iframe'
	});
	$("a.iframeVerbesserungsvorschlag").fancybox({
		'titleShow' 	: false,
		'width'			: 600,
		'height'		: 580,
		'autoScale'		: false,
		'type'			: 'iframe'
	});
	$("a.iframeWeiterempfehlen").fancybox({
		'titleShow' 	: false,
		'width'			: 600,
		'height'		: 620,
		'autoScale'		: false,
		'type'			: 'iframe'
	});
	$("a.iframeAutoHeight").fancybox({
		'titleShow' 	: false,
		'width'			: 500,
		'height'		: 350,
		'autoScale'		: false,
		'type'			: 'iframe'
	});
	$("a.iframeWerbung").fancybox({
		'titleShow' 	: false,
		'width'			: 500,
		'height'		: 430,
		'autoScale'		: false,
		'type'			: 'iframe'
	});
	/*$("a.iframeVideo").fancybox({
		'titleShow' 	: false,
		'width'			: $(this).attr("rel"),
		'height'		: $(this).attr("rev"),
		'autoScale'		: false,
		'type'			: 'iframe'
	});*/
	
	$("a.iframeVideo").each(function() {
        var width = $(this).attr('rel');
		var height = $(this).attr('rev');

        $(this).fancybox({
           	'titleShow' : false,
			'width'		: parseInt(width)+60,
			'height'	: parseInt(height)+140,
			'autoScale'	: false,
			'type'		: 'iframe',
			'showNavArrows': false
        });
    });

	
	
	$("a[rel~=lightbox]").fancybox({
		'titlePosition' : 'inside',
		'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"><strong>Bild ' + (currentIndex + 1) + ' von ' + currentArray.length + (title.length ? ':</strong> &nbsp; ' + title : '') + '</span>';
		}
	});


	$('a.drucklink').click(function() {
		window.print();
		return false;
	});


});

// Keine jQuery-Funktionen, nur der Sauberheit halber in eine externe Datei gepackt
function UnCryptMailto(s, shift) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) {
		n=s.charCodeAt(i);
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift));
	}
	return r;
}
function linkTo_UnCryptMailto(s, shift)	{
	location.href=UnCryptMailto(s, shift);
}

function chkFormular () {
  if (document.Formular.anfrageName.value == "") {
    alert("Bitte Ihren Namen eingeben!");
    document.Formular.anfrageName.focus();
    return false;
  }
  if (document.Formular.anfrageStrasse.value == "") {
    alert("Bitte Ihre Strasse eingeben!");
    document.Formular.anfrageStrasse.focus();
    return false;
  }
  if (document.Formular.anfragePLZOrt.value == "") {
    alert("Bitte Ihren Wohnort eingeben!");
    document.Formular.anfragePLZOrt.focus();
    return false;
  }
  if (document.Formular.anfrageEMmail.value == "") {
    alert("Bitte Ihre E-Mail-Adresse eingeben!");
    document.Formular.anfrageEMmail.focus();
    return false;
  }
  if (document.Formular.anfrageEMail.value.indexOf("@") == -1) {
    alert("Keine E-Mail-Adresse!");
    document.Formular.anfrageEMail.focus();
    return false;
  }
}
