// JavaScript Document

$(document).ready(function(){
	/* Common functions... */
	clearClick();
	setBrowserClass();
	
	/* Show non-flash content */
	if(!hasFlash){
		$('.noflash').show();
		cssTweaks();
	}else{
		$('.noflash').remove();
		cssTweaks();
	}
	
	/* PNG support for IE6 */
	 if(isIE(6)){
		$(document).pngFix();
	}
});


function cssTweaks(){
	// CSS adjustments
	$('blockquote').each(function(i,elem){ $(elem).find('p:last').css('margin',0); });
	$('#text ol li').wrapInner('<span></span>');
	
	// Adjust sidebar height
	//adjustSidebarHeight();
	
	// Position the body background at the bottom of the screen
	setTimeout('adjustBodyBackground();', 500);
	window.onresize = adjustBodyBackground;
	
	// Center-align navigation dropdowns
	if(!isIE(6) && !isIE(7)){
		$('#nav li').each(function(i,elem){
			$(elem).find('ul').wrap('<div></div>');
		});
		centerNavDropdowns();
	}
}

function adjustSidebarHeight(){
	var sidebarHeight = $('#sidebar_inner').outerHeight() + $('#sidebar_top').outerHeight();
	sidebarHeight -= 80; // subtract negative top margin of #sidebar_inner
	$('#sidebar').css('height', sidebarHeight + 'px');
}

function adjustBodyBackground(){
	var bodyHeight = $('body').outerHeight();
	var screenSize = getScreenSize();
	var size = screenSize.split('x');
	if(bodyHeight > 1150 && size[1] <= bodyHeight){
		$('body').css('background-position','center bottom');
	}else{
		$('body').css('background-position','center 750px');
	}
}

function centerNavDropdowns(){
	var leftpos = 0;
	$('#nav>li>a').each(function(i,elem){
		leftpos += 22; // add left margin
		var topWidth = $(elem).outerWidth()/2;
		var $li = $(elem).parent();
		try{
			var $div = $li.find('div');
			var ddWidth = $div.outerWidth()/2
			var diff = topWidth - ddWidth;
			var newpos = leftpos + diff;
			$div.css("margin-left", newpos);
		}catch(e){}
		leftpos += $(elem).outerWidth();
		leftpos += 22; // add right margin
	});
}