$(document).ready(function() {
	// the startpage animation
	$('div.#startblock > div').each(function(){
		$(this).hover(function(){

			$('div.#startblock > div:not(.current)')
				.find('img#sujet-background').stop()
				.animate({opacity:'1.0'}, {queue:false, duration:1500})
				.find('div#info-text').stop();

			$(this).addClass('current');

			$('div.#startblock > div:not(.current)').each(function(){
				$(this).animate({width:'115px'}, {queue:false, duration:1500});
			});

			$(this).animate({width:'306px'}, {queue:false, duration:1500});
			$(this).find('img#sujet-background').animate({opacity:'0.5'}, {queue:false, duration:1500});
			$(this).find('div#info-text').css('opacity','0').css('display','block').animate({opacity:'1'}, {queue:false, duration:1000});
			$(this).find('span.highlight-more-link').fadeIn();

		}, function(){
			$(this).find('div#info-text').fadeOut();
			$(this).find('span.highlight-more-link').fadeOut();
			$('div.#startblock > div:not(.current)').find('img#sujet-background').animate({opacity:'1.0'}, {queue:false, duration:1500});
			$(this).stop();
			$(this).removeClass('current');
			$('div.#startblock > div:not(.current)').stop();
		});
	});

	// mouseleave the entire block at startpage
	$('div#startblock').mouseleave(function(){
		$('div.#startblock > div').stop();
		$('div.#startblock > div').each(function(){
			$(this).animate({width:'147px'}, {queue:false, duration:1500}).find('img#sujet-background').animate({opacity:'1.0'}, {queue:false, duration:1500});;
		});

	});
	$('div#startholder, div#startpage-logo').mouseenter(function(e){
		$('div#startpage-logo').fadeOut();
	});
	$('div#startholder').mouseleave(function(){
		$('div#startpage-logo').fadeIn();
	});

	// background animation in navigation
	if(!(jQuery.browser.msie && jQuery.browser.version == "6.0")) {
		$('ul.current a').each(function(){
			$(this).mouseover(function(e){
				var link = $(this).position();
				var ul = $('ul.current').position();
				var top = link.top - ul.top;
				//alert(ul.top + '/' + link.top);
				$('ul.current').stop().animate(
					{backgroundPosition:"(0 "+ top +"px)"},
					{duration:500})
				});
			});
		$('ul.current').mouseout(function(){
			$(this).animate({backgroundPosition:'(0 -15px)'}, {duration:500, queue:false});
		});
	}

	// ie6 png fix
	if (navigator.appVersion.indexOf("MSIE 6") > -1) $("img[src$='.png']").ifixpng();
	//$('ul.current').ifixpng();


	// datepicker will crash ie7
	$('.datePicker').datePicker({clickInput:true});

	// gallery scroll
	$('div#galleryTop a#galleryTopLink').click(function(){
		$('div#gallery').scrollTo( '-=193', 800 , {axis:'y'});
		return false;
	});
	$('div#galleryBottom a#galleryBottomLink').click(function(){
		$('div#gallery').scrollTo( '+=193', 800 , {axis:'y'});
		return false;
	});

	//hover for images (colorScheme is set in pagelayout)
	$('li.gallery').hover(function(){
		$(this).find('div.attribute-caption').toggleClass(colorScheme);
	}, function(){
		$(this).find('div.attribute-caption').toggleClass(colorScheme);
	});

	// click event on gallery-items
	//if(!(jQuery.browser.msie && jQuery.browser.version == "6.0")){

		$('li.gallery').live('click', function(e) {
			e.preventDefault();
			var _url = '/gallery/getArticle?node=' + $(this).attr('node') + '&colorScheme=' + colorScheme;
			$.nyroModalManual({
				url: _url,
				zIndexStart : 10000,
				minWidth: '650',
				minHeight: '300',
				endFillContent: function(elts, settings){
					var modalPrint = elts.content.find('a#modalPrint');
					$(modalPrint).attr('href', _url + '&mode=print');
				}
			});
			return false;
		});
	//}

	// gallery functions
	// gallery switches left and right
	// switch on link-click
	//if(!(jQuery.browser.msie && jQuery.browser.version == "6.0")){
		var active = 0;
		// collect nodes
		var nodes = new Array();
		$('a.galleryLink').each(function(){
			nodes.push($(this).attr('node'));
		});

		$('ul.gallery a.galleryLink').click(function(){
			$('div#gallery').fadeOut();
			$('div#galleryLoader').fadeIn();
			$('div#galleryInner').load('/gallery/getImages', 'node=' + $(this).attr('node'), function(responseText, textStatus, XMLHttpRequest){
				$('div#gallery').fadeIn();
				$('div#galleryLoader').fadeOut();
			});

			for( i in nodes){
				if(nodes[i] == $(this).attr('node')){
					active = parseInt(i);
				}
			}
			return false;
		});


		// click on left
		$('a#galleryLeftLink').click(function(){
			var newActive = active - 1;
			if(newActive == -1) return false;
			$('div#gallery').fadeOut();
			$('div#galleryLoader').fadeIn();
			$('div#galleryInner').load('/gallery/getImages', 'node=' + nodes[newActive], function(responseText, textStatus, XMLHttpRequest){
				$('div#gallery').fadeIn();
				$('div#galleryLoader').fadeOut();
			});

			for( i in nodes){
				if(i == newActive){
					active = parseInt(i);
				}
			}
			return false;
		});
		// click on right
		$('a#galleryRightLink').click(function(){
			var newActive = active + 1;
			if(newActive >= nodes.length) return false;
			$('div#gallery').fadeOut();
			$('div#galleryLoader').fadeIn();
			$('div#galleryInner').load('/gallery/getImages', 'node=' + nodes[newActive], function(responseText, textStatus, XMLHttpRequest){
				$('div#gallery').fadeIn();
				$('div#galleryLoader').fadeOut();
			});

			for(var i in nodes){
				if(i == newActive){
					active = parseInt(i);
				}
			}
			return false;
		});
	//}

	// the search form
	$("input[name='SearchText']").val('Suche').blur(function(){
		if(!$(this).val()){
			$(this).val('Suche');
		}
	}).focus(function(){
		if($(this).val() == 'Suche'){
			$(this).val('');
		}
	});


	// fix for ie6
	if(jQuery.browser.msie && jQuery.browser.version == "6.0"){
		window.setTimeout(function(){$('.print-link').removeClass('ie7_class0')}, 500);
		window.setTimeout(function(){$('#highlight-background-trick').show()}, 500);
	}
});
