//Quelle: http://code.google.com/p/jquery-autocomplete/source/browse/jquery.autocomplete.js  
// ...Erweiterung von http://docs.jquery.com/Plugins/Autocomplete (siehe oben)

$(document).ready(function() {
	
	$(function() {
	   if ($('#facsimile').length > 0) {
	       $('#facsimile').bind("contextmenu",function() {return false;});
	   }
	});

	$(function() {
		
		if ($('#vector').length > 0) {
			
			var url = jQuery.trim($('#vector').text());
			
			$('#vector').css("display", "block") ;
			$('#vector').html("&nbsp;");
			$('#vector').addClass("loadingAnim");

			$.ajax({
				url: url,
				success: function(data) {
					$('#vector').removeClass("loadingAnim");
					$('#vector').html(data);
				}
			});	
		}
	});
	
	$(function() {
		
		if ($('#LOC_ent').length > 0) {
			
			var url = jQuery.trim($('#LOC_ent').text());
			
			$('#LOC_ent').css("display", "block") ;
			$('#LOC_ent').html("&nbsp;");
			$('#LOC_ent').addClass("loadingAnim");

			$.ajax({
				url: url,
				success: function(data) {
					$('#LOC_ent').removeClass("loadingAnim");
					$('#LOC_ent').html(data);
				}
			});	
		}
	});
	
	$(function() {
		
		if ($('#PER_ent').length > 0) {
			
			var url = jQuery.trim($('#PER_ent').text());
			
			$('#PER_ent').css("display", "block") ;
			$('#PER_ent').html("&nbsp;");
			$('#PER_ent').addClass("loadingAnim");

			$.ajax({
				url: url,
				success: function(data) {
					$('#PER_ent').removeClass("loadingAnim");
					$('#PER_ent').html(data);
				}
			});	
		}
	});
	
	$(function() {
		
		if ($('#numdocs').length > 0) {
			
			var url = jQuery.trim($('#numdocs').text());
			
			$('#numdocs').css("visibility", "visible") ;
			$('#numdocs').html("");

			$.ajax({
				url: url,
				success: function(data) {
					$('#numdocs').html(data);
				}
			});	
		}
	});
	
	$(function() {
		if ($('#fulltext')) {
			var options = { 
				serviceUrl: autoSuggestURL,
				minChars:3, 
				maxHeight:400,
				width:544,
				zIndex: 9999,
				deferRequestBy: 500,  
				noCache: false
			};
			$('#fulltext').autocomplete(options);
		}
	});
	
	$(function() {
		if ($('#context')) {
			var options = { 
				serviceUrl: autoContextURL,
				minChars:3,  
				maxHeight:400,
				width:544,
				zIndex: 9999,
				deferRequestBy: 500,  
				noCache: false
			};
			$('#context').autocomplete(options);
		}
	});

	// autocomplete fuer mehr als fulltext
	// Standard beim erstmalig Laden der Seite
	
	var acA, acB, acC;
	
	$(function() {
		acA = registerAutoComplete("#txA", $('#flA').val() + "_key");
	});
	
	$(function() {
		acB = registerAutoComplete("#txB", $('#flB').val() + "_key");
	});
	
	$(function() {
		acC = registerAutoComplete("#txC", $('#flC').val() + "_key");
	});
	
	// Autocomplete neu registrieren beim Select
	$('#flA').change(
		function() {
			$('#' + acA.mainContainerId).remove();
			acA = registerAutoComplete("#txA", $('#flA').val() + "_key");
		}
	);	
	
	$('#flB').change(
		function() {
			$('#' + acB.mainContainerId).remove();
			acB = registerAutoComplete("#txB", $('#flB').val() + "_key");
		}
	);
	
	$('#flC').change(
		function() {
			$('#' + acC.mainContainerId).remove();
			acC = registerAutoComplete("#txC", $('#flC').val() + "_key");
		}
	);
	
	function registerAutoComplete(target, field){
		if ($(target)) {
			var options = { 
				serviceUrl: autoCompleteURL,
				minChars:3, 
				maxHeight:400,
				width:350,
				zIndex: 9999,
				deferRequestBy: 500, 
				params: {dictionary: field}, 
				noCache: false
			};
			return $(target).autocomplete(options);
		}
	}


//	$('.coll2 .coll2Visible1:even').css('background-color','grey');

	//Drilldownmenue zeigt nur die ersten 8 Items in der Liste und danach ein "Mehr" bzw. "Weniger" Link um die Liste ganz zu oeffnen (multilingual)
	$('ul.drillDown').each(function() {
		$.more = $('.hidden a:first-child').text();
		$.less = $('.hidden a:last-child').text();
		var $this = $(this), lis = $this.find('li:gt(7)').hide();
		if(lis.length>0){
			$this.append($("<li class='more'>").append($("<a>").text($.more)).click(function(){
				lis.toggle();
				$(this).find('a').text($(this).text() === $.more ? $.less : $.more);
			}));
		}
	});


});
