$(document).ready(function(){

	$('a.to-top').click(function(){$.scrollTo('#top','normal'); return false;});

	if (navigator.userAgent.indexOf('Windows')>0) {
  		$('.button_newsletter a, .button_tellafriend a').css('font-size','13.5px');
    }

	
	$('.nav li ul').hide();
	$('.nav li.current_page_ancestor ul').show();
	$('.nav li.current_page_item ul').show();
	
	$('body.einladen a.show_tipp, body.single.blog a.show_tipp').click(function(){
		$('span.tipp').toggle();
	});
	
	$('.newsletter p img').unwrap();
	
	
	
	var h1 = $('.sidebar-left').height();
	var h2 = $('.content').height();
	if (h1>h2) $('.sidebar-left .feature-area-guenter').remove();
	var h1 = $('.sidebar-left').height();
	if (h1>h2) $('.sidebar-left .feature-banner:eq(0)').remove();
	var h1 = $('.sidebar-left').height();
	if (h1>h2) $('.sidebar-left .feature-banner:eq(0)').remove();
	var h1 = $('.sidebar-left').height();
	if (h1>h2) $('.sidebar-left .feature-banner:eq(0)').remove();
	
	$('.nav li a, .subnav li a').each(function(){
		$(this).after('<span class="bg-navitem">&nbsp;</span>');
		var h = $(this).parent().height();
		var w = $(this).parent().width();
		$(this).parent().find('.bg-navitem').css({'height':h,'width':w}).hide();
		$(this).hover(function(){
				var h = $(this).parent().height();
				var w = $(this).parent().width();
				
				$(this).parent().find('.bg-navitem').show();
				var dim = [w,h];
				$(this).parent().find('.bg-navitem').css({'height':0,'width':0,'left':dim[0]/2,'top':dim[1]/2});
				$(this).parent().find('.bg-navitem').animate({'height':dim[1],'width':dim[0],'left':0,'top':0},100);
			},
		function(){
				$(this).parent().find('.bg-navitem').fadeOut(100);
			}
		);
	});
	
	$('.link_buy a').each(function(){
		$(this).addClass('button-buy').wrapInner('<span />');
		$(this).after('<span class="button_stub"></span>');
	});
	
	
	
	window.changeSlide = function(stage, slide) {
		window.clearTimeout(window['stageTimeout' + stage]);
		var s = $('#stage-' + stage);
		if ($('#stage-' + stage).data('state') != 'init') {
			$('#stage-' + stage).find('.bg .claim-container').animate({'width':0},200);
			$('#stage-' + stage).find('.bg').removeClass('bg');
			$('#slide-' + stage + '-' + s.data('current_slide')).removeClass('fg').addClass('bg');
		} 
		else {
			$('#stage-' + stage).data('state', 'normal');
		}
		
		tmp = $('#stage-' + stage).data('current_slide');
		tmp++;
		if (tmp > $('#stage-' + stage).data('count_slides') - 1) tmp = 0;
		if (slide != 'auto') tmp = slide;
		$('#stage-' + stage).data('current_slide', tmp);
		$('#stage-' + stage).find('.slide').not('.bg').hide();
		$('#slide-' + stage + '-' + tmp).addClass('fg').hide().fadeIn(1000);
		$('#slide-' + stage + '-' + tmp).find('.claim-container').css('width',0).show().animate({'width':0},500).animate({'width':'300px'},500);
		$('#stage-' + stage).find('.nav-stage ul li').removeClass('active');
		$('#stage-' + stage).find('.nav-stage ul li#item-' + stage + '-' + tmp).addClass('active');
		if (slide == 'auto') {
			window['stageTimeout' + stage] = window.setTimeout(function() {
				window.changeSlide(stage, 'auto');
			}, 8000);
		} else {
			window.clearTimeout(window['stageTimeout' + stage]);
		}
		
	};
	

	window.addPreloader = function(stage) {
		window.animatePreloader = function(stage) {
			var s = $('#stage-' + stage);
			var h = 40;
			var o = s.find('.stage-preloader').data('bgoffset') - h;
			if (o <= -480) o = 0;
			s.find('.stage-preloader').css('background-position', '0 ' + o + 'px');
			s.find('.stage-preloader').data('bgoffset', o);
		}
		var s = $('#stage-' + stage);
		s.find('.stage-preloader').data('bgoffset', 0);
		window['preloaderInterval' + stage] = window.setInterval(window.animatePreloader, 50, stage);
	}
		
	var b = 0;
	$('.stage').each(function() {
		$(this).attr('id', 'stage-' + b);
		$(this).data('count_slides', $(this).find('.slide').length);
		$(this).data('current_slide', -1);
		$(this).data('slides_loaded', 0);
		$(this).data('state', 'init');
		if ($.browser.msie===true) {
			$(this).find('.slide img').each(function(){
			var ts = new Date().getTime();
			var s = $(this).attr('src')+'?'+ts;
			$(this).attr('src',s);
		});
	}
	
	if ($(this).find('.slide').length > 1) {
		$(this).find('.slide img').each(function() {
			if (this.complete) {
				var s = $(this).parents('.stage');
				var tmp = s.data('slides_loaded') + 1;
				$(this).parents('.slide').hide();
				s.data('slides_loaded', tmp);
				if (tmp == s.data('count_slides')) {
				    s.find('.slides').css('visibility', 'visible');
				    s.find('.nav-stage').css('visibility', 'visible').hide().fadeIn('normal');
				    s.find('.stage-preloader').remove();
				    var id = s.attr('id').replace(/stage-/, '');
				    clearInterval(window['preloaderInterval' + id]);
				    if (s.data('count_slides') == 1) {
				        s.find('.slide:first-child').show();
				    } 
				    else {
				        window.changeSlide(id, 'auto');
				    }
			}
		} 
		else {
		    $(this).load(function(){
		    	var s = $(this).parents('.stage'); 
		    	var tmp = s.data('slides_loaded') + 1;
		    	$(this).parents('.slide').hide();
		    	s.data('slides_loaded', tmp);
		    	if (tmp == s.data('count_slides')) {
		    	    s.find('.slides').css('visibility', 'visible');
		    	    s.find('.nav-stage').css('visibility', 'visible').hide().fadeIn('normal');
		    	    s.find('.stage-preloader').remove();
		    	    var id = s.attr('id').replace(/stage-/, '');
		    	    clearInterval(window['preloaderInterval' + id]);
		    	    if (s.data('count_slides') == 1) {
		    	        s.find('.slide:first-child').show();
		    		} else {
		    			window.changeSlide(id, 'auto');
		    		}
				}
			});
		}
	});
	
	$(this).append('<div class="nav-stage"><ul></ul></div>');
	c = 0;
	window.addPreloader(b);
	$(this).find('.slide').each(function() {
		$(this).find('.claim').wrap('<span class="claim-container" />');
		$(this).attr('id', 'slide-' + b + '-' + c);
		$(this).parents('.stage').find('.nav-stage ul').append('<li id="item-' + b + '-' + c + '">&nbsp;</li>');
		if (c == 0) {
			$(this).addClass('fg').show();
			$(this).find('.claim-container').show();
			$('.slide:first-child').css('z-index', 1);
			$(this).parents('.stage').find('.nav-stage ul li#item-' + b + '-' + c).addClass('active');
		} 
		else if (c == 1) {
			$(this).addClass('bg');
		}
		$(this).parents('.stage').find('.nav-stage ul li#item-' + b + '-' + c).click(function() {
			var id = $(this).attr('id').replace(/item-/, '').split('-');
			window.changeSlide(id[0], id[1]);
		});
	c++;
	});
	} else {
		$(this).find('.slide').show();
		$(this).find('.slides').css('visibility', 'visible');
		$(this).find('.stage-preloader').remove();
		}
	b++;
	}); 
	

	$('.tv').each(function(){
		var n = '<div class="nav-panel"><div class="back">&nbsp;</div><ol>';
		$(this).find('.video-container').each(function(){
			n += '<li>&nbsp;</li>';
		});
		n += '</ol><div class="forward">&nbsp;</div></div>';
		$(this).append(n);
		$(this).find('.video-container:eq(0)').show();
		$(this).data('current-item',0);
		$(this).find('ol li:first-child').addClass('active');
		$('.tv .nav-panel li').click(function(){
			clearInterval(window.changeTvInterval);
			var i = $(this).index();
			nav_panel(i);
			return false;
		});
		$('.tv .nav-panel .forward, .tv .nav-panel .back').click(function(){
			clearInterval(window.changeTvInterval);
			var current = $('.tv').data('current-item');
			var max = $('.tv .video-container').length-1;
			if ($(this).hasClass('forward')) {
				var i = current+1;
				if (i>max) i = 0;
			} else {
				var i = current-1;
				if (i<0) i = max;
			}
			nav_panel(i);
		});
		nav_panel = function(i){
			var c = $('.tv').data('current-item');
			var target = -224;
			if (i<c) target = 224;
			$('.tv .video-container:eq('+c+')').animate({'left':target+'px'},200,function(){$(this).hide()});
			$('.tv .video-container:eq('+i+')').show().css('left',-target+'px').animate({'left':0},200);
			$('.tv .nav-panel li').removeClass('active');
			$('.tv .nav-panel li:eq('+i+')').addClass('active');
			$('.tv').data('current-item',i);
		};
		window.autoChangeTv = function(){
			var current = $('.tv').data('current-item');
			var max = $('.tv .video-container').length-1;
			var i = current+1;
			if (i>max) i = 0;
			nav_panel(i);
		}
		$('.tv .video-container').mouseover(function(){
			clearInterval(window.changeTvInterval);
		});
		window.changeTvInterval = setInterval(window.autoChangeTv,6000);
	});
	
	$('.homepage').each(function(){
		/*
		$('.nav-meta').animate({'zoom':'auto'},0).slideDown('normal');
		$('.header').animate({'zoom':'auto'},500).slideDown('normal');
		$('.content-panel .feature-container .col').hide();
		$('.content-panel').animate({'zoom':'auto'},1000).slideDown('normal');
		$('.content-panel .col-1').animate({'zoom':'auto'},1200).slideDown('normal');
		$('.content-panel .col-2').animate({'zoom':'auto'},1400).slideDown('normal');
		$('.content-panel .col-3').animate({'zoom':'auto'},1600).slideDown('normal');
		$('.content-panel .feature-area-books').animate({'zoom':'auto'},1800).slideDown('normal');
		$('.footer-sitemap').animate({'zoom':'auto'},2000).slideDown('normal');
		*/
	});
	
	
	var max_h = 0;
	$('.homepage .feature-container > .col').each(function(){
		if ($(this).height()>max_h) max_h = $(this).height();
	});
	$('.homepage .feature-container > .col').height(max_h);
	$('.homepage .feature-area-guenter').height(max_h-35);
	
		var max_h = 0;
	$('.footer-sitemap .col').each(function(){
		if ($(this).height()>max_h) max_h = $(this).height();
	});
	$('.footer-sitemap .col').height(max_h);
	
	$('#searchform input[name="s"]').focus(function(){$(this).val('')});
	
	$('#promo-footer').animate({'bottom':'-130px'},2000).animate({'bottom':-50},{duration: 1000,easing: 'easeOutElastic'}).animate({'bottom':-50},4000).animate({'bottom':'-130px'},1000).animate({'bottom':'-130px'},2000).animate({'bottom':-50},{duration: 1000,easing: 'easeOutElastic'});
	
	/* Startseite Teaser "Nichtraucher in 90 Minuten" Zusatz "DVD" */
	$('.home #post-5182 .col h3').append('<span style="color: #333;"> [DVD]</span>')
	
});
