resize = function() {
  // PROMO KIT //
  bodegonWidth = $('#bodegon-carousel').width();
  bodegonHeight = bodegonWidth * 43.97 / 100;
  $('#bodegon-carousel').css('height', bodegonHeight.toFixed());

  planocasaWidth = $('.plano-casa').width();
  planocasaHeight = $('.plano-casa').height();
  inumHeight = $('.plano .inum').height();

  i1x = planocasaWidth * 50.00 / 100 - inumHeight;
  i1y = planocasaHeight * 12.52 / 100 - inumHeight;

  i2x = planocasaWidth * 24.95 / 100 - inumHeight;
  i2y = planocasaHeight * 36.64 / 100 - inumHeight;

  i3x = planocasaWidth * 39.93 / 100 - inumHeight;
  i3y = planocasaHeight * 92.05 / 100 - inumHeight;

  i4x = planocasaWidth * 60.53 / 100 - inumHeight;
  i4y = planocasaHeight * 54.63 / 100 - inumHeight;

  i5x = planocasaWidth * 81.94 / 100 - inumHeight;
  i5y = planocasaHeight * 67.54 / 100 - inumHeight;

  //alert(i1x.toFixed() +' '+ i1y.toFixed());
  //$('.i1 .popover').css({ "margin-top" : -i1x.toFixed(), "margin-left" : -i1y.toFixed() });

  fotodetectoresHeight = $('#bodegon-carousel .carousel-indicators li.item-2').height() + 40;
  $('#bodegon-carousel .carousel-inner-wrap').css('top', fotodetectoresHeight);

  itemimgWidth = $('.item-1 .item-img').width() * 60 / 100;
  $('#bodegon-carousel .item-img').css('height', itemimgWidth.toFixed());

};

$(window).resize(resize);
$(document).ready(resize);

$(document).ready(function() {
  "use strict";

  /* LOADER  */
  $(window).on('load', function() {
    $(".loader-inner").fadeOut();
    $(".loader").fadeOut("slow");
  });

  /* NAVBAR COLLAPSE ON SCROLL */
  $(window).on('scroll', function(){
    var b = $(window).scrollTop();
    if( b > 60 ){
      $(".navbar").addClass("top-nav-collapse");
    } else {
      $(".navbar").removeClass("top-nav-collapse");
    }
  });

  /* PAGE SCROLLING FEATURE */
  $('a.smooth-scroll').on('click', function(event) {
    var $anchor = $(this);
    $('html, body').stop().animate({
      scrollTop: $($anchor.attr('href')).offset().top - 100
    }, 1500, 'easeInOutExpo');
    event.preventDefault();
  });

  /* BACK TO TOP BUTTON */
  var offset = 200,
  offset_opacity = 1200,
  scroll_top_duration = 700,
  $back_to_top = $('.top');

  $(window).on('scroll', function() {
    ($(this).scrollTop() > offset ) ? $back_to_top.addClass('is-visible') : $back_to_top.removeClass('is-visible fade-out');
    if($(this).scrollTop() > offset_opacity) {
      $back_to_top.addClass('fade-out');
    }
  });
  //smooth scroll to top
  $back_to_top.on('click', function(event){
    event.preventDefault();
    $('body,html').animate({
      scrollTop: 0
      }, scroll_top_duration
    );
  });

  /* WOW ANIMATIONS */
  new WOW().init();

  /* HIDE MOBILE MENU AFTER CLICKING  */
  $('.navbar-nav>li>a:not(#dLabel)').on('click', function(){
    $('#navbar-collapse').removeClass("in").addClass("collapse"); 
  });

  /* BOOTSTRAP FIX FOR IE10 in Windows 8 and Windows Phone 8 */
  if(navigator.userAgent.match(/IEMobile\/10\.0/)) {
    var msViewportStyle = document.createElement('style');
    msViewportStyle.appendChild(document.createTextNode('@-ms-viewport{width:auto!important}'));
    document.querySelector('head').appendChild(msViewportStyle);
  }

  var magnific = $('.popup-youtube');
  if(magnific.length > 0){
    magnific.magnificPopup({
      //disableOn: 700,
      type: 'iframe',
      mainClass: 'mfp-fade',
      removalDelay: 160,
      preloader: false,
      fixedContentPos: false
    });
  } 

  $('[data-toggle="popover"]').on('click', function(e) { 
    e.preventDefault(); return true; 
  });

});

$(function(e) {
  e.preventDefault;

  $('.i1 a').popover({
    html: true,
    trigger: "click",
    placement: "bottom",
    container: ".plano",
    title: function(){
      return $(this).data('title')+'<span class="close">&times;</span>';
    }
  });
  $('.i2 a').popover({
    html: true,
    trigger: "click",
    placement: "right",
    container: ".plano",
    title: function(){
      return $(this).data('title')+'<span class="close">&times;</span>';
    }
  });
  $('.i3 a').popover({
    html: true,
    trigger: "click",
    placement: "top",
    container: ".plano",
    title: function(){
      return $(this).data('title')+'<span class="close">&times;</span>';
    }
  });
  $('.i4 a').popover({
    html: true,
    trigger: "click",
    placement: "left",
    container: ".plano",
    title: function(){
      return $(this).data('title')+'<span class="close">&times;</span>';
    }
  });
  $('.i5 a').popover({
    html: true,
    trigger: "click",
    placement: "top",
    container: ".plano",
    title: function(){
      return $(this).data('title')+'<span class="close">&times;</span>';
    }
  });

  $('body').on('hidden.bs.popover', function (e) {
    $(e.target).data("bs.popover").inState.click = false;
  });

  $('.inum a').on('shown.bs.popover', function(e){
    var popover = $(this);
    $(this).parent().parent().parent().find('div.popover .close').on('click', function(){
      popover.popover('hide');
    });
  });

  // 1
  $('.i1 a').on('shown.bs.popover', function() {
    $('.i1').addClass("i-active");
    $('#ic1').addClass('ic-active');
    $('#i1-carousel').carousel({ interval: 4000 });
  });
  $('.i1 a').on('hidden.bs.popover', function() {
    $('.i1').removeClass("i-active");
    $('#ic1').removeClass('ic-active');
  });
  $('#ic1').hover(function(){     
    $('.i1').toggleClass('i-hovered');
  });
  $('.i1').hover(function(){     
    $(this).toggleClass('i-hovered');
  });

  // 2
  $('.i2 a').on('shown.bs.popover', function() {
    $('.i2').addClass("i-active");
    $('#ic2').addClass('ic-active');
    $('#i2-carousel').carousel({ interval: 4000 });
  });
  $('.i2 a').on('hidden.bs.popover', function() {
    $('.i2').removeClass("i-active");
    $('#ic2').removeClass('ic-active');
  });
  $('#ic2').hover(function(){     
    $('.i2').toggleClass('i-hovered');
  });
  $('.i2').hover(function(){     
    $(this).toggleClass('i-hovered');
  });

  // 3
  $('.i3 a').on('shown.bs.popover', function() {
    $('.i3').addClass("i-active");
    $('#ic3').addClass('ic-active');
    $('#i3-carousel').carousel({ interval: 4000 });
  });
  $('.i3 a').on('hidden.bs.popover', function() {
    $('.i3').removeClass("i-active");
    $('#ic3').removeClass('ic-active');
  });
  $('#ic3').hover(function(){     
    $('.i3').toggleClass('i-hovered');
  });
  $('.i3').hover(function(){     
    $(this).toggleClass('i-hovered');
  });

  // 4
  $('.i4 a').on('shown.bs.popover', function() {
    $('.i4').addClass("i-active");
    $('#ic4').addClass('ic-active');
    $('#i4-carousel').carousel({ interval: 4000 });
  });
  $('.i4 a').on('hidden.bs.popover', function() {
    $('.i4').removeClass("i-active");
    $('#ic4').removeClass('ic-active');
  });
  $('#ic4').hover(function(){     
    $('.i4').toggleClass('i-hovered');
  })
  $('.i4').hover(function(){     
    $(this).toggleClass('i-hovered');
  });

  // 5
  $('.i5 a').on('shown.bs.popover', function() {
    $('.i5').addClass("i-active");
    $('#ic5').addClass('ic-active');
    $('#i5-carousel').carousel({ interval: 4000 });
  });
  $('.i5 a').on('hidden.bs.popover', function() {
    $('.i5').removeClass("i-active");
    $('#ic5').removeClass('ic-active');
  });
  $('#ic5').hover(function(){     
    $('.i5').toggleClass('i-hovered');
  });
  $('.i5').hover(function(){     
    $(this).toggleClass('i-hovered');
  });

  // close previously opened popovers by clicking outside them
  $(document).on('click', function(e) {
    $('.inum a').each(function() {
      if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
        $(this).popover('hide');
      }
    });
    if(!$(e.target).is('.i1 a') && $('.popover').has(e.target).length === 0){
      $('.i1').removeClass('i-active');
      $('#ic1').removeClass("ic-active");
    }
    if(!$(e.target).is('.i2 a') && $('.popover').has(e.target).length === 0){
      $('.i2').removeClass('i-active');
      $('#ic2').removeClass("ic-active");
    }
    if(!$(e.target).is('.i3 a') && $('.popover').has(e.target).length === 0){
      $('.i3').removeClass('i-active');
      $('#ic3').removeClass("ic-active");
    }
    if(!$(e.target).is('.i4 a') && $('.popover').has(e.target).length === 0){
      $('.i4').removeClass('i-active');
      $('#ic4').removeClass("ic-active");
    }
    if(!$(e.target).is('.i5 a') && $('.popover').has(e.target).length === 0){
      $('.i5').removeClass('i-active');
      $('#ic5').removeClass("ic-active");
    }
  });


  if($("#libro-de-reclamaciones").length) {
    $("input[name='documento_numero']").keydown(function(e){
      if(e.keyCode == 32){
        return false;
      }
    });

    $("input[name='documento_numero']").on('input', function() {
      $(this).val($(this).val().replace(/[^0-9]/g, ''));
    });

    $('#documento_tipo').change(function(){
      var $numDoc = $('#documento_numero');
      if($(this).val() == 'DNI'){ 
        $numDoc.attr('maxlength','8');
      } else {
        $numDoc.attr('maxlength','12');
      }
    });

    // var disableSubmit = false;
    // $('.wpcf7-submit').click(function() {
    //   $(this).val("Enviando...");
    //   if (disableSubmit == true) {
    //     $(this).prop("disabled",true);
    //     return false;
    //   }
    //   disableSubmit = true;
    //   return true;
    // });

    // document.addEventListener( 'wpcf7submit', function( event ) {
    //   $('.wpcf7-submit').val("Enviar");
    //   disableSubmit = false;
    // }, false );

    //var ticket = $('input[name=ticket]').val();
    var ticket = $('input#ticket').val();
    document.addEventListener( 'wpcf7mailsent', function( event ) {
      location = '/libro-de-reclamaciones/gracias/?ticket='+ ticket;
    }, false );
  }
  // END LIBRO DE RECLAMACIONES

  // LANDING UPSELLING
  if($("#upselling").length) {
    $("input[name='telefono']").keydown(function(e){
      if(e.keyCode == 32){
        return false;
      }
    });

    $("input[name='telefono']").on('input', function() {
      $(this).val($(this).val().replace(/[^0-9]/g, ''));
    });

    // var disableSubmit = false;
    // $('.wpcf7-submit').click(function() {
    //   $(this).val("Enviando...");
    //   if (disableSubmit == true) {
    //     $(this).prop("disabled",true);
    //     return false;
    //   }
    //   disableSubmit = true;
    //   return true;
    // });

    // document.addEventListener( 'wpcf7submit', function( event ) {
    //   $('.wpcf7-submit').val("Enviar");
    //   disableSubmit = false;
    // }, false );

    document.addEventListener( 'wpcf7mailsent', function( event ) {
      location = '/gracias/';
    }, false );
  }
  // LANDING UPSELLING
});