// Used across all projects for various functions.

// Unsure about this one
function _d(mValue) {
	if (console) {
		if (console.log) {
			console.log(mValue);
		}
	} else {
		return false;
	}
	return true;
}


// Footer and rate-this-topic
function stickyFooter(){
	$('.push-sticky-footer').height($('div.rate-this-topic').outerHeight(true)+$('#footer-placeholder').outerHeight(true));
}

$(window).resize(function () {
		stickyFooter();
	});


$(window).on('load', function(){
		stickyFooter();

		// Code syntax highlighting 
		$(document).ready(function() { 
			var $code = jQuery('pre'); 
			jQuery('pre.copy-code').wrap( '<div class="code-wrapper"></div>').before('<a class="copy-button" href="javascript:void(0);" data-copied="Code copied">Copy</div>'); 
			jQuery('.code-wrapper a.copy-button').on('click', copyCode); 
 
			if (typeof(hljs) !== 'undefined') { 
				hljs.configure({languages: []}); 
				$code.each(function(i, block) { 
					hljs.highlightElement(block); 
				}); 
			} 
 
		}); 

	    // angular
		require.config({
		    paths: {
		        angular: '.../dist/angular.min.js',
		    }
		});

		// Version picker
		$('#version-picker').change(function(){
				var href = $(this).val();
				if(href){
					window.open(
						href,
						'_blank'
						);
				}
			});

		// Menu phone
		$('.phone-menu').click(function(){
				if($('#main-menu nav').is(':visible')){
					$('#main-menu nav').slideUp();
				}else{
					$('#main-menu nav').slideDown();
				}
			});

		// General global variables
		var scrollTop;
		var windowPhoneWidth = 767;


		// Making version picker redirect us when selecting an option
		$('.breadcrumbContainer select').change(function() {
		    var optionValue = $(this).val();
		    if (optionValue != "") {
		        window.location.href = $(this).val();
		    }
		});

	    // Pre-pending <hr> (seperator) tag to <h2> tags.

		var firstHr = true;
		$('.main-content h2').each(function() {
			if (!firstHr) {
				$(this).before('<div class="separator"><div class="separator-up"></div><div class="separator-middle"></div><div class="separator-down"></div></div>');
			} else {
				firstHr = false;
			}
		});

	});

// create a copy button for code snippets
function copyCode(e) { 
	e.preventDefault; 
	var $button = jQuery(this); 
	var copied = $button.attr('data-copied'); 
 
	MadCap.Utilities.CopyToClipboard($('pre', $button.parents('.code-wrapper'))); 
	if (copied) { 
		jQuery('.code-wrapper a.copy-button').html('Copy'); 
		$button.html(copied); 
	} 
} 


/*
 * Returns the footer height inside the visible window area.
 */
function getFooterHeight() {
	var $el = $('div.rate-this-topic')
	scrollTop = $(this).scrollTop()
	scrollBot = scrollTop + $(this).height()
	elTop = $el.offset().top
	elBottom = elTop + $el.outerHeight() + $('#footer-placeholder').outerHeight()
	visibleTop = elTop < scrollTop ? scrollTop : elTop
	visibleBottom = elBottom > scrollBot ? scrollBot : elBottom
	footerHeight = visibleBottom - visibleTop
	return (footerHeight > 0) ? footerHeight:0
}

/*
 * Returns content area height.
 */
function getContentAreaHeight() {
	var windowHeight = $(window).height()
		breadCrumbHeight = $(".breadcrumbContainer").height()
		footerHeight = getFooterHeight()
		contentAreaHeight = windowHeight-breadCrumbHeight-footerHeight-60;
	return (contentAreaHeight > 0)?contentAreaHeight:0;
}