//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function(){	
 
	//Bind tracking classes
	$(".trackDownload,.trackOutbound,.trackEmail").click(function(){
 
		var eventType = this.className,
			relValue = $(this).attr("rel"),
			eventCategory;		
 
		//Check if rel attribute is set. If so, use the rel value as the event label.
		if (relValue.length) {
 
			var eventLabel = relValue;
 
			if (eventType == "trackDownload") {
				eventCategory = "download";
			} else if (eventType == "trackOutbound") {
				eventCategory = "outbound";
			} else {
				eventCategory = "mailto";
			}
		}
		//Otherwise we'll use the URL or filename as the event label.
		else {	
 
			var trackURL = $(this).attr("href"),
				eventLabel;
 
			if (eventType == "trackDownload") {
				eventLabel = trackURL.substr(trackURL.lastIndexOf("/") + 1);
				eventCategory = "download";
			} else if (eventType == "trackOutbound") {
				eventLabel = trackURL;
				eventCategory = "outbound";
			} else {
				eventLabel = trackURL.substr(7);
				eventCategory = "mailto";
			}
		}
 
		//Send the event to Analytics
		pageTracker._trackEvent(eventCategory, 'click', eventLabel);		
 
	});

	//	PDF links open new tab
	$("a[href$='pdf']").click(function(){
		$(this).attr("target","_blank");

		var trackURL = $(this).attr("href");
		var	eventLabel = trackURL.substr(trackURL.lastIndexOf("/") + 1);

		//Send the event to Analytics
		pageTracker._trackEvent("download", 'click', eventLabel);		

	});

	//	External links target new window
	$("a[href^='http']").attr("target","_blank");

});


//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
var stop_rotating = false;
var current_cycle = 1;
var curr_timeout;
var currentTallest;
var total_banners;
var	current_home_banner	= 1;

//*********************************************************************************************************
//		Function:	startRotation()
//---------------------------------------------------------------------------------------------------------
//	What it does:	start the homepage rotation
//*********************************************************************************************************
function startRotation()
{
	total_banners = $('.home_banner').size();
	curr_timeout = setTimeout( "NextHomeBanner(1)", speed );
}

//*********************************************************************************************************
//		Function:	NextHomeBanner()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show the next homepage banner
//*********************************************************************************************************
function NextHomeBanner(q)
{
	current_home_banner	= q;

	if(q >= total_banners )
	{
		q = 1;
		if( banner_cycles && (current_cycle  >=banner_cycles) && !stop_rotating)
		{
			return;
		}
		current_cycle++;
	}
	else
	{
		q++;
	}

	var next		= '#home_banner_'+q;
	var next_thumb	= '#thumb_nav_div_'+q;

	var link = $(next+' a').attr('href');
	$('#home_banner_link').attr('href',link);

	// unset current thumb and banner image
	$('.thumb_nav_div_active').removeClass('thumb_nav_div_active'); 
//	$(".home_banner:visible").toggle(); 
	$(".home_banner:visible").fadeOut("fast"); 

	// set current thumb and banner image
//	$(next).toggle();
	$(next).fadeIn("fast");
	$(next_thumb).addClass('thumb_nav_div_active');
	
	if( stop_rotating )
	{
		return;
	}
	curr_timeout = setTimeout( "NextHomeBanner("+q+")", speed );
}

//*********************************************************************************************************
//		Function:	GoToHomeBanner()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show a certain homepage banner and stop the animation
//*********************************************************************************************************
function GoToHomeBanner(q)
{
	if( q != current_home_banner )
	{
		stop_rotating = true;
		clearTimeout(curr_timeout);
		NextHomeBanner(q);
	}
}


function resetlink(selector_id)
{
	$('#'+selector_id).click();

	$('#'+selector_id).removeClass('sm2_playing');
}


//*********************************************************************************************************
//		Function:	ToggleVideo()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show/hide the video
//*********************************************************************************************************
function ToggleVideo( id )
{
	if( !id)
	{
		$("#site_dim").fadeOut("fast");
		$('#movie_container').fadeOut("fast");
		$("#movie_container").html('');
		return;
	}

	$("#site_dim").fadeIn("fast");
	$('#movie_container').fadeIn("fast");
	

	var d = new Date();
	var video_url = '/site/aja_video.php?a='+id+'&t='+d.getTime();
	$.ajax({ 
		url: video_url
		, success: function(html)
			{
				$("#movie_container").html(html); 
				//alert(html);
			}
	});
}

function TogglePopupDiv( )
{
	$("#site_dim").toggle();
	$('#popup_div_container').toggle();

}


//*********************************************************************************************************
//		Function:	show_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	hide the old item and show the new item
//*********************************************************************************************************
function show_item( widget_num,old_num,new_num)
{
	var current=	'#current_'+widget_num;
	var old_id =	'#item_'+widget_num +'_' + old_num;
	var new_id =	'#item_'+widget_num +'_' + new_num;

	//	Don't cycle the same item
	if( old_id == new_id )
	{
		return;
	}

	$(old_id).css({"display":"none"});
	$(new_id).css({"display":"block"});
	$(current).html(new_num);
}

//*********************************************************************************************************
//		Function:	next_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	gets the next rotating widget item 
//*********************************************************************************************************
function next_item( widget_num )
{
	var total_id	= '#total_'+widget_num;
	var current_id	= '#current_'+widget_num;
	var total_items		= $(total_id).html();
	var old_num		= $(current_id).html();
	var new_num;

	// make sure this is an int.
	old_num = old_num*1;
	
	if(old_num >= total_items )
	{
		new_num = 1;	
	}
	else
	{
		new_num = old_num + 1;
	}

	show_item(widget_num,old_num,new_num);
}

//*********************************************************************************************************
//		Function:	prev_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	scrolls to a current item on the timeline
//*********************************************************************************************************
function prev_item(widget_num)
{
	var total_id		= '#total_'+widget_num;
	var current_id		= '#current_'+widget_num;

	var total_items		= $(total_id).html();
	var old_num		= $(current_id).html();

	// make sure this is an int.
	old_num = old_num*1;

	if(old_num == 1)
	{
		new_num	= total_items ;	
	}
	else
	{
		new_num = old_num - 1;
	}

	show_item(widget_num,old_num,new_num);
}



//*********************************************************************************************************
//		document.ready:::::::
//---------------------------------------------------------------------------------------------------------
//*********************************************************************************************************
$(document).ready(function(){

	//	IE opacity fix for home page features
	$('#site_dim').css('filter', 'alpha(opacity=50)');
	$('.banner_overlay').css('filter', 'alpha(opacity=65)');
	$('.thumb_nav_div a').css('filter', 'alpha(opacity=35)');

	//	Force these elements to "haslayout" or else there will be js errors
	if($.browser.msie){
		  $("h2.icon div").css("border","1px solid #C1D39F");
	}

	//	Vertical align
	$('h2.icon a').vAlign();
	$('h2.icon div').vAlign();



	//	Hide empty menus
	$(this).find('ul.m1').HideEmpty();
	$(this).find('ul.m2').HideEmpty();

	//	Menu positioning, offset 5 pixels higher
	$("#nav-main a").hover( function() {
		var button_id	= $(this).attr("id");
		var	menu_id		= "#m-" + button_id.substring( 2, button_id.length );

		var p = $( this );
		var offset = p.offset();

		$( menu_id ).css( "top", ( offset.top + $( this ).height() ) );
		$( menu_id ).css( "left", offset.left );
	});

	$(".sm2_link").toggle(
		function (event) {
			event.preventDefault();
			
			$(this).removeClass('sm2_paused');
			$(this).addClass('sm2_playing');
			
			var id = $(this).attr('id').substring(7);

			var selector_id = $(this).attr('id');

			soundManager.play(id,{
				onfinish:function(){
					resetlink(selector_id);
				}
			});
		},
		function (event) {
			event.preventDefault();
			$(this).removeClass('sm2_playing');
			$(this).addClass('sm2_paused');

			var id = $(this).attr('id').substring(7);
			soundManager.stop(id);
		}
	);

	//---------------------------------------------------------------------------------------------------------
	//		hover image
	//---------------------------------------------------------------------------------------------------------
		$('.button-image').hover( 
			function()
			{
				if( $(this).attr('hover_image'))
				{
					$(this).attr('src',$(this).attr('hover_image'));
				}
			},
			function()
			{
				if( $(this).attr('normal_image'))
				{
					$(this).attr('src',$(this).attr('normal_image'));
				}
			}
		);

	//---------------------------------------------------------------------------------------------------------
	//		remove text from the textboxes when one focuses on the textbox
	//---------------------------------------------------------------------------------------------------------
    $('.label_in_textbox').each(function(){
		$(this).attr('label',$(this).val());

		$(this).focus(function(){
			var label = $(this).attr('label');
			var value = $(this).val();
			
			//alert('l='+label +' v='+value);
			if( label == value )
			{
				$(this).val('');
			}
			
		});

		$(this).blur(function(){
			var label = $(this).attr('label');
			var value = $(this).val();

			if( !value )
			{
				$(this).val(label);
			}
		});
    });

	//---------------------------------------------------------------------------------------------------------
	//	make sure rotating items height is uniform so the prev/next links dont move up and down.
	//---------------------------------------------------------------------------------------------------------
	$('.rotating_items').each(function(q){
		currentTallest = 0;
		$(this).find('div.rotating_item').each(function(){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
			//alert($(this).height());
		});
		currentTallest = (currentTallest*1) + 16;

		//$(this).css({"height":currentTallest +'px !important;'});
		$(this).height(currentTallest);
		///alert($(this).attr('id') + currentTallest);
	});
	
	//---------------------------------------------------------------------------------------------------------
	//	track downloads
	//---------------------------------------------------------------------------------------------------------
	var trackablefileTypes = ['doc','xls','pdf','mp3'];
 
	$('a').each(function() {
		var a = $(this);
		var href = a.attr('href');
		if(href)
		{		
			var hrefArray = href.split('.');
			if( hrefArray )
			{
				var extension = hrefArray[hrefArray.length - 1];
				if ( extension && $.inArray(extension,trackablefileTypes) != -1) {
					a.click(function() {
						pageTracker._trackPageview('/documents/' + href);
					});
				}
			}
		}

	});	

 // track external links
	$('a').each(function() {
		var a = $(this);
		var href = a.attr('href');

		// see if the link is external
		if ( href &&  (href.substring(0,4) == 'http' )   && (! href.match(document.domain)))
		{
			// if so, add the GA tracking code
			a.click(function() {
			pageTracker._trackPageview('/external-link/' + href);
			});
		}
	 
	});

//---------------------------------------------------------------------------------------------------------
//	Expandible widgets
//---------------------------------------------------------------------------------------------------------
    $("h2.expand").click(function () {
		$(this).nextAll("div.expandable").slideToggle("fast");
    });    

//---------------------------------------------------------------------------------------------------------
// IE 6
//---------------------------------------------------------------------------------------------------------
//	if( is_ie6 )
//	{
//		//DD_belatedPNG.fix('.png_fix');
//	}



//---------------------------------------------------------------------------------------------------------
//	Menu tabs
//---------------------------------------------------------------------------------------------------------
	$("ul.m1").hover(
		function()
		{
			var	button_name	= $(this).attr("id").substr(2);
			$("a#b-"+button_name).addClass("highlight");
		},
		function()
		{
			var	button_name	= $(this).attr("id").substr(2);
			$("a#b-"+button_name).removeClass("highlight");
		}
	);


	//	Active menu background image replace
	$("#nav-main").find("a.active").parent().each(function () {
		var	section	= $("#nav-main").find("a.active").attr("id").substr(2);

		$("#navbar-bg").addClass( section );
//		$(this).css('background-image','url(/site/images/nav/' + imgname + ')' );

//		$(this).css("display","noneid yellow;");
//		alert( $(this).attr("class") + ')' );
	});




	//	newmainnav-10012
	//	Active menu background image replace
	$("#newmainnav").find("a.active img#newmainnav-10012").parent().each(function () {
		$("#navbar-bg").addClass( "newmainnav-10012" );
	});






	
	//	Submenu indicators
	$("#newmainnav li.i1").find("ul").parent().each(function () {
		$(this).find("a.a1").addClass("showsub");
	});



	$(".directlink input").click( function() {
		$(this).select();
	});

});

jQuery.fn.HideEmpty = function() {
  return this.each(function(){
	var itemcount	= $(this).find('li').length;
	if( !itemcount || itemcount == 0 )
	  {
		$(this).css( "visibility", "hidden" );
		$(this).css( "display", "none" );
	  }
  });
};

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	mh = parseInt( Math.pow( mh, 0.99 ) );
	$(this).css('padding-top', mh);
	});
};
})(jQuery);

