
$(window).load(function() {
  "use strict";
    /* ==============================================
    PRELOADER
    =============================================== */
    var preloaderDelay = 800;
    var preloaderFadeOutTime = 1000;

    function hidePreloader() {
        var loadingAnimation = $('#loading-animation');
        var preloader = $('.main');

        loadingAnimation.fadeOut();
        preloader.delay(preloaderDelay).fadeOut(preloaderFadeOutTime, function() {
          jQuery('.animate').waypoint(function() {
               var animation = jQuery(this).attr("data-animate");
               jQuery(this).addClass(animation);
               jQuery(this).addClass('animated');
          }, { offset: '80%' });
         });
    }

    hidePreloader();

});

/* DOCUMENT READY  ----------- */
jQuery(document).ready(function() {

"use strict";

/*Removed call to video for checkout pages*/


/* ==============================================
    BRI SCROLL PAGE WITH EASING EFFECT
    =============================================== */
  $('#getstarted, .arrow-down-liteBlue').bind('click', function(e) {
      e.preventDefault();
      var target = this.hash;
      $.scrollTo(target, 750, {
        easing: 'easeInSine',
        axis: 'y',
        offset: -100
      });
  });
	$('.arrow-down-medBlue, .arrow-down-orange, .arrow-down-blue, .arrow-down-green-data, #webcam-hotzone, #webcam-features-scroll').on('click', function(e) {
		e.preventDefault();
		var target = this.hash;
		$('html, body').animate({
			scrollTop: $(target).offset().top - 240
		}, 750, 'easeInSine');
	});
	$('#vps-plans-block-scroll').on('click', function(e) {
		e.preventDefault();
		var target = this.hash;
		$('html, body').animate({
			scrollTop: $(target).offset().top - 140
		}, 750, 'easeInSine');
	});
  $('.scrollit-pricing').bind('click', function(e) {
      e.preventDefault();
      var target = this.hash;
      $.scrollTo(target, 750, {
        easing: 'easeInSine',
        axis: 'y',
        offset: -220
      });
  });

  /* ==============================================
    ACTIVE LINKS ON NAVIGATION BAR
    =============================================== */
  jQuery('body').scrollspy({ offset: 300,target: 'nav' })

  /* ==============================================
    TESTIMONIALS SLIDER
    =============================================== */
	var owl = $("#testimonials-slides");
	owl.owlCarousel({
		loop:true,
		nav : true, // Show next and prev buttons
		smartSpeed : 600,
		//dotsSpeed : 800,
		//singleItem:true,
		slideBy:1,
		items : 1,
		navText : ["<span class='testimonial-icon' aria-hidden='true' data-icon='&#x3c;'></span>", "<span class='testimonial-icon' aria-hidden='true' data-icon='&#x3d;'></span>"],
		dots : false
	});
	/* ==============================================
    DASHBOARD SLIDER
    =============================================== */
	var owl = $("#dashboard-demo");
	owl.owlCarousel({
		loop:true,
		nav : true, // Show next and prev buttons
		smartSpeed : 600,
		//dotsSpeed : 800,
		//singleItem:true,
		slideBy:1,
		items : 1,
		navText : ["<span class='dashboard-icon' aria-hidden='true' data-icon='&#x3c;'></span>", "<span class='dashboard-icon' aria-hidden='true' data-icon='&#x3d;'></span>"],
		dots : false,
        margin:15,
        autoHeight:true,
        navContainer: "#navit",
		onInitialized : function(){
        var activeImg = $('.owl-carousel').find('.active').find('img');
        //var comment = activeImg.attr('alt');
        var title = activeImg.attr('title');
        $('#image-caption').html('<h3>'+title+'</h3>');
    }

	});

	owl = $('#dashboard-demo').owlCarousel();
		$(".prev").click(function () {
    	owl.trigger('prev.owl.carousel');
	});
	$(".next").click(function () {
    	owl.trigger('next.owl.carousel');
	});

	owl.on('translated.owl.carousel', function(event) {
    	var activeImg = $(this).find('.active').find('img');
    	//var comment = activeImg.attr('alt');
    	var title = activeImg.attr('title');
    	$('#image-caption').html('<h3>'+title+'</h3>');

	});


    $('.popup-link, .popup-link-inception').magnificPopup({
  		type: 'ajax',
    	fixedContentPos: false,
    	fixedBgPos: true,
 		overflowY: 'auto',
		closeBtnInside: true,
    	preloader: false,
    	midClick: true,
    	removalDelay: 500,
   		mainClass: 'my-mfp-slide-bottom'

  	});



 /*POPUP WITHIN A POPUP ON WEBCAM SOFTWARE PAGE: (GET STARTED button in popup webcam sample popups), the faux button in the video sample popup closes the popup and prompts a click on .popup-link-incenption#faux-popup to open second form popup. Also applied to the Standard and Premium Feature Popups.*/

    $(document).on('click', '#myButton', function(e){
     	e.preventDefault();

		$.magnificPopup.close();

    	window.setTimeout(openNewAjaxPopup, 501);
	});

  	function openNewAjaxPopup() {
  		$( ".popup-link-inception#faux-popup" ).trigger( "click" );
	}
	$(document).on('click', '#myButton2', function(e){
     	e.preventDefault();

		$.magnificPopup.close();

    	window.setTimeout(openNewAjaxPopup2, 501);
	});

  	function openNewAjaxPopup2() {
  		$( ".popup-link-inception#faux-popup2" ).trigger( "click" );
	}
	//Security Services Signup Button
	$(document).on('click', '#SS-SignupButton', function(e) {
		e.preventDefault();
		$.magnificPopup.close();
		window.setTimeout(openSAQPopup, 501);
	});

	function openSAQPopup() {
		$(".popup-link-inception[href='/pop_security_form']").trigger("click");
	}

// Map real targets -> faux link IDs
var chainMap = {
  // Standard features
  '/pop_timelapse':              '#faux-timelapse',
  '/pop_webcamstats':            '#faux-webcamstats',
  '/pop_ptz':                    '#faux-ptz',
  '/pop_webcamrecording':        '#faux-webcamrecording',
  '/pop_webcam_security':        '#faux-webcam-security',
  '/pop_scheduler':              '#faux-scheduler',
  '/pop_smartpull_streaming':    '#faux-smartpull',
  '/pop_playlist':               '#faux-playlist',
  '/pop_webcam_signup_options':  '#faux-webcam-signup',

  // Premium features
  '/pop_white-label':            '#faux-white-label',
  '/pop_super_timelapse':        '#faux-super-timelapse',
  '/pop_live_rewind':            '#faux-live-rewind',
  '/pop_vod':                    '#faux-vod',
  '/pop_playlist':               '#faux-prerolls',      // differentiate from standard playlist if needed
  '/pop_cdn_streaming':          '#faux-cdn-streaming'
};


// Standard and Premium Feature Popups with popups function to close popup and open faux popups
$(document).on('click', '.mfp-content .feature-popup a.popup-link, .mfp-content .feature-popup a.popup-link-inception', function (e) {
  if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;

  var href = this.getAttribute('href');
  if (!href) return;

  // Only intercept if we have a faux mapping for this link
  var fauxSel = chainMap[href];
  if (!fauxSel) return;

  e.preventDefault();

  // Close current popup
  $.magnificPopup.close();

  // Then "click" the already-initialized hidden faux link (same #myButton pattern)
  window.setTimeout(function () {
    var $faux = $(fauxSel);
    if ($faux.length) {
      $faux.trigger('click');
    } else {
      // Optional: warn if mapping exists but faux anchor missing
      if (window.console) console.warn('Popup chain faux link not found:', fauxSel);
    }
  }, 501); // match removalDelay/animation
});

  /* ==============================================
    TOOLTIPS AND CHECKOUT PROGRESS, WATCHA WATCHIN, CART POPUP SLIDER
    =============================================== */

 	 $('[data-toggle="tooltip"]').tooltip({container: 'body'});

     $('#checkout-progress .nav-pills > li.checkit > a > span.stepit').html("<span data-icon='&#x4e;' aria-hidden='true'></span>");

  	 $('#whatcha-watchin').click(function(){
    $('#whatcha-watchin-block').slideDown('1500', "swing", function () {
        // Animation complete.
    });
	});

    $('#whatcha-watchin-block .close-this').click(function(){
    $('#whatcha-watchin-block').slideUp('1500', "swing", function () {
        // Animation complete.
    });
	});

        
    // Cart UI: Set the cart-drop down to drop down after 1 second and retract after 2 more seconds with animation duration of 1.5 seconds.
    
    setTimeout(function() {
    $('#cart-popup-block').slideDown('1500', "swing", function () {
        // Animation complete.
        	setTimeout(function() {
      		// Do something after 5 seconds
     	 		$('#cart-popup-block').slideUp('1500', "swing", function () {
        		// Animation complete.
    			});
            }, 2000);
    });
    }, 1000);
        
    
    $('#cart-popup').click(function(){
    $('#cart-popup-block').slideDown('1500', "swing", function () {
        // Animation complete.
    });
	});

    $('#cart-popup-block .close-this').click(function(){
    $('#cart-popup-block').slideUp('1500', "swing", function () {
        // Animation complete.
    });
	});



  /* ==============================================
    /* LOAD THE ANIMATIONS IF THE BROWSER IS NOT IE
  ================================================== */
  if( !device.tablet() && !device.mobile() ) { //Load the animations if the device is not a mobile or tablet
    $('head').append('<!--[if !IE]><!--><link rel="stylesheet" type="text/css" media="screen" href="/css/animate.css"><!--<![endif]-->');
  }


}); /* END DOCUMENT READY  ----------- */
