/* ************************************************************************** */
/* **************************** IMAGE CAPTIONS ****************************** */
/* ************************************************************************** */

// find any captioned images and size the containing div to be the same
// as the image, so that the caption won't force it to expand beyond the limit of the image
resizeCaptions = function() {
	jQuery('#text div.captioned_image_container').each(function(){
		var imgWidth = jQuery(this).children('img').width();
		jQuery(this).css('width', imgWidth);
	});
} // end resizeCaptions

// use these instead of jQuery(document).ready() so that they don't run until all images loaded
if (window.attachEvent) window.attachEvent("onload", resizeCaptions); // IE
if (window.addEventListener) window.addEventListener("load", resizeCaptions, false); // W3C-compliant

/* ************************************************************************** */
/* **************************** DROP-DOWN NAVIGATION ************************ */
/* ************************************************************************** */

// from http://www.htmldog.com/articles/suckerfish/dropdowns/
sfHover = function() {
	var sfEls = document.getElementById("primary_nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function(trigger_event) {
			navover(this.id, trigger_event); // jkr added this line for vpr
		}
		sfEls[i].onmouseout=function(trigger_event) {
			navout(this.id, trigger_event); // jkr added this line for vpr
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
if (window.addEventListener) window.addEventListener("DOMContentLoaded", sfHover, false); // jkr added so this runs in firefox, too


function navover(nav_li_id, trigger_event) {
	var trigger_li = jQuery('#'+nav_li_id);
	trigger_li.addClass('sfhover');
	if (!trigger_li.hasClass('empty')) { // items with no 2nd level items

		pattern = /_li/i;
		tabname = nav_li_id.replace(pattern, ''); // strip 'listen_li' to straight 'listen'

		// only go through positioning if not already done -- save a lot of processing
		positioned_var = tabname + '_positioned';
		if (typeof window[positioned_var] == 'undefined') {
			// indicate we've already positioned this element
			window[positioned_var] = true;
			var primary_nav = jQuery('#primary_nav');
			var container_coords = {
				left: primary_nav.offset().left,
				right: primary_nav.offset().left + primary_nav.width(), 
				width: primary_nav.width()
			};
			var trigger_li = jQuery('#'+nav_li_id);
			var trigger_li_coords = {
				left: trigger_li.offset().left,
				right: trigger_li.offset().left + trigger_li.width(), 
				width: trigger_li.width()
			};
			var pixels_to_edge = container_coords.right - trigger_li_coords.left;
			var subnav_div = jQuery('#'+tabname+"_sub");
			// shove way left so it'll expand to natural size
			subnav_div.css('left', '-2000px');
			var subnav_div_coords = {
				left: subnav_div.offset().left,
				right: subnav_div.offset().left + subnav_div.width(), 
				width: subnav_div.width()
			};
			// fix the width at that size, so it won't change later
			subnav_div.css('width', subnav_div_coords.width);

			if (subnav_div_coords.width > pixels_to_edge) {
				subnav_nudge_left = subnav_div_coords.width - pixels_to_edge + 2; // 5 added just to give a bit of room
				subnav_div.css('left', '-'+ subnav_nudge_left + 'px');
			} else {
				// subnav_div.css('left', 'auto');
				subnav_div.css('left', '0px');
			}
		} // end if not already positioned
	} // end if not an empty menu
} // end function 

function navout(nav_li_id,trigger_element) {
	var trigger_li = jQuery('#'+nav_li_id);
	trigger_li.removeClass('sfhover');
} // end function 

/* ************************************************************************** */
/* **************************** CLICK TRACKING ****************************** */
/* ************************************************************************** */

/* ************************ rotator tracking ************************/
function track_rotator_click (source_url, destination_url, rotator_position) {
	// replace any bad characters
	replace_pattern = /[\/|:]/g;
	replace_replacement = '~';
	source_url = source_url.replace(replace_pattern, replace_replacement);
	destination_url = destination_url.replace(replace_pattern, replace_replacement);
	// generate the string to be passed to urchintracker
	path_to_record = '/_events/click/link/rotators/source=' + source_url + '/destination=' + destination_url + '/position=' + rotator_position + '/';
	// actually record
	urchinTracker(path_to_record);
} // end function

/* ************************ headline tracking ************************/
function track_headline_click (source_url, service, destination_url, list_position) {
	// replace any bad characters
	replace_pattern = /[\/]/g;
	replace_replacement = '~';
	source_url = source_url.replace(replace_pattern, replace_replacement);
	destination_url = destination_url.replace(replace_pattern, replace_replacement);
	// generate the string to be passed to urchintracker
	path_to_record = '/_events/click/link/headline/content_provider=' + service + '/source=' + source_url + '/destination=' + destination_url + '/position=' + list_position + '/';
	// actually record
	//alert(path_to_record);
	urchinTracker(path_to_record);
} // end function

/* ************************ special banner tracking ************************/
function track_special_banner_click (link_description,source_url, destination_url) {
	// replace any bad characters
	replace_pattern = /[\/]/g;
	replace_replacement = '~';
	source_url = source_url.replace(replace_pattern, replace_replacement);
	destination_url = destination_url.replace(replace_pattern, replace_replacement);
	// generate the string to be passed to urchintracker
	path_to_record = '/_events/click/link/special_banner/' + link_description + '/source=' + source_url + '/destination=' + destination_url + '/';
	// actually record
	//alert(path_to_record);
	urchinTracker(path_to_record);
} // end function