$(document).ready(function() {
	/* **********************************************************************
	 * Erweiterung wg. dynamischer Layoutvorlagen (TK - 20.01.2010):
	 * Untertitel müssen via JS an die Breite des img-Tags angepasst werden, 
	 * da nirgends eine feste Breite hinterlegt ist und alles gefloatet wird!
	 * **********************************************************************/
	if($(".varCaption").length > 0){
		$(".varCaption").each(function () {
			/*if($(this).parent("div").hasClass("varImageCenter")){
				// keine Breitenzuweisung, da Untertitel über die volle Breite gehen soll!
			}else{*/
				$(this).parent("div").find("img").load(function(){
					widthImg = $(this).width();
					$(this).parent("div").find(".varCaption").css("width",widthImg+"px");
				});
			//}
		});	
	};
	
});

$(function(){	
	
	$("ul.zebra-list li:odd").addClass("odd");	
	
/*
	$('a.text-increase').click(function(){
		$('a.text-decrease').removeClass('active');
		$('a.text-increase').addClass('active');		
		$('body').addClass("text-increased");
		var today = new Date();
		today.setTime( today.getTime() );
		var expires_date = new Date( today.getTime() + (1000) );

		document.cookie = "font-size=large;";
		return false;
	})
	$('a.text-decrease').click(function(){
		$('a.text-increase').removeClass('active');
		$('a.text-decrease').addClass('active');
		$('body').removeClass("text-increased");
		var today = new Date();
		today.setTime( today.getTime() );
		var expires_date = new Date( today.getTime() + (1000) );

		document.cookie = "font-size=small;";
		return false;
	})
	$('a.text-contrast').click(function(){
		//toggle contrast
		if(!$('body').hasClass('text-contrasted')){
			$('.text-contrast').addClass("active");
			$('head').append('<link rel="stylesheet" type="text/css" href="$file/contrast.dark.css" />');
			$('body').addClass("text-contrasted");
			var today = new Date();
			today.setTime( today.getTime() );
			var expires_date = new Date( today.getTime() + (1000) );

			document.cookie = "contrast=dark;"
		} else {
			$('.text-contrast').removeClass("active");			
			$('body').removeClass("text-contrasted");
			var today = new Date();
			today.setTime( today.getTime() );
			var expires_date = new Date( today.getTime() + (1000) );

			document.cookie = "contrast=light;";
		}
		return false;
	})
	
	*/
	
    /* jr/fdi 2.11.2010 auskommentiert
	if( $("div.row div.image a").length > 0 ) {
		$("div.row div.image a img").each( function() {
			var link = $(this).parent('a').attr('href');			
			$(this).parent('a').css({"position":"relative"}).removeAttr("href"); 			
			var html_link= $('<a rel="gal" class="fb_image" style="position:absolute;right:2px;bottom:4px;" href="'+link+'"></a>');
			$(this).after(html_link);
			html_link.fancybox();
		});
	}
    */
	
	// verstecke leere .row Elemente
	
	if( $(".row").length > 0 ) {
		$(".row").each( function() {
			if( $.trim( $(this).html() ) == "" ) {
				$(this).hide();
			}
		});		
	}
	
	
  
	/* form validation */
	if($("[name='_formular']").length > 0 ) {
		$("[name='_formular']").validate();
	}
	/* navi */
	$("#search_query").click(function(){
		if($(this).val()=="Suchbegriff...")
		$(this).val("");
	});
	$("#start_search").click(function(){
		var query = $('#search_query').val();
		var dbpath = $("#dbpath").val();
		document.location.href= dbpath + "results?SearchView&Query="+query+" AND [sprache]=de&SearchOrder=1&SearchMax=1000&SearchWV=FALSE&SearchThesaurus=TRUE&lang=de"; 
		return false;
	});
	// Bürger ABC
	if( $("#buergerABC").length > 0 ) {
		$("#buergerABC").appendTo( $("#content") );
	}
	
	// Subnav
	$('ul#nav>li').mouseenter(function(){
				
		if($(this).find('ul.sub-nav:hidden').length > 0)
		{
			$('a.opened').removeClass('opened');
			$('ul.sub-nav:visible').fadeOut('fast');
			$(this).find('a').addClass('opened');
			$(this).find('ul.sub-nav').show();
		}
		else
		{
			$('a.opened').removeClass('opened');
			$('ul.sub-nav:visible').fadeOut('slow');
		}
	})
	$('ul.sub-nav').mouseleave(function(){
		$(this).fadeOut('slow');
		$('a.opened').removeClass('opened');
	})

	
	$('table.zebra-list tr:odd').addClass("odd");		



	//----------------- BILDERGALERIE -----------------
	try
	{		
		var gallery = $('#thumbs').galleriffic({
	        delay:                     3000, // in milliseconds
	        numThumbs:                 5, // The number of thumbnails to show page
	        preloadAhead:              10, // Set to -1 to preload all images
	        enableTopPager:            false,
	        enableBottomPager:         true,
	        maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
	        imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
	        controlsContainerSel:      '#controls', // The CSS selector for the element within which the slideshow controls should be rendered
	        captionContainerSel:       '#caption', // The CSS selector for the element within which the captions should be rendered
	        loadingContainerSel:       '#loading', // The CSS selector for the element within which should be shown when an image is loading
	        renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
	        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
	        playLinkText:              'Galerie abspielen',
	        pauseLinkText:             'Galerie anhalten',
	        prevLinkText:              'vorheriges Bild',
	        nextLinkText:              'n&auml;chstes Bild',
	        nextPageLinkText:          'n&auml;chste Seite &rsaquo;',
	        prevPageLinkText:          '&lsaquo; vorherige Seite',
	        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
	        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
	        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	        defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
			onSlideChange:             undefined,
	        onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
	        onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
			onPageTransitionOut:       undefined,
			//onPageTransitionIn:        undefined,
			onPageTransitionIn:        function() {
				var prevPageLink = this.find('a.prev').css('display', 'none');
				var nextPageLink = this.find('a.next').css('display', 'none');
				
				if (this.displayedPage > 0)
					prevPageLink.css('display', 'block');

				var lastPage = this.getNumPages() - 1;
				if (this.displayedPage < lastPage)
					nextPageLink.css('display', 'block');

				this.fadeTo('fast', 1.0);
			},
	        onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
	        onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
	    });	
		
		/**************** Event handlers for custom next / prev page links **********************/
		gallery.find('a.prev').click(function(e) {
			gallery.previousPage();
			e.preventDefault();
		});

		gallery.find('a.next').click(function(e) {
			gallery.nextPage();
			e.preventDefault();
		});
	}
	catch (err)
	{
		// bei Fehler nicht initialisieren
	};	
	//----------------- /BILDERGALERIE -----------------	
	
	//----------------- FORMULAR-VALIDIERUNG -----------------
	if( $("#formular_content").length > 0 ) {
		$("[name='_formular']").validate();
	};

});
