$(document).ready(function() { 
	if ( window.notiz_gespeichert == '' ){
    	 
    	 notiz_gespeichert = $.ajax({
			  type: "POST",
			  data: "getword=notiz_gespeichert&sp="+sp,
			  url: "./ajax/merken.php",
			  async: false
			 }).responseText;
		 /*
		$.post("./ajax/merken.php", 
			{ getword: "notiz_gespeichert", sp: 'de'},
		  	function(data){
			    notiz_gespeichert = data;
		});	
		*/
    }
	
//* ======== Allgemeines ======== *//

	// == PDF-Liste (Kasuistiken) == //
	$('h3.pdfklapp').click(function() { 
		$(this)
			.toggleClass('zu')
			.next('ul.pdflist')
			.slideToggle(500);
	});
	$('h3.pdfklapp:gt(0)').addClass('zu').next('ul.pdflist').hide();	


  // == Lade-Anzeige == //
	$("p#loading").ajaxStart(function() { 
		$(this).show();
	  }).ajaxSuccess(function() { 
		$(this).hide();
	});


  // == Datei/Bild merken == //
	$('.bildbox a.drucken, td.text a.drucken').after('<div class="gemerkt">Datei gemerkt</div>');
	$('.bildbox a.drucken, td.text a.drucken').click(function() {
		//alert( $(this).attr('id') );
		var mediasaved = $(this);
		$.post("./ajax/medienmerken.php", 
				{ idmedia_elements: $(this).attr('id') },
			  	function(data){
				    //alert("Data Loaded: " + data);

				    if ( data == 'noerror' ){
				    	//var mediasaved = true;
						//$(this).toggleClass('merken');
						mediasaved.toggleClass('merken');
						//$(this).parent().find('.gemerkt').slideToggle(500);
						mediasaved.parent().find('.gemerkt').slideToggle(500);
				    }

			  	});	

		return false;
	});
	
  
  // == Notiz lï¿½schen == //
	$(".notizloeschen").click(function() {
		$.post("./ajax/merken.php?del=1", 
				{ del: 1, merklisteid: $(".notizloeschen").attr('id')},
			  	function(data){
				    //alert("Data Loaded: " + data);
				    if ( data == 'noerror' ){
						$("#tr_"+$(".notizloeschen").attr('id')).remove();

                                                if($("table.sortierbar tr").length > 1) {
                                                    $('table.sortierbar').trigger("update");
                                                    $('table.sortierbar').trigger("appendCache");
                                                }
						return false;
				    }
			  	});			
		return false;
	});


  // == Notiz Anmerkung einfï¿½gen == //
	$("div.notiz input.button").click(function() {
		var notizid = $(this).attr('id');
		//var anmerkung = $("#text_"+notizid).val();
		$.post("./ajax/merken.php", 
				{ merklisteid: notizid, anmerkung: $("#text_"+notizid).val()},
			  	function(data){
				    //alert("Data Loaded: " + data);

				    if ( data == 'noerror' ){
						$('#head').append('<div class="gesichert">'+notiz_gespeichert+'!</div>');
						$('.gesichert').hide().fadeIn(100, function() { 
							$(this).fadeOut(2000);
			
						});
						return false;
				    }
			  	});			
		return false;
	});


  // == Lesezeichen lÃ¶schen == //
	$(".lesezeichenloeschen").click(function() {
		var delid = $(this).attr('id');
		$.post("./ajax/lesezeichen.php",
				{ del: 1, lesezeichenid: $(this).attr('id')},
			  	function(data){
				    if ( data == 'noerror' ){
						$("tr:has(td #"+delid+")").remove();

                                                if($("table.sortierbar tr").length > 1) {
                                                    $('table.sortierbar').trigger("update");
                                                    $('table.sortierbar').trigger("appendCache");
                                                }

						return false;
				    }
			  	});
		return false;
	});




  // == Lesezeichen alle lÃ¶schen == //
	$("#lesezeichenalo").click(function() {
		var delid = $(this).attr('id');
		$.post("./ajax/lesezeichen.php",
				{ del: 1, lesezeichenid: "all"},
			  	function(data){
				    if ( data == 'noerror' ){
						$("tr:has(td a.lesezeichenloeschen)").remove();
                                                if($("table.sortierbar tr").length > 1) {
                                                    $('table.sortierbar').trigger("update");
                                                    $('table.sortierbar').trigger("appendCache");
                                                }
						return false;
				    }
			  	});
		return false;
	});




  // == Notiz-Tabelle und Navigationspunkt == //
/* 	$('table.sortierbar:has(.eintrag) tr').find('td:eq(2)').prepend('<span title="Notiz anzeigen / ausblenden" alt="Notiz anzeigen / ausblenden" class="open"></span>'); */
	$('table.sortierbar:not(.kasu2) span.open').click(function() { 
		$(this).toggleClass('an').parent().find('.eintrag, .notiz').slideToggle(500);


	});
	$('table.kasu tr').find('div.eintrag:odd').addClass('wechsel');
	$('table.kasu tr:first-child div.eintrag').show().parent().find('.open').addClass('an');
	
	/*
	$('table.sortierbar tr').find('td:eq(3)').addClass('eintrag_loeschen').find('a').click(function() { 
		$(this).parent().parent().remove();
		return false;
	});
	*/
	$('a.aan').click(function() { 
		$(this).prev().find('.eintrag, .notiz').slideDown(500);
		return false;
	});
	$('a.aaus').click(function() { 
		$(this).prev().prev().find('.eintrag:visible, .notiz:visible').slideUp(500);
		return false;
	});
	$('table.sortierbar:has(.eintrag) + a.alo').click(function() { 
		$(this).prev().prev().prev().find('tr:gt(0)').remove();
		return false;
	});
	$('table.sortierbar:has(:not(.eintrag)) + a.alo').click(function() { 
		$(this).prev().find('tr:gt(0)').remove();
		return false;
	});	
	
  // == Login-Box == //
	$('#rechte_spalte div.login.eingeloggt a').hide();
	$('#rechte_spalte div.login.eingeloggt h3').prepend('<span class="open"></span>');
	$('#rechte_spalte div.login.eingeloggt h3 span.open').click(function() { 
		$(this).toggleClass('an').parent().parent().find('a').slideToggle(500);
	});	
	

	// == Button-Layer fï¿½r Video-Tabelle == //
	$('table.videotab td a:has(img)').each(function() { 
		$(this).append('<a class="layerlink" href="#" title="Video"></a>');	
	});	
	
	
  // == Navigations-Extra-Klassen == //
	$('#nav li:first').addClass('first');
	$('#service li a:first').addClass('first');
	$('#service li a:last').addClass('noborder');
		
  // == Seite-schlieï¿½en-Alarm == //
	/*
	$('body').append('<div class="alert"><strong>Achtung:</strong><br />Ihre Daten wurden nicht gespeichert!<br />Wollen Sie den Browser schlieï¿½en? <a title="Ja" href="#">Ja</a>&nbsp;&nbsp;&nbsp;<a class="nein" title="Nein" href="#">Nein</a></div>');
	$('.alert').hide();
	$('li.close a').click(function() {
		$('body').append('<div class="closebg"></div>');
		$('.alert').show();
	});
	$('.alert a.nein').click(function() {
		$('.alert').hide();
		$('.closebg').remove();
	});
	*/
		
  // == Abgerundete Ecken bei Medien == //
	$('div.bildbox span:has(a.download)').addClass('bottom');
	
  // == Themen-Navi == //
	$('.themen ol li:odd, .box.themen2 ol li:odd').addClass('wechsel');
	$('.themen ol li').hover(function() {
		$(this).addClass('hover');
	  }, function() {
		$(this).removeClass('hover');
	});
	
	// == Top-Anker == //
	$('a.top').click(function() { 
		$('#content').animate({ scrollTop: 0 }, 400, function() { 
			$('<span class="oben"> - Sie befinden sich am Seitenanfang!</span>')
				.appendTo('#inhalt :header:first')
				.fadeOut(3400, function() { 
					$(this).remove();
				});
		}); 
		return false;
	});
	
	// == Kapitelliste == //


 	$('#kapitelliste').change(
		 function() {
		 	window.location.href = $(this).val();
		 }
	);
	
	$('#cmanCompendiumSelect').change(
		function(){
			if ( $(this).val() != '') window.location.href = $(this).val();
		}
	);
	
	$('#cmanLangSelect').change(
		function(){
			if ( $(this).val() != '') window.location.href = $(this).val();
		}
	);
	
	$.getScript('js_me/optional/selectbox.js', function() { 
		$('#cmanLangSelect, #cmanCompendiumSelect').selectbox();
	});
		
	
	
/* ======== ENDE Allgemeines ========= */



//* ======== Optionales ======== *//

  // == Checkboxen (Fragebogen) == //
	if($("form#fragebogen").length) { // JS-Datei nur laden, wenn sie auch tatsï¿½chlich benï¿½tigt wird... 
		$.getScript("js_me/optional/checkbox.js");
	};
	
  // == Textmarkierung == //
	if($("#breadcrumb:has(a.n_speichern)").length) { 
		$.getScript("js_me/optional/textspeichern.js");
	};

  // == Sortierbarkeit fÃ¼r Tabellen mit entsprechender Klasse == //
	if($("table.sortierbar").length) {
		$.getScript("js_me/optional/tablesorter.js", function() {
			$('table.sortierbar').tablesorter({
				sortForce: [[0,0]],
				highlightClass: 'highlight'
			});
		});
	};

  // == Thickbox == //
	if($('#inhalts_container a.play, #inhalts_container img[width="410"], #inhalts_container img[width="152"], #inhalts_container img[width="615"], .glbox a,dfn').length) { 
		$('#inhalts_container img[width="615"], #inhalts_container img[width="410"], #inhalts_container img[width="152"]').each(function() { 
			var imgtitel = this.alt;
			var imghref = this.src.replace(/_content/g, '_big');
			$(this).wrap('<a class="thickbox" title="'+imgtitel+'" href="'+imghref+'"></a>');
			$(this).filter('[@width="410"], [@width="152"]:not(.bild_links)').parent().parent().prepend('<a class="thickbox zoom" title="'+imgtitel+'" href="'+imghref+'"></a>');
		});
		$('p.rechte_spalte, div.rechte_spalte').next(':has(a.thickbox.zoom)').prev().prepend('<br /><br />');

		/* Videos */
		$('#inhalts_container a.play').show().addClass('thickbox');
		$('#inhalts_container a.play').each(function() {   
			var link = $(this).attr('href');
			var tb_einst = "&KeepThis=true&TB_iframe=true&height=456&width=480";
			var swf = 'grafiken/movie.php?swfname='+link.replace(/.swf/g, '')+tb_einst;
			var flv = 'grafiken/movie.php?flvname='+link.replace(/.flv/g, '')+tb_einst;			
			if ($(this).is('[@href$=".swf"]')) { 
				$(this).attr({ href: swf });
			} else if ($(this).is('[@href$=".flv"]')) { 
				$(this).attr({ href: flv });
			};
		});	
		
		$.getScript("js_me/optional/thickbox_lang.js", function() { 
			// Glossar
			$('.glbox a').click(function() {
				$.get("http://"+domaintxt+"/ajax/getLangTxt.php?sp="+sp+"&txt=Glossar","", function(data){
					tb_show(data, this.href+'?TB_iframe=true&height=250&width=400', null);
				}); 
				return false;
			});	
			$('dfn').attr('title',"click here for further informations");
			$('dfn').click(function() {
			    var classArr=$(this).attr('class').split('_');
				var glId=classArr[1];
				$.get("http://"+domaintxt+"/ajax/getLangTxt.php?sp="+sp+"&txt=Glossar","", function(data){
					tb_show(data, 'glossarText.php?tpl=tbtxt&sp='+sp+'&gbid='+glId+'&TB_iframe=true&height=250&width=400', null);
				}); 
				return false;
			});		
		});
	};
	// Startseite mit Sprachwahl
	if($("body.portal").length) { 
		$.getScript("js_me/optional/thickbox_lang.js", function() { 
			tb_show('Sprachwahl', 'sprachwahl.htm?TB_iframe=true&height=200&width=300&modal=true', null);
		});
	};
	
   // == Glossar-Btns == //
   if($(".a_z_leiste").length){
       $(".a_z").click(function(){
           $.get("http://"+domaintxt+"/glossarText.php?do=liste&char="+$(this).text()+"&sp="+sp,"",
                 function(data){
                     $(".glbox").remove();
                     $(".a_z_leiste").after(data);
                     $('.glbox a').click(function() {
                     	$.get("http://"+domaintxt+"/ajax/getLangTxt.php?sp="+sp+"&txt=Glossar","", function(data){
							tb_show(data, this.href+'?TB_iframe=true&height=250&width=400', null);
						 }); 
					     return false;
					 });	
					 $('dfn').attr('title',"click here for further informations");
					 $('dfn').click(function() {
					     var classArr=$(this).attr('class').split('_');
						 var glId=classArr[1];
						 $.get("http://"+domaintxt+"/ajax/getLangTxt.php?sp="+sp+"&txt=Glossar","", function(data){
							tb_show(data, 'glossarText.php?tpl=tbtxt&sp='+sp+'&gbid='+glId+'&TB_iframe=true&height=250&width=400', null);
						 }); 
						 return false;
					 });
                 });           
       });
   }	
   
   // Autocomplete mit Glossarbegriffen fÃ¼r Glossarsuche/Suche
    if($(".autocomplete").length) { 
		$.getScript("js_me/optional/jquery.autocomplete.js", function() { 
		    $.get("http://"+domaintxt+"/ajax/sucheSuggest.php?sp="+sp,
    	 		function(data){
            		$(".autocomplete").autocomplete(eval('(' + data + ')'), { 						
            		//	minChars: 1,
					//	max: 5,
					//	matchContains: true
					//	//scroll: true,
					//	//scrollHeight: 120
            		});
            	});
		});
	};
   

//* ======== ENDE Optionales ========= *//

  // == Tooltip == //
	$.getScript("js_me/optional/tooltip.js", function() { 
		$('.login label[title]').Tooltip({
			extraClass: "maxwidth",
		    track: true,
			delay: 0,
			showURL: false
		});
		$('body').find('#tooltip').hide();
	});

	$("form#glossar").keypress(function(event){
        if(event.keyCode==13){
	        searchGlossar();
	        return false;
        }
    });
    $("form#begriffe").keypress(function(event){
        if(event.keyCode==13){
	        searchContent();
	        return false;
        }
    });
    
    // Kasuistiken
	$('table.kasu2 span.open, table.kasu2 td h4').click(function() { 
		$(this).parent().parent().find('dl').slideToggle(500);
		$(this).parent().parent().find('span.open').toggleClass('an');
	});
	$('table.kasu2 tr:odd').addClass('wechsel');
	$('table.kasu2:first tr:first-child dl').show().parent().parent().find('.open').addClass('an');
	
	$('a.kasu_an').click(function() { 
		$('dl').slideDown(500);
		$('span.open').addClass('an');
		return false;
	});
	$('a.kasu_aus').click(function() { 
		$('dl').slideUp(500);
		$('span.open').removeClass('an');
		return false;
	});

	// == Button-Layer für Video-Tabelle == //
	$('table.videotab td a:has(img)').each(function() { 
		$(this).append('<a class="layerlink" href="#" title="Video"></a>');	
	});	

 	// == Datei/Bild merken == //
	/* 	$('.bildbox a.drucken, td.text a.drucken').after('<div class="gemerkt">Datei gemerkt</div>'); */
	/*	$('.bildbox a.drucken, td.text a.drucken').click(function() {
		$(this).toggleClass('merken');
		$(this).parent().find('.gemerkt').slideToggle(500);
		return false;
	});*/
	
	$('.bildbox a.drucken, td.text a.drucken').click(function() {
		
		//alert( $(this).attr('id') );
		var mediasaved = $(this);
		$.post("./ajax/medienmerken.php", 
				{ idmedia_elements: $(this).attr('id') },
			  	function(data){
				    //alert("Data Loaded: " + data);

				    if ( data == 'noerror' ){
				    	//var mediasaved = true;
						//$(this).toggleClass('merken');
						mediasaved.toggleClass('merken');
						//$(this).parent().find('.gemerkt').slideToggle(500);
						mediasaved.parent().find('.gemerkt').slideToggle(500);
				    }

			  	});	

		return false;
	});
	
  
  // == Notiz löschen == //
	$(".notizloeschen").click(function() {
		$.post("./ajax/merken.php?del=1", 
				{ merklisteid: $(".notizloeschen").attr('id')},
			  	function(data){
				    //alert("Data Loaded: " + data);
				    if ( data == 'noerror' ){
						$("#tr_"+$(".notizloeschen").attr('id')).remove();
						
				    }
			  	});			
		return false;
	});


  // == Notiz Anmerkung einfügen == //
	$("div.notiz input.button").click(function() {
		var notizid = $(this).attr('id');
		//var anmerkung = $("#text_"+notizid).val();
		$.post("./ajax/merken.php?anm=1", 
				{ merklisteid: notizid, anmerkung: $("#text_"+notizid).val()},
			  	function(data){
				    //alert("Data Loaded: " + data);
				    if ( notiz_gespeichert == '' ){
				    	 notiz_gespeichert = $.ajax({
							  type: "GET",
							  url: "./ajax/merken.php?getword=notiz_gespeichert&sp="+sp,
							  async: false
							 }).responseText;
				    }
				    if ( data == 'noerror' ){
						$('#head').append('<div class="gesichert">'+notiz_gespeichert+'!</div>');
						$('.gesichert').hide().fadeIn(100, function() { 
							$(this).fadeOut(2000);
			
						});
						return false;
				    }
			  	});			
		return false;
	});
    
});


/* ==== GlossarSuche ====*/
function searchGlossar(){
    $.post("http://"+domaintxt+"/glossarText.php?do=suche&sp="+sp,{suchBegriff:$('#glossar_suche').val()},
        function(data){
            $('#sresult').remove();
            $(".abstand2").before("<div id=\"sresult\">"+data+"</div>");
            $.getScript("js_me/optional/jquery.scrollTo.js",function(){
                $('#content').scrollTo('#sresult',800,{});
            })
            
        }
        );
        return false;
}
/* ==== Suche ====*/
function searchContent(){
    if ( did == '0' ){
    	searchdid = 'all'; 
    }
    else {
    	searchdid = did;
    }
    
    $.post("http://"+domaintxt+"/ajax/sucheContent.php?searchFor="+searchdid+"&sp="+sp,{suchBegriff:$('#begriff_suche').val()},
        function(data){
            $('#sresult').remove();
            $(".abstand2").before("<div id=\"sresult\">"+data+"</div>");
            $.getScript("js_me/optional/jquery.scrollTo.js",function(){
                $('#content').scrollTo('#sresult',800,{});
            })        
        }
        );
        return false;
}

//* ======== Registrierung ======== *//

$(document).ready(function()
	{
		$('#uad_country').change(
			function()
			{	
				$.post('ajax/getLangForJscript.php',{'lang':sp,'var':'RegErr_Ansprechpartner_waehlen'},
					function(msg)
					{
						optionText=msg;
					}
				);
				$.post('index.php',{"did":0,"sp":"de","id":"registrierung","content":"getfsfoptions","countryId":$(this).val()},function(msg){$('#uad_forceId').html(msg).prepend('<option value="NULL">'+optionText+'</option>');});
			}
		);
	}
);

/* === Kasuistiken === */

function addAutor(sp) {
	var anr = $(".kasu_autoren").length;
	$.post("http://"+domaintxt+"/ajax/kasuistiken.php?sp="+sp,{
		action: 'addAutor',
		aid: anr
	},
        function(data){
            $('#kasu_autoren').append(data);
        }
    );
}

function deleteAutor(gid) {
	$('#kasu_autor_anr'+gid).val('');
	$('#kasu_autor_vn'+gid).val('');
	$('#kasu_autor_nn'+gid).val('');
	$('#kasu_autoren'+gid).hide();
}


function addGrafik(sp) {
	var gnr = $(".kasu_grafiken").length;
	$.post("http://"+domaintxt+"/ajax/kasuistiken.php?sp="+sp,{
		action: 'addGrafik',
		gid: gnr
	},
        function(data){
            $('#kasu_grafiken').append(data);
        }
    );
}

function deleteGrafik(gid) {
	$('#kasu_graf_datei'+gid).val('');
	$('#kasu_grafiken'+gid).hide();
}
// Standard-JS-PopUp
function popup(url, title) {
    var leftPos = (screen.width) ? (screen.width - 640) / 2:0;
    var topPos = (screen.height) ? (screen.height - 540) / 2:0;
    var props = "width=640, height=540, left="+leftPos+", top="+topPos+" toolbar=0, personalbar=0, menubar=0, scrollbars=1, resizable=0, status=0";
    var Fenster = window.open(url, title, props);
    if (window.focus) {
        Fenster.focus()
    }
    return false;
};
