var black = (function () {

function balance_tops (nodes) {
    var local_max = 0;
    nodes.each(function () {
        local_max = Math.max($(this).offset().top, local_max);
    });
    nodes.each(function () {
        var me = $(this);
        var needs = local_max - me.offset().top;
        me.css('margin-top', needs + 20);
    });
};

(function($) {
    var offsetMethod = $.fn.offset;
    $.fn.offset = function () {
        var offset = offsetMethod.call(this),
            bottom = offset.top + this.outerHeight(),
            right = offset.left + this.outerWidth(),
            a = arguments;
        return (a.length) ? this.animate({
                                         top  : a[0].top  || a[0],
                                         left : a[0].left || a[1]
                                         }, 
							             (a[0].top ? a[1] : a[2]) || 1) : $.extend(offset, {
                                             bottom: bottom,
                                             right: right
                                             });
    };
})(jQuery);



$(function () {

    $('#nav a').add('a.scroll').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;
		}
	});
	
	$('#menuTop a').add('a.scroll').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;
		}
	});
	
	$('#topPage a').add('a.scroll').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;
		}
	});
	
	 // THIS IS REALLY FREAKING BIZARRE 
    // I SHOULD NOT HAVE TO DO THIS BUT IE (6 & 7) ARE PRETTY MESSED UP.
    $('#news').css({'position': 'absolute'});
	var nav = $('#nav');
	nav.css({'position': 'absolute'});
	
	var elem = $('#nav');

  

	
	var topmost_point = nav.offset().top;
    var left_point = $('#container').offset().left;
	var PADDING_TOP = 5; 
    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() > REAL_TOP) && ((the_window.scrollTop()-600) < 3200)) {
            if ($.browser.msie && $.browser.version == "6.0") {
                nav.css('top', the_window.scrollTop() + PADDING_TOP);
            } else if (!NAV_IS_FIXED) {
        	    nav.css({
                    left: left_point,
        	        top: PADDING_TOP,
        	        position: 'fixed'
        	    });
        	    NAV_IS_FIXED = true;
        	}
        } else {
            if (NAV_IS_FIXED) {
                nav.css({
                    position: 'absolute',
                    top: topmost_point,
                    left: ''
                });
                NAV_IS_FIXED = false;
            }
        }
	});
	
 
	var topP = $('#topPage');
	topP.css({'position': 'absolute'});
	
	var topmost_pointr = topP.offset().top;
    var left_pointr = $('#container').offset().right-1000;
	var PADDING_TOPR = 35; 
    var REAL_TOPR = topmost_pointr - PADDING_TOPR;
	var the_windowr = $(window);
	var NAV_IS_FIXEDR = (topP.css('position') == 'fixed');
	
	the_windowr.scroll(function () {
        if ((the_windowr.scrollTop() > REAL_TOPR) && ((the_window.scrollTop()-600) < 2500)) {
            if ($.browser.msie && $.browser.version == "6.0") {
                topP.css('top', the_windowr.scrollTop() + PADDING_TOPR);
            } else if (!NAV_IS_FIXEDR) {
        	    topP.css({
                    right: left_pointr,
					top: 'inherit',
        	        bottom: PADDING_TOPR,
        	        position: 'fixed'
        	    });
        	    NAV_IS_FIXEDR = true;
				
        	}
        } else {
            if (NAV_IS_FIXEDR) {
                topP.css({
                    position: 'absolute',
                    top: topmost_pointr,
					bottom: 'inherit',
                    right: '-80px'
                });
                NAV_IS_FIXEDR = false;
            }
        }
	});
 
});
 
})();

