function onePageNav(navigation, header_offset) {
	$(navigation +' li, #mobilenavigation li').each(function(){
		$(this).removeClass('active');
	});
	$('li#l1').addClass('active');
	$(navigation +' li a , #mobilenavigation li a').click(function(e){
				if(!$('body.job').length && !$(this).hasClass('no_prevent')) 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);
			});
	console.log('scroll = ' + $('div#l1').offset().top);
	$(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 - 300 && scroll_height < scroll_top + innerHeight - header_offset - 300) {
				current_id = current_id.replace('div','li');
				if(!$(current_id).hasClass('active')) {
					$(current_id).siblings().removeClass('active');
					$(current_id).addClass('active');
				}
				break;
			}
		}
	});
}

$(function(){

	var job_id = String(window.location.href).split('/').pop().toLowerCase();
	console.log(job_id);

	$('.job_documents').each(function(){
		
		if($(this).hasClass(job_id)) $(this).show();
	});
	
});


$(function(){
	$('.module.a').attr('id', 'l1');
	$('.module.b').attr('id', 'l2');
	$('.module.d').attr('id', 'l3');

	onePageNav('ul#headernavigation',100);

	$('.strapline h2 a').click(function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop: $('div#l2').offset().top  }, 1000);//Animate scroll

	});
});

// //WRAP NAVIAGTION SO WE CAN RIGHT ALIGN IT
// $('ul#inner-bottomnavigation').wrapAll('<div id="nav_wrapper"></div>');

// var src = $('#outer-header a.logo img').attr('src');
// //INSERT LOGO
// $('#nav_wrapper').prepend('<img src="' + src + '" />')


$(function(){
// CUSTOM FUNCTIONS
	// JOB CARDS
	html ='';
	toggleBar = ' <div id="toggle-bar"> <h1>Latest jobs</h1> <div class="job-card-toggle" id="table"></div> <div class="job-card-toggle active" id="tile"></div> </div>';
	$( ".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('.jobTable');
	$('.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);
	});

});


// START OF ALL INTERACTIVE FUNCTIONS
$(function(){
	//AUTOMATIC EXPLORE CARD TITLES, IMGS AND LINKS
	//Switch your your navigation
	$('#headernavigation li').not('.active').each(function(index, el) {

		var page_title = $('a', this).text(); 
		var page_link = $('a', this).attr('href'); 
		var slider_img = $('#inner-slideshow img').eq(index).attr('src'); 

		console.log(slider_img);
		console.log('index is = ' + index);

		$('.explore-card').eq(index).find('h1').html(page_title);
		$('.explore-card').eq(index).find('a').attr('href', page_link);
		$('.explore-card').eq(index).find('img').attr('src', slider_img);

		if (index > 1) {
			return false;
		}
	});


	$('#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');
		    }
		});


		// 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');
});




	// DROP DOWN MENU - WE DONT UNCOMMENT UNLESS NECESSARY
	// DROP DOWN MENU - WE DONT UNCOMMENT UNLESS NECESSARY

	// $(function(){
	// 	$("#inner-topnavigation, #inner-navigation, #inner-bottomnavigation").addClass("dropdown");
	//     $("ul.dropdown li").hover(function(){
	//         $(this).addClass("hover");
	//         $('ul:first',this).css('display', 'block');
	//     }, function(){
	//         $(this).removeClass("hover");
	//         $('ul:first',this).css('display', 'none');
	//     	});
	//     $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
	// });

	// 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');
		});
	});



// This is the tracking code. Now in the Global.js
document.write(unescape("%3Cscript type='text/javascript' src='/jobs/styleapps/trackingcode.js' %3E%3C/script%3E"));

function preventDefaultAction(e) {
	var Browser = {
		Version: function() {
			var version = 999; // we assume a sane browser
			if(navigator.appVersion.indexOf("MSIE") != -1) {
				// bah, IE again, lets downgrade version number
				version = parseFloat(navigator.appVersion.split("MSIE")[1]);}
				return version;
			}
		}
	// If IE
	if (Browser.Version() !== 999) {
		event.returnValue = false;
	}
	// Firefox, Safari, Opera
	else {
		e.preventDefault();
		return false;
	}
}
function stringToBool(input) {
	if(input == "true"){ return true;  }
	else if(input == "false"){ return false; }
}

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var firstTracker;
var secondTracker;
setTimeout('startGA();', 1000);
function startGA() {
	if (typeof(_gat) == 'object') {
		firstTracker = _gat._getTracker("UA-7391736-1");
		firstTracker._trackPageview();
		secondTracker = _gat._getTracker("UA-4658834-1"); 
		secondTracker._setDomainName("none"); 
		secondTracker._setAllowLinker(true);
		secondTracker._trackPageview();
	}
}

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");

//ULTIMATE CODE

var found = 0;
var mode;
var mobile;
var header;

(function () {
    var lastPath = location.pathname.split('/').length;

    if (location.pathname.split('/')[lastPath - 1].length > 0) {
        var current = location.pathname.split('/')[lastPath - 1];
        mode = 1;
    } else {
        var current = location.pathname.split('/')[lastPath - 2];
        mode = 2;
    }

    var menuItems = document.querySelectorAll('#headernavigation li');
    var menuItemsAnchor = document.querySelectorAll('#headernavigation li a');
    for (var i = 0; i < menuItems.length; i++) {
        menuItems[i].classList.remove("active");
        if (menuItemsAnchor[i].getAttribute("href").indexOf(current) > 1) {
            found++;
            if (found == 1) {
                mobile = i;
            } else {
                header = i;
            }
        }
    }
    if (found == 1) {
        menuItems[mobile].className = "active";
    }
    if (found == 2) {
        menuItems[mobile].className = "active";
        menuItems[header].className = "active";
    }
    if (found > 2) {
                var checker=menuItems.length/2;
        if (menuItemsAnchor[0].getAttribute("href") == menuItemsAnchor[Math.floor(checker)].getAttribute("href")) {
            menuItems[0].className = "active";
            menuItems[menuItems.length / 2].className = "active";
        } else {
            menuItems[0].className = "active";
        }
    }
})();

