$(document).ready(function(){
	
	//TAB-CENTERING FUNCTION
	function centerTab(){
	
		frameWidth = $(window).width() - 260;
		remainingWidth = frameWidth - 985;
		
		frameHeight = $(window).height() - (($('#header').height() - 33) + ($('#footer').height() - 7));
		remainingHeight = frameHeight - $('#tab').height();
		marginHeight = (remainingHeight/2) - 46.5;
		
		if (remainingWidth>0)
			$('.tabContent').css('padding-right',remainingWidth/2 + 'px');
		
		if (marginHeight>0){
			$('#tab').css('margin-top', marginHeight + 'px');
		}else{
			$('#tab').css('margin-top', '0px');
		}
		
	};
	
	//BACKGROUND-RESIZING FUNCTION
	function resizeBackground(){
		windowWidth = $(window).width();
		windowHeight = $(window).height();
		imageWidth = $('#pageBackground img').width();
		imageHeight = $('#pageBackground img').height();
		imageRatio = 0.5652;
		//alert(windowHeight + ' < ' + imageHeight);
		if (imageHeight > (windowWidth * imageRatio)){
			if ((windowHeight / imageRatio) < windowWidth)
				$('#pageBackground img').removeAttr('style').width(windowWidth);
			else
				$('#pageBackground img').removeAttr('style').height(windowHeight);
		}else{
				$('#pageBackground img').removeAttr('style').width(windowWidth);
		}
		/*if (windowHeight < imageHeight) {
			if (windowHeight > windowWidth)
				$('#pageBackground img').removeAttr('style').height(windowHeight);
			else
				$('#pageBackground img').removeAttr('style').width(windowWidth);
		}
		else {
			if (imageWidth < windowWidth)
				$('#pageBackground img').removeAttr('style').width(windowWidth);
			else
				$('#pageBackground img').removeAttr('style').height(windowHeight);
		}*/
	}
	
	function careersHeight() {
		if ($('#sideInfo').length){
			sideInfoHeight = $('#sideInfo').height();
			$('#mainContent').css('min-height', sideInfoHeight - 25 + 'px');
		}
	}
	
	//FIRST LETTER CAPITALISATION FUNCTION
	function capitaliseFirstLetter(string)
	{
    	return string.charAt(0).toUpperCase() + string.slice(1);
	}
	
	//FILENAME GETTING FUNCTION - GET PAGE ID
	function xtractFile_sans(data){
        var m = data.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
        return m[2];
    }
	pageId = xtractFile_sans($('#pageBackground img').attr('src'));
	
	//HIGHLIGHT APPROPRIATE CAREERS MENU ITEM (JOB)
	//curJob = $('#mainContent h2').attr('name');
	//$('#currentOpenings li a[name="' + curJob + '"]').parent().addClass('selected');
	
	//CHECKING FOR CURRENT PAGE AND SETTING SELECTED CLASS IN NAV
	//curPage = capitaliseFirstLetter($('body').attr('class'));
	//if (curPage)
	//	$('#nav li:contains("' + curPage + '")').addClass('selected');
	
	//CHECKING FOR SIDE CONTENT IN TAB AND ADJUSTING MAINCONTENT'S MARGIN ACCORDINGLY
	/*if ($('#tab #sideInfo').length)
		$('#tab #mainContent').css('margin-left', $('#tab #sideInfo').width() + 'px');*/		
	
	//DETECTING PAGE AND SIZE, THEN CHANGING BACKGROUND APPROPRIATELY
	if (($(window).height() < 720)&&($(window).width() < 1280))
		resolution = '720';
	else
		resolution = '1080';
			
	bgImgPath = $('#pageBackground img').attr('src');
	$('#pageBackground img').attr('src', bgImgPath.replace('_720', '_' + resolution));
	
	//INITIALLY CENTER THE TAB AND RESIZE THE BACKGROUND
	centerTab();
	resizeBackground();
	careersHeight();
	
	//CENTER THE TAB EVERY TIME THE WINDOW IS RESIZED
	$(window).resize(function(){
		centerTab();
		resizeBackground();
		if (resolution == '720'){
			if (($(window).height() > 720) || ($(window).width() > 1280))
				$('#pageBackground img').attr('src', bgImgPath.replace('_720', '_1080'));
		}
	});
	
	$('.newsTab:first').css('margin-top', '0px');
	
	coverLetterEntry = $('form #form_coverLetterEntry');
	if (coverLetterEntry.length){
		coverLetterEntry.focus(function(){
			coverLetterEntry.animate({ height: '300px' }, 'slow');
		});
		coverLetterEntry.blur(function(){
			coverLetterEntry.animate({ height: '88px' }, 'slow');
		});
	}
	
});
