$(document).ready(
		function() {
			
			$("#menu").show();
			
			// custom menu hover functions for TYPO3 generated menu items
			$("#menu .subMenu").hide().css("position","absolute").css("top","43px");
			$("#menu ul li a").removeAttr("onmouseover");
			$("#menu ul li a").removeAttr("onmouseout");
			$("#menu ul li a").removeAttr("onfocus");
			$("#menu ul li").bind('mouseenter',function() {

					$(this).find("img").first().attr("src",eval($(this).find("img").attr("name")+ "_h.src"));
				
						if ($(this).hasClass("mainMenuItem")) {
					
							$("#menu .subMenu").hide();
							$(this).find(".subMenu").show();
							
							/**
							 * determine wheather to align left to the menu point or if too big to the right
							 */
							
							subMenuWidth = $(this).find(".subMenu").width();
							containerWidth = $("#container").width();
							menuX = $(this).position().left+$("#menu").position().left+32;
							if ((containerWidth - menuX) < subMenuWidth) {
								$(this).find(".subMenu").css("right","0px");
							} else {
								$(this).find(".subMenu").css("left",$(this).position().left + 32 + "px");
							}
							$(this).addClass("active");
						
						}
					});
			
			$("#menu ul li").bind('mouseleave',function() {
				$(this).find("img").first().attr("src",eval($(this).find("img").attr("name")+ "_n.src"));
				if ($(this).hasClass("mainMenuItem")) {
					$(this).removeClass("active");
					$(this).find(".subMenu").hide();
				}
			});
			
			// home top contents
			$("#top .top-nav li").bind('mouseover', function() {
				$("#top .top-nav li").removeClass("active");
				$(this).addClass("active");
				var $index = $('#top .top-nav li').index($(this));
				$("#top-images .image").hide();
				$("#top-images .image:nth-child("+($index+1)+")").show();
			});
			
			$("#top-images .image").hide();
			$("#top-images .image").first().show();
			
			// home tabs
			$("#side-tabs .panels div.panel").hide();
			$("#side-tabs div.controls div").append(
					'<div class="arrow"></div>');
			$("#side-tabs div.controls div").bind('mouseover', function() {
				$("#side-tabs div.controls div").removeClass("active");
				$(this).addClass("active");
				$("#side-tabs .panels div.panel").hide();
				$($(this).find("a").attr("href")).show();
			});
			$("#side-tabs .panels div.panel").first().show();
			$("#side-tabs div.controls div").first().addClass("active");
			$('#side-tabs div.controls a').click(function(e) {
			    e.preventDefault();
			});
			
			// offering tabs
			$("#contentNuggetTabs .panels div").hide();
			
			$("#contentNuggetTabs ul.controls li").bind('mouseover', function() {
				var href = $(this).find("a").attr("href");
				activateOfferingContent(href.substring(href.length-1));
			});
			$("#contentNuggetTabs ul.controls li").click(function() {
				return false;
			});
			if ($("#contentNuggetTabs")) {
				activateOfferingContent(1);
			}
			
			// offering content panels
			$(".contentNuggetContainer").bind('mouseover', function() {
				var id = $(this).attr("id");
				activateOfferingContent(id.substring(id.length-1));
			});
			
			// dirty ... remove toptheme mouseover ... last second change
			$(".toptheme .contentNuggetContainer").unbind('mouseover');
			$(".toptheme .contentNuggetContainer").removeClass("active");
			
			/**
			 * job teaser box
			 */
			
			if ($("#jobTeaser")) {
				//$("#jobTeaser ul.teasers li").css("opacity","0").css("position","absolute").css("top","0px");
				$("#jobTeaser ul.teasers li").hide().css("position","absolute").css("top","0px");
				
				$("#jobTeaser ul.teasers").css("position","absolute").css("top","0px").css("left","0px");
				var width = $("#jobTeaser").width();
				var count = $("#jobTeaser ul.teasers li").length;
				var index = 0;
				$('#jobTeaser ul.teasers li').each(function() {
					var randomHeight = Math.floor(Math.random()*110);
					var randomDelay = Math.floor(Math.random()*2000)
					//$(this).delay(randomDelay).fadeIn(2000);
					$(this).delay(randomDelay).fadeIn(1000,function() {
						if ($(this).hasClass("new")) {
							$(this).append("<img class='banderole' src='fileadmin/s21/img/careers/banderole.png' />");
							$(this).find(".banderole").hide().fadeIn(1000);
						}
						
					});
					//$(this).css("left",20+index*width/count+"px");
					$(this).css("top",50+randomHeight+"px");
					index ++;
				});
			}
			
				/* contact boxes */
			if ($(".contactBoxControl")) {
				$(".contactBoxControl").bind('mouseover', function() {
					$($(this).attr("href")).stop().show();
				});
				$(".contactBox").css("position","absolute");
				$('.contactBoxControl').each(function() {
					var $position = $(this).position();
					var $target = $($(this).attr("href"));
					
					if ($target.hasClass("above")) {
						$target.css("top",$position.top - 23 - $target.height() + "px");
					} else {
						$target.css("top",$position.top + 18 + "px");
					}
					$target.css("left",$position.left + $(this).width()/2 - 15 + "px");
				});
				
				$(".contactBoxControl").bind('mouseout', function() {
					$($(this).attr("href")).stop().hide();
				});
				
				$(".contactBox").hide();
				$('.contactBoxControl').click(function(e) {
				    e.preventDefault();
				});
			}
			$(".accordion").accordion({ autoHeight: false });
		});

function activateOfferingContent(n){
	
	// content panel
	
	$(".contentNuggetContainer").removeClass("active");
	$(".offering #content-"+n).addClass("active");
	
	// tab
	$("#contentNuggetTabs ul.controls li").removeClass("active");
	$($("#contentNuggetTabs ul.controls li")[n-1]).addClass("active");
	
	$("#contentNuggetTabs .panels div").hide();
	$($($("#contentNuggetTabs ul.controls li")[n-1]).find("a").attr("href")).show()
	
	//alert(n);
	return;
	//total = $('.gallery ul li');
	target.find('li.active').removeClass('active').stop().animate({opacity:0},400);
	target.find('ul').stop().animate({height:n.height()},400);
	n.addClass('active').stop().animate({opacity:1},400);
	return false;
}
