//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
	var IE;
//-------------------------------------------------------------------------------------------------------
//	Track Public & Extranet Downloads
//-------------------------------------------------------------------------------------------------------
$(document).ready(function(){	
 
//-------------------------------------------------------------------------------------------------------
//	External Links
//-------------------------------------------------------------------------------------------------------
	$('a[href^="http://"]').attr("target", "_blank");

	//	Bind click event for downloads
	$(".trackDownload").click(function(){

		//	Set event info
		var	event_type		= 'Download',
			event_category	= $(this).attr("category"),
			event_label		= $(this).attr("filename");

		//	If filename not specifiec, get from URL
		if( !event_label )
		{
			var trackURL = $(this).attr("href");
			event_label = trackURL.substr(trackURL.lastIndexOf("/") + 1);
		}
 
//		alert( event_type + " : " + event_category + " : " + event_label );

		//	Send the event to Analytics
//		pageTracker._trackEvent( event_type, event_category, event_label );

		//	Use asynchronous tracker	
		_gaq.push(['_trackPageview', event_label ]);
	});

	//	PDF links open new tab
	$("a[href$='pdf']").click(function(){
		$(this).attr("target","_blank");
	});

});

$(document).ready(function(){	
 
	//Bind tracking classes
	$(".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, 'Download', eventLabel);		
//		pageTracker._trackEvent(eventCategory, 'click', eventLabel);		
	});

	//	Read-More function
	$("a.readmore").click(function(event){
		event.preventDefault();
		$(this).hide("fast");
		$(this).next("div").slideDown("fast");
	});

//---------------------------------------------------------------------------------------------------------
//	Expandible widgets
//---------------------------------------------------------------------------------------------------------
    $("h2.expand").click(function () {
		$(this).next("div.expandable").slideToggle("fast");
    });    

	//---------------------------------------------------------------------------------------------------------
	//		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);
			}
		});
    });

//-------------------------------------------------------------------------------------------------------
//	Resources
//-------------------------------------------------------------------------------------------------------
	$(".resources-row .item:last-child").css("padding-right","0px");



//-------------------------------------------------------------------------------------------------------
//	Main navigation shadow highlights
//-------------------------------------------------------------------------------------------------------
	$("#mainnav li.i0").each(function(index) {
		img_el		= $(this).find("img");

		//	Image width + left margin + right margin
		var	highlight_width	= 28 + img_el.innerWidth();
		
		var	li_id	= $(this).attr("id");
		var	str_br	= li_id.indexOf("-");
		
		var	highlight_id	= "highlight" + li_id.substr( str_br, li_id.length );

		$(this).find("a").clone().appendTo(this).wrap('<div id="'+highlight_id+'" class="testhover" style="width:'+highlight_width+'px;"></div>');
	});


	$("#mainnav li.i0").hover(
		function () {
			//	Set width (webkit browsers don't know the image width on document ready)
			img_el		= $(this).find("img");
			var	highlight_width	= 28 + img_el.innerWidth();

//			$(this).find(".testhover").width( highlight_width );

			$(this).css( "z-index", 10002 );

			if( IE )
			{
				$(this).find(".testhover").css("display","block");
			}
			else
			{
				$(this).find(".testhover").fadeIn(75);
			}
		},
		function () {
			$(this).css( "z-index", 10001 );

			if( IE )
			{
				$(this).find(".testhover").fadeOut(75);
			}
			else
			{
				$(this).find(".testhover").css("display","none");
			}
	});

	$(".program").hover(
		function () {
			if( IE )
			{
				$(this).find("a.program-caption").css("display","block");
			}
			else
			{
				$(this).find("a.program-caption").fadeIn(75);
			}
		},
		function () {
			if( IE )
			{
				$(this).find("a.program-caption").css("display","none");
			}
			else
			{
				$(this).find("a.program-caption").fadeOut(75);
			}
	});


	//	Prepend top graphic
	$(".paper206").each(function(index) {
		$(this).prepend('<img src="/site/images/bg_paper206_top2.png">');
	});

	//	Tab highlight/mouseover
	$(".tabs li").hover(
		function () {
			$(this).addClass("tab1-current");
		},
		function () {
			$(this).removeClass("tab1-current");
	});

	//	Tab highlight/mouseover
//	$(".tabs a.current").parentsUntil("ul").css("border","1px solid red");
//	$(".tabs a").wrapInner('<span class="tabc2"></span>').wrapInner('<span class="tabc1"></span>');

	//---------------------------------------------------------------------------------------------------------
	//	Share Box
	//---------------------------------------------------------------------------------------------------------
    $("#sharebox").click(
		function (event) {
			$("#sharebox").toggleClass("active");
			$("ul#share").slideToggle(75);

			
	});
    $("a#sharebutton").click(
		function (event) {
			event.preventDefault();
	});

	//	External links target new window
	$('#body a[href^="http://"]').attr("target", "_blank");
});

function loadMailForm(title, link)
{
	$("#videobox").css('display','block');
	$('#videobox').load("/site/emailpage.php?title="+title+"&link="+link);
	$('#videobox').center(true);
};

function closeMailForm()
{
	$("#videobox").css('display','none');
};

//-------------------------------------------------------------------------------------------------------
//	Featured Banners
//-------------------------------------------------------------------------------------------------------
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)
{
	stop_rotating = true;
	clearTimeout(curr_timeout);

	if( q != current_home_banner )
	{
		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;

	$(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);
}

/***
@title:
Center

@version:
2.0

@author:
Andreas Lagerkvist

@date:
2008-09-17

@url:
http://andreaslagerkvist.com/jquery/center/

@license:
http://creativecommons.org/licenses/by/3.0/

@copyright:
2008 Andreas Lagerkvist (andreaslagerkvist.com)

@requires:
jquery

@does:
This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

@howto:
jQuery('#my-element').center(true); would center the element with ID 'my-element' using absolute position (leave empty for fixed).

@exampleHTML:
<p>I should be fixed centered</p>

<p>The paragraph above and the paragraph beneath this one are centered. They should be in the middle of the viewport.</p>

<p>I should be absolutely centered</p>

@exampleJS:
jQuery('#jquery-center-example p:first-child').center();
jQuery('#jquery-center-example p:last-child').center(true);
***/
jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'50%', 
			zIndex:		'60000'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};

(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);

