$(document).ready(function() {
	// lightbox
	$('a[rel*=lightbox]').lightBox({
		imageLoading:'/images/spinner.gif',
		imageBtnClose:'/images/close.gif',
		txtImage:'Bild',
		txtOf:'von'
	});
	// faq
	$('.faq ul').accordion({
		header:'h4',
		active:false,
		//autoHeight:false,
		clearStyle:true,
		collapsible:true
		//animated:'easeslide'
	});
	$('#faq_query')
	.change(function() {
		updateFaqList($(this).val());
	})
	.keyup(function() {
		updateFaqList($(this).val());
	});
	$('#faq_reset').click(function() {
		$('#faq_query').val('');
		updateFaqList('');
	});
	
	// teaser
	$('#teaser a').mouseover(function() {
		if (!$(this).data('color')) {
			$(this).data('color', $(this).css('color'));
		}
		if (!$('#teaser').data('color')) {
			$('#teaser').data('color', $('#teaser').css('background-color'));
		}
		$(this).stop(true).animate({'color':'#fff'});
		$('#teaser').stop(true).animate({'backgroundColor':$(this).data('color')});
	}).mouseout(function() {
		$(this).stop(true).animate({'color':$(this).data('color')});
		$('#teaser').stop(true).animate({'backgroundColor':$('#teaser').data('color')});
	});
	
	// labeled input fields
	$('input.labeled').each(function() {
		$(this).data('label', $(this).val());
	}).focus(function() {
		if ($(this).data('label') == $(this).val())
			$(this).val('');
	}).blur(function() {
		if ($(this).val().length == 0)
			$(this).val($(this).data('label'));
	});
	
	// form contract (only allow nec address
	$('input.plz[name$="PLZ"]').change(function() {
		var plz = $('input.plz[name="PLZ"]');
		var vplz = $('input.plz[name="VerbrauchsstellePLZ"]');
		if ((plz.val() != 96465 && vplz.val() != 96465) || $.inArray(vplz.val(), ['', '96465', 'PLZ']) < 0) {
			$('#wrongPLZ').show();
		} else {
			$('#wrongPLZ').hide();
		}
	});
	
	// equal heights for overview
	equalHeightOverview();
	setTimeout(equalHeightOverview, 3000);
	
	$('.toggle h3').click(function() {
		$(this).nextUntil('h3').slideToggle('fast');
		return false;
	}).nextUntil('h3').hide();
	
	// teaser flip headlines
	$('.flip :not(:first-child)').hide();
	setTimeout(flipNext, 3000);
});

function flipNext() {
	var current = $('.flip :visible');
	var next = current.next();
	if (next.length == 0)
		next = $('.flip :first-child');
	
	current.slideUp(500);
	next.slideDown(500);
	
	setTimeout(flipNext, next.hasClass('quick') ? 1500 : 3000);
}

function updateFaqList(query) {
	$('.faq li').each(function() {
		if ($(this).text().toLowerCase().indexOf(query.toLowerCase()) >= 0) {
			$(this).fadeIn();
		} else {
			$(this).fadeOut();
		}
	});
}

// make neighboring boxes equal height
var equalHeightOverview = function() {
	var row = 0;
	var offset = 0;
	var height = 0;
	
	$('.overview li').each(function() {
		var thisOffset = Math.round($(this).offset()['top']);
		if (offset != thisOffset) {
			// equalize
			$('.overview li.row'+row).css('min-height', height);
			
			// reset
			offset = thisOffset;
			height = 0;
			row++;
		}
		$(this).addClass('row'+row);
		height = Math.max(height, $(this).height());
	});
}
