// JavaScript Document


//Wenn Website eingebette, dann Frame loswerden
if (top.frames.length!==0) { top.location=self.document.location }

// jQuery AJAX
$(document).ready(function(){
	
	/* Aktuellen Link in der Hauptnavigation hervorheben
	 * ############################################################ */
	var path = location.pathname.substring(1);
	if (path != '') {
		$('#navigation a[href$="' + path + '"]').addClass('current');
		$('.subNavigation > li > a[href$="' + path + '"]').addClass('current');
		$('.subNavigation > li > a[href$="' + path + '"]').parent('li').parent('ul').parent('li').children('a').addClass('section');
	} else {
		$('#navigation a:first').addClass('current');
	}
	
	// Hover Navigation
	$('#navigation > ul > li > a').hover(function() {
		$('#navigation ul > li > .current').addClass('oldCurrent');
		$('#navigation ul > li > .section').addClass('oldCurrent');
	}, function() {
		$('.oldCurrent').removeClass('oldCurrent');
	});
	
	
	/* SubNavigation
	 * ############################################################ */
	$('.subNavigation').css({'display' : 'block'}).hide();
	
	var in_delay = 300;
	var in_fade = 250;
	
	var out_delay = 500;
	var out_fade = 150;
	// Hover für Elemente mit subnavi
	$('#navigation li:has(.subNavigation)').hover(function() {
		
		$(this).children('a').addClass('tmpSection');
		$(this).children('a').removeClass('oldCurrent');
		
		// Alle anderen Elemten nach hinten
		$('.subNavigation').css({'z-index' : '900'});
		
		// Einblenden der subnavi und nach vorne bringen
		$(this).children('.subNavigation').css({'z-index' : '1000'})
			.stop(true, true)
			.delay(in_delay)
			.fadeTo(in_fade, 1, function() {
				$(this).show();
			});
		
		// H1 nach unten
		$('h1').stop(true, true).delay(in_delay).animate({'margin-top' : '90px'}, in_fade);
		
	}, function() {
		if ($(this).children('a').hasClass('current') == false && $(this).children('a').hasClass('section') == false) {
			// Ausblenden der subnavi und in die Reihe der anderen Elemten bringen
			$(this).children('.subNavigation')
				.stop(true, true)
				.delay(out_delay)
				.fadeTo(out_fade, 0, function() {
					$(this).hide().css({'z-index' : '900'});
				});
			
			// H1 nach oben falls Element mit subnavi nicht aktiv
			if ($('#navigation > ul > li:has(.subNavigation) > a').hasClass('section') == false && $('#navigation ul > li:has(.subNavigation) > a').hasClass('current') == false) {
				$('h1').stop(true, true).delay(out_delay).animate({'margin-top' : '53px'}, out_fade);
			}
		}
		//$('#navigation .oldCurrent').removeClass('oldCurrent');
		$('#navigation .tmpSection').removeClass('tmpSection');
	});
	
	// Hover SubNavigation
	$('.subNavigation').hover(function() {
		if ($(this).parent('li').children('a').hasClass('section') == false && $(this).parent('li').children('a').hasClass('current') == false) {
			$(this).parent('li').children('a').addClass('tmpSection');
			
			$('#navigation ul > li > .section').addClass('oldCurrent');
			$('.subNavigation > li > .section').addClass('oldCurrent');
			
			$('#navigation ul > li > .current').addClass('oldCurrent');
			$('.subNavigation > li > .current').addClass('oldCurrent');
		}
	}, function() {
		if ($(this).parent('li').children('a').hasClass('section') == false && $(this).parent('li').children('a').hasClass('current') == false) {
			$('#navigation .tmpSection').removeClass('tmpSection');
			$('#navigation .oldCurrent').removeClass('oldCurrent');
		}
	});
	
	// H1 unten lassen wenn eine subnavi angezeigt wird
	$('.section').parent('li').children('.subNavigation').show(0, function() {
		$('h1').css({'margin-top' : '90px'});
	});
	$('.current').parent('li').children('.subNavigation').show(0, function() {
		$('h1').css({'margin-top' : '90px'});
	});
	
	
	/* Value von Inputs
	 * ############################################################ */
	var formElements = new Array(
		$('input[type="text"]')
	);
	
	for (var i = 0; i < formElements.length; i++) {
		formValue(formElements[i]);
	}
	
	$('#SearchForm_SearchForm_Search').each(function() {
		// de_DE
		var attrTitle = 'Website durchsuchen';
		
		if ($(this).val() == '' || $(this).val() == attrTitle) {
			$(this).val(attrTitle).css({'color' : '#666'});
		}
		
		// en_GB
		var attrTitle_en_GB = 'Browse website';
		
		if ($(this).val() == '' || $(this).val() == attrTitle_en_GB) {
			$(this).val(attrTitle_en_GB).css({'color' : '#666'});
		}
		
		$(this).focus(function() {
			// de_DE
			if ($(this).val() == attrTitle) {
				$(this).val('').css({'color' : '#333'});
				$(this).attr('rel', attrTitle);
			}
			// en_GB
			if ($(this).val() == attrTitle_en_GB) {
				$(this).val('').css({'color' : '#333'});
				$(this).attr('rel', attrTitle_en_GB);
			}
		}).blur(function() {
			// Internationalisation
			if($(this).val() == '') {
				$(this).css({'color' : '#666'}).val($(this).attr('rel'));
				$(this).removeAttr('rel');
			}
		});
	});
	
	/* Rückrufzeit ausblenden bei Umgehendem Rückruf
	 * ############################################################ */
	$('#immediately').change(function() {
		if ($(this).attr('checked') == 'checked') {
			$('#time').attr('disabled', 'disabled');
			$('#time').val('');
		} else {
			$('#time').removeAttr('disabled');
		}
	});
	
	if ($('#immediately').attr('checked') == 'checked') {
		$('#time').attr('disabled', 'disabled');
		$('#time').val('');
	}
});



/**
 * 		Plugins
 * ###############################################################################################################
 */

/* Form value
 * ############################################################ */
function formValue(formElement) {
	formElement.each(function() {
		var attrTitle = $(this).attr('title');
		
		if($(this).val() == '' || $(this).val() == attrTitle) {
			$(this).val(attrTitle).css({'color' : '#666'});
		}
		
		$(this).focus(function() {
			if($(this).val() == attrTitle) {
				$(this).val('').css({'color' : '#333'});
			}
		}).blur(function() {
			if($(this).val() == '') {
				$(this).css({'color' : '#666'}).val(attrTitle)
			}
		});
	});
}


/* Header animation
 * ############################################################ */
function headerAnimation(startDelay) {
	// Header vorbereiten
	$('#search').css({'margin-bottom' : '0px'});
	$('#rightBar').css({'top' : '-41px'});
	$('#headerImages').css({'height' : '50px'});
	$('#headerImages .item').fadeTo(0, 0);
	$('#navigation').css({'top' : '0'});
	
	// Startverzögerung
	$('body').delay(startDelay).fadeTo(0, 1, function() {
		
		var headerImages = $('#headerImages');
		var smallItem = $('#headerImages .item');
		
		var delay_time = 250;
		var item_count = 5;
		
		for (var i = 0; i < item_count; i++) {
			// Blende Header-items ein
			smallItem.eq(i).delay(delay_time * i)
				.fadeTo(delay_time * 2, 1);
		}
		
		var header_timing = 1000;
		
		// Vergrößere Header-viewport
		headerImages.delay(delay_time * (item_count+1))
			.animate({'height' : '+=100px'}, function() {
		
				// Hover-timing
				var in_speed = 300;
				var out_speed = 500;
				var in_delay = 300;
				var out_delay = 0;
				var item_opacity = 1;
				
				// Hover-effect
				smallItem.hover(function() {
					// Slide in
					smallItem.each(function() {
						$(this).stop(true, true).delay(in_delay)
							.animate({
									 width: '171px'
									 }, in_speed * 2);
					});
					
					$(this).stop(true, true).delay(in_delay)
						.animate({
								 width: '296px'
								 }, in_speed);
				}, function() {
					// Slide out
					smallItem.each(function() {
						$(this).stop(true, true).delay(out_delay)
							.animate({
									 width: '196px'
									 }, out_speed / 2);
					});
					
					$(this).stop(true, true).delay(out_delay)
						.animate({
								 width: '196px'
								 }, out_speed);
				});
		});
		
		// IE-fix für fonts
		this.style.removeAttribute('filter');
	});
}

/* end Plugin elements */
