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

    //	Init searchbar
	$('form#search input[name="findtext"]').liveSearch({url: '/site/quicksearch.php?findtext='});

	//	No right padding on last item (IE8)
	$('#mainnav li:last a').css("padding-right",0);

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

//-------------------------------------------------------------------------------------------------------
//	News (archive links) menu hidden
//-------------------------------------------------------------------------------------------------------
	$("#mainnav-1065 ul").remove();

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

//-------------------------------------------------------------------------------------------------------
//	Form toggle
//-------------------------------------------------------------------------------------------------------
	$("#openform a").click( function(e) {
		e.preventDefault();
		$(this).parentsUntil("#right").find("form").slideToggle(75);
	});

//-------------------------------------------------------------------------------------------------------
//	Share/Email
//-------------------------------------------------------------------------------------------------------
	var email_url;

	// select the overlay element - and "make it an overlay"
	$("#emailform").overlay({

		// some mask tweaks suitable for facebox-looking dialogs
		mask: {

			// you might also consider a "transparent" color for the mask
			color: '#fff',

			// load mask a little faster
			loadSpeed: 200,

			// very transparent
			opacity: 0.5
		},

		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap	= this.getOverlay().find(".contentWrap");
			wrap.empty().prepend('<iframe id="emailthis" src="'+email_url+'"></iframe>');
		},

		// disable this for modal dialog-type of overlays
		closeOnClick: false

	});


	$("a#share-email").click(function(event){
		email_url	= $(this).attr("href");
		event.preventDefault();
		$("#emailform").overlay().load();
	});

//-------------------------------------------------------------------------------------------------------
//	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,#search input').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 );
		});

	});


//---------------------------------------------------------------------------------------------------------
//	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:	80,

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

		}

	
	});

//---------------------------------------------------------------------------------------------------------
//	Vertical align
//---------------------------------------------------------------------------------------------------------
	$(".valign").each(function(i){
		var object_height	= $(this).height();
		var parent_height	= $(this).parent().height();
		var padding_height	= parseInt( ( parent_height - object_height ) / 2 );
		$(this).css('padding-top', padding_height );
	});


	$("object").attr("wmode","transparent");

//---------------------------------------------------------------------------------------------------------
//	FAQ lists
//---------------------------------------------------------------------------------------------------------
	$("dl.faq dt").click(function(){
		$(this).next("dd").slideToggle(50).fadeToggle(50);
	});


});


