/* global Modernizr */

// config
require.config( {
	paths: {
		jquery:              'assets/js/fix.jquery',
		underscore:          'assets/js/fix.underscore',
		bootstrapAffix:      'bower_components/bootstrap-sass/assets/javascripts/bootstrap/affix',
		bootstrapAlert:      'bower_components/bootstrap-sass/assets/javascripts/bootstrap/alert',
		bootstrapButton:     'bower_components/bootstrap-sass/assets/javascripts/bootstrap/button',
		bootstrapCarousel:   'bower_components/bootstrap-sass/assets/javascripts/bootstrap/carousel',
		bootstrapCollapse:   'bower_components/bootstrap-sass/assets/javascripts/bootstrap/collapse',
		bootstrapDropdown:   'bower_components/bootstrap-sass/assets/javascripts/bootstrap/dropdown',
		bootstrapModal:      'bower_components/bootstrap-sass/assets/javascripts/bootstrap/modal',
		bootstrapPopover:    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/popover',
		bootstrapScrollspy:  'bower_components/bootstrap-sass/assets/javascripts/bootstrap/scrollspy',
		bootstrapTab:        'bower_components/bootstrap-sass/assets/javascripts/bootstrap/tab',
		bootstrapTooltip:    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/tooltip',
		bootstrapTransition: 'bower_components/bootstrap-sass/assets/javascripts/bootstrap/transition',
	},
	shim: {
		bootstrapAffix: {
			deps: [
				'jquery'
			]
		},
		bootstrapAlert: {
			deps: [
				'jquery'
			]
		},
		bootstrapButton: {
			deps: [
				'jquery'
			]
		},
		bootstrapCarousel: {
			deps: [
				'jquery'
			]
		},
		bootstrapCollapse: {
			deps: [
				'jquery',
				'bootstrapTransition'
			]
		},
		bootstrapDropdown: {
			deps: [
				'jquery'
			]
		},
		bootstrapPopover: {
			deps: [
				'jquery'
			]
		},
		bootstrapScrollspy: {
			deps: [
				'jquery'
			]
		},
		bootstrapTab: {
			deps: [
				'jquery'
			]
		},
		bootstrapTooltip: {
			deps: [
				'jquery'
			]
		},
		bootstrapTransition: {
			deps: [
				'jquery'
			]
		},
		jqueryVimeoEmbed: {
			deps: [
				'jquery'
			]
		}
	}
} );

require.config( {
	baseUrl: BuildPressVars.pathToTheme
} );

require( [
		'jquery',
		'underscore',
		'assets/js/SimpleMap',
		'assets/js/HeaderCarousel',
		'assets/js/StickyNavbar',
		'assets/js/TouchDropdown',
		'bootstrapCollapse',
		'bootstrapTooltip',
		// 'bootstrapTab',
		// 'jqueryVimeoEmbed'
	], function ( $, _, SimpleMap ) {
	'use strict';

	/**
	 * remove the attributes and classes from collapsible navbar when the window is resized
	 */
	$( window ).on( 'resize', _.debounce( function () {
		if ( Modernizr.mq( '(min-width: 992px)' ) ) {
			$( '#buildpress-navbar-collapse' )
				.removeAttr( 'style' )
				.removeClass( 'in' );
		}
	}, 500 ) );

	/**
	 * Maps
	 */
	$( '.js-where-we-are' ).each( function () {
		if ( window.google ) {
			new SimpleMap( $( this ), {
				latLng:  $( this ).data( 'latlng' ),
				markers: $( this ).data( 'markers' ),
				zoom:    $( this ).data( 'zoom' ),
				type:    $( this ).data( 'type' ),
				styles:  $( this ).data( 'style' ),
			}).renderMap();
		}
	} );

	/**
	 * Tooltips from BS
	 */
	$( '[data-toggle="tooltip"]' ).tooltip();


	/**
	 * The countdown widget.
	 */
	(function () {
		$( '.js-pt-countdown-widget' ).each( function( index, el ) {
			var endDate     = $( this ).data( 'timestamp' ) * 1000,
					currentDate = Date.now(),
					diff        = ( endDate - currentDate ) / 1000;

			// If the date has already expired.
			if ( 0 > diff ) {
				diff = 0;
			}

			// Instantiate the countdown timer.
			var clock = $( this ).FlipClock( diff, {
				clockFace: 'DailyCounter',
				autoStart: true,
				countdown: true
			} );

			// Hide labels for this countdown widget.
			if ( $( this ).data( 'hide-labels' ) ) {
				$( this ).find( '.flip-clock-label' ).hide();
			}
		} );
	}());
} );
