$(document).ready(function() {

  $("#wo-safetorrents").mouseover(function () { $("#video-preview a").removeClass("active"); $(this).addClass("active"); $("#video-preview").attr("class", "wo-safetorrents"); });
  $("#w-safetorrents").mouseover(function () { $("#video-preview a").removeClass("active"); $(this).addClass("active"); $("#video-preview").attr("class", "w-safetorrents"); });

  $("#login_form").compact_form();

  $('.login-link').hover(
      function () {
        $(this).addClass('active');

        loginLinkOffset = $('.login-link').position();
        loginLinkTop = loginLinkOffset.top + $('.login-link').outerHeight() - 1;

        $('#login_form').css('left', loginLinkOffset.left +'px');
        $('#login_form').css('top', loginLinkTop +'px');
        $('#login_form').css('background-position', ($('.login-link').outerWidth() - 2) +'px top');

        $('#login_form').show();
      },
      function () {
        $(this).removeClass('active');
        $('#login_form').hide();
      }
  );

  $('.login-link > a').click(function () {return false;});

  $('.pricing td').mouseenter(
    function () {
      panel = $('.panel');
      panel.animate({
        left: parseInt($(this).position().left) - (panel.width() - $(this).width())/2 - 4
      }, 100);
    }
  );

  $('.pricing thead td:eq(1)').mouseenter();

  if ($('.popup-login-form')) {
    $('body').click(function (e) {
      if ($('.popup-login-form:visible').length && !$('.popup-login-form:animated').length && !$(e.target).parents('.popup-login-form').length && !$(e.target).parents('.download-now-landing-button').length && !$(e.target).is('.popup-login-form'))
        $('.popup-login-form').fadeOut();
    });
  }

  $('.download-now-landing-button').click(function () {
    $('.popup-login-form').fadeIn();
  });

  /* plimus page js */
  $('.pli-td-label, .pli-label').each(function () {this.innerHTML = this.innerHTML.replace(/\s?:/,'')});
  $('#pli-tr-phone input[name="workPhone"]').attr('size', '40');


  if ($('#pli-tr-first-name').length) {
    $('<input type="button" class="next-section" value="Continue" />').appendTo('form[name="tdForm"]').click(function () {
      if ($('#pli-tr-billing-description:visible').length)
        checkSecondStep(true);
      else
        checkFirstStep(true);
    });

    $('form[name="tdForm"]').submit(function () {
      _gaq.push(['t2._trackEvent', 'PaymentPageActions', 'FormSubmit']);
    });

    checkFirstStep(false);
    checkSecondStep(false);

  }

});

function checkFirstStep(isFireEvent) {
  if ($('#pli-tr-first-name input').val() && $('#pli-tr-last-name input').val() && $('#pli-tr-email input').val()) {
    $('#pli-tr-billing-description, #pli-tr-address-line-1, #pli-tr-city, #pli-tr-zip-code, #pli-tr-country, #pli-tr-phone').fadeIn();
    
    if (!$('#state').attr('disabled')) {
      $('#pli_states').show();
    }

    location.replace('#pli-tr-billing-description');

    _gaq.push(['t2._trackEvent', 'PaymentPageActions', 'UnfoldSection', 'Contacts']);
    
    return true;
  }
  else if (isFireEvent) {
    $('form[name="tdForm"]').validate().element( "#pli-tr-first-name input" );
    $('form[name="tdForm"]').validate().element( "#pli-tr-last-name input" );
    $('form[name="tdForm"]').validate().element( "#pli-tr-email input" );
  }
  
  return false;
}

function checkSecondStep(isFireEvent) {
  if ($('#pli-tr-address-line-1 input').val() && $('#pli-tr-city input').val() && $('#pli-tr-zip-code input').val() && $('#pli-tr-country input').val() && $('#pli-tr-phone input').val()) {
    $('.buynow .next-section').hide();
    $('#pli-tr-payment-method, #pli-tbl-credit-card, #nextButtonRow').fadeIn();
    
    _gaq.push(['t2._trackEvent', 'PaymentPageActions', 'UnfoldSection', 'CreditCard']);
    
    return true;
  }
  else if (isFireEvent) {
    $('form[name="tdForm"]').validate().element('#pli-tr-address-line-1 input');
    $('form[name="tdForm"]').validate().element('#pli-tr-city input');
    $('form[name="tdForm"]').validate().element('#pli-tr-zip-code input');
    $('form[name="tdForm"]').validate().element('#pli-tr-country input');
    $('form[name="tdForm"]').validate().element('#pli-tr-phone input');
  }
  
  return false;
}


