$(document).ready(function(){
	$('#mainimg').width($(window).width());
	$(window).resize(function(){
		$('#mainimg').width($(window).width());	
	});
	$('nav a').click(function(){
		if ($(this).parent().hasClass('selected')) {}
		else {
			$('.selected').removeClass('selected');	
			if (!$(this).parent().hasClass('logo')) { $(this).parent().addClass('selected'); }		
		}
	});
	if (window.location.hash && window.location.hash != '#neverthesame') {
		$('nav a[href='+window.location.hash+']').parent().addClass('selected');
	}
	var scrolling = 'on';
	var what = $('#what').offset().top;
	var why = $('#why').offset().top;
	var cyc = $('#claimyourcampus').offset().top;
	var nts = $('#ntscamps').offset().top;
	var help = $('#help').offset().top;
	var follow = $('#follow').offset().top;
	var point = 'top';
	$(window).scroll(function(){
		if (scrolling === 'on') {
			if ($(this).scrollTop() < what && point != 'top') {
				point = 'top';
				$('.selected').removeClass('selected');	
			}
			if ($(this).scrollTop() > what && $(this).scrollTop() < why && point != 'what') {
				point = 'what';
				$('.selected').removeClass('selected');	
				$('nav a[href=#what]').parent().addClass('selected');
			}
			if ($(this).scrollTop() > why && $(this).scrollTop() < cyc && point != 'why') {
				point = 'why';
				$('.selected').removeClass('selected');	
				$('nav a[href=#why]').parent().addClass('selected');
			}
			if ($(this).scrollTop() > cyc && $(this).scrollTop() < nts && point != 'cyc') {
				point = 'cyc';
				$('.selected').removeClass('selected');	
				$('nav a[href=#claimyourcampus]').parent().addClass('selected');
			}
			if ($(this).scrollTop() > nts && $(this).scrollTop() < help && point != 'nts') {
				point = 'nts';
				$('.selected').removeClass('selected');	
				$('nav a[href=#ntscamps]').parent().addClass('selected');
			}
			if ($(this).scrollTop() > help && $(this).scrollTop() < follow && point != 'help') {
				point = 'help';
				$('.selected').removeClass('selected');	
				$('nav a[href=#help]').parent().addClass('selected');
			}
			if ($(this).scrollTop() > follow && point != 'follow') {
				point = 'follow';
				$('.selected').removeClass('selected');	
				$('nav a[href=#follow]').parent().addClass('selected');
			}
		}
	});
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
		  scrolling = 'off';
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
			window.setTimeout(function() {  
    			scrolling = 'on';
			}, 300); 
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }
});
