(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	
	var ph = $(this).parent().height(); //Grab the height of the parent	

	//Remove the height of the box and navigation
	var ph = ph - $(this).height();
	
	var ph = ph - $('#navigation').height();
	
	var mh = ph / 2;
	
	$(this).css('margin-top', mh);
	});
};
})(jQuery);



$(document).ready(function() {
	// Preload navigation images
	jQuery.preLoadImages("/static/media/img/contact_down.png", "/static/media/img/language_down.png", "/static/media/img/nav_hover.png", "/static/media/img/subnav_bg.png", "/static/media/img/subnav_top.png", "/static/media/img/subnav_bottom.png", "/static/media/img/language_bg.png", "/static/media/img/language_top.png", "/static/media/img/language_bottom.png");
	
	$("#navigation li").hoverIntent(
	    function () {
	    	// Show the subnav div
	        $(this).find(".subnav").slideDown('fast').show();
	    }, 
	    function () {
	    	// Hide the subnav div
		    $(this).find(".subnav").slideUp('fast').hide();
	    }
	);
	
	// Change contact arrow on hover
	$("#header_contact a").hover(
	    function () {
	        $(this).find('.arrow').attr('src','/static/media/img/contact_down.png');
	    }, 
	    function () {
		    $(this).find('.arrow').attr('src','/static/media/img/contact_arrow.png');
	    }
	);
	// Change language arrow on hover
	$("#language a").hover(
	    function () {
	        $(this).find('.arrow').attr('src','/static/media/img/language_down.png');
	    }, 
	    function () {
		    $(this).find('.arrow').attr('src','/static/media/img/language_arrow.png');
	    }
	);
	
	
	$(".subnav").hover(function(){
		//Over.
		$(this).parent().css('background', 'top left repeat-x url(/static/media/img/nav_hover.png)');
	    
	}, function(){		
		$(this).parent().css('background', '');
	});
	
	// CONTACT DETAILS
	
	$("#header_contact li").hoverIntent(
	    function () {
	    	// Show the details div
	        $(this).find(".details").slideDown('fast').show();
	    }, 
	    function () {
	    	// Hide the details div
		    $(this).find(".details").slideUp('fast').hide();
	    }
	);
	
	// TABLE ROW HOVER
	
	$("#content .vacancies_table tr").hover(
		function () {
	    	// Show the details div
	        $(this).find("td").css({color: "#FFF", backgroundColor: "#000"});
	        $(this).find("td a").css({color: "#FFF"});
	    }, 
	    function () {
	    	// Hide the details div
		    $(this).find("td").css({color: "#000", backgroundColor: "#EEE"});
		    $(this).find("td a").css({color: "#2062AC"});
	    }
	);
	
	
	
		//Vertically align div
	if($('#testimonial').length) {
    	$('#testimonial').vAlign();
	}
	
	//If there are more than 2 items in the right content.
	if($('#content_right > div').length > 1) {
	
		//Check to see if the right-side content is smaller - if so we need to do something.
		if($('#content_right').height() < $('#content_left').height()) {
		
			//Get the height of the content block.
			var height = $('#content_left').height();
		
			//Get the height of all the right contents children (the boxes siblings)
			$('#content_right').children().each(function() {
				height = height - $(this).height();
			});
			//Now we have the remaining height
			//We want to break that up.
			//outside = height*0.2;//20%
			//inside = height*0.3;

			//Add this to the bottom margin of the first child.
			//$('#content_right').children(':first').css('margin-top', outside)
			//$('#content_right').children(':first').css('margin-bottom', inside)
			//$('#content_right').children(':last ').css('margin-top', outside)
			//$('#content_right').children(':first').css('margin-bottom', inside)
		
		}
	}
	
	
	//If we want to split lis
	//Grab the total number of li's
	var colsize = Math.round($('.splitcol').find('li').size() / 2);

	$('.splitcol').find('li').each(function(i) {
	   if (i>colsize) {
		    $(this).addClass('right_col');
	   }
	});

	$('.splitcol > .right_col').insertAfter('.splitcol').wrapAll("<ul class='splitcol'></ul>").removeClass("right_col");
});

