//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Track Public & Extranet Downloads
//-------------------------------------------------------------------------------------------------------
$(document).ready(function(){	
 
	//	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 );
	});

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


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

    $("#footernav li").prepend('|');

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

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

	//	Force these elements to "haslayout" or else there will be js errors
	if($.browser.msie){
		  $("h2.icon div").css("border","1px solid white");
	}

	//	Vertical align
	$('h2.icon a').vAlign();
	$('h2.icon div').vAlign();

	//	Hide empty
	$("#sectionnav:empty").hide();

	//	Overlay
	$("li[rel],a[rel]").overlay();

	//	Submit Button styling
	$("input[type='submit']").addClass("submitbutton");

	//	External links target new window
	$("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');
};

$(document).ready(function(){	
 
	
	//	Submenu indicators
	$("#mainnav li.i1").find("ul").parent().each(function () {
		$(this).find("a.a1").addClass("showsub");
	});

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


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

}


/***
@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:		'99'
		}).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);

