$(function() {

	// in field labels
	$('form.search label, #kontakt label, label.infield, .tx-powermail-pi1 label, #betreuer label, #kontaktformular label, #comment-user-details label').addClass('infield').inFieldLabels();

	// Since IE6 doesn't support input:pseudo classes
	$('input:text').addClass('text');
	$('input:submit').addClass('submit');
	
	// Q-Tip is my title but I don't think that is vital, for me to be your idol, so dig this recital!
	$('.tooltip').cluetip({
		splitTitle: '|', tracking: false, cluetipClass: 'das', sticky: false, showTitle: false, arrows: true, width: '180px',
		positionBy: 'bottomTop',
		topOffset: '20',
		leftOffset: '10'
	});
	
	// Tabs
	/*$("#tabs").tabs({
		fx:"fadeIn",
		syncheights: true,
		tabbody:'.tab-content',
		currentInfoText:'',
		tabhead: 'h3.tab',
		select: function(event, ui) {
			console.log('changed tab');
		}
	})
	// Accordion
	$('.tab-content').accordion({
		header: 'h3'
	});
	*/
	
	// navigation out delay
	var config = {    
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100,  // number = milliseconds for onMouseOver polling interval    
		over: doOpen,   // function = onMouseOver callback (REQUIRED)    
		timeout: 1200,   // number = milliseconds delay before onMouseOut    
		out: doClose    // function = onMouseOut callback (REQUIRED)       
	};

	function doOpen() {
		$(this).addClass("hover");
		$('ul:first',this).css('display', 'block');
	}

	function doClose() {
		$(this).removeClass("hover");
		$('ul:first',this).css('display', 'none');
	}

	$("#nav li").hoverIntent(config);
 
	
	$('#nav > ul > li > a').each(function(index, element) { 
		if(index<2) { 
			$(element).css('cursor','default');         
			$(element).hover(function() {
				$(this).css('cursor','default');
			});
			$(element).click(function() {
				return false;
			});
		}
	});


	// dynamicly add the navigation to the tabs
	var navigation = '<ul>';
	$("#tabs > div").each(function(index, element) {
		// h2.tab
		$(element).attr('id', 'tabs-'+index);
		var headline = $('h2:first', element).remove().text();
		navigation += '<li><a href="#tabs-' + index + '">' + headline + '</a></li>';
	}); 
	navigation += '</ul>';
	$("#tabs").prepend( navigation );
	
	
	
	

	$("#tabs").tabs({
		select: function(event, ui) {
			$('.accordion').accordion( "option", "clearStyle", true ).find('.ui-accordion-content-active:first').css('height', 'auto');
		}
	});

	// select 2nd tab of no comments are posted in the comments form
	$('#comments #tabs #commentslist:not(:has(ol))').parent().parent().tabs('select', 1);

	
	
	// backward copatibility for acordion
	$(".accordion h3").next().wrap('<div>');
	$(".accordion h3 a").contents().unwrap('a');
	//replaceWith
	
	$('.accordion').accordion({
		//clearStyle: true
	});
	
	$('.ui-accordion-content').css('height', 'auto');
	
	
	// IE last-child support
	$('.front #col2 .post:last-child').addClass('last');

	// Frontpage focus cycle
	if($('FAKEFAKEbody.front').length > 0) {
		$('#focus .slider').cycle({
			fx: 'scrollVert',
			next: '#focus .down',
			prev: '#focus .up',
			timeout: 15000
		});
	} 


	// Frontpage keyvisual cycle
	if($('body.front').length > 0) {
		$('#focus ul.slideshow').cycle({
			fx: 'scrollLeft',
			speed: 1200,
			easeOut: 'easeOutCirc',
			easeIn: 'easeOutCirc',
			timeout: 8000
		});
	}

	// Frontpage focus playbtn
	$('#focus .slider li a').hover(function(){
		$(this).append('<div class="play"></div>');
	}, function(){
		$('#focus .slider .play').remove();
	});
	
	var toolbar = $("#focus .slider"),
		pager = $('#focus .pager'),
		speed = 200,
		closeAnimationPositions = [ 3, 2, 1, 0, 5, 4, 3, 2, 1, 0 ],
		closeSpeed = Math.round(speed / closeAnimationPositions.length),
		close = $("#focus a.toggle").click(function (e) {
			e.preventDefault();
			
			if (toolbar.is(":animated")) {
				return;
			}
			
			var hide = (toolbar.css("top") == "0px");
			
			toolbar.animate({
				top: (hide ? -240 : 0),
				opacity: (hide ? 0 : 100)
			}, speed);
			
			
			pager.animate({
				top: (hide ? 0 : 246) //246
			}, speed);
		
			$('#focus a.down, #focus a.up').hide(); // remember to reactivate toggle() 
			$('#focus .tvspot').toggle(); 
			
			var positions = closeAnimationPositions.slice(0);
			if (hide) {
				positions.reverse();
			}
		
			$(positions).each(function(i, position) {
				var offset = position * -19;
				setTimeout(function() {
					close.css({ backgroundPosition: "0 " + offset + "px" });
				}, i * closeSpeed);
			});
	});
	
	$('#focus .pager').prepend('<span class="tvspot" style="display: none;">TV-Spots</span>');
	var hideFocusSlider = window.setTimeout(function(){
		if($('#focus .slider').is(':hidden')) $('#focus .pager .toggle').click();
	}, 10000);


	$('#focus .slider a').click(function(e){
		e.preventDefault();

		var iframe = $(this).next('iframe').clone();
		$('body').addClass('overlayed').prepend(iframe).prepend('<div class="overlay" />');
	
		$(iframe).wrap('<div class="video" />');
		$('body > .video').prepend('<a href="#" class="closeBtn" title="Schließen">Schließen</a>');

		$('.overlay, .video .closeBtn').click(function(){
			$('body').removeClass('overlayed');
			$('body > .overlay').remove();
			$('body > .video').remove();
		});
	}); 
	
	
	/* Feedback tab */
	$('#feedbackTab a').click(function(e){
		e.preventDefault();

		var $iframe = $('<iframe src="https://www.ergo.de/de/resources/Forms/DAS/feedback/feedback" scrolling="auto" border="0" frameborder="0"></iframe>');
		$('body').addClass('overlayed').prepend($iframe).prepend('<div class="overlay" />');
		$('.overlay').height($(document).height());
		//.addClass('overlayed').append('<div class="overlay" />').prepend(iframe);
	
		$iframe.wrap('<div class="feedback" />');
		$('body > .feedback').prepend('<a href="#" class="closeBtn" title="Schließen">Schließen</a>');

		$('.overlay, .feedback .closeBtn').click( function(event){
		    event.preventDefault();
			$('body').removeClass('overlayed');
			$('body > .overlay').remove();
			$('body > .feedback').remove();
		});
	}); 


	// make links open in new window
	$('a.external-link,a.download,a.external-link-new-window').click(function(){
		window.open(this.href);
		return false;
	});


	// z-index fix for IE 7
	if ( $.browser.msie && parseInt($.browser.version, 10) == 7 ) {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}

	// video fadein in areasearch results
	$('#betreuer-results a.youtube').click(function(e) {
		e.preventDefault();
		$(this).parent().fadeOut();
		$(this).parent().parent().find('.youtube-preview').fadeOut();
		
		$(this).parent().parent().parent().parent().find('div.video').slideDown();
	});
	$('#betreuer-results .play').click(function(e) {
		e.preventDefault();
		$(this).parent().parent().parent().parent().find('a.youtube').click();
	});


	// custom contact form validation
	/*
	if(console.log) {
		console.log( $.tools.validator );
		console.log( $('input[type=select]#uid41') );
	}
	*/
	/*
	$("#kontaktformular").validator({
		console.log('validiert feld');
	});
	*/ 
	$("#kontaktformular #uid41").change(function(event) {
		switch( $(this).find("option:selected").index() ) {
			case 3:
				$('#uid25').attr('required','required');                                      
				$('#uid38').attr('required','');
				$('#uid39').attr('required','');
				break;
			case 4:                                    
				$('#uid25').attr('required',''); 
				$('#uid38').attr('required','required');
				$('#uid39').attr('required','required');
				break;
			default:                                    
				$('#uid25').attr('required',''); 
				$('#uid38').attr('required','');
				$('#uid39').attr('required','');
				break;
		}
		
	});
	//$("#kontaktformular").validator().rules('add',{})
	/*
	console.log($("#kontaktformular").validator);
	$.tools.validator.fn('input[type=select].callback', 'required',
		function(input, value) {
			console.log( 'validiert feld' );
			return false;//value.length > 0;
		} 
	);
	*/
	/*
	$.tools.validator.fn('input[type=select].callback', 'required',
		function(input, value) {
			console.log( 'validiert feld' );
			return false;//value.length > 0;
		} 
	);
	*/

});
