//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function(){	

//-------------------------------------------------------------------------------------------------------
//	External Links
//-------------------------------------------------------------------------------------------------------
	$('a[href^="http://"]').attr("target", "_blank");

//-------------------------------------------------------------------------------------------------------
//	Share widget
//-------------------------------------------------------------------------------------------------------
	$("#share").hover(
		function () {
			$(this).find("ul").fadeIn( 75 );
		}, 
		function () {
			$(this).find("ul").fadeOut( 75 );
		}
	);

//-------------------------------------------------------------------------------------------------------
//	Share/Email
//-------------------------------------------------------------------------------------------------------
	$("a#share-email").overlay({
		mask: 'darkred',
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap	= this.getOverlay().find(".contentWrap");
			var	url		= this.getTrigger().attr("href");
			wrap.empty().prepend('<iframe id="emailthis" src="'+url+'"></iframe>');
		}
	});

//-------------------------------------------------------------------------------------------------------
//	Social Media Share - Open new window
//-------------------------------------------------------------------------------------------------------
	$("a#share-facebook,a#share-twitter,a#share-linkedin").click(function(event){

		//	Master category for all shares
		var	category	= 'Share';

		//	Share category
		var	subcategory	= $(this).attr("id");
		subcategory		= subcategory.split("-");
		subcategory		= subcategory[ ( subcategory.length - 1 ) ];

		//	Label based on title
		var	label		= $(document).attr("title");

		//	Send the event to Analytics
		_gaq.push(['_trackEvent', category, subcategory, label ]);
		
		var	attributes	= "width=550,height=300,location=0,menubar=0,toolbar=0";
		window.open( $(this).attr("href"), "share", attributes, true );
		event.preventDefault();
	});

//-------------------------------------------------------------------------------------------------------
//	Track Downloads
//-------------------------------------------------------------------------------------------------------
	$("a.download,a[href$='pdf'],a[href$='doc'],a[href$='mp3']").click(function(){

		//	Master category for all downloads
		var	category	= 'Downloads';

		//	Path of file being downloaded
		var	filepath	= $(this).attr("href");

		//	Use the file extention as the "action" parameter for the event
		filepath		= filepath.split(".");
		var	filetype	= filepath[ ( filepath.length - 1 ) ].toUpperCase();

		//	Label based on title or href
		var	label		= $(this).attr("title");

		if( !label )
		{
			label		= $(this).attr("href");
		}

		//	Send the event to Analytics
		_gaq.push(['_trackEvent', category, filetype, label ]);

		//	Open in new tab	
		$(this).attr("target","_blank");
	});

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

	//---------------------------------------------------------------------------------------------------------
	//	remove text from the textboxes when one focuses on the textbox
	//---------------------------------------------------------------------------------------------------------
    $('.inline-label input,.inline-label textarea').each(function(){

		$(this).focus(function(){
			$(this).siblings("label").fadeTo( 75, 0 );
		});

		$(this).blur(function(){
			if( $(this).val() )
			{
				fade_on_blur	= 0;
			}
			else
			{
				fade_on_blur	= 1.0;
			}
			$(this).siblings("label").fadeTo( 75, fade_on_blur );
		});

		if( $(this).val() )
		{
			$(this).siblings("label").fadeTo( 0, 0 );
		}

	});

//---------------------------------------------------------------------------------------------------------
//	Add submenu indicators
//---------------------------------------------------------------------------------------------------------
	$("nav#mainnav li.i1").find("ul").parent().each(function () {
		$(this).find("a.a1").addClass("showsub");
	});

//---------------------------------------------------------------------------------------------------------
//	Video overlay
//---------------------------------------------------------------------------------------------------------
	$("iframe[title='YouTube video player']").attr("enablejsapi",1);

	var	ytplayer;

	var triggers = $("div[rel]").overlay({
		top:	17,
		left:	17,

		closeOnClick: true,

		onBeforeLoad: function() {

			var videoplayer = this.getOverlay().find("embed");
			videoplayer.attr("wmode","transparent");

			
			var videoplayerid = this.getOverlay().attr("id");
			ytplayer = this.getOverlay();

			this.getOverlay().find("a.close").html("close");

			var slideshowapi = $("ul.slidebuttons").data("slideshow");

			slideshowapi.stop();

		},

		onLoad: function() {

			var videoplayerid = this.getOverlay().attr("id");
			var	player_id	= "yt" + videoplayerid.substr( 5, videoplayerid.length );

			ytplayer	= document.getElementById(player_id);

			//	Stop the video
			if( ytplayer )
			{
				ytplayer.playVideo();
			}

		},

		onBeforeClose: function() {

			var videoplayerid = this.getOverlay().attr("id");
			var	player_id	= "yt" + videoplayerid.substr( 5, videoplayerid.length );

			ytplayer	= document.getElementById(player_id);

			//	Stop the video
			if( ytplayer )
			{
				ytplayer.stopVideo();
			}

			var slideshowapi = $("ul.slidebuttons").data("slideshow");

			slideshowapi.play();

		}

	
	});

//-------------------------------------------------------------------------------------------------------
//	Position Client overlay
//-------------------------------------------------------------------------------------------------------
	$('.valign-center').each( function() {
		var top_offset	= ( $(this).parent().height() - $(this).height() ) / 2;
		$(this).css("margin-top",top_offset+"px");
	});


//-------------------------------------------------------------------------------------------------------
//	Full width if no side content
//-------------------------------------------------------------------------------------------------------
	var template	= $("#right").attr("id");

	if( !template )
	{
		$("article#main").css("width","auto");
	}

//-------------------------------------------------------------------------------------------------------
//	Contact Us Form
//-------------------------------------------------------------------------------------------------------
	$("#contact-form").overlay({
		//	positioning
		top:			125,
		left:			315,

		effect:			"apple",
		load:			false,

		onBeforeLoad: function() {
			this.getOverlay().find("a.close").html("close");
		}

	});
	
	$("a#contact").click(function(event){
		event.preventDefault();
		var contactformapi = $("#contact-form").data("overlay");
		contactformapi.load();
	});

//-------------------------------------------------------------------------------------------------------
//	GSA
//-------------------------------------------------------------------------------------------------------
	$("#gsa-box").overlay({
		//	positioning
		top:			475,
		left:			315,

		effect:			"apple",
		load:			false,

		onBeforeLoad: function() {
			this.getOverlay().find("a.close").html("close");
		}

	});
	
	$("img#gsa").click(function(event){
		event.preventDefault();
		var gsaapi = $("#gsa-box").data("overlay");
		gsaapi.load();
	});



});


