jQuery(document).ready(function() {
	/**
	 * find all navigation links and add them the onclick function for the slide effect
	 * based on the given name the images will switch (images must be predefined in code)
	 **/
	 
	 	
	var mainNavigationRuntime = 0; 
	jQuery('.NavigationBg .mainNavigationCss ul').find('li').each( function() {
	
		mainNavigationRuntime++;
		
		var liContent = jQuery(this).html();
		//liContent contains something like: <a id="menu-mainNav_32" class="main" href="/mediathek">Mediathek</a>
				
		var beginOfId = liContent.indexOf('id=');
	
		if (beginOfId != -1) {
			var beginOfIdPlus = 4;
		
			//handles the missing " after id= in Internet Explorer
			var safetyCheck = liContent.substring(beginOfId+3, beginOfId+4);
			if (safetyCheck != '"') {
				beginOfIdPlus = 3
			}
		
			var removedString = liContent.substring(0, beginOfId+beginOfIdPlus);
			liContent = liContent.replace(removedString, "");
			liContent = liContent.replace("menu-mainNav_", "");
																
			var charIsNumber = true;
			var charAtPos;
			var i = -1;
			while (charIsNumber == true) {
				i++;
				charAtPos = liContent.charAt(i);
				
		 		if ( (charAtPos == 0) || (charAtPos == 1) || (charAtPos == 2) || (charAtPos == 3) ||
				 	 (charAtPos == 4) || (charAtPos == 5) || (charAtPos == 6) || (charAtPos == 7) || 
				 	 (charAtPos == 8) || (charAtPos == 9) ) {
					charIsNumber = true;
				} else {
					charIsNumber = false;
				}
			}
			
			liContent = liContent.substring(0, i);
			
			jQuery(this).find('a').bind('mouseenter', function() { navMouseOver(liContent, jQuery(this).html()); }).attr({"href": "javascript:void(0);"});
			
			jQuery('.subNavigationDiv').append('<div class="navLinkId_'+liContent+'" style="display:none;"></div>');
			jQuery('.navigationContainerForInit').find('#menu-mainNav_'+liContent).next('ul').clone().appendTo('.navLinkId_'+liContent);
			
		}	

		if (jQuery(this).hasClass("active")) {
			var halfLinkWidth = jQuery(this).find('a').css("width").replace("px", "") / 2;
			var linkPos = jQuery(this).find('a').position();
			var newPosLeft = (linkPos.left + halfLinkWidth) - 96;
						
			var newDiv = '<div class="mainNavActiveSignal" style="left:'+newPosLeft+'px;"></div>';
			jQuery(this).append(newDiv);
			
			jQuery('.backButton').attr({"onclick": "backButtonClick('"+ liContent +"', '"+ jQuery(this).find('a').html() +"');"});
		} 
	});
		 

	jQuery('.NavigationBg').bind('mouseleave', function() { jMouseOut(); });	
	

	
	jQuery('.NavDropdown .subNavigationDiv').find('div').each( function() {
		jQuery(this).find('li:last').addClass('lastSubnavigationli');
	});
		
	jQuery('.NavDropdownSubMenu').find('ul').each( function() {
		jQuery(this).find('li:last').addClass('lastSubSubNavigationli');
	});
	
	
	
	/*
	 * edit the breadcrumbs
	 */
	jQuery('.UserIsHere').find('a').each(function() {
		jQuery(this).addClass('breadcrumbLink');
		if (jQuery(this).hasClass('backButtonImprint') == false) {
			jQuery(this).attr({"href": "javascript:void(0);"});
		}
	});
	
	
	
	/**
	 * find every form button and add some attributes
	 **/
	jQuery('.contentPlaceholder').find(':submit')
		.attr({"onmousedown": "this.style.backgroundColor='#962b24'; this.style.color='white'"})
		.attr({"onmouseup": "this.style.backgroundColor='#f0f0f0'; this.style.color='black'"})
		.attr({"onmouseout": "this.style.backgroundColor='#f0f0f0'; this.style.color='black'"});
	
	/**
	 * remove the br in form
	 **/
	jQuery('.contentPlaceholder').find('#anrede-element').find('br').remove();
		
	/**
	 * if user has added an image to the content, give the image style and hover effect
	 **/
	if ( jQuery('.contentPlaceholder').find('img').size() > 0) {
		var imgNumber = 0;
		if ( jQuery('.contentPlaceholder').find('.noInitActionOnTheImages').size() == 0 ) {
			jQuery('.contentPlaceholder').find('img').each( function () {
				imgNumber++;
				jQuery(this)
					.attr({"onmouseover": "showContentImgHover('"+imgNumber+"');"})
					.attr({"onmouseout": "hideContentImgHover('"+imgNumber+"');"})
					.css({"position": "relative", "left": "5px", "z-index": "2"});
				
				var imgPosition    = jQuery(this).position();
				var newDivPosTop   = imgPosition.top - 4;
				var newDivPosLeft  = imgPosition.left - 4;
				var newDivWidth    = jQuery(this).width() + 6;
				var newDivHeight   = jQuery(this).height() + 6;				
				var HoverDivWidth  = newDivWidth - 2;
				var HoverDivHeight = newDivHeight - 2;
				jQuery(this).parent()
					.append("<div class='imgStylingDiv' id='imgNumberForStyle"+imgNumber+"' style='z-index:1; left:"+newDivPosLeft+"px; top:"+newDivPosTop+"px; width: "+newDivWidth+"px; height:"+newDivHeight+"px;'>" +
							"<div class='imgHoverDiv' id='imgNumberForHover"+imgNumber+"' style='z-index:1; display:none; width:"+HoverDivWidth+"px; height:"+HoverDivHeight+"px;'></div></div>");
			});
		} else {
			if ( jQuery('.contentPlaceholder').find('#galleryImages').size() == 0 )
			{
				if ( jQuery('.contentPlaceholder').find('.simpleImageBrickJs').size() == 0 ) {
					jQuery('.contentPlaceholder').find('img').css({"border": "1px solid #962b24", "margin-left": "5px", "margin-top": "5px", "z-index": "1"});
				} else {
					jQuery('.contentPlaceholder').find('img').css({"border": "1px solid #962b24", "z-index": "1"});
				}
			} 
		}
	}
	
	
	/**
	 * remove sub sub navigation points
	 **/
	if ( jQuery('.subNavigationDiv ul').find('a').size() > 0) {
		jQuery('.subNavigationDiv ul').find('a').each( function () { 
			jQuery(this).attr({"onmouseover": "hideSubMenu()"});
		});	
	}
	
	
	
	/**
	 * gallery actions
	 **/
	 jQuery('.galleryDivContainer')		
	 	.bind("mouseenter", function () { fadeInTheArrows(); })
		.bind("mouseleave", function () { fadeOutTheArrows(); });
	
	
	/**
	 * add hover animation for event links
	 **/
	if ( jQuery('.eventContainer').find('a').size() > 0 ) {
		jQuery('.eventContainer').find('a').each( function() { 
			jQuery(this).attr({"onmouseover": "jQuery(this).css({'text-decoration': 'underline'})", "onmouseout": "jQuery(this).css({'text-decoration': 'none'})"});
		});
	}	
	
	if ( jQuery('.contentPlaceholder .eventBrickHeadline').find('a').size() > 0 ) {
		jQuery('.contentPlaceholder').find('.eventBrickHeadline').each(function() {
			jQuery(this).find('a').attr({"onmouseover": "jQuery(this).css({'text-decoration': 'underline'})", "onmouseout": "jQuery(this).css({'text-decoration': 'none'})"});
		});
	}
	
	/**
	 * Grundriss (Startseite)
	 **/
	if (jQuery('.bqGrundrissContainer').size() > 0) {
		jQuery('.bqGrundrissContainer').bind('mouseenter', function() { showHideOverlay(1); });
		jQuery('.bqGrundrissContainer').bind('mouseleave', function() { showHideOverlay(2); });
	}
	
});
