var docHeight = $(document).height();

$(function () {
	if( !jQuery.browser.msie || jQuery.browser.version.substr(0,1) >= 7 ) {
	    // Do glow on navigation menu
		var section = $('body').attr('id');
		if( section != '' ) {
			$('body#'+section+' #nav .'+section).append('<img src="/img/header/glow.png" id="glow">');
		}
 	} else {
 		document.execCommand("BackgroundImageCache", false, true);
		$(window).scroll(function () {
			var ieBody = (document.compatMode && document.compatMode=='CSS1Compat') ? document.documentElement : document.body;

			if( ieBody.scrollTop+ieBody.clientHeight > docHeight ) {
				var p = (ieBody.scrollTop+ieBody.clientHeight) - docHeight;
				$('#bg').css('top', p+'px');
			}
		});
	}
 	
    $(window).resize(resize);
    resize();
    
	$('img, input[type=image], .pngfix').ifixpng();
	
    $('#social li a').cmtip();
    $('a.newwindow').click(function() {
		window.open(this.href);
		return false;
	});
});

function positionBackground() {
	var $win = $(window);
	var $bgimg = $('#bg img');
	
	var winRatio = $win.width() / $win.height();
	if( winRatio < 1.333 ) {
		$bgimg.css('width', 'auto').css('height', '100%');
		var offset = $bgimg.width() - $win.width();
		$bgimg.css('top', 0).css('left', offset/2);
	} else {
		$bgimg.css('width', '100%').css('height', 'auto');
		var offset = $bgimg.height() - $win.height();
		$bgimg.css('left', 0).css('top', offset/2);
	}
}

function resize() {
	docHeight = $(document).height();
	var $content = $("#content");
	
	$content.css('overflow', 'hidden')
		.css('margin-top', 0)
		.css('padding-bottom', 0);
			
	var height = $(window).height(),
		chromeHeight = $("#header").height() + $("#footer").height() + $("#crosslink").height()+1
		+ parseInt($content.css('margin-bottom'));
	if( height > $content.height() ) {
		var diff = (height - chromeHeight- $content.height())/2;
		if( diff > 0 ) {
			if( $("#header").height() ) {
				$content.css('margin-top', diff);
			} else {
				$content.css('padding-top', diff);
			}
			$content.css('padding-bottom', diff);
		}
	}
	
	positionBackground();
}