/*
 * Props to blackestate.co.nz for the scolling functionality
 * and the magic scrolling nav
 */

// Scroll functionality for nav links
$(function () {
	
	$('#nav a, #loginBar a').click(function () {
  	var target = $(this.hash);
    var hash = this.hash;
    if(target.length) {
      var targetOffset = target.offset().top;
      $('html,body').animate({scrollTop: targetOffset}, 600);
      return false;
		}
	});
	
	var nav = $('#nav');
	nav.css({'position': 'absolute'});

	
	/*
	* MAGICAL SCROLLING NAV
	* (not that magical)	
	*
	* This is using lots of vars because it gets called all the time
	* on scroll, so it needs to be fast.
	*/
	var topmost_point = nav.offset().top;
	var left_point = $('div#pageWrap').offset().left;
	var PADDING_TOP = 0; // MAGIC NUMBER
	var REAL_TOP = topmost_point - PADDING_TOP;
	var the_window = $(window);
	var NAV_IS_FIXED = (nav.css('position') == 'fixed');
	
	the_window.scroll(function () {
		
		if(the_window.scrollTop() > 300) {
			if ($.browser.msie && $.browser.version == "6.0") {
				nav.css('top', the_window.scrollTop() + 40);
			} 
			else if(!NAV_IS_FIXED) {
				nav.css({
					'left': left_point + 10,
					'top': 30,
					'position': 'fixed'
				});
				NAV_IS_FIXED = true;
			}
		} 
		else if($.browser.msie && $.browser.version == "6.0") {
			  	nav.css({
						position:'absolute',
						top: 420,
						left: left_point
					});
		}
		else {
			if(NAV_IS_FIXED) {
				nav.css({
				  position: 'absolute',
				  top: 258,
				  left: 10
				});
				NAV_IS_FIXED = false;
			}
		}
		
		// For some reason, if I add this left_point again within the scroll, it works
		left_point = $('div#pageWrap').offset().left;
		
		if(NAV_IS_FIXED) {
			nav.css('left', left_point + 10);
		}
	});
	the_window.resize(function() {
		left_point = $('div#pageWrap').offset().left;
		if(NAV_IS_FIXED) {
			nav.css('left', left_point + 10);
		}	
	});
}); // end jquery
