(function ($){
	$(document).ready(function(){
		
		$('div[id^="linkSection_"]').each(function(item, elem) {
		
			var formerClass = $(elem).attr('class');
			
			$(elem).mouseenter(function(){
				$(this).attr('class', 'stdContBlock MW_mouseover');
			});
			
			$(elem).mouseleave(function(){
				if($(this).hasClass('MW_mouseover')){				
					$(this).attr('class', formerClass);
				}
			});
		
			$(elem).click(function(){
				$('div[id^=linkSection_]').each(function(index, elem){
					$(elem).attr('class', 'stdContBlock MW_default');
				});
				$(this).attr('class', 'stdContBlock MW_active');
				var attribute = $(this).attr('id');
				var index = attribute.indexOf("_");
				var numberOfSection = attribute.substr(index+1, 1);
				var section = "section_" + numberOfSection;
				goToByScroll(section);
			});
		});

		$(window).scroll(function(){

			var section_top, section_next, contentBottom_top = 0;
			var sections = $('div[id^="section_"]');
			var bodyScroll = $('html').scrollTop() + 20;

			for(var i = 0; i < sections.length; i++)
			{
				section_top = $(sections[i]).offset().top;
				contentBottom_top = $("#contentBottom").offset().top;
				
				if(i < sections.length - 1){
					
					section_next = $(sections[i+1]).offset().top;
				}

				if(i < sections.length - 1)
				{
					if(i == 0 && bodyScroll < section_top) changeNavi(0, 0);
					if(bodyScroll >= section_top && bodyScroll <= section_next) changeNavi(1, i);				
				
				}else if(i == sections.length - 1){
					
					if(bodyScroll >= section_top && bodyScroll <= contentBottom_top) changeNavi(1, i);
					if(bodyScroll >= contentBottom_top) changeNavi(0, 0);	
					
					
				}else{
					if(bodyScroll < section_top) changeNavi(0, 0);
				}
			}
		});
		
		function changeNavi(a, b) {

			var linkSections = $('div[id^="linkSection_"]');
			if(a == 0){
				for(var i = 0; i < linkSections.length; i++){
					$(linkSections[i]).attr('class', 'stdContBlock MW_default');
				}
			}
			if(a == 1){
				for(var i= 0; i < linkSections.length; i++){
					if(i == b){
						$(linkSections[i]).attr('class', 'stdContBlock MW_active');
					}else{
						$(linkSections[i]).attr('class', 'stdContBlock MW_default');
					}
				}
			}
		}
		
		function goToByScroll(id) {
			var aOffsetTop = $("#" + id).offset().top;
			var aScrollTop = aOffsetTop - 20

			$('html,body').animate({
				scrollTop : aScrollTop   
			}, 'slow');

		}

		$('#pageBgHome').parallax({
			"coeff" : 0.95
		});

		$(window).bind('scroll', function() {
			windowTop = $(window).scrollTop();
			if (windowTop < 230) {
				$("#headerStdContent").removeClass('fade');
			} else {
				// add background fade to header menu
				if (windowTop > 230) {
					$("#headerStdContent").addClass('fade');
				}
			}
		});

	});
})(jQuery);

