main.js

Download Link
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
 * Author: Matthijs Molhoek, 66Themes
 * URL: http://themeforest.net/user/66themes
 *
 * Project Name: Fragment -  WordPress Theme
 * Version: 1.0
 * Date: 27-08-2013
 * URL: -
 */

jQuery(document).ready(function ($) {
    /*global $:false */
    "use strict";

     // Init main header slides
     // SlitSlider by CoDrops | Plugin URL: http://tympanus.net/Tutorials/FullscreenSlitSlider/index2.html
     function initMainSlider() {
         var mainSlider = $( '.main-slider' ).slitslider({
            // transitions speed
            speed : 1500,
            // if true the item's slices will also animate the opacity value
            optOpacity : false,
            // amount (%) to translate both slices - adjust as necessary
            translateFactor : 230,
            // maximum possible angle
            maxAngle : 25,
            // maximum possible scale
            maxScale : 2,
            // slideshow on / off
            autoplay : true,
            // keyboard navigation
            keyboard : false,
            // time between transitions
            interval : 5000,
            // callbacks
            onBeforeChange : function( slide, idx ) { return false; },
            onAfterChange : function( slide, idx ) { return false; }
         });

         // Pause slider when out of view for performance
         window.onscroll = function() {
             if(mainSlider !== undefined) {
                 var targetOffset = $(".wrap").offset().top-200;
                 ($(window).scrollTop() <= targetOffset) ? (mainSlider.play()) : (mainSlider.pause());
             }
         };
     }


     // Toggle nav-active class, show/hide main menu
     $(document).on("click", ".nav-trigger, .main-nav .button-close a", function(){
        $('body').toggleClass('nav-active');
     });

     // Convert menu item URLs to fragment hashtags
     $('.main-nav .menu-item-object-page a').each(function(){
        if (!$(this).parent().hasClass("modal-page-load")) {
            var url = $(this).attr('href');
            var array = url.split('/');

            var lastsegment = array[array.length-2];

            $(this).attr('href', '#section-'+lastsegment);
        }
     });

     // Scroll to section on nav item click
     $(document).on("click", '.main-nav a[href*="#"]', function(e){
        if ( !$(this).parent().hasClass('social-icons') ) {
            $('body').toggleClass('nav-active');

            e.preventDefault();

            $('html, body').animate({
                scrollTop: $($(this).attr("href")).offset().top
            }, 1000);
        }
     });

     // Main header enter button click, move wrap to top
     $(document).on("click", ".state-enter", function(){
        $('html, body').animate({
            scrollTop: $(".wrap").offset().top - $('.top-bar').height()
        }, 1000);
     });

     // Move to top of tab content on tab item click
     $(document).on("click", ".tabs-nav a", function(e){
        e.preventDefault();

        $(this).on('shown', function (e) {
            $('html, body').stop().animate({
                scrollTop: $(this).closest('.container').find('.tab-content').offset().top - $('.top-bar').height()
            }, 200, function(){
                // Refresh waypoints
                $.waypoints('refresh');

                // Refresh Page ScrollSpy
                $('[data-spy="scroll"]').each(function () {
                    var $spy = $(this).scrollspy('refresh');
                });
            });
        });
     });

     // Remove empty p tags
     $('p:empty').remove();

     // Handle appear event for animated elements
     var wpOffset = 80;
     if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
        wpOffset = 100;
     }
     $.fn.waypoint.defaults = {
        context: window,
        continuous: true,
        enabled: true,
        horizontal: false,
        offset: 0,
        triggerOnce: false
     };
     $('.animated').waypoint(function(direction) {
        var elem = $(this);
        var animation = elem.data('animation');
        if ( !elem.hasClass('visible') && elem.attr('data-animation') !== undefined ) {
            if ( elem.attr('data-animation-delay') !== undefined ) {
                var timeout = elem.data('animation-delay');

                setTimeout(function(){
                    elem.addClass( animation + " visible" );
                }, timeout);
            } else {
                elem.addClass( elem.data('animation') + " visible" );
            }
        }
     }, { offset: wpOffset+'%' });

     // HTML5 Placeholder attribute polyfill for non-supporting browsers(IE)
     $('input, textarea').placeholder();

     // Init Bootstrap tabs visible for sliders to init, will be hidden afterwards
     $('.tab-pane .flexslider').closest('.tab-pane').css('display', 'block');

     // Add odd class to odd counted section wraps
     $(".section-wrap:odd").addClass('odd');

     // AJAX page load for Portfolio/Blog and whatever needs to be loaded
     $(document).on("click", ".portfolio-link, .article-link, .more-link, .gallery-item a, .page-links a, .modal-page-load", function(e) {
        // Disable default anchor beviour
        e.preventDefault();

        // Disable body scrolling
        $('body').addClass('modal-active');

        // Remove Content from Modal
        $('.modal-content').html();

        // Set loading state for page modal
        $('.modal-page').addClass('modal-loading');

        // Create and show loading spinner
        var spinnerOpts = {
            lines: 9, // The number of lines to draw
            length: 0, // The length of each line
            width: 5, // The line thickness
            radius: 11, // The radius of the inner circle
            corners: 1, // Corner roundness (0..1)
            rotate: 0, // The rotation offset
            direction: 1, // 1: clockwise, -1: counterclockwise
            color: '#000', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: true, // Whether to use hardware acceleration
            className: 'spinner', // The CSS class to assign to the spinner
            zIndex: 2e9, // The z-index (defaults to 2000000000)
            top: '0', // Top position relative to parent in px
            left: '0' // Left position relative to parent in px
        };
        var modalSpinner = new Spinner(spinnerOpts).spin( $('.modal-spinner')[0] );

        // Load page into modal
        $('.modal-page .modal-content').load($(this).attr('href'), function() {
            // Load success function

            // Remove loading state for page modal
            $('.modal-page').removeClass('modal-loading').addClass('modal-loaded');

            // Remove Spinner
            $('.modal-spinner').html('');

            initFlexSliders();

            cbpBGSlideshow.init();

            // Create and show slideshow loading spinner
            var spinnerOpts = {
                lines: 9, // The number of lines to draw
                length: 0, // The length of each line
                width: 10, // The line thickness
                radius: 11, // The radius of the inner circle
                corners: 1, // Corner roundness (0..1)
                rotate: 0, // The rotation offset
                direction: 1, // 1: clockwise, -1: counterclockwise
                color: '#fff', // #rgb or #rrggbb
                speed: 1, // Rounds per second
                trail: 60, // Afterglow percentage
                shadow: false, // Whether to render a shadow
                hwaccel: true, // Whether to use hardware acceleration
                className: 'spinner', // The CSS class to assign to the spinner
                zIndex: 99, // The z-index (defaults to 2000000000)
                top: '0', // Top position relative to parent in px
                left: '0' // Left position relative to parent in px
            };
            var slideshowSpinner = new Spinner(spinnerOpts).spin( $('.portfolio-slider')[0] );

            $('#submit-button').addClass('button');

            var commentform=$('#commentform'); // find the comment form
            commentform.prepend('<div class="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
            var statusdiv = $('.comment-status'); // define the infopanel

            // Validate the contact form
            commentform.validate({
                submitHandler: function(form) {
                //serialize and store form data in a variable
                var formdata = commentform.serialize();
                //Add a status message
                statusdiv.html('<p>Processing...</p>');
                //Extract action URL from commentform
                var formurl = commentform.attr('action');
                //Post Form with data
                $.ajax({
                    type: 'post',
                    url: formurl,
                    data: formdata,
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        console.log(textStatus, errorThrown);
                        statusdiv.html('<p class="ajax-error">You might have left one of the fields blank, or be posting too quickly</p>');
                    },
                    success: function(data, textStatus){
                        if(data == "success")
                            statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
                        else
                            statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
                            commentform.find('textarea[name=comment]').val('');
                    }
                });
                return false;
                }
            });
        });
     });
     $('#submit-button').addClass('button');


     // Reply comment button click
     $(document).on("click", ".comment-reply-link", function(e) {
        var elem = $(this);
        var commentsContainer = elem.closest('.comment-list');
        var commentsCollect = commentsContainer.find('li');
        var selectedComment = elem.closest('.comment');
        var commentIndex = selectedComment.attr("id");
        var selectedCommentAuthor = selectedComment.find('.comment-author').text();
        var respondContainer = elem.closest('.comments').find('#respond');

        respondContainer.find('#reply-title').text('Leave a reply to ' + selectedCommentAuthor);
        respondContainer.find('#comment_parent').val(commentIndex);

        return false;
     });


     // Modal close button click
     $(document).on("click", ".modal-top-bar .button-close", function(e) {
        // Disable default anchor beviour
        e.preventDefault();
        $(this).closest('.modal-page').removeClass('slider-active');

        $('.modal-content').html('');

        // Enable body scrolling
        $('body').removeClass('modal-active');
        $('.modal-page').removeClass('modal-loading modal-loaded');

        // Refresh waypoints
        $.waypoints('refresh');
     });


     // Modal content switcher click
     $(document).on("click", ".modal-page .content-switcher a", function(e) {
        var clickedElem = $(this);
        var containerModal = clickedElem.closest('.modal-page');

        // Disable default anchor beviour
        e.preventDefault();

        clickedElem.closest('.content-switcher').find('.active').removeClass('active');
        clickedElem.addClass('active');

        if( clickedElem.hasClass('switcher-slider') ) {
            containerModal.find('.portfolio-slider').addClass('visible');

            containerModal.addClass('slider-active');
        }
        else {
            containerModal.find('.portfolio-slider').removeClass('visible');

            containerModal.removeClass('slider-active');
        }
     });



     // Setup toggles
     $('.wrap').find('.toggle .toggle-content').stop().slideToggle(0);
     $('.wrap').find('.toggle-open').find('.toggle-content').stop().slideToggle(0);

     $('.toggle .toggle-header').click(function(){
        $(this).parent().toggleClass('toggle-open').find('.toggle-content').stop().slideToggle(200);
     });



     // Expandables, calculate and set height
     // Set height of any active exandable holders
     setupExpandable($('.expandable-active'));
     $(document).on("click", ".expandable-trigger", function(){
        setupExpandable( $(this) );
     });

     function setupExpandable(obj) {
        // Get all relevant elements
        var exHolder = obj.closest('.expandable-holder');
        var exElem = obj.closest('.expandable');
        var exContent = exElem.find('.expandable-content');

        // Deactivate all active expandables and remove holder margin
        $('.expandable-active').closest('.expandable-holder').attr('style', '').removeAttr('style');
        $('.expandable-active').toggleClass('expandable-active');

        exElem.toggleClass('expandable-active');

        // Get expandable holder and expandable content area height
        var exHolderHeight = exHolder.height();
        var exContentHeight = exContent.height();

        // Postion expandable content area below holder
        exContent.css('top', exHolderHeight + 'px');

        // Set expandable holder bottom offset
        exHolder.css('margin-bottom', exContentHeight+45 + 'px');
     }

     $(window).load(function(){
        // Init any defined slider
        // FlexSlider by WooThemes | http://www.woothemes.com/flexslider/
        initFlexSliders();


        // Init main header slides
        // SlitSlider by CoDrops | Plugin URL: http://tympanus.net/Tutorials/FullscreenSlitSlider/index2.html
        initMainSlider();


        // Init Isotope
        initIsotope();


        // Setup Content Overlay
        $('.overlay-trigger').each(function(){
            $(this).setupContentOverlay();
        });


        // Refresh Page ScrollSpy
        $('[data-spy="scroll"]').each(function () {
            var $spy = $(this).scrollspy('refresh');
        });

        $('.button-status').removeClass('state-loading').addClass('state-enter');
     });

     $(window).resize(function(){
        reLayoutIsotope();

        $('.overlay-trigger').recalculateClip();

        // Refresh Page ScrollSpy
        $('[data-spy="scroll"]').each(function () {
            var $spy = $(this).scrollspy('refresh');
        });

        // Refresh waypoints
        $.waypoints('refresh');
     });

     jQuery.fn.setupContentOverlay = function() {
        // Store required data
        var triggerElem = $(this);
        var overlayId = $(this).data('trigger-overlay');
        var overlayElem = $('#' + overlayId);

        $(this).fadeIn(0, function(){
            // Trigger clip values [top, right, bottom, left]
            var clipRect =
            [
                triggerElem.position().top,
                triggerElem.position().left+triggerElem.outerWidth(),
                triggerElem.position().top+triggerElem.outerHeight(),
                triggerElem.position().left
            ];
            // Set clip rectangle for content overlay
            if ( !$('html').hasClass('cssanimations') ) {
                overlayElem.animate({
                    clip: 'rect('+ clipRect[0] +'px, '+ clipRect[1] +'px, '+ clipRect[2] +'px, '+ clipRect[3] +'px)'
                }, 400);
            } else {
                overlayElem.css('clip', 'rect('+ clipRect[0] +'px, '+ clipRect[1] +'px, '+ clipRect[2] +'px, '+ clipRect[3] +'px)');
            }
        }).hide().delay(400).fadeIn(500, function(){
            overlayElem.removeClass('overlay-active');
        });
    };

    // Enlarge overlay on trigger click
    $(document).on("click", ".overlay-trigger", function(){
        // Store required data
        var triggerElem = $(this);
        var overlayId = $(this).data('trigger-overlay');
        var overlayElem = $('#' + overlayId);
        var overlayElemContainer = overlayElem.closest('.section-divider');

        overlayElem.addClass('overlay-active');

        // Trigger clip values [top, right, bottom, left]
        var clipRect =
        [
            0,
            overlayElemContainer.outerWidth(),
            overlayElemContainer.outerHeight(),
            0
        ];
        $(this).fadeOut(500, function(){
            // Set clip rectangle for content overlay
            if ( !$('html').hasClass('cssanimations') ) {
                overlayElem.animate({
                    clip: 'rect('+ clipRect[0] +'px, '+ clipRect[1] +'px, '+ clipRect[2] +'px, '+ clipRect[3] +'px)'
                }, 400);
            } else {
                overlayElem.css('clip', 'rect('+ clipRect[0] +'px, '+ clipRect[1] +'px, '+ clipRect[2] +'px, '+ clipRect[3] +'px)');
            }
        });

        // If vimeo has autoplay, start playing!
        var videoElem = overlayElem.find('.vimeo-autoplay');
        if(videoElem.length > 0) {
            setTimeout(
                function playVimeo() {
                    var iframe = videoElem[0],
                    player = $f(iframe);
                    player.api('play');
                },
            900);
        }
    });

    // Minimize overlay on close click
    $(document).on("click", ".container-overlay .button-close", function(){
        var overlayElem = $(this).closest('.overlay-active');
        $(this).closest('.section-divider').find('.overlay-trigger').setupContentOverlay();

        // If vimeo has autoplay, pause playing!
        var videoElem = overlayElem.find('.vimeo-autoplay');
        if(videoElem.length > 0) {
            setTimeout(
                function pauseVimeo() {
                    var iframe = videoElem[0],
                    player = $f(iframe);
                    player.api('pause');
                },
            400);
        }
    });

    jQuery.fn.recalculateClip = function() {
        return this.each(function() {
            // Store required data
            var triggerElem = $(this);
            var overlayId = triggerElem.data('trigger-overlay');
            var overlayElem = $('#' + overlayId);
            var overlayElemContainer = overlayElem.closest('.section-divider');
            var clipRect;

            if(overlayElem.hasClass('overlay-active')) {
                // Trigger clip values [top, right, bottom, left]
                clipRect =
                [
                    0,
                    overlayElemContainer.outerWidth(),
                    overlayElemContainer.outerHeight(),
                    0
                ];
            }
            else {
                // Trigger clip values [top, right, bottom, left]
                clipRect =
                [
                    triggerElem.position().top,
                    triggerElem.position().left+triggerElem.outerWidth(),
                    triggerElem.position().top+triggerElem.outerHeight(),
                    triggerElem.position().left
                ];
            }

            // Set clip rectangle for content overlay
            overlayElem.css('clip', 'rect('+ clipRect[0] +'px, '+ clipRect[1] +'px, '+ clipRect[2] +'px, '+ clipRect[3] +'px)');
        });
     };

     function initFlexSliders() {
        $('.flexslider').flexslider({
            animation: "slide",
            nextText: "",
            prevText: "",
            keyboard: false,
            slideshow: false,
            smoothHeight: true,
            start: function(){
                $('.tab-content>.tab-pane, .pill-content>.pill-pane').attr('style', '').removeAttr('style');
                $.waypoints('refresh');
                reLayoutIsotope();

                // Refresh Page ScrollSpy
                $('[data-spy="scroll"]').each(function () {
                    var $spy = $(this).scrollspy('refresh');
                });
            },
            after: function(){
                // Init Isotope
                reLayoutIsotope();
            }
        });
    }

    function initIsotope() {
        // Init Portfolio masonry
        var $container = $('.portfolio-container');
        $container.isotope({
            resizable: false,
            masonry: {
                columnWidth: 1
            }
        });
        $(document).on("click", ".filter-portfolio a", function(e) {
            e.preventDefault();
            $(this).closest('.filters').find('a').removeClass('selected');
            $(this).toggleClass('selected');

            var selector = $(this).attr('data-filter');
            $('.portfolio-container').isotope({
                filter: selector,
                masonry: {
                    columnWidth: 1
                }
            }, function() {
                // Refresh waypoints
                $.waypoints('refresh');

                // Refresh Page ScrollSpy
                $('[data-spy="scroll"]').each(function () {
                    var $spy = $(this).scrollspy('refresh');
                });
            });

            return false;
        });


        // Init Portfolio masonry
        $('.entries-container').isotope({
            itemSelector : '.blog-entry'
        }, function() {
                // Refresh waypoints
                $.waypoints('refresh');

                // Refresh Page ScrollSpy
                $('[data-spy="scroll"]').each(function () {
                    var $spy = $(this).scrollspy('refresh');
                });
            });
    }

    function reLayoutIsotope() {
        $('.isotope').isotope( 'reLayout' );
    }

    // Contact form validation & AJAX submit
    // Create loading spinner
    var spinnerOpts = {
        lines: 9, // The number of lines to draw
        length: 0, // The length of each line
        width: 5, // The line thickness
        radius: 11, // The radius of the inner circle
        corners: 1, // Corner roundness (0..1)
        rotate: 0, // The rotation offset
        direction: 1, // 1: clockwise, -1: counterclockwise
        color: '#000', // #rgb or #rrggbb
        speed: 1, // Rounds per second
        trail: 60, // Afterglow percentage
        shadow: false, // Whether to render a shadow
        hwaccel: true, // Whether to use hardware acceleration
        className: 'spinner', // The CSS class to assign to the spinner
        zIndex: 2e9, // The z-index (defaults to 2000000000)
        top: '0', // Top position relative to parent in px
        left: '0' // Left position relative to parent in px
    };
    var targetSpinner = $('.spinner-holder').hide();
    var spinner = new Spinner(spinnerOpts).spin(targetSpinner[0]);

    // Validate the contact form
    var v = $("#contactForm").validate({
        submitHandler: function(form) {
            var feedbackElem = $(form).find('.feedback-text').hide();
            var spinnerElem = $(form).find('.spinner-holder').fadeIn(300);

            // If it validates, send the form
            jQuery(form).ajaxSubmit({
                //target: "#loader",
                success: function() {
                    // Fade out the spinner and show the succes text
                    spinnerElem.fadeOut(300, function(){
                        feedbackElem.fadeIn(300).html("Thank you for your message, we'll get back to you ASAP!");
                    });

                    $(form).find('input[type="submit"]').attr('disabled', 'disabled');
                }
            });

            return false;
        }
    });
});