(function($){
$('html').addClass('js-on');
    function callOnDomready(){
$('body').removeClass('js-off');
   
   $('body').each(function(){
   	
	var slideShowTimer,
		wrapper 	= $('#wrapper-branding'),
		toolTip 	= $('<div class="teaser tooltip" role="tooltip" aria-expanded="false"></div>')
                			.css({opacity: 0})
                			.appendTo('#stage'),
		index 	= 0
	;
	
	
	function switchNavTeaser(ui){
		
		var oldBranding = $('> .branding-image', wrapper),
		item	= ui.menuItem,
		id 		= item
					.find('.box')
					.attr('id'),
		toolTipContents = $('div.teaser', ui.menuItem)
					.html(),
		link = $('h3', ui.menuItem)
				.clone()
				.find('a')
				.html('')
		;
		
		if (!oldBranding.is('.' + id)) {
		toolTip
			.stop(true, true)
			.css({
			opacity: '0'
			})
			.html(toolTipContents)
			.find('h4', this)
			.wrapInner(link)
			.end()
			.animate({
			opacity: '.8'
			}, {
			duration: 600
			})
		;
		}
		
		if (!oldBranding.is('.' + id)) {
		$('<div class="branding-image ' + id + '" />')
			.stop(true, true)
			.css('opacity', '0')
			.prependTo(wrapper).animate({
			opacity: '1'
			}, {
			duration: 600
			})
		;
		
		oldBranding
			.stop(true, true)
			.animate({
			opacity: '0'
			}, {
			duration: 600,
			complete: function(){
				$(this).remove();
			}
			})
		;
		
		}
	}
	
	function showFirstTooltip() {
		var ui = ui || {};
                                    var list = $('#stage > .nav-global');
                                    index = 0;
                                    ui.menuItem = list.find('li.has-menu:eq('+index+')');
                                    link = $('h3', ui.menuItem)
                                        .clone()
                                        .find('a')
                                        .html('')
                                    ;
		var toolTipContents = $('ul.nav-global li.active div.teaser').html();
		toolTip
		.stop(true,true)
		.html(toolTipContents)
                                    .find('h4', this)
                                    .wrapInner(link)
                                    .end()
		.css({opacity: '0.8'})
		 ;
		 
	}
	
	function initSlideshow(e) {
		
		var ui = ui || {};
		
		slideShowTimer = setInterval(function(e){
		
		var list = $('#stage > .nav-global'),
			listlength = list.children('li.has-menu').length;
			
		index += 1;
		if(index > listlength-1) { //leave out the searchbox
			index = 0;
		}
		
		ui.menuItem = list.find('li.has-menu:eq('+index+')');
		switchNavTeaser(ui);
		
		}, 8000);
		
	}
	
	
	$('#wrapper-branding')
		.inOut(function(){}, function(e){
		
		$('ul.nav-global', this).simpleMenu("hideAll",e);
		//optional restartSlideshow
		//initSlideshow()
		
	});
	
   	$('ul.nav-global', this)
		.simpleMenu({
		closeOnItemLeave: false, // false funktioniert nur bei einfacher verschachtelung
		closeOnMenuLeave: false,
		addKeyNav: true,
		menuItemSel: 'h3 > a',
		show: function(e, ui){
			
			clearInterval(slideShowTimer);
			switchNavTeaser(ui);
			
		}
            })
	;
	
	showFirstTooltip();
	initSlideshow();
	
   });
   
    $('#navServiceMeta ul').before('<dl id="fontsize"><dt>' + SCALE_IMG + '</dt><dd class="default"><a class="current font" href="#" rel="normale Schrift" title="' + SCALE_IMG_NORMAL + '">A</a></dd><dd class="big" ><a class="font" href="#" rel="große Schrift" title="' + SCALE_IMG_LARGE + '">A</a></dd><dd class="huge"><a class="font" href="#" rel="sehr große Schrift" title="' + SCALE_IMG_LARGER + '">A</a></dd></dl>');
    createStyleSheetSwitcher();
    }
   
    function createStyleSheetSwitcher(){
            var links = $('a.font');
            links.click(function()
                              {
                                 switchStylestyle(this.getAttribute("rel"));
                                 return false;
                              });
                          var c = readCookie('style');
                          if (c) {
			  switchStylestyle(c);
			  }
                           function switchStylestyle(styleName)
                           {
                               
                              links.removeClass('current');
                              links.filter('[rel='+ styleName +']').addClass('current');
                              $('link[rel*=style][title]').each(function(i)
                              {
                                 this.disabled = true;
                                 if (this.getAttribute('title') == styleName) {
				 this.disabled = false;
				 }
                              });
                              createCookie('style', styleName, 365);
                           }
                    function saveActiveStyle(){
                        $('link[rel*=style][title]').each(function(i)
                              {
                                 if(!this.disabled){
                                    createCookie('style', this.getAttribute('title'), 365);
                                    return false;
                                 }
                              });
                    }
    
                     $(window).unload(saveActiveStyle);
                    // cookie functions http://www.quirksmode.org/js/cookies.html
                    function createCookie(name,value,days)
                    {
			var expires;
                        if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = "; expires=" + date.toGMTString();
			}
			else {
			expires = "";
			}
                        document.cookie = name+"="+value+expires+"; path=/";
                    }
                    function readCookie(name)
                    {
                        var nameEQ = name + "=";
                        var ca = document.cookie.split(';');
                        for(var i=0;i < ca.length;i++)
                        {
                            var c = ca[i];
                            while (c.charAt(0) == ' ') {
				c = c.substring(1, c.length);
			}
                            if (c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length, c.length);
			}
                        }
                        return null;
                    }
                    function eraseCookie(name)
                    {
                        createCookie(name,"",-1);
                    }
                    // cookie functions
    }
    $('html').addClass('js-on');
    $(callOnDomready);
})(jQuery);
