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

//---------------------------------------------------------------------------------------------------------
//	Predictive Search
//---------------------------------------------------------------------------------------------------------
	$('#header-top input[name="findtext"]').liveSearch({url: '/ajax/search.php?findtext='});

//---------------------------------------------------------------------------------------------------------
//	Clear the search input field
//---------------------------------------------------------------------------------------------------------
	var clearMePrevious = '';

	// clear input on focus
	$('.clearonfocus').focus(function()
	{
		if($(this).val()==$(this).attr('title'))
		{
			clearMePrevious = $(this).val();
			$(this).val('');
		}
	});
	
	// if field is empty afterward, add text again
	$('.clearonfocus').blur(function()
	{
		if($(this).val()=='')
		{
			$(this).val(clearMePrevious);
		}
	});

//-------------------------------------------------------------------------------------------------------
//	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");
    });   
    
//---------------------------------------------------------------------------------------------------------
//	Overlay 
//---------------------------------------------------------------------------------------------------------
    $("a[rel]").overlay({
		effect: 'apple',
		onBeforeClose: function()
		{ 
			$f("*").each(function() {
			    this.stop(); 
			});

		//	contentsHTML = this.getOverlay().find("#videobox").html();
		//	this.getOverlay().find("#videobox").html('');
		//	this.getOverlay().find("#videobox").html(contentsHTML);
		}
	});

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


});


