/* Description: 
Common javascript fucntion for all of the site

1. Set IE css
*/
$(document).ready(function() {
	// Set IE only behavior 
	if ($.browser.msie || $.browser.opera) { // && jQuery.browser.version.substr(0,3) == '6.0'
		// Set up rounded corners
		if ($('.ads').length) {
			$(".ad").cornerz({'radius' : 10, 'corners' : "tl tr", 'background' : '#009966', 'borderWidth' : 1, 'borderColor' : '#DEDEDE'});
		}
		
		// Look for all round corner class based on any type of element, not just div 
		// Sets borderwidth to 0
		// TODO: account for border width, maybe by requiring a new border specific class
		$("[class*='round_corners_']").each(function(){
			var corners = '',
				radius,
				matches,
				currentElement = $(this),
				classes = currentElement.attr('class'),
				backgroundColor = '';

			// check for 'round_corners_all' in classes			
			// exec() executes a search for a match in a string. It returns an array of information.
			// matches contains regex information, [0] = entire string matched, [1] = first parathesis
			if (matches = /round_corners_all_(\d+)px/.exec(classes)) {
				corners = "tl tr bl br";
				radius = matches[1];
			}
			else { // only one radius for all corners
				if (matches = /round_corners_top_left_(\d+)px/.exec(classes)) {
					corners += "tl";
					radius = matches[1];
				}
				if (matches = /round_corners_top_right_(\d+)px/.exec(classes)) {
					corners += " tr";
					radius = matches[1];
				}
				if (matches = /round_corners_bottom_left_(\d+)px/.exec(classes)) {
					corners += " bl";
					radius = matches[1];
				}
				if (matches = /round_corners_bottom_right_(\d+)px/.exec(classes)) {
					corners += " br";
					radius = matches[1];
				}
			}
			// remove any leading spaces
			corners = $.trim(corners);
		
			//console.log('corners are "%s", radius is %s', corners, radius);
			
			if (backgroundColor = findBackgroundColor(currentElement)) {
				// border-color is undefined in FF, ok in IE, assume that all side are the same color
				borderColor = currentElement.css('border-left-color');
				
				currentElement.cornerz({'radius' : radius, 'corners' : corners, 'background' : backgroundColor, 'borderWidth' : 0}); //, 'borderColor' : borderColor
			}
			else {
				currentElement.cornerz({'radius' : radius, 'corners' : corners, 'borderWidth' : 0});
			}
		});
	} // end of IE/Opera specific stuff
	
	// Find all a tags with external links, add target _blank (to handle window focus with named window) and add external icon
	$('a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
		}).attr('target', '_blank') /// add target to all external links
		.not(':has(img)').addClass('external');// For all <a> not containing an <img> add class - instead of appeading image -> prevents IE border of image problem
		
	// Open PDF or ppt files in new window
	$('a[href$="PDF"], a[href$="pdf"], a[href$="PPT"], a[href$="ppt"]').attr('target', '_blank');
	$('a[href$="PDF"], a[href$="pdf"]').addClass('pdf');
		
	// Newsletter specific stuff
		// Hide all subsections
		$('.newsstory').hide();	
		
		// Show the section navigation: previous, next links
		$(".newletter_section_nav").css("display", "block");// show() doesn't work for some reason
		$(".backtotop").hide();
		$("#newsletter #content h2").css("margin-top", "1em");
});

function findBackgroundColor(element) {
	var bgCol = "";
	// find the background color - ascend all the way to BODY if	necessary
	while (!bgCol || bgCol=='transparent') {
		element = element.parents(':first');
		bgCol = element.css('backgroundColor');
		if (!element[0] || element[0].tagName == 'body') {
			break;
		}
	}
	if (bgCol && bgCol != 'transparent') {
		return bgCol;
	}
	else {
		return false;	
	}
}