﻿$(function () {

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(afterAjaxUpdate);
    $("body").delegate('#apply-overlay', 'click', removeOverlay);
    $('.btn-reapply').click(reapplyClick);
    // $('.get-started').click(gotoStep2);
    $('.btn-calculate_repayments').click(gotoStep3);
    $('#feature').delegate('.superbox', 'click', function (e) { e.preventDefault(); $('.fire-popup').trigger('click') });
    $("body").delegate("#superbox .final-apply", "click", finalapplyClick);

    /*$('#btn-guarantee_apply').click(function (e) {
        e.preventDefault();
        boxApplication();
    }); */

    if (window.location.hash == "#apply") {
        boxApplication();
    } else if (window.location.hash == "#reapply") {
        boxApplication();
       // gotoStep2();
    }
});


var gotoStep2 = function (e) {
    //e.preventDefault();
    $('#step-1').fadeOut(500);
}

var gotoStep3 = function () {
    $(this).removeClass('btn-calculate_repayments').addClass('btn-recalculate_repayments').closest('.form');
    $('#step-2-bg').fadeOut(500,moveCalculator);
}

var moveCalculator = function () {
    $('#calculator-wrapper').animate({
        left: '300px'
    },350);
}

var finalapplyClick = function () {
    var thisvalue = $(this).closest('#mobile-container').find('.mobile-send input').val();
    $('.mobile-send input').not('#superbox .mobile-send input').val(thisvalue);
    //alert($('.mobile-send input').not('#superbox .mobile-send input').val());
    $('#superbox .close a').trigger('click');
    $('.final-apply').not('#superbox #mobile-container .final-apply').trigger('click');
}

var removeOverlay = function () {
    $('#apply-overlay').fadeOut(300, destroyOverlay);
}

var destroyOverlay = function () {
    $('#apply-overlay').remove();
}



var afterAjaxUpdate = function () {
    // $.superbox();
    $(".datepicker input").datepicker({ showAnim: "fadeIn", dateFormat: "dd/mm/yy", minDate: 2, maxDate: 60 });
    if ($('#mobile-container .mobile-error').not('#superbox #mobile-container .mobile-error').length > 0) {
        $('#mobile-container p').html("Sorry, this mobile Number has already been registered with another SaveMyBacon account.").addClass("mobile-error-message");
        $('.fire-popup').trigger('click');
    } else {
        $('#mobile-container p').html("Please enter your mobile number and we will text you a unique verification code. Use this code to complete the application form.").removeClass("mobile-error");
    }
}

var boxApplication = function(){
    $('body').append('<div id="apply-overlay"></div>');
    $('#apply-overlay').css({
        opacity: 0
    }).fadeTo(350 , .60);
}

var reapplyClick = function () {
    //gotoStep2();
    $('body').append('<div id="apply-overlay"></div>');
    $('#apply-overlay').css({
        opacity: .55
    });
}


