$(document).ready(function(){
	
	//CENTER NAV BOX IN MIDDLE OF SCREEN
	function centerNav(){
		var leftSpace = ($(window).width() - $('#nav').width()) / 2;
		$('#nav').css('left', leftSpace - 1 + 'px');
	};
	
	//SCROLLING FUNCTION
	function pageScroll(destination){
		if (destination == '#')
			$('html, body').animate({ scrollTop: 0 }, 'slow');
		else
			$('html, body').animate({ scrollTop: $('.page.' + destination.replace('#', '')).offset().top - 75 }, 'slow');
	};
	
	//DETERMINE NECESSARY SIZE OF ORANGE BAR AT BOTTOM
	function scaleOrange() {
		if ($('.page.contact').length > 0) {
			if ($(window).height() > $('.page.contact').height() + 45 + 56 + 75) {
			orangeHeight = $(window).height() - (75 + 45 + $('.page.contact').height());
				$('.orangeBar#bottom').height(orangeHeight);
			}else{
				$('.orangeBar#bottom').height(56);
			}
		}
	};
	
	function scaleBackground(){
		if ($(window).width() > 1656){
			$('#backgroundImage img').width($(window).width() + 'px')
				.css({
					'margin-left' : '-' + $(window).width()/2 + 'px',
					'left' : '50%'
				});
		}else{
			$('#backgroundImage img').width('1656px')
				.css({
					'margin-left' : '0px',
					'left' : '-348px'
				});
		}
	}
	
	function navFade() {
		if ($('.page.contact').length > 0) {
			navOffset = $('#nav').offset().top;
			pagesArray = new Array("services","faq","contact");
			pageOffset = new Array();
			offsetDiff = new Array();
			navOpacity = new Array();
			for (var i in pagesArray){
				curPage = pagesArray[i];
				pageOffset[curPage] = $('a[name="' + curPage + '"]').offset().top;
				offsetDiff[curPage] = (pageOffset[curPage] - navOffset) + 9;
				if (offsetDiff[curPage] < 0)
					offsetDiff[curPage] = offsetDiff[curPage] * -1;
				navOpacity[curPage] = 40 + offsetDiff[curPage]/5;
				if (navOpacity[curPage] >= 100)
					navOpacity[curPage] = 100;
				
				//CHANGING NAV ITEM OPACITY
				$('#nav a[href="#' + curPage + '"]').css('opacity', navOpacity[curPage]/100);
			}
		}
	}
	
	navFade();
	centerNav();
	scaleOrange();
	scaleBackground();
	$(window).resize(function(){
		centerNav();
		scaleOrange();
		scaleBackground();
	});
	
	//BACK TO TOP FADE IN/OUT ON SCROLL
	$(window).scroll(function(){
		if (($(window).scrollTop() < 60)){
			$('#nav').removeClass('scroll');
			$('a#backToTop').fadeOut();
		}else{
			$('#nav').addClass('scroll');
			$('a#backToTop').fadeIn();
			navFade();
		}
		/*if ($('body').scrollTop() >= 60){
			$('#nav').addClass('scroll');
			$('a#backToTop').fadeIn();
		}else{
			$('#nav').removeClass('scroll');
			$('a#backToTop').fadeOut();
		}*/
	});
	
	//BACK TO TOP BUTTON MOUSEOVER EFFECTS
	$('a#backToTop').mouseenter(function(){
		$('a#backToTop').fadeTo('fast',0.5);
	}).mouseleave(function() {
		$('a#backToTop').fadeTo('fast',1);
	});
	
	//NAV LINK EVENT AND HASH CHANGE
	$('#nav a').click(function(e){
		if (location.pathname == '/') {
			e.preventDefault();
			var nextHash = '#' + $(this).attr('href').replace('http://' + location.host + '/#', '');
			location.hash = nextHash.replace('#', '#j');
			pageScroll(nextHash);
		}
	});
	
	//HASH FIX ON PAGELOAD
	$(window).load(function() {
		if(location.hash.charAt(1) == 'j') {
			location.hash = location.hash.replace('#j', '#');
		}
	});
	
	if ($.browser.msie){
		if ($.browser.version == '7.0'){
			$('a#backToTop').css('display', 'inline').attr('id', 'backToTopIE');
		}
	}
	
});
