$(document).ready(
	function()
	{
	
		if ($(".modalInput").length > 0) {
			var formular = $('<div class="modal" id="prompt"><h2>Kontaktformular</h2><form id="contactform" method="post" onSubmit="return false"><div class="name"><label>Name:</label><input required="required" /></div><div 	class="mail"><label>E-Mail:</label><input type="email" name="email" required="required" /></div><div class="phone"><label>Telefonnummer:</label><input /></div><div class="msg"><label>Nachricht:</label><textarea name="msg" cols="10" rows="10" required="required" ></textarea></div><input type="submit" id="submit" value="Absenden" /></form><div class="success" style="display: none;">Anfrage wurde gesendet.</div></div>').appendTo(document.body);
		}
		
		var name;
		var email;
		$('.modalInput').click(function(){
			name = ($(this).parent().children().eq(0).text());
			email = ($(this).prev().children().eq(1).text());
		});
		$(".modalInput").overlay({
	
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.7
			},
			
			closeOnClick: true,

			onBeforeLoad : function() {
			}
		});

		$("#contactform").submit(function(event){
            event.preventDefault();

			var json = {
				"recipientname": name,
				"recipientmail": email,
				"title": "Kontaktanfrage: " + document.title,
				"link": '' + window.location,
				"sendername": $('.name input').val(),
				"sendermail": $('.mail input').val(),
				"senderphone": $('.phone input').val(),
				"sendermsg": $('.msg textarea').val()
			};
			//class.tx_fgad_content_elements_pi8_eid.php//
			$.ajax({
				type: 'POST',
				url: '/ihk/index.php?eID=ihkcontactmsg',
				data: json,
				dataType: 'json',
				success: function(){ 
					$('form#contactform').hide(function(){$('div.success').fadeIn();});
				},
				error:function (){
                    alert('Anfrage fehlgeschlagen.');
                }
			});	
		});	
		
		
		var navigation_popup_over = false;
		var mousemove_bound = false;
		
		$('.navigation-main .navigation-item a').mouseover(navigationMainOver).mouseout(navigationMainOut);
		$('.box-news-navigation a, .box-news-navigation p, .box-news-navigation h3').mouseover(BoxNewsNavigationOver).mouseout(BoxNewsNavigationOut).mouseover(BoxNewsNavigationClick);
		$('.box-quicklinks-navigation a').hover(boxQuicklinksSwitch);
		
		
		
		//	remove the value of the submit button in IE 7
		//if($.browser.msie == true && $.browser.version < 8)
		//{
		//	$('.box-search .search-submit, #tx_mnogosearch_pi1_submit').attr({value: '', title: 'Suchen'});
		//}
		
		function navigationMainOver(event)
		{		
			navigation_item = $(event.target);
			
			if(!navigation_item.parent().hasClass('branchen') && !navigation_item.parent().hasClass('jahresthema'))
			{
				

					navigation_item.stop().animate(
						{backgroundPosition: '0 -44px'}, 
						{duration: 200}
					);

				
				if(navigation_item.parent().hasClass('ausbildung'))
				{
					//navigation_popup_over = true;
					
					navigationPopupShow();					
					
					if(!mousemove_bound)
					{
						$('.wrapper-2').bind('mousemove', navigationPopupMousemove);
						mousemove_bound = true;
					}
				}
				
				else
				{
					//navigation_popup_over = false;
				}
			}
		}
		
		function navigationMainOut(event)
		{
			navigation_item = $(event.target);
			
			if(!navigation_item.parent().hasClass('branchen') && !navigation_item.parent().hasClass('jahresthema'))
			{
				if(!navigation_item.parent().hasClass('active') 
				&& !navigation_item.parent().hasClass('current') 
				&& !navigation_item.parent().hasClass('ausbildung'))
				{
					navigation_item.stop().animate(
						{backgroundPosition: '0 0'}, 
						{duration: 200}
					);					
				}
				
				else
				{
					navigationPopupHide();
				}		
			}
		}
		
		
		
		function navigationPopupShow()
		{
			$('.navigation-popup').show();
			$('.navigation-main .ausbildung a').css({color: '#353739', textShadow: 'none'});
		}
		
		function navigationPopupHide()
		{
			if(!navigation_popup_over)
			{
				$('.navigation-popup').hide();
				
				$('.navigation-main .ausbildung a').stop().animate(
					{backgroundPosition: '0 0'}, 
					{duration: 200}
				);
				
				if(mousemove_bound)
				{
					$('.wrapper-2').unbind('mousemove', navigationPopupMousemove);
					mousemove_bound = false;
				}
				
				$('.navigation-main .ausbildung a').css({color: '#fff', textShadow: ' #000 0 0 2px'});
			}
		}
		
		function navigationPopupMousemove(event)
		{
			var popup_offset = $('.navigation-popup').offset();
			var trigger_offset = $('.navigation-main .ausbildung').offset();
			
			var trigger_position = {X: trigger_offset.left, Y: trigger_offset.top};
			var popup_position = {X: popup_offset.left, Y: popup_offset.top};
			var mouse_position = {X: event.pageX, Y: event.pageY};
			
			if(
				mouse_position.X < popup_position.X + $('.navigation-popup').width() + 40 &&
				mouse_position.X > popup_position.X &&
				mouse_position.Y < popup_position.Y + $('.navigation-popup').height() + 50 &&
				mouse_position.Y > popup_position.Y
			)
			{
				navigation_popup_over = true;
			}
			
			else if(
				mouse_position.X < trigger_position.X + $('.navigation-main .ausbildung').width() &&
				mouse_position.X > trigger_position.X &&
				mouse_position.Y < trigger_position.Y + $('.navigation-main .ausbildung').height() &&
				mouse_position.Y > trigger_position.Y)
			{
				navigation_popup_over = true;
			}
			
			else
			{
				navigation_popup_over = false;
				navigationPopupHide();
			}
		}
		
		function BoxNewsNavigationOver(event)
		{
			
			var navigation_item;
			
			if($(event.target).is('h3, p'))
			{
				navigation_item = $(event.target).parent();
			}
			
			else if($(event.target).is('a'))
			{
				navigation_item = $(event.target);
			}
			
			if(
				!navigation_item.hasClass('active') &&
				!navigation_item.hasClass('selected')
			)
			{
				navigation_item.addClass('active');
				navigation_item.stop().animate(
					{backgroundPosition: '0 0'}, 
					{duration: 150}
				);
			}	
		}
		
		function BoxNewsNavigationOut(event)
		{
			clearInterval(window.boxNewsInterval);
			window.boxNewsInterval = setInterval(BoxNewsAuto,8000);
			
			var navigation_item;
			
			if($(event.target).is('h3, p'))
			{
				navigation_item = $(event.target).parent();
			}
			
			else if($(event.target).is('a'))
			{
				navigation_item = $(event.target);
			}
			
			if(
				navigation_item.hasClass('active') && 
				!navigation_item.hasClass('selected')
			)
			{
				$('.box-news-navigation .active').removeClass('active');
			
				navigation_item.stop().animate(
					{backgroundPosition: '-194px 0'}, 
					{duration: 150}
				);
			}
		}
		
		function BoxNewsNavigationClick(event)
		{
			clearInterval(window.boxNewsInterval);
		
			var navigation_item;
			
			event.preventDefault();
			
			if($(event.target).is('h3, p'))
			{
				navigation_item = $(event.target).parent();
			}
			
			else if($(event.target).is('a'))
			{
				navigation_item = $(event.target);
			}
			
			if(navigation_item.hasClass('selected'))
			{
				//dont do nothing
			}
			
			else
			{
				$('.box-news-navigation a.selected').stop().animate(
					{backgroundPosition: '-194px 0'}, 
					{duration: 150}
				);
				
				$('.box-news-navigation a.selected').removeClass('selected').removeClass('active');
				
				navigation_item.addClass('selected').addClass('active');
				
				BoxNewsNavigationLoadContent(navigation_item.parent().attr('class'));
			}			
		}
		
		function BoxNewsNavigationLoadContent(contentID)
		{
			$('.box-news-content>div').not('.active').not('.'+contentID).css('z-index',1).hide();
			$('.box-news-content .active').css('z-index',2).show().removeClass('active');
			$('.box-news-content .' + contentID).css('z-index',3).addClass('active').hide().fadeIn(300);			
		}
		
		
		
		function boxQuicklinksSwitch(event)
		{
			if(!$(event.target).hasClass('active'))
			{
				$('.box-quicklinks-navigation a.active').removeClass('active');
				
				$('.content-quicklinks').each(function() 
				{
					if($(this).hasClass('active'))
					{
						$(this).removeClass('active').hide();
					}
					
					else
					{
						$(this).addClass('active').show();
					}
				});
				
				$(event.target).addClass('active');
			}
		}
		
		
		window.initBoxNews = true;
		
		function BoxNewsAuto() {
		
			if (window.initBoxNews===true) {
				var index = -1;
				window.initBoxNews = false;
			} else {
				var index = $('.box-news-navigation a.selected').parent().index();
			}
			
			var next = index+1;
			if (next>$('.box-news-navigation li').length) next = 0;
			var next_item = $('.box-news-navigation li:eq('+next+')');
		
			$('.box-news-navigation a.selected').stop().animate(
					{backgroundPosition: '-194px 0'}, 
					{duration: 150}
			);
				
			$('.box-news-navigation a.selected').removeClass('selected').removeClass('active');
			
			next_item.find('a').addClass('selected').addClass('active');
			next_item.find('a').stop().animate(
				{backgroundPosition: '0 0'}, 
				{duration: 150}
			);
				
			BoxNewsNavigationLoadContent(next_item.attr('class'));
			
		}
		
		window.boxNewsInterval = setInterval(BoxNewsAuto,4000);
		
		
	}
);
