function checkHovers() {
	jQuery('#navigation ul li ul').each(function(i){
	
		x = parseInt($(this).css('left'));
				
		if (x>-1) {
			$('#navigation ul > li:eq('+(i+1)+')').addClass('hover');
		} else {
			$('#navigation ul > li:eq('+(i+1)+')').removeClass('hover');
		}
		
	
	});
}


function calculateTimeout(currElement, nextElement, opts, isForward) { 
	var index = opts.currSlide;
  	var time = jQuery('.headgal'+index).find('.headgal-time').html();
  	time = parseFloat(time);
  	
  	return time;
}

jQuery(document).ready(function(){
	
	// COUNTER-KLASSEN FUER HEADGALERIE ELEMENTE
	var headcount = 0;
	jQuery('.headgal-item').each(function(){
		jQuery(this).addClass('headgal'+headcount);
		headcount++;
	});

	var IE6 = false;
	if (jQuery.browser.version == "6.0" && document.attachEvent) {
		IE6 = true;
	}

	//target blank
	jQuery("a[href^='http://']").attr('target','_blank');
	jQuery("a[href^='http://']").addClass('extern');
	
	
	jQuery("a[href$=.pdf]").addClass('pdf');
	jQuery("a[href$=.doc]").addClass('doc');
	jQuery("a[href$=.xls]").addClass('xls');
	jQuery("a[href$=.docx]").addClass('docx');
	jQuery("a[href^=mailto]").addClass('mailto');
	
	jQuery("ul li:first-child").addClass('first');
	jQuery("ul li:last-child").addClass('last');
	
	
	
	var sitemapcode = jQuery('#sitemap').html();
	jQuery('#sitemap').remove();
	jQuery('#container').prepend('<div id="sitemap" style="display: block;">'+sitemapcode+'</div>')
	
	jQuery('#sitemap-link').show();
	jQuery('#sitemap-content').hide();
	
	jQuery('#sitemap-link a').click(function(){
		if (jQuery("#sitemap-content").is(":hidden")) {
			jQuery('#sitemap-content').slideDown(1000);
			jQuery('#sitemap-link a').addClass('active');
		}
		
		else {
			jQuery('#sitemap-content').slideUp(1000);
			jQuery('#sitemap-link a').removeClass('active');
		}
		jQuery(this).trigger('blur');
      	return false;
	});
	 
	 
	jQuery('a span.soh-mouseover').removeClass('soh-mouseover');
	jQuery("a").hover(function(){
		jQuery(this).find('span.soh').addClass('soh-mouseover');
	},function(){
			
		var thisparent = jQuery(this).parent().parent().parent();	
					
		if(thisparent.hasClass('nav-inner')){

		} else {
			jQuery(this).find('span.soh').removeClass('soh-mouseover');
		}
		
	});
	
	jQuery('#navigation .active a span.soh').addClass('soh-mouseover');
	jQuery('#navigation .active a span.soh').addClass('soh-mouseover');
	
	jQuery("#navigation li").hover(function(){
		jQuery(this).find('span.soh').addClass('soh-mouseover');
	},function(){
			
		jQuery(this).find('span.soh').removeClass('soh-mouseover');
		if(jQuery(this).find('span.soh').hasClass('soh-active')){ jQuery(this).find('span.soh-active').addClass('soh-mouseover'); }
		
	});


	//for special effects in the navi
	
	// IE6 flickers with these lines
	if (!IE6) {
		$('#navigation ul ul').parent().addClass('sub');		
		
		//fix the corner for hovers
		$('#navigation ul ul').parent().mouseover(function(){
			$(this).children('a').addClass('corner');
		});
		$('#navigation ul ul').parent().mouseout(function(){
			$(this).children('a').removeClass('corner');
		});
	} else {
		// Fix IE6 again..
		setInterval('checkHovers()',100);
	}
		
	$("#navigation li.sub").hover(
		function () {
			$(this).addClass('hover');
		}, 
		function () {
			$(this).removeClass('hover');
		}
	);

	if (document.attachEvent)
	{
		// Suckerfish Hovers
		var sfEls = document.getElementById("navigation").getElementsByTagName("UL")[0].getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}

	/* Bookmark this page */
	jQuery('a.bookmark').mouseover(function(){	jQuery(this).attr('href','');  });
	jQuery('a.bookmark').click(function(){
		var title = document.title;
		var url = window.location.href;
		   if (document.all)
		     window.external.AddFavorite(url, title);
		   else if (window.sidebar)
		     window.sidebar.addPanel(title, url, "")
		   else if (window.sidebar&&window.sidebar.addPanel)
		     window.sidebar.addPanel(title,url,"");
		     
		return false
		     
	});
	
	if(jQuery('.chrome').size() > 0){
		jQuery('a.bookmark').parent().remove();
	}
	
	/* Druckvorschau */
	jQuery('a.printpreview').click(function(){
		x = window.open(window.location.href+"?print=1",'printpreview','width=700,height=500,scrollbars=yes');
		return false;
	});
	
	jQuery('a.print').click(function(){
		window.print();
		return false;
	});
	
	/* E-Mail this page */	
	jQuery('a.email').click(function(){
		window.location = 'mailto:?subject=Link: '+window.location.href+'&body='+window.location.href+"\n\n";
		return false;
	});
	
	//### minigallery-pager--------------------------------------------------

	$('div.galerie-container div.galitem p').css({display:'none'});
	
	$('div.galerie-container').each(function(i){
	
		$(this).children('div.galitem').each(function(j) {
			if (j>0) {
	//			$(this).children('div.galitem')
				$(this).css({opacity:"0"});
			}
		});
	
	});
	
	
	$('div.galerie-container').each(function(i){
		var x = $(this);
		var galitem = x.children('div.galitem:eq(0) p').html();
		
		
		//alert(galitem.html());
		
		pHtml = $(this).children('div.galitem p:eq(0)').html();	
		
		x.parent().children('p.galtext').html(pHtml);
		x.parent().children('p.galtext').css({zIndex:99,position:'absolute'});
	});
	
		
	$('div.galerie-container').bind('swap',function(e, myName, myValue){
		var x = $(this);		
			
			x.parent().children('p.galtext').animate({opacity:"0"},300);
		
			x.children('div.galitem:nth-child(2)').animate({opacity:"1"},1000,function(){
				
				pHtml = $('div.galerie-container div.galitem:eq(1) p').html();
				
				x.parent().children('p.galtext').html(pHtml);
				x.parent().children('p.galtext').animate({opacity:"1"},300);
					
				x.append(x.children('div.galitem:nth-child(1)'));
				x.children('div.galitem:last-child').css({opacity:"0"});
						
				//reset, just in case someone is clicking too much.
				x.children('div.galitem:first-child').css({opacity:"1"});
				
				x.children('div.galitem').each(function(i) {
					if (i>0) { 
						$(this).css({opacity:"0"}); 
						$(this).children('p').css({opacity:"1"});
					}
				});
			});		
	});
	
	
	setInterval(function(){
		$('div.galerie-container').trigger('swap');
	},8000);
	
	//###----------------------------------------------------------------------------------------------

	jQuery('#contactmenu').show();
	
	jQuery('#contactmenu p a').click(function(){

		if(jQuery('#contactnavi').is(':hidden')){
		
			if(jQuery('.msie').size() > 0 ){
				jQuery('#contactnavi').show();
			} else {
				jQuery('#contactnavi').fadeIn(350);
			}
			
		} else {
		
			if(jQuery('.msie').size() > 0 ){
				jQuery('#contactnavi').hide();
			} else {
				jQuery('#contactnavi').fadeOut(350);
			}
		}
		
		jQuery(this).trigger('blur');
		return false;
			
	});
	
	jQuery('body').click(function(){
	
		if(jQuery('#contactnavi').is(':visible')){
			
			if(jQuery('.msie').size() > 0 ){
				jQuery('#contactnavi').hide();
			} else {
				jQuery('#contactnavi').fadeOut(350);
			}
			
		}
	
	});

	
	// HEADER SLIDER
	if(jQuery('.headgal1').size() > 0 ){	
		jQuery('#header-gallery').before('<div id="pager">').cycle({ 
		    fx: 'scrollHorz', 
		    speed: 1000, 
		    pager:  '#pager',
		    timeoutFn: calculateTimeout
		});
	}
		
	jQuery('#pager a').click(function(){
		jQuery('#header-gallery').cycle('pause');
		jQuery(this).trigger('blur');
	});
	
	//jQuery('#contactnavi-content').removeClass('invsbl');
	//jQuery('h3#contactnav').addClass('invsbl').hide();
	
	jQuery('#contactnavi-content ul li.active span.soh').addClass('soh-active');
	

	// Formularstruktur umbauen
	
	var timebox = jQuery('.form-rueckruf .time').html();	
	jQuery('.form-rueckruf .time').remove();
	
	jQuery('.form_time .formradio:last-child').append('<div class="time">' + timebox + '</div>');
	
	
});
