//Cufon.set('fontFamily', 'Parable');
//Cufon.replace('h1, #headerStatement p:not(p.more), #homeRotation .item .content h2, ');
//.article-content h2, #breadcrumbs div,  #breadcrumbs li
//Cufon.replace('#headerPressReleases a, .teaser h2, .submenu h2, #breadcrumbs li a, .article-downloads .item a, .article-links .item a', {'hover': true});

(function($) {
// !$.fn.pagination
	$.fn.pagination = function(elements, options, events) {
		var self = this;
		var targets = {};
		var pages = $(this);
			pages.each(function(page) {
				$(this).find('*').each(function() {
					if (this.id)
						targets[this.id] = {
							'page': page,
							'top': $(this).offset().top
						};
				});
			});
		var widths = pages.map(function() {
			return $(this).outerWidth(true);
		});
		var elements = jQuery.extend({
			'wrapper': $('.pagination'),
			'current': $('.pagination').find('.current'),
			'count': $('.pagination').find('.count'),
			'prev': $('.pagination').find('.prev'),
			'next': $('.pagination').find('.next'),
			'links': $('.pagination').find('a')
		}, elements || {});
		var options = jQuery.extend({
			'startWith': 0,
			'duration': 500
		}, options || {});
		var events = events || {};

		this.idle = 0;
		this.index = options.startWith;
		this.count = pages.size();
		elements.wrapper.height($('.section:eq(' + self.index + ')').height());
		
		var getOffset = function(overrideIndex) {
			var index = overrideIndex || self.index;
			var number = 0;
			for (; index > 0; index--)
				number+= widths[index];

			return number;
		}
		var prev = function(ev) {
			if (!self.idle && self.index > 0) {
				self.index = self.index - 1;
				self.moveToIndex();
				elements.wrapper.height($('.section:eq(' + self.index + ')').height());
				$('html, body').animate({scrollTop: $('#menu').offset().top - 5}, 0);
			}
		}
		var next = function(ev) {
			if (!self.idle && self.index < self.count - 1) {
				self.index = self.index + 1;
				self.moveToIndex();
				elements.wrapper.height($('.section:eq(' + self.index + ')').height());
				$('html, body').animate({scrollTop: $('#menu').offset().top - 5}, 0);
			}
		}
		var updateText = function() {
			elements.current.text(self.index + 1);
			elements.count.text(self.count);
			if (events['updateText'] && events['updateText'].length)
				events['updateText'].map(function(fn) { fn(this); });
		}
		var updateButtons = function() {
			elements.wrapper.show();
			elements.prev.removeClass('prev-disabled');
			elements.next.removeClass('next-disabled');

			if (self.index == 0)
				elements.prev.addClass('prev-disabled');
			if (self.index == self.count - 1)
				elements.next.addClass('next-disabled');
			if (self.count == 0)
				elements.wrapper.hide();
			if (events['updateButtons'] && events['updateButtons'].length)
				events['updateButtons'].map(function(fn) { fn(this); });
		}

		this.update = function() {
			updateText();
			updateButtons();
		}
		this.registerEvent = function(type, fn) {
			if (events[type])
				events[type].push(fn);
			else events[type] = [fn];
		}
		this.unregisterEvent = function(type) {
			if (events[type])
				delete events[type];
		}
		this.moveToOffset = function(offset, force) {
			self.idle = true;
			var callback = function() {
				self.update();					
				self.idle = false;
				if (events['moveToOffset'] && events['moveToOffset'].length)
					events['moveToOffset'].map(function(fn) { fn(offset, force); });
			}

			if (force || offset == elements.wrapper.scrollLeft()) {
				callback();
			} else elements.wrapper.animate(
				{ 'scrollLeft': offset },
				options.duration,
				callback
			);
		}
		this.moveToIndex = function(overrideIndex) {
			self.index = overrideIndex || self.index;
			self.moveToOffset(getOffset(self.index));
			if (events['moveToIndex'] && events['moveToIndex'].length)
				events['moveToIndex'].map(function(fn) { fn(overrideIndex); });
		}
		this.moveToLink = function(id) {
			if (targets[id]) {
				self.registerEvent('moveToOffset', function(offset, force) {
					self.idle = true;
					self.unregisterEvent('moveToOffset');
					$('html:not(:animated),body:not(:animated)').animate(
						{ 'scrollTop': targets[id].top },
						force ? 0 : options.duration,
						function() {
							self.idle = false;
							if (events['moveToLink'] && events['moveToLink'].length)
								events['moveToLink'].map(function(fn) { fn(id); });
						}
					);
				});
				self.moveToIndex(new Number(targets[id].page));
				elements.wrapper.height($('.section:eq(' + self.index + ')').height());
			}
		}

		elements.prev.click(function(ev) {
			ev.preventDefault();
			prev();
		});
		elements.next.click(function(ev) {
			ev.preventDefault();
			next();
		});
		elements.links.click(function(ev) {
			if (this.hash) {
				ev.preventDefault();
				self.moveToLink(this.hash.replace('#', ''));
			}
		});
		this.moveToOffset(getOffset(self.index), true);
	}

// !$.fn.mouseDelay
	$.fn.mouseDelay = function() {
		var delay_in = arguments[0] || 1000;
		var delay_out = arguments[1] || 2000;
		var delay_target = arguments[2] || function(el) { return el; };
		var delay_to = false;
		var delay_el = $(this).filter('.hover') || false;
		var elements = $(this);
			elements.each(function(index, el) {
				var self = this;
				var active = elements.filter('.active');
				var focusEl = elements.filter('.active');
				this.element = $(el);
				this.element.hover(
					function(ev) {
						ev.preventDefault();

						if (delay_to) {
							clearTimeout(delay_to);
							delay_to = setTimeout(function() {
								active.removeClass('active');
								delay_target(self.element).addClass('hover');
								self.element.parent().children('li').not(self.element).removeClass('hover');
								clearTimeout(delay_to);
								delay_to = false;
							}, delay_in);
						} else {
							delay_el = $(this);
							delay_to = setTimeout(function() {
								active.removeClass('active');
								self.element.parent().children('li').not(self.element).removeClass('hover');
								delay_target(self.element).addClass('hover');
								clearTimeout(delay_to);
								delay_to = false;
							}, delay_in);
						}
						if(focusEl != $(this)) 
							focusEl = false;						
						
					},
					function(ev) {
						ev.preventDefault();
	
						if (delay_to)
							clearTimeout(delay_to);
	
						delay_el = $(this);
						delay_to = setTimeout(function() {
							if ((active && !focusEl) || focusEl == $(this))
								active.addClass('active');
							delay_target(self.element).removeClass('hover');
							clearTimeout(delay_to);
							delay_to = false;
						}, delay_out);
					}
				);
				// focus
				this.element.focusin(function(ev) {
					ev.preventDefault();
					delay_in = 0;
					delay_out = 0;
					delay_el = focusEl;
					self.element.parent().children('li').not(this).removeClass('hover');
					focusEl = $(this);
					
					self.element.parent().children('li').not(this).mouseleave();
					self.element.mouseenter();
				});
/*
				this.element.focusout(function() {
					delay_to = false;
					self.element.mouseleave();
				});
*/
			});

		return this;
	}

// !$.fn.glossary
	$.fn.glossary = function() {
		var self = this;
		var defaults = {
			'list': $('.list'),
			'default': 'A',
			'url': 0,
			'variable': 'var',
			'url': '/glossary.php'
		};
		
		var options = $.extend({}, defaults, options);

		this.idle = false;
		this.update = function(v) {
			if (!self.idle) {
				self.idle = true;
				options.list.load(
					options.url + '?' + options.variable + '=' + v,
					function() {
						self.idle = false;
					}
				);
			}
		}

		if (options.defaults)
			this.update(options.defaults);
		$(this).each(function(index, el) {
			$(this).click(function(ev) {
				ev.preventDefault();
				self.update(this.rel);
			});
		});
	}

// !$.fn.suggest
	$.fn.suggest = function(url, opts) {
		var url = url || location.href.replace(/(^\/#\/|^\/#|^#\/|^#|^\/)/, '');
    	var cache = {};

		$(this).each(function(index, el) {
			var self = this;
			this.element = $(el);
			
			this.options = jQuery.extend({
				'search': 'input.searchterm',
				'result': 'ul.ui-autocomplete',
				'closer': 'li.close',
				'minLength': 3,
				'maxRows': 5
			}, opts || {});
			
			this.closer = this.options.closer ? this.element.find(this.options.closer).detach() : false;

			this.element.find(this.options.search).autocomplete({
                'source': function(request, response) {
                    var term = request.term;
                    if (term in cache) {
                        response(cache[term]);
                        return;
                    }

                    url += '&'+this.element.attr('name') + '=' + this.element.attr('value');

                    lastXhr = $.getJSON(
                        url,
                        null,
                        function(data,status,xhr) {
                            cache[term] = data;
                            if (xhr === lastXhr) {
                                response(data);
                            }
                        }
                    );
                },
                'minLength': self.options.minLength,
		'appendTo': self.element.find('form fieldset'),
				'open': function(ev, ui) {
					$('<li class="close"><a href="/#">schließen</a></li>')
						.click(function(ev) {
							ev.preventDefault();
							self.element.find(self.options.result).slideUp(300);
						})
						.appendTo(self.element.find(self.options.result));
				},
				'select': function(ev, ui) {
					console.log(ui.item);
					self.element.find(self.options.search).val(ui.item.value);
					self.element.find('form').submit();
				}
			});
		});
		return this;
	}

// !$.fn.inputDefault	
	$.fn.inputDefault = function () {
		$(this).each(function (index, el) {
			var self = this;
			this.element = $(el);
			this.element.data({
				'default': this.element.val()
			});
			this.element.focus(function () {
				if ($(this).val() == $(this).data('default')) $(this).val('');
			});
			this.element.blur(function () {
				if (/^\s*$/.test($(this).val() || ' ')) $(this).val($(this).data('default'));
			});
		});
		return this;
		
	}






// !$.fn.slider	
   $.fn.slider = function (opts) {
        $(this).each(function (index, el) {
            var self = this;
            this.idle = false;
            this.current = 0;
            this.element = $(el);
            this.options = jQuery.extend({
                'container': '.slider-inner',
                'item': '.item',
                'next': '.next',
                'prev': '.prev',
                'nums': '.number',
                'current': '.current',
                'count': '.count',
                'disabler': false,
                'displaycount': false,
                'offset': 1,
                'onstart': function () {},
                'onstop': function () {}
            }, opts || {});
			var events = events || {};
			
            this.items = this.element.find(self.options.container + ' ' + this.options.item).each(function (i, el) {
                $(el).addClass('item-' + i);
            });
            this.activateRelated = function () {
                this.element.find(this.options.nums).removeClass('active').eq(self.current).addClass('active');
            }
            this.itemcount = this.items.size();
			
			this.updateText = function() {
				$(self).find(self.options.current).text(self.current + 1);
				$(self).find(self.options.count).text(self.itemcount);
				if (events['updateText'] && events['updateText'].length)
					events['updateText'].map(function(fn) { fn(this); });

			}
            
            
            this.scrollTo = function (offset) {
                if (!self.idle) {
                    self.idle = true;
                    self.options.onstart(self);
                    self.activateRelated();
                    self.element.find(self.options.container).animate({
                        'scrollLeft': offset
                    }, self.options.duration, function () {
                        self.idle = false;
                        self.options.onstop(self);
                    });
                }
            }
            this.getOffset = function (index) {
                return self.element.find(self.options.container).scrollLeft() + $(self.items[index ? index : self.current]).position().left
            }
            this.updateButtons = function() {
				if(self.options.disabler)
				{
					this.element.find(this.options.prev).removeClass('prev-disabled');
					this.element.find(this.options.next).removeClass('next-disabled');
		
					if (self.current == 0)
						this.element.find(this.options.prev).addClass('prev-disabled');
					if (self.current == self.items.size() - 1)
						this.element.find(this.options.next).addClass('next-disabled');
				}
			}
            this.scrollNext = function () {
                self.current = (self.current + self.options.offset >= self.items.size()) ? 0 : self.current + self.options.offset;
                self.scrollTo(self.getOffset());
                self.updateButtons();
                if(self.options.displaycount) this.updateText();
            }
            this.scrollPrev = function () {
                self.current = (self.current - self.options.offset < 0) ? self.items.size() - self.options.offset : self.current - self.options.offset;
                self.scrollTo(self.getOffset());
                self.updateButtons();
                if(self.options.displaycount) this.updateText();
            }
            this.element.find(this.options.next).click(function (ev) {
                ev.preventDefault();
                if(!self.options.disabler || self.current != self.items.size() - 1) self.scrollNext();
            });
            this.element.find(this.options.prev).click(function (ev) {
                ev.preventDefault();
                if(!self.options.disabler || self.current != 0) self.scrollPrev();
            });
            this.element.find(this.options.nums).each(function (index, el) {
                $(el).addClass('number-' + index).removeClass('active').click(function (ev) {
                    ev.preventDefault();
                    self.current = index;
                    self.scrollTo(self.getOffset());
                })
            });
            this.activateRelated();
            this.updateButtons();
			if(self.options.displaycount) this.updateText();
        });
        return this;
	}

   $.fn.paginationmarginal = function (opts) {
        $(this).each(function (index, el) {
            var self = this;
            this.element = $(el);
            this.options = jQuery.extend({
                'item': '.item',
                'section': 'section',
                'paginationcontainer': 'article-right-col-pagination',
                'prev': 'prev',
                'next': 'next',
                'offset': 5
            }, opts || {});
            this.currentitem = 1;
            this.currentgroup = 1;
            this.totalitems = $(self).find(self.options.item).size();
            if(self.totalitems > self.options.offset)
            {
		   		$(self).find(self.options.item).each(function(index, el){
		   			$(el).addClass('group-' + self.currentgroup);
		   			$(el).addClass('item-' + self.currentitem);
		   			if(self.currentitem/self.options.offset == parseInt(self.currentitem/self.options.offset) || self.currentitem == self.totalitems)
		   			{
		   				$(self).find('.group-' +  self.currentgroup).wrapAll('<div class="' + self.options.section + '" />');
		   				self.currentgroup++;
		   			}
		   			self.currentitem++;
		   		});
		   		$(self).append('<div class="' + self.options.paginationcontainer + '"><a class="' + self.options.prev + ' ' + self.options.prev + '-disabled" title="vorherige Seite"><img src="/fileadmin/templates/img/icons/pagination-arrow-wai-left-no-14x17.png" alt="vorherige Seite" /></a><span class="current"></span> von <span class="count"></span><a class="' + self.options.next + '" title="nächste Seite"><img src="/fileadmin/templates/img/icons/pagination-arrow-wai-right-no-14x17.png" alt="nächste Seite" /></a></div>');
				$(self).slider({
				    'container': '.outer-wrapper',
				    'item': '.' + self.options.section,
				    'disabler': true,
				    'displaycount': true
				});
	   		}
        });
        return this;
   }




// !$.fn.tabs	
    $.fn.tabs = function (opts) {
        $(this).each(function (index, el) {
            var self = this;
            this.previous = 0;
            this.current = 0;
            this.element = $(el);
            this.options = jQuery.extend({
                'handlers': '.tab-list li',
                'contents': '.tab-content',
                'active': 'active',
                'preload': true,
                'direction': false,
                'leftside': 'left-border',
                'rightside': 'right-border',
                'leftsideactive': 'left-active',
                'rightsideactive': 'right-active',
                'current': false,
                'ajax': false,
                'adjustHeight': false,
                'callback': function () {}
            }, opts || {});
            this.setDirections = function () {
                self.handlers.removeClass(self.options.leftside + ' ' + self.options.rightside + ' ' + self.options.leftsideactive + ' ' + self.options.rightsideactive);
                self.handlers.filter(':lt(' + self.current + ')').addClass(self.options.leftside);
                self.handlers.filter(':gt(' + self.current + ')').addClass(self.options.rightside);
                switch (self.options.direction) {
                case 'ltr':
                    if (self.current == 0) self.handlers.eq(self.current).addClass(self.options.leftsideactive);
                    else self.handlers.eq(self.current).addClass(self.options.rightsideactive);
                    break;
                case 'rtl':
                    if (self.current == self.handlers.length - 1) self.handlers.eq(self.current).addClass(self.options.rightsideactive);
                    else self.handlers.eq(self.current).addClass(self.options.leftsideactive);
                    break;
                default:
                    return;
                }
            }
            this.load = function (num) {
                var num = num || self.current;
                var lnk = self.handlers.eq(num).is('a') ? self.handlers.eq(num) : self.handlers.eq(num).find('a:first');
                var url = lnk.attr('rel') ? lnk.attr('rel') : lnk.attr('href');
                var height = self.options.adjustHeight ? (self.contents.eq(num).css('height') || '0px') : '0px';
                height = parseInt(height.replace(/px/g, '')) > 0 ? height : 'auto';
                self.contents.eq(num).addClass('loading').css('height', self.contents.eq(self.previous).height() + 'px').html('').load(self.options.ajax, {
                    'tab': url
                }, function (res) {
                    self.contents.eq(num).removeClass('loading').css('height', height);
                    self.options.callback(self);
                });
            }
            this.activate = function (num) {
                self.handlers.removeClass(self.options.active);
                self.handlers.eq(num).addClass(self.options.active);
                self.contents.hide();
                self.contents.eq(num).show();
                self.previous = self.current;
                self.current = num;
                if (self.options.direction) self.setDirections();
                if (self.options.ajax) self.load(num);
                else self.options.callback(self);
            }
            this.activateHandler = function (ev) {
                ev.preventDefault();
                self.activate($(this).data('num'));
            }
            this.handlers = $.isFunction(this.options.handlers) ? this.options.handlers(this) : this.element.find(this.options.handlers).each(function (index) {
                $(this).addClass('num-' + index).data({
                    'num': index
                });
            }).click(this.activateHandler);
            this.contents = $.isFunction(this.options.contents) ? this.options.contents(this) : this.element.find(this.options.contents).each(function (index) {
                $(this).addClass('num-' + index).data({
                    'num': index
                });
            });
            if (this.options.preload && this.options.ajax) {
                this.current = this.handlers.filter('.' + this.options.active + ':first').data('num') || 0;
                this.activate(this.options.current ? this.options.current : this.current);
            }
            if (!this.options.ajax) {
                this.current = this.contents.index(this.contents.filter(':visible')) || 0;
                this.activate(this.options.current ? this.options.current : this.current);
            }
        });
        return this;
    }
})(jQuery);

$(document).ready(function() {

    // detect if css is supported
	if($('#homeLink img').css('marginTop') == '20px')
	{
		var cssEnabled = true;
	}

	if(cssEnabled){
		
		if($('#homeRotation div.item').size() > 1){
			$('#homeRotation').append('<div class="controls"><a href="#" class="playpause stop">Animation beenden</a><div class="numbers" style="display: none"></div></div>');
			$('#homeRotation div.item').each(function(index, el){$('#homeRotation .controls .numbers').append('<a href="#" class="number">' + (index+1) + ' </a>');});
			$('#homeRotation div.item:gt(0)').hide();
			var homeRotation = setInterval(function(){
					$('#homeRotation div.item:first').fadeOut(1500)
					.next('div.item').fadeIn()
					.end().appendTo('#homeRotation');
					$('#homeRotation div.controls').appendTo('#homeRotation');
					}, 
				10000);
				 
				$('#homeRotation .playpause').toggle(
					function(){
						$(this).removeClass('stop').addClass('play').html('Animation starten');
						$('#homeRotation .controls .numbers').show();
						window.clearInterval(homeRotation);
						$('#homeRotation div.item').wrapAll('<div class="slider-outer" />').wrapAll('<div class="slider-inner" />').fadeIn();
						$('#homeRotation').slider({'container': '.slider-outer'});
						$('#homeRotation div.item').each(function(index, el){
							$(this).find('a').focusin(function(){
								$('#homeRotation .numbers .number-' + index).trigger('click');
							});
						});
						
					},
					function(){
						$('#homeRotation div.item a').unbind('focusin');
						$(this).removeClass('play').addClass('stop').html('Animation beenden');
						$('#homeRotation .controls .numbers').hide();
			 			$('#homeRotation div.item').unwrap('.slider-inner').unwrap('.slider-outer').removeClass().addClass('item');
			 			$('#homeRotation div.item:gt(0)').hide();
			 			homeRotation = setInterval(function(){
				  			$('#homeRotation div.item:first').fadeOut(1500)
					 		.next('div.item').fadeIn()
					 		.end().appendTo('#homeRotation');
					 		$('#homeRotation div.controls').appendTo('#homeRotation');
					 		}, 
				 		10000);
					}
				); 
		}


		if ($(".publications-slider .inner-wrapper > li.item").size() > 1) {
	    	$('.publications-slider .focus-teaser-inner').append('<a class="prev" title="vorherige Seite">&laquo; vorherige Seite</a><a class="next" title="nächste Seite">nächste Seite &raquo;</a>');
			$('.publications-slider').slider({
			    'container': '.outer-wrapper',
			    'item': '.item'
			});
	    }
		if ($(".publications-slider-small .inner-wrapper > li.item").size() > 1) {
	    	$('.publications-slider-small').append('<a class="prev" title="vorherige Seite"><img src="/fileadmin/templates/img/icons/pagination-arrow-wai-left-no-14x17.png" alt="vorherige Seite" /></a><a class="next" title="nächste Seite"><img src="/fileadmin/templates/img/icons/pagination-arrow-wai-right-no-14x17.png" alt="nächste Seite" /></a>');
			$('.publications-slider-small').slider({
			    'container': '.outer-wrapper',
			    'item': '.item',
				'disabler': true
			});
	    }
	    if ($(".press-teaser .inner-wrapper > div.group").size() > 1) {
	    	$('.press-teaser .pagination').append('<a class="prev" title="vorherige Seite">&laquo; vorherige Seite</a><a class="next" title="nächste Seite">nächste Seite &raquo;</a>');
			$('.press-teaser').slider({
			    'container': '.outer-wrapper',
			    'item': '.group'
			});
	    }
	    
	    $(".article-gallery").each(function(index, el){
	    
	    	if($(el).find('div.item').size() > 1){
	    		$(el).removeClass('article-gallery-noslider');
		    	$(el).append('<a class="agprev" title="vorherige Seite">&laquo; vorherige Seite</a><a class="agnext" title="nächste Seite">nächste Seite &raquo;</a>');
				$(el).slider({
				    'container': '.inner',
				    'item': '.item',
				    'next': '.agnext',
				    'disabler': true,
				    'prev': '.agprev'
				});
				$(el).find('.zoom').html('Bildergalerie vergrößern');
	    	}
	    });
	    $(".media-container").each(function(index, el){
	    
	    	if($(el).find('div.item').size() > 1){
	    		$(el).addClass('media-container-slider');
		    	$(el).find('.media-content-container').append('<a class="prev" title="vorherige Seite">&laquo; vorherige Seite</a><a class="next" title="nächste Seite">nächste Seite &raquo;</a>');
				$(el).slider({
				    'container': '.slider-wrapper',

				    'disabler': true,
				    'item': '.item'
				});
	    	}
	    });
	    
		// !Pagination    
	    if ($('.article-content .section').size() > 1) {
	    	$('.article-content .article-pagination').append('<a class="prev prev-disabled" title="vorherige Seite">&laquo; vorherige Seite</a><span class="current">1</span> von <span class="count">1</span><a class="next next-disabled" title="nächste Seite">nächste Seite &raquo;</a>');
	    }
		$('.article-content .section').pagination({
			'wrapper': $('.article-content .outer-wrapper'),
			'current': $('.article-content').find('.current'),
			'count': $('.article-content').find('.count'),
			'prev': $('.article-content').find('.prev'),
			'next': $('.article-content').find('.next'),
			'links': $('.article-directory a, .article-content a')
		}, {
			'duration': 1,
			'startWith': 0
		});
	
		// !Glossary
		$('.glossary-teaser .alphabetical-index a').glossary({
			'list': $('.glossary-teaser .topics-list'),
			'default': '9',
			'variable': 'key',
			'url': '/js.glossary.ajax.php'
		});
	
	
		if($('#menu').hasClass('iPad')) {
			
			$('#menu > ul > li > h2 > a').each(function(index, el){
				$(el).click(function(){
					$('#menu > ul > li > h2 > a').not($('#menu > ul > li > h2 > a')[index]).parents('li').removeClass('hover');
					if(!$(this).parents('li').hasClass('hover')) {
						$(this).parents('li').addClass('hover');
						return false;
					}
				});
			});
			$('#contentWrapper, #header').click(function() {
				$('#menu > ul > li > h2 > a').parents('li').removeClass('hover');
			});			
		
		} else {
			$('#menu > ul > li:not(#menu .flyout li)').mouseDelay(1000, 500); //, function(el) { return el.parents('li:first'); });
			$('#menu .flyout .closer a').click(function(){
				$('#menu .flyout').css('left', '-10000px'); 
				$('#menu ul li').removeClass('hover')
			});
			$('#menu ul li:not(#menu .flyout li)').mouseenter(function(){
				$(this).children('div.flyout').removeAttr('style')
			});
			$('#breadcrumbs a, #homeRotation a').focusin(function(){
				$('#menu > ul > li').mouseleave();
				$('#menu .flyout').css('left', '-10000px'); 
				$('#menu ul li').removeClass('hover')
		
			});
		}
	
		$('.servicebar .contact-small textarea').inputDefault();
		$('.newsletter-teaser input.text').inputDefault();
		
		$('.submitonchange').change(function(e) {
	        e.target.form.submit();  
	    });
	    
	    $('.media-content-container .flowplayer, .heroteaser .flowplayer a, .article-content .article-video .teaser-img, .article-container .c50r .article-video .teaser-img').append('<span class="play_btn">&nbsp;</span>');
	   
	    $('.article-glossary .glossary-container, .links-container, .downloads-container').paginationmarginal();
	
	   	$('.article-downloads .downloads-container .item:last-child').addClass('lastChild');
	   	$('.article-links .links-container .item:last-child').addClass('lastChild');
	}   
	
	//@Andre hide elements that should be visible w/o js
	$('.invisible-by-js').addClass('invisible');
 
    	
});


// setting the flowplayier license keys based on domain
//var flowplayerkey;
if (document.domain == 'bmg.bund.de' || document.domain == 'www.bmg.bund.de' ) {
	flowplayerkey = '#$cd6c901d1fc6cc81167';
} else if (document.domain == 'bundesgesundheitsministerium.de' || document.domain == 'www.bundesgesundheitsministerium.de') {
	flowplayerkey = '#$2e418a66b46aa121493';
} else {
	flowplayerkey = 'INVALIDDOMAIN';
}


// Encryption for email links. Taken from TYPO3's default javascript
function decryptCharcode(n,start,end,offset){
	n=n+offset;
	if(offset>0&&n>end){
		n=start+(n-end-1);
	}else if(offset<0&&n<start){
		n=end-(start-n-1);
	}
	return String.fromCharCode(n);
}

function decryptString(enc,offset){
	var dec="";
	var len=enc.length;
	for(var i=0;i<len;i++){
		var n=enc.charCodeAt(i);
		if(n>=0x2B&&n<=0x3A){
			dec+=decryptCharcode(n,0x2B,0x3A,offset);
		}else if(n>=0x40&&n<=0x5A){
			dec+=decryptCharcode(n,0x40,0x5A,offset);
		}else if(n>=0x61&&n<=0x7A){
			dec+=decryptCharcode(n,0x61,0x7A,offset);
		}else{
			dec+=enc.charAt(i);
		}
	}
	return dec;
}
function linkTo_UnCryptMailto(s){
	location.href=decryptString(s,-1);
}



/***** BITV ****/
/* http://www.highresolution.info/weblog/entry/skiplinks_best_practices/ */
var YAML_focusFix = {
	init: function() {
		var skipClass = 'wai-skip';

		var userAgent = navigator.userAgent.toLowerCase();
		var is_webkit = userAgent.indexOf('webkit') > -1;
		var is_ie = userAgent.indexOf('msie') > -1;
		var i = 0;
		var links, skiplinks = [];

		if (true || is_webkit || is_ie) {
			// find skiplinks in modern browsers ...
			if ( document.getElementsByClassName !== undefined) {
				skiplinks = document.getElementsByClassName(skipClass);

				for (i=0; i<skiplinks.length; i++) {
					this.setTabIndex(skiplinks[ i ]);
				}
			} else {
				// find skiplinks in older browsers ...
				links = document.getElementsByTagName('a');
				for (i=0; i<links.length; i++) {
					var s = links[ i ].getAttribute('href');
					var c = links[ i ].getAttribute('class');
					if (s !== null && c !== null && s.length > 1 && c.indexOf(skipClass) != -1 && s.substr(0, 1) == '#' ) {
						this.setTabIndex(links[ i ]);
					}
				}
			}
		}
	},

	setTabIndex: function( skiplink ){
		var target = skiplink.href.substr(skiplink.href.indexOf('#')+1);
		var targetElement = document.getElementById(target);

		if (targetElement !== null) {
			// make element accessible for .focus() method
			targetElement.setAttribute("tabindex", "-1");
			skiplink.setAttribute("onclick", "document.getElementById('"+target+"').focus();");
		}
	}
};
$(document).ready(function() {

//	$('#menuItem1 h2.navmain-level1 a').attr('id', 'wai-skiptarget-n1');
//	$('#menuItem2 h2.navmain-level1 a').attr('id', 'wai-skiptarget-n2');
//	$('#menuItem3 h2.navmain-level1 a').attr('id', 'wai-skiptarget-n3');
//	$('#menuItem4 h2.navmain-level1 a').attr('id', 'wai-skiptarget-n4');
//	$('#menuItem5 h2.navmain-level1 a').attr('id', 'wai-skiptarget-n5');
//	$('#wai-skip-n1').attr('href', '#wai-skiptarget-n1');
//	$('#wai-skip-n2').attr('href', '#wai-skiptarget-n2');
//	$('#wai-skip-n3').attr('href', '#wai-skiptarget-n3');
//	$('#wai-skip-n4').attr('href', '#wai-skiptarget-n4');
//	$('#wai-skip-n5').attr('href', '#wai-skiptarget-n5');

	YAML_focusFix.init();

	$('.closer a').click(function(){
		window.location.hash = '#wai-skiptarget-top'; // when flyout is closed, go to page top and focus direct before skipmenu
		$('#wai-skiptarget-top').focus();
		$('#wai-skiptarget-top').attr('tabindex', '-1');
	});
});

