$(document).ready(function(){
	initPageWidth();
	initMenuAnimation($("#hidden_menuItem").val());
	
	initReplaceHeadlines();
	initWorkRoomDefaults();
});

$(window).load(function(){
  initAccentImagePlacement();
});

function initMenuAnimation(thisPage){
	thisPage -= 1;
	
	/* 
		A Fix for IE
	*/
	/* Inactivated
		$(".menu_sub_item").each(function(){
			var tmpOpacity = $(this).css('opacity');
			if(tmpOpacity > 0){
				$(this).animate({'opacity' : '0'}, 250);
			}
		});
		$("#menu_sub_item_" + thisPage).animate({'opacity' : '1'}, 250);
		//End of fix
	*/
	
	//Find out the default state of the indikator
	var thisPagePosition = $("#menu_items a:eq(" + thisPage + ")").position();
	var thisPageWidth = $("#menu_items a:eq(" + thisPage + ")").width();
	
	var thisPageSetPosition = thisPagePosition.left - 934 - parseInt($("#wrapper").css('marginLeft')) + (thisPageWidth / 2);
	
	//Set default state
	$("#menu_indikator").css({ 'backgroundPosition' : thisPageSetPosition + 'px -1px' });
	
	/* Inactivated
		//Show first message
		$("#menu_sub_item_" + thisPage).animate({'opacity' : '1'}, 2500);
	*/
	$("#menu_sub_item_" + thisPage).css({'display' : 'block'});
	
	/* Inactivated 
	//Loop through messages and set them to absolute position
	var menu_sub_item_heights = new Array();
	$(".menu_sub_item").each(function(i){
		menu_sub_item_heights[i] = $(this).height();
		$(this).css({
			'position' : 'absolute',
			'display' : 'block'
		});
	});
	*/
	
	//When hovering a link in the menu, this activates
	$("#menu_items a").mouseover(function(){
		$("#menu_indikator").stop(1); //If another animation is active - stop it
		var mouseOverThis = $(this);
		var tempPosition = mouseOverThis.position();
		var tempWidth = mouseOverThis.width();
		
		var newPosition = tempPosition.left - 934 - parseInt($("#wrapper").css('marginLeft')) + (tempWidth / 2);
		$("#menu_indikator").animate({ 'backgroundPosition' : newPosition + 'px -1px' }, 1000);
		
		/* Inactivated 
		var identifier = getIdentifier(mouseOverThis.attr('id'));
		var closeThis = getActiveMenuSubItem();
		
		if(identifier != closeThis){
			$(".menu_sub_item").stop(1);
			$(".menu_sub_item").each(function(){
				var tmpOpacity = $(this).css('opacity');
				if(tmpOpacity > 0){
					$(this).animate({'opacity' : '0'}, 250);
				}
			});
			$("#menu_sub_item_" + identifier).wait(250).animate({'opacity' : '1'}, 250);
			$("#menu_sub").css({ 'height' : menu_sub_item_heights[identifier] + 'px' });
			//$("#menu_sub").animate({ 'height' : menu_sub_item_heights[identifier] + 'px', 'width' : '925px' }, 250);
		}
		*/
		
	});
	
	$("#menu").mouseleave(function(){
		/* Inactivated $(".menu_sub_item").stop(1); */
		$("#menu_indikator").stop(1);
		$("#menu_indikator").animate({ 'backgroundPosition' : thisPageSetPosition + 'px -1px' }, 500);
		
		/* Inactivated 
		var closeThis = getActiveMenuSubItem();
		if(thisPage != closeThis){
			$(".menu_sub_item").each(function(){
				var tmpOpacity = $(this).css('opacity');
				if(tmpOpacity > 0){
					$(this).animate({'opacity' : '0'}, 250);
				}
			});
			$("#menu_sub_item_" + thisPage).animate({'opacity' : '1'}, 250);
			$("#menu_sub").css({ 'height' : menu_sub_item_heights[thisPage] + 'px' });
		}
		*/
	});
}

function getActiveMenuSubItem(){
	var returnMe = false;
	$(".menu_sub_item").each(function(){
		var tmpOpacity = $(this).css('opacity');
		if(tmpOpacity > 0){
			returnMe = getIdentifier($(this).attr('id'));
		}
	});

	return returnMe;
}

function initReplaceHeadlines(){
	$("h1.headlineChange").each(function(){
		var thisObject = $(this);
		thisObject.replaceWith('<img src="/images/text/' + thisObject.attr('id') + '.png" alt="' + thisObject.text() + '" />');
	});
	
	//After changing the headlines, the Accent-image might need to be re-positioned
	initAccentImagePlacement();
}

function initAccentImagePlacement(){
	var accentImage = $("#firstBoxAccent");
	var boxWrapper = $(".box90_wrapper");
	var boxWrapperPosition = boxWrapper.position();
	
	var bottomPx = boxWrapper.height() + boxWrapperPosition.top - accentImage.height() + 25;
	
	accentImage.css({ 'top' : bottomPx + 'px' });
	
}

function initPageWidth(){
	var pageWidth = $(window).width();
	var contentWidth = $(document).width();
	var wrapperMarginLeft = parseInt($("#wrapper").css('marginLeft'));
	var firstBoxAccentLeft = $("#firstBoxAccent").position().left;
	
	if(pageWidth < contentWidth){
		var newMargin = wrapperMarginLeft - (contentWidth - pageWidth);
		if(newMargin <= 5){
			newMargin = 5;
		}
		$("#wrapper").css({ 'marginLeft' : newMargin + 'px' });
		
		var newLeft = (firstBoxAccentLeft - wrapperMarginLeft) + newMargin;
		$("#firstBoxAccent").css({ 'left' : newLeft + 'px' });
	} else {
		//No need for action
	}
}
