$(document).ready(function(){

 	// get hash (#) from URL
	var index = $(location).attr('hash');

 	// get element containing hash id
	var current = '';
	if (index.length) {
		current = $('body').find(index);
	}


	/**
	 * toggle panels
	 * expand/collapse content
	 */

	$('.toggle-trigger')
		.find('a').bind('click', function(e) {
			var a = $(this);

			// slide panel above trigger
			a.parent().parent('.toggle-panel').children('div:first-child').slideToggle(300, function() {
				a.parent().closest('.toggle-item').children('.toggle-trigger').children('a').updateTrigger(); // related trigger
				a.updateTrigger(); // self
				if (current) {
					$('html,body').animate({scrollTop: current.offset().top}, 1000); // scroll to element
		         current = '';
				}
			});
			// slide panel below trigger
			a.parent().siblings('.toggle-panel').children('div:first-child').slideToggle(300, function() {
				a.parent().siblings('.toggle-panel').children('.toggle-trigger').children('a').updateTrigger(); // related trigger
				a.updateTrigger(); // self
				if (current) {
					$('html,body').animate({scrollTop: current.offset().top}, 1000); // scroll to element
		         current = '';
				}
			});

			a.blur(); // unfocus
			e.preventDefault();
		});

	// activate element containing hash id
	if (current.length) {
		current.find('.toggle-trigger:last').find('a').click();
	}

	// fn.updateTrigger
	$.fn.updateTrigger = function() {
		return this.each(function() {
			switch (true) {
				// is .expand
				case $(this).hasClass('expand'):
					// change to .collapse
					$(this).removeClass('expand').addClass('collapse');
					// change link text (.action only)
					if ($(this).hasClass('action')) {
						var e = $(this).children('span:last-child');
						
						if (e.text() == 'Lesen Sie mehr…') {
							e.text('Text ausblenden');
						}
						if (e.text() == 'Grafiken einblenden') {
							e.text('Grafiken ausblenden');
						}
						if (e.text() == 'Tabelle einblenden') {
							e.text('Tabelle ausblenden');
						}
						if (e.text() == 'Bereich einblenden') {
							e.text('Bereich ausblenden');
						}
					}
					break;
				// is .collapse
				case $(this).hasClass('collapse'):
					// change to .expand
					$(this).removeClass('collapse').addClass('expand');
					// change link text (.action only)
					if ($(this).hasClass('action')) {
						var e = $(this).children('span:last-child');
						
						if (e.text() == 'Text ausblenden') {
							e.text('Lesen Sie mehr…');
						}
						if (e.text() == 'Grafiken ausblenden') {
							e.text('Grafiken einblenden');
						}
						if (e.text() == 'Tabelle ausblenden') {
							e.text('Tabelle einblenden');
						}
						if (e.text() == 'Bereich ausblenden') {
							e.text('Bereich einblenden');
						}
					}
					break;
			}
		});
	};


	/**
	 * glossary
	 */

	// get hash (#) from URL
	var g_index = index.toLowerCase().substr(0, 2);
	if (g_index.length == 0 || g_index.match(/#[a-z]/) == null) g_index = '#a';

	// init vars
	var glossary       = $('.glossary');
	var glossary_index = $('.glossary-index');
	var glossary_item  = glossary.children(g_index);

	if (glossary.length > 0) {

		// check status of selected index
		if (glossary_item.length == 0) {
			// disabled? -> change index to '#a'
			g_index = '#a'; 
			$(location).attr('hash', g_index);
			glossary_item  = glossary.children(g_index);
		}

		// initial toggle on load
		glossary_item.show();
		glossary_index.find('a[href$="'+g_index+'"]').parent().addClass('current');

		// toggle action
		glossary_index
			.find('a').bind('click', function(e) {
				$(this).glossaryToggle()
				e.preventDefault();
			});
	}

	// fn.glossaryToggle
	$.fn.glossaryToggle = function() {
		return this.each(function() {
			var a = $(this);
			if (a.parent().is(':not(.current, .disabled)')) {

				// toggle panes
				glossary.children('div:visible').slideUp(200, function() {
					glossary.children(a.attr('href')).slideDown(200);
				});

				// clear index marker
				glossary_index.find('.current').removeClass('current');
				// set index marker
				a.parent().addClass('current');
				$(location).attr('hash', a.attr('href'));
			};
		});
	};


	/**
	 * auto focus form fields
	 */

	$(".autofocus").focus();


	/**
	 * toggle field content
	 * clear on focus, replace with default on blur
	 */

	$.fn.toggleFieldContent = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$("#quicklink").not(".autofocus").toggleFieldContent();
	$("#searchfield").toggleFieldContent();


	/**
	 * colorbox
	 */

	// »Videobotschaft der Geschäftsführung«
	$("a[rel*='video']").colorbox({
		iframe: true,
		innerWidth: 958,
		innerHeight: 574,
		opacity: 0.80
	});
	$("#introduction").colorbox({
		open: true, // auto open
		iframe: true,
		innerWidth: 958,
		innerHeight: 574,
		opacity: 0.80
	});


	/**
	 * external links
	 *
	 * filters href != hostname or rel="external"
	 * adds .external to elements not containing img, div or mailto
	 * opens external links in new window/tab
	 */
/*
	$('a, area').filter(function() {
		return this.hostname && (this.hostname).split(":")[0] !== (location.hostname).split(":")[0] || $(this).attr('rel') == 'external';
	})
	.not(':has(img, div, mailto)')
	.addClass('external')
	.end()
	.click(function(e) {
		open(this.href); 
		e.preventDefault();
	});
*/

});
