var inMenu = 0;

function closeCheck() {
	if (inMenu < 1) {
		$('#angebotemenu').css('display', 'none');
		$('#angebotenavlink').removeClass('active');
		$('#ueumenu').css('display', 'none');
		$('#ueunavlink').removeClass('active');
	}
}


jQuery(document).ready(function(){

	$('div.accordion').accordion({ header: 'h3', autoHeight: false });
	$('div.tabs').tabs();

	$('#angebotenavlink').mouseover(function(event) {
		$('#angebotemenu').css('display', 'block');
		$(this).addClass('active');
		inMenu++;
	});
	
	$('#angebotenavlink').mouseout(function(event) {
		inMenu--;
		window.setTimeout('closeCheck()', 10);
	});

	$('#angebotemenu').mouseover(function(event) {
		inMenu++;
	});

	$('#angebotemenu').mouseout(function(event) {
		inMenu--;
		window.setTimeout('closeCheck()', 10);
	});
	
	$('#ueunavlink').mouseover(function(event) {
		$('#ueumenu').css('display', 'block');
		$(this).addClass('active');
		inMenu++;
	});
	
	$('#ueunavlink').mouseout(function(event) {
		inMenu--;
		window.setTimeout('closeCheck()', 10);
	});

	$('#ueumenu').mouseover(function(event) {
		inMenu++;
	});

	$('#ueumenu').mouseout(function(event) {
		inMenu--;
		window.setTimeout('closeCheck()', 10);
	});
	
	$('a.anmelden-rollup').click(function(event) {
		var tabID = $(event.target).parents('div.accordion').get(0).id
		var IDLocationMap = {
			'tabs-1': 'anmelden-rollup-maxhofstr',
			'tabs-2': 'anmelden-rollup-oberfoehring'
		};
		var ru = $('#'+IDLocationMap[tabID]);
		$(event.target).parents('div.accordion-body').append(ru);
		ru.hide();
		ru.slideDown();
		return false;
	});
	
	$('a.colorbox').colorbox({opacity:0.7});
});

function toggleBlogComments(a) {
	var p = $(a).parent();
	if (!a.as_blog_status)
		a.as_blog_status = 0;
		
	if (!a.as_blog_status) {
		$('.blog-comments', p.prev()).slideDown();
		$('img.closed', p).hide();
		$('img.open', p).show();
	} else {
		$('.blog-comments', p.prev()).slideUp();
		$('img.closed', p).show();
		$('img.open', p).hide();
	}
	a.as_blog_status = !a.as_blog_status;
}

function gallery_nav_rollover(div, isOver) {
	if (div.style.filter) {	// IE 6
		var d = div;
		var on = (-1 != d.style.filter.search(/_act/));
		d.style.filter = (on ? d.style.filter.replace(/_act/, '') : d.style.filter.replace(/\.png/, '_act.png'));
	} else {
		var d = div.firstChild;
		var on = (-1 != d.src.search(/_act/));
		d.src = (on ? d.src.replace(/_act/, '') : d.src.replace(/\.png/, '_act.png'));
	}
}		

function gallery_nav_click(div) {
	var currentPic = $('.galerie .thumbs img.active');
	var mainPic = $('.galerie .main .img img').get(0);
	var numPics = $('.galerie .thumbs img').length;
	var currentPicIdx = currentPic.prevAll().length;	// 0-based
	var direction = $(div).hasClass('left') ? 'left' : 'right';
	if ((currentPicIdx == 0 && direction == 'left') || (currentPicIdx+1 == numPics && direction == 'right')) return;
	
	var newPic = (direction == 'left') ? currentPic.prev() : currentPic.next();
	newPic.addClass('active');
	currentPic.removeClass('active');
	mainPic.src = newPic.get(0).src.replace(/\/thumbs/, '');
}

function gallery_goto(thumb) {
	$('.galerie .thumbs img.active').removeClass('active');
	$(thumb).addClass('active');
	$('.galerie .main .img img').get(0).src = thumb.src.replace(/\/thumbs/, '');
}

function colorbox_close() {
	$('#cboxClose').click();
}

var headerFadeIndex;
var headerImages;

function headerFade() {
	var oldImg = headerImages.eq(headerFadeIndex);
	oldImg.animate({opacity:0}, 1000);
	headerFadeIndex = (headerFadeIndex + 1) % headerImages.length;
	var newImg = headerImages.eq(headerFadeIndex);
	newImg
		.css('opacity', 0)
		.appendTo($('#headerrotation'))
		.animate({opacity:1}, 1500);
}

// http://james.padolsey.com/javascript/jquery-delay-plugin/
$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

function mybuild() {
	var as = $('a', $(this));
	as.css('opacity', 0);
	as.each(function(i) {
		$(this).delay(i*50).animate({opacity: 1}, 300);
	});
}

var currentTab;

jQuery(document).ready(function() {
	
	// initialize headerrotation
	headerImages = $('#headerrotation img');
	headerFadeIndex = headerImages.length - 1;
	if (headerImages.length > 1)
		window.setInterval('headerFade()', 6000);

});
