$(document).ready(function()
{

	// put the search in the middle on load
	var windowHeight = $(window).height();
	var positionTop = ((windowHeight - 138)/2);

	// enable smooth scrolling effect when a question is selected
	$.localScroll();

	// save selectors as variables to increase performance
	var $navItem = $(".nav li a");
	var $window = $(window);
	var $first = $('#first');
	var $second = $('#second');
	var $third = $('#third');
	var $fourth = $('#fourth');
	var $fifth = $('#fifth');
	var $sixth = $('#sixth');
	var $seventh = $('#seventh');
	var $eighth = $('#eighth');
	var $ninth = $('#ninth');
	var $tenth = $('#tenth');
	
	function adjustContentPosition() {
		//if(windowHeight < 800) $("ul.nav, .content").css("top", "50px");
		//else $("ul.nav, .content").css("top", "200px");
	}
	
	adjustContentPosition();


	$navItem.bind({
		/*"mouseenter": function() {
			$(this).animate({ backgroundColor: "#000000", color: "#ffffff" }, 300);
		},
		"mouseleave": function() {
			if(!$(this).hasClass("active")) {
				$(this).animate({ backgroundColor: "#ffffff", color: "#000000" }, 500);
		  	}
		},*/
		
		"click":function() {
			$navItem.removeAttr("style");
			$navItem.removeClass("active");
			$(this).addClass("active");
		}
	});

	// bind class "inview" to the visible section in the viewport
	$('.section').bind('inview', function (event, visible)
	{
		if (visible == true) { $(this).addClass('inview'); }
		else { $(this).removeClass('inview'); }
	});

	// function that is called for every pixel the user scrolls. Determines the position of the background
	// arguments: horizontal position of bg, viewport height, scrollbar position, background offset from top, scroll speed multiplier for bg, prevent positive value
	function newPos(x, windowHeight, pos, adjuster, multiplier, reset)
	{
		var result = -((windowHeight + pos) - adjuster);
		if(reset && result > 0) result = 0;
		return x + "px " + (result * multiplier)  + "px";
		
	}

	// function to be called whenever the window is scrolled or resized

	function Move()
	{

		//STOP VIDEO
		/**$f("*").each(function() {
			this.stop();
		});*/
		
		stopVideo();


		// get the position of the scrollbar
		var pos = $window.scrollTop();	
		
		
		if(!jQuery.browser.msie && jQuery.browser.version < 9){
			$('.inview').each(function()
			{
				if($(this).attr('id') != 'first' && $(this).attr('id') != 'tenth')
				{
					$(this).css({'backgroundPosition': newPos(50, windowHeight, pos, 2000, 0.6, false)});
					$(this).find('.answer').show();
	
					var blockPosTop = $(this).find('.content').offset().top;
					var blockHeight = $(this).find('.content').height();
	
					if(pos - blockPosTop + windowHeight > 0 && pos - blockPosTop + (windowHeight / 2) - (blockHeight / 2) < 0)
					{
						$(this).find('.content').css('opacity', (pos - blockPosTop + windowHeight) / ((windowHeight + blockHeight) / 2));
					}
				
					if(pos - blockPosTop + (windowHeight / 2) - (blockHeight / 2) > 0 && pos - blockPosTop - blockHeight < 0)
					{
						$(this).find('.content').css('opacity', -1 * (pos - blockPosTop - blockHeight) / ((windowHeight + blockHeight) / 2));
					}
				}
			});

		}

		// only move the background of the section which has the inview class
		if($first.hasClass('inview'))
		{
			$first.css({'backgroundPosition': newPos(230, windowHeight, pos, 1200, 0.6, true)});
		}

		if($second.hasClass('inview'))
		{
			$second.css({'backgroundPosition': newPos(230, windowHeight, pos, 2050, 0.6, false)});
		}

		if($third.hasClass('inview'))
		{
			$third.css({'backgroundPosition': newPos(230, windowHeight, pos, 3200, 0.6, false)});
		}

		if($fourth.hasClass('inview'))
		{
			$fourth.css({'backgroundPosition': newPos(230, windowHeight, pos, 4400, 0.6, false)});
		}

		if($fifth.hasClass('inview'))
		{
			$fifth.css({'backgroundPosition': newPos(230, windowHeight, pos, 5600, 0.6, false)});
		}

		if($sixth.hasClass('inview'))
		{
			$sixth.css({'backgroundPosition': newPos(230, windowHeight, pos, 6800, 0.6, false)});
		}

		if($seventh.hasClass('inview'))
		{
			$seventh.css({'backgroundPosition': newPos(230, windowHeight, pos, 8000, 0.6, false)});
		}

		if($eighth.hasClass('inview'))
		{
			$eighth.css({'backgroundPosition': newPos(230, windowHeight, pos, 9200, 0.6, false)});
		}

		if($ninth.hasClass('inview'))
		{
			$ninth.css({'backgroundPosition': newPos(230, windowHeight, pos, 10400, 0.6, false)});
		}

		if($tenth.hasClass('inview'))
		{
			$tenth.css({'backgroundPosition': newPos(230, windowHeight, pos, 11400, 0.6, true)});
		}
	}

	// when user resizes the window
	$window.resize(function()
	{
		// recalculate the height of the window
		windowHeight = $(window).height();
		adjustContentPosition();
		// move the background
		Move();
	});

	// when user scrolls
	$window.bind('scroll', function()
	{
		// move the background
		Move();
	});
});

