function onePageNav(navigation, header_offset, flex) {

	//ASSIGN IDS TO CONTENT
	var content_areas = ['.module.a', '.module.b', '.module.c','.module.d', '.module.e', '.module.f', '.module.g'];
	$(navigation + ' li').each(function(){
		for(var i = 0; i < content_areas.length; i++) {
			var area = $(content_areas[i]);
			if (content_areas[i] == '#content' && area.is(':visible') && !area.find($(this).attr('id')).length) {
				$(area).wrapAll('<div id=' + $(this).attr('id') + '></div>');
				break;
			}
			else if(area.is(':visible') && !area.attr('id')) {
				area.attr('id', $(this).attr('id'));
				break;
			}
		}
	});

	$(navigation +' li, #mobilenavigation li').each(function(){
		$(this).attr('href', $(navigation + ' li#l1').attr('href'));
		$(this).removeClass('active');
	});
	$(navigation +' li a , #mobilenavigation li a').click(function(e){
	if(!$('body#job').length) e.preventDefault();//Disable href unless on job page

	$('body').removeClass('nav-open');// Close mobile menu if open

	/* ANIMATE SCROLL ON CLICK */
	var id = 'div#' + $(this).parent().attr('id');// Get id


	$('html, body').animate({scrollTop: $(id).offset().top - header_offset }, 1000);//Animate scroll
});

	var ids = [];
	/* DETECT SCROLL */
	$(navigation +' li a').each(function(){
	var id = 'div#' + $(this).parent().attr('id');// Get id
	ids.push(id);
});
	$(window).scroll(function(){
		for(var i = ids.length -1; i >= 0; i--) {
			current_id = ids[i];

			var scroll_height = $(window).scrollTop();
			var inner_height =  $(current_id).innerHeight();
			var scroll_top = $(current_id).offset().top;

			if(scroll_height > scroll_top - header_offset - flex && scroll_height < scroll_top + innerHeight - header_offset - flex && scroll_height > $('#outer-slideshow').innerHeight() - flex) {
				current_id = current_id.replace('div','li');
				if(!$(current_id).hasClass('active')) {
					$(current_id).siblings().removeClass('active');
					$(current_id).addClass('active');
				}
				break;
			}
			else if (scroll_height < $('#outer-slideshow').innerHeight() - flex) {
				$(navigation +' li, #mobilenavigation li').each(function(){

		$(this).removeClass('active');
	});
			}
		}
	});
}

function jobCards(){
	// CUSTOM FUNCTIONS
	// JOB CARDS
	$('.module.d').wrapInner('<div class="sg-row"><div class="c12"></div></div>');
	html ='';
	toggleBar = ' ';
	$( ".module .jobTable .jobTitle a" ).each(function( index ) {
		
		var jobTitle = $( this ).html();
		var jobLink = $( this ).attr('href');
		var jobLocation = $( this ).parent().siblings('.jobLocation').html();

		html += '<a href="' + jobLink + '">';
		html += '<div class="job-card">';
		html += '<h1>' + jobTitle + '</h1>';
		html += '<h2>' + jobLocation + '</h2>';
		html += '<div class="applylinks">View</div>';
		html += '</div>';
		html += '</a>';
	});


	$(toggleBar + "<div id='job-cards'> " + html+"<div class='clear' /></div>").insertAfter('.module .jobTable');
	$('.module .jobTable').hide();

	$('body').on('click', '.job-card-toggle:not(.active)', function(event) {
		$(this).addClass('active').siblings('.job-card-toggle').removeClass('active');
		var  jobCardHeight = $('#job-cards').height();
		$('#job-cards').height(jobCardHeight);
		$('.job-card').fadeOut(300).delay(300).fadeIn(300);
		setTimeout(function() {
			$('.job-card').toggleClass('alt');
		}, 300);
	});

}

function singleJob(){
	$(' .jobTable').each(function(){
		var job_link = $(this).find('.jobListItem').first().find('.jobTitle a').attr('href');
		var job_table = $(this);
		$.ajax({
			url :job_link,
			type: "GET",
			job_table: job_table,
			success: function(data){
				$($(data).find('#text #text').html()).insertAfter(job_table);
				job_table.hide();
				$('a.Viewall').hide();
			},
			error : function(e) {
				job_table.hide();
				$('a.Viewall').hide();
			}
		});
	}); 
}

function backToTop(){
	$('#outer-main').append('<img class="back-to-top" src="https://microcontrib.hays.com/documents/5705082/5706116/back-to-top.png" alt="Back to top icon" />');
	$('body').on('click', '.back-to-top', function(){
		$('html, body').animate({
			scrollTop: 0
		}, 2000);
	});

	if($(window).scrollTop() > 300) $('.back-to-top').fadeIn();
	else $('.back-to-top').fadeOut();

	$(window).scroll(function(){
		if($(window).scrollTop() > 300) $('.back-to-top').fadeIn();
		else $('.back-to-top').fadeOut();
	});

}

//Activating plugins code
$(function(){
	jobCards();



	onePageNav('ul#headernavigation', 70, 300);
	backToTop();

	$('.strapline a').click(function(e){
		e.preventDefault();
		$('body,html').animate(
			{scrollTop: $('.module.d').offset().top} , 1000
			);
	});
});

//Video slide code	
$(function(){
	$('.right').click(function(){
		$('.video').each(function(){
			var id = $(this).attr('id');
			switch(id) {
				case 'v1':
				$(this).attr('id', 'v2');
				break;
				case 'v2':
				$(this).attr('id', 'v3');
				break;
				case 'v3':
				$(this).attr('id', 'v1');
				break;
			}
		});

		$('.video-slider h3 span').text($('#v2').attr('data-name'));
		$('.video-slider h3 a').attr('href',$('#v2').attr('data-link'));
	});
	$('.left').click(function(){
		$('.video').each(function(){
			var id = $(this).attr('id');
			switch(id) {
				case 'v1':
				$(this).attr('id', 'v3');
				break;
				case 'v2':
				$(this).attr('id', 'v1');
				break;
				case 'v3':
				$(this).attr('id', 'v2');
				break;
			}
		});
		$('.video-slider h3 span').text($('#v2').attr('data-name'));
		$('.video-slider h3 a').attr('href',$('#v2').attr('data-link'));
	});
});

//NAV SCROLL FUNCTION
$(function(){
	var header = $('#outer-header').offset().top;
	if ($(this).scrollTop() > header){ 
		$('#outer-header').addClass('fixed'); 

	}
	else{
		$('#outer-header').removeClass('fixed');
		
	}

	$(window).scroll(function(){    
		if ($(this).scrollTop() > header){ 
			$('#outer-header').addClass('fixed'); 

		}
		else{
			$('#outer-header').removeClass('fixed');

		}
	});
});
// START OF ALL INTERACTIVE FUNCTIONS
$(function(){



	$('#inner-slideshow > p').children().prependTo('#inner-slideshow');
	$('#inner-slideshow > p').remove();
	$('#outer-slideshow').flexslider({
		selector: "#inner-slideshow > img",
		initDelay: 0,
		animationSpeed: 1100, 
		slideshowSpeed: 4500,
		animation: "fade",
		start: function(slider){
			// $('#inner-slideshow').removeClass('loading');
		}
	});

// Wrap content of modules

// Get height dynamically from the slideshow
function wrapElements(){
	$('.strapline').wrap( "<div class='straplineWrapper'></div>" );
}

function setInitialHeight(){
	var slideShowHeight = $('#outer-slideshow img').height();
	$( ".straplineWrapper" ).css({
		height: slideShowHeight
	})
}

function updateContentHeight(){
	$( window ).resize(function() {
		var slideShowHeight = $('#outer-slideshow img').height();
		$( ".straplineWrapper" ).css({
			height: slideShowHeight
		})
	});
}

wrapElements();
setInitialHeight();
updateContentHeight();



$(function(){
	//MOBILE NAV
	var mobile_nav = function(menu_container,mobile_nav) {

		//Insert neccesary html
		$(menu_container).append('<div id="rect1" /><div id="rect2" /><div id="rect3" />');
		$('<div id="mobile_back"></div>').insertBefore($(menu_container).parent());

		//Assign vatiables
		var animating = false,
		toCloseIcon = true,
		list_item = mobile_nav + ' li';
		back = '#mobile_back';

		//Click event
		$(menu_container + ', ' + back + ',' + mobile_nav).click(function(){
			console.log(animating);
			if (!$(mobile_nav).hasClass('nav-open') && !animating) {
				animating = true;
				$(menu_container).addClass('animate');
				var t_delay = 300;
				$(back).fadeIn();
				$(list_item).each(function(){
					$(this).delay(t_delay).fadeIn(500);
					t_delay += 50;
				});
				$('#topbar h5').fadeIn();
				$(list_item + ', ' + menu_container).promise().done(function(){  
					animating = false;
				});
				$(mobile_nav).addClass('nav-open');
			} else if(!animating) {
				animating = true;
				$(menu_container).removeClass('animate');
				$(list_item).fadeOut();
				$(back).fadeOut();
				$(list_item +', ' + back + ', ' + menu_container).promise().done(function(){
					animating = false;
				});
				$(mobile_nav).removeClass('nav-open');
				$('#topbar h5').fadeOut();
			}
		});

	}
	mobile_nav('.Menu', 'ul#mobilenavigation');
});




// TAB SYSTEM
var tabNav = $('ul#tabnav'),
tabNavButtons = $('ul#tabnav li'),
tabContent = $('div#tabs > div');

$(tabNavButtons[0]).attr('id','active');
$(tabContent[0]).show();

tabNav.on('click','li',function(e){
	e.preventDefault();
	curTab = $(this),
	target = curTab.children().attr('href');

	curTab.stop().attr('id','active')
	.siblings('li')
	.removeAttr('id');

	$('div#'+target).stop().show('slow')
	.siblings('div')
	.stop()
	.hide('slow');
});

// PROFILE BOX GRID
$('.boxgrid.captionfull').hover(function(){
	$(".cover", this).stop().animate({
		bottom:'0px'
	},{queue:false,duration:160});

}, function() {
	$(".cover", this).stop().animate({
		bottom:'-100%'
	},{queue:false,duration:160});
});      

// FAQS ACCORDIAN
var expander = $('.expand'),
collapser = $('.collapse');

expander.click(function(){
	$(this).next('.collapse').slideToggle(250)
	.siblings('.collapse')
	.slideUp(250);

	$(this).toggleClass('open')
	.siblings(expander)
	.removeClass('open');
});    
});


document.getElementById("outer-footer").parentElement.style.display = "flex";

if (window.location.href.includes("job-details")){
    document.getElementById("Form1").parentElement.id = "inner-main";
}

$(".moveIt").appendTo("#jobsGoesHere");
