"use strict";

/**
 * Table of Contents
 *
 * meridian_one_slider_center ( center slider info )
 * meridian_one_services_tabs ( Initiate tabs for services )
 * meridian_one_services_tabs_center ( centers tab info )
 * meridian_one_google_map_height ( Google map )
 *
 * # Document Ready
 * # Window Load
 * # Window Resize
 */

/**
 * Center slider info
 */
function meridian_one_slider_center() {

	// Center textual part
	jQuery('#slider').each(function(){

		var thumbHeight = jQuery(this).height(),
		mainHeight = jQuery(this).find('.slide-info').outerHeight(),
		topOffset = 0;

		if ( thumbHeight > mainHeight ) {
			topOffset = mainHeight / 2;
			topOffset = topOffset  * -1;
			jQuery(this).find('.slide-info').css({ marginTop : topOffset });
		}

	});

}

/**
 * Initiate tabs for services
 */
function meridian_one_services_tabs() {

	// switching tabs
	jQuery(document).on( 'click', '.section-services-tabs-nav-item', function(e){

		e.preventDefault();

		var container = jQuery(this).closest('.section-services-main'),
		nav = container.find('.section-services-tabs-nav'),
		navItems = nav.find('.section-services-tabs-nav-item'),
		content = container.find('.section-services-tabs-content'),
		contentItems = content.find('.section-services-tabs-content-item');

		var nth = jQuery(this).index();

		// remove current active
		nav.find('.active').removeClass('active');
		content.find('.active').removeClass('active');

		// add new active
		navItems.eq(nth).addClass('active');
		contentItems.eq(nth).addClass('active');

		// center info
		meridian_one_services_tabs_center();

	});

	// create navigation
	jQuery('.section-services-tabs-nav').each(function(){

		var container = jQuery(this).closest('.section-services-main'),
		nav = jQuery(this),
		navItems = nav.find('.section-services-tabs-nav-item'),
		content = container.find('.section-services-tabs-content'),
		contentItems = content.find('.section-services-tabs-content-item');		

		container.find('.section-services-tabs-content-item').each(function(){

			var tabTitle = jQuery(this).find('.section-services-tabs-content-info-title').text(),
			tabIcon = jQuery(this).find('.section-services-tabs-content-info-icon').html();

			nav.append('<a href="#" class="section-services-tabs-nav-item"><span class="section-services-tabs-nav-item-icon">' + tabIcon + '</span><span class="section-services-tabs-nav-item-title">' + tabTitle + '</span></a>');

		});

		nav.find('.section-services-tabs-nav-item:first-child').trigger('click');

	});

}

/**
 * Center service tabs info
 */
function meridian_one_services_tabs_center(){

	jQuery('.section-services-tabs-content-info').each(function(){

		var info = jQuery(this),
		infoParent = info.closest('.section-services-tabs-content-item'),
		infoHeight = info.outerHeight(),
		infoParentHeight = infoParent.height(),
		heightDiff = infoParentHeight / 2 - infoHeight / 2;

		if ( heightDiff > 0 ) {
			info.css({ top : heightDiff });
		}

	});

}

/**
 * Fix Google map height
 */
function meridian_one_google_map_height() {

	if ( jQuery('.section-contact').length ) {

		var sectionHeight = jQuery('.section-contact-form').outerHeight();
		jQuery('.section-contact-map iframe').height( sectionHeight );

	}

}

/**
 * # Document Ready
 */
jQuery(document).ready(function($){

	jQuery('body').fitVids();

	meridian_one_services_tabs();
	meridian_one_google_map_height();

	// magnific popup for tour section video
	$('.section-tour-hook').magnificPopup({ type : 'iframe' });

	// Add number for process
	$('.section-process-items').each(function(){
		var count = 0;
		$(this).find('.widget').each(function(){
			count++;
			$(this).find('.section-process-item-number').html('0' + count);
		});
	});

	// Add a chevron icon to menu items with children
	$('#navigation #primary-menu > li:has(ul) > a').append('<span class="fa fa-chevron-down"></span>');

	// mobile nav arrows
	$('#mobile-navigation li:has(ul)').append('<span class="fa fa-plus"></span>');

	// mobile nav expand
	$(document).on( 'click', '#mobile-navigation li .fa', function(e){		

		// stop the anchor click event
		e.stopPropagation();

		// toggle class
		$(this).closest('li').toggleClass( 'expand' );

	});

	// mobile nav show/hide
	$(document).on( 'click', '.header-mobile-nav-hook', function() {
		$('#mobile-navigation').toggleClass('active');			
	});

	// smooth scroll to anchor
	$('a[href*="#"]:not([href="#"])').click(function() {

		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			
			var target = $(this.hash);
			var target_id = target.attr('id');
			target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
			if (target.length) {
				$('#navigation .current-menu-item').removeClass('current-menu-item');
				$('#navigation a[href="#' + target_id + '"]').closest('li').addClass('current-menu-item');
				$('#mobile-navigation').removeClass('active');
				$('html,body').animate({
					scrollTop: target.offset().top - $('.page-template-template-home-php #header').outerHeight()
				}, 1000);
				return false;
			}

		}

	});

});

/**
 * # Window Load
 */
jQuery(window).load(function(){

	meridian_one_slider_center();
	meridian_one_google_map_height();
	meridian_one_services_tabs_center();

});

/**
 * # Window Resize
 */
jQuery(window).resize(function(){

	meridian_one_slider_center();
	meridian_one_google_map_height();
	meridian_one_services_tabs_center();

});