var JHAM = {

	// JHAM.color
	color: '#000000',

	// JHAM.init
	init:function(){
		// clr is a static, defined variable supplied by HTML page
		JHAM.color = clr;

		// add magnifying glass to images with enlarged versions
		jQuery('a.fancybox').append('<span class="magglass"></span><span class="clearleft"></span>');

		// add / handle fancybox functionality to images as found
		jQuery('a.fancybox').each(function() {
			var imgWidth = jQuery(this).find('img').attr('data-width');
			var imgHeight = jQuery(this).find('img').attr('data-height');
			imgWidth = (imgWidth ? imgWidth : jQuery(this).find('img').width());
			imgHeight = (imgHeight ? imgHeight : jQuery(this).find('img').height());
			var thisWidth = jQuery(this).width();
			if (thisWidth < imgWidth) {
				imgHeight = (thisWidth / imgWidth) * imgHeight;
				imgWidth = thisWidth;
			}
			jQuery(this).width(imgWidth);
			jQuery(this).height(imgHeight);
			jQuery(this).addClass(jQuery(this).find('img').attr('class'));
			jQuery(this).find('img').removeClass();
		});
		
		jQuery('div.user').hover(function(){
			jQuery(this).children('.userbio').fadeIn();
			jQuery(this).css('background-color',JHAM.color);
		}, function(){
			jQuery(this).children('.userbio').fadeOut();	
			jQuery(this).css('background-color','transparent');
		});
				
		JHAM.resizeClr();
			
		var resizeTimeout;		
		jQuery(window).resize(function(){
			// on resize, clear the timeout
			clearTimeout(resizeTimeout);
			// set the timout for 100 ms. if resized in that time, it will clear and do it again.
			resizeTimeout = setTimeout(function() {
				JHAM.resizeClr();
			}, 100);
			
			JHAM.mobileComments();
		});
								
		JHAM.analytics();
		
		JHAM.setMenu();
		
		jQuery('#menu-main-navigation a').each(function(){
			if(window.location.pathname == jQuery(this).attr('href')){
				jQuery(this).parent().addClass('current-menu-item');
			}
		});
		
		JHAM.mobileComments();
	},
	
	// JHAM.setColor
	setColor: function() {
		jQuery('.clr').animate({
			backgroundColor: JHAM.color,
			complete: function(){
				if(window.innerWidth <= 898){
					JHAM.resizeClr();
				}
			}
		}, cw, function() {
	// sets background color of main nav links to white
	// 		jQuery('body.home #access li a:not(.clr)').css('background-color', '#fff');
			switching = 1;
		});
		jQuery('.clr1').animate({
			backgroundColor: JHAM.color,
			complete: function(){
				if(window.innerWidth <= 898){
					JHAM.resizeClr();
				}
			}
		}, cw, function() {
			jQuery('#name h1 a:not(.clr1)').css('background-color', '#000');
		});
	},

	
	// JHAM.resizeClr
	// calls clr function again on window resize
	resizeClr: function() {
		if (window.innerWidth > 898) {
			if(jQuery('#utility a span.clr').length < 0){
				jQuery('#utility a').append('<span class="clr"></span>');
			}
			jQuery('#utility .current_page_item a span').addClass('clr');
			jQuery('#utility .current-page-ancestor a span').addClass('clr');
			jQuery('#access .current_page_item').addClass('clr');
			jQuery('.archive #access .current_page_parent').addClass('clr');
			jQuery('#access .current-menu-ancestor').addClass('clr');
			jQuery('#access .current-page-ancestor').addClass('clr');
			jQuery('#access ul ul').addClass('clr');
		
			// adds styles to li and its children on mouseover
			jQuery('#menu-main-navigation > li:not(.current_page_item,.current-page-ancestor,.current-menu-ancestor, .archive .current_page_parent)').off("mouseover").on("mouseover", function() {
				jQuery(this).addClass('clr').css('background-color', JHAM.color);
				jQuery(this).children('a').css('color', '#fff');
			});
			
			// removes styles from li and its children on mouseout
			jQuery('#menu-main-navigation > li:not(.current_page_item,.current-page-ancestor,.current-menu-ancestor, .archive .current_page_parent)').off("mouseout").on("mouseout", function() {
				jQuery(this).removeClass('clr').css('background-color', '#ffffff');
				jQuery(this).children('a').css('color', '#000');
			});
			
			JHAM.color = '#777777';
			JHAM.setColor(JHAM.color);
		} else {
			jQuery('#utility .current_page_item a span').removeClass('clr');
			jQuery('#utility .current-page-ancestor a span').removeClass('clr');
			jQuery('#access .current_page_item').removeClass('clr');
			jQuery('.archive #access .current_page_parent').removeClass('clr');
			jQuery('#access .current-menu-ancestor').removeClass('clr');
			jQuery('#access .current-page-ancestor').removeClass('clr');
			jQuery('#access ul ul').removeClass('clr');
		
			// adds styles to li and its children on mouseover
			jQuery('#menu-main-navigation > li:not(.current_page_item,.current-page-ancestor,.current-menu-ancestor, .archive .current_page_parent)').off("mouseover").removeClass('clr').css('background-color', '').children('a').css('color', '');
			
			// removes styles from li and its children on mouseout
			jQuery('#menu-main-navigation > li:not(.current_page_item,.current-page-ancestor,.current-menu-ancestor, .archive .current_page_parent)').off("mouseout").removeClass('clr').css('background-color', '').children('a').css('color', '');
			
			jQuery('ul.menu, ul.sub-menu, li.menu-item').removeClass('clr').removeClass('clr1').css('background-color', '');
		}
	},
		
	// JHAM.analytics
	analytics: function() {
		// Analytics Tracking
		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
		(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
		m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
		})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
		
		ga('create', 'UA-8477085-11', 'auto');
		ga('send', 'pageview');	
		
		/* track downloads of files */
		// Moved this first because all Wordpress links including relative start with whole domain/url
	    jQuery('a[href$=".pdf"],a[href$=".ppt"],a[href$=".pptx"],a[href$=".doc"],a[href$=".docx"],a[href$=".xls"],a[href$=".xlsx"],a[href$=".zip"]').each(function(){
	        jQuery(this).bind('click',function(event){
	            var file = jQuery(this).attr('href').split('/').pop();
	            ga('send', 'event', 'File Download', window.location.pathname + ' - ' + file);
	        });
	    });
		
	    /* track outbound links */
	    jQuery('a[href^=http]').each(function(){
	        jQuery(this).bind('click',function(event){
	            var url = jQuery(this).attr('href').split('//').pop().split('/'),
	                host = url.shift();
	            // made sure to check for host since all Wordpress URLs contain the http plus host etc.
				if(host != window.location.hostname){
					ga('send', 'event', 'External Link', host + '/' + url.join('/'));
				}
	        });
	    });
	
	    /* track email address clicks */
	    jQuery('a[href^=mailto]').each(function(){
	        jQuery(this).bind('click',function(event){
	            var email = jQuery(this).attr('href').split(':').pop();
	            ga('send', 'event', 'Email Link', window.location.pathname + ' - ' + email);
	        });
	    });
	
	    /* track form submissions */
	    jQuery('form').each(function(){
		    // added id because Wordpress doesn't name its forms, but they do have unique ids
		    var form = jQuery(this).attr('name') == undefined ? jQuery(this).attr('id') : jQuery(this).attr('name');
	        jQuery(this).bind('submit',function(event){
	            ga('send', 'event', 'Form Submission', window.location.pathname + ' - ' + form);
	        });
	    });
    
	},
	
	//	JHAM.setMenu
	setMenu: function(){
		jQuery("a.hamburger, a.x").click(function() {
			jQuery('#menu-nav').toggleClass('responsive');
			jQuery('ul.menu, ul.sub-menu, li.menu-item').removeClass('clr').removeClass('clr1').css('background-color', '');
		});

	},
	
	// Hides all but first two comments, and on click, shows more.
	mobileComments: function(){
		var length = jQuery('.commentlist > li.comment').length,
			height = 0,
			fullheight = 0;
			
		jQuery('.commentlist > li.comment').each(function(){
			fullheight += jQuery(this).outerHeight(true);
		});
		
		if (length > 1){
			// show first two if more than one comment
			height += jQuery('.commentlist .comment:nth-child(1)').height();
			height += jQuery('.commentlist .comment:nth-child(2)').height();
			jQuery('.commentlist').height(height + 'px');
		} else {
			// set height auto and hide "Show More Comments" link
			jQuery('.commentlist').height('auto');
			jQuery('#comments a.show-more').hide(); 
		}
		
		if(window.innerWidth > 1020){
			jQuery('.commentlist').height('auto');
		}
		
		jQuery('#comments a.show-more').off('click').on('click', function(){
			if(jQuery('#comments a.show-more').html() == "Show More Comments"){
				jQuery('#comments a.show-more').html("Show Less Comments");
				jQuery('.commentlist').height(fullheight + 'px');
			} else {
				jQuery('#comments a.show-more').html("Show More Comments");
				jQuery('.commentlist').height(height + 'px');
			}
		});
	}

};
var switching = 1;
jQuery.fn.fadeIn = function(speed, callback) {
	return this.animate({
		opacity: 'show'
	}, speed, function() {
	//	if (jQuery.browser.msie) this.style.removeAttribute('filter'); //
		if (jQuery.isFunction(callback)) callback();
	});
};
jQuery.fn.fadeOut = function(speed, callback) {
	return this.animate({
		opacity: 'hide'
	}, speed, function() {
	//	if (jQuery.browser.msie) this.style.removeAttribute('filter'); //
		if (jQuery.isFunction(callback)) callback();
	});
};
jQuery.fn.fadeTo = function(speed, to, callback) {
	return this.animate({
		opacity: to
	}, speed, function() {
	//	if (to == 1 && jQuery.browser.msie) this.style.removeAttribute('filter'); //
		if (jQuery.isFunction(callback)) callback();
	});
};
var cw = 700;

jQuery(document).on('ready post-load',function() {
	JHAM.init();
});