﻿$(function () {

    $("button").button();

    $('#dlgReferFriend').dialog({
        autoOpen: false,
        height: 400,
        width: 375,
        modal: true,
        buttons: {
            'Send': SendReferralEmail,
            'Close': function () {
                $(this).dialog('close');
            }
        },
        close: function () {
            $('#lblReferralEmailResponse').hide();
        }
    });

    // Handles the contact submit buttion click
    $('#btnSignUp').click(function () {
        window.location = "/SignUp";
    });

    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function () {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({ 'width': biggestRow });
            $(this).find(".row:last").css({ 'margin': '0' });

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({ 'width': rowWidth });

        }
    }

    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }


    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 10, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 50, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' });
    $("ul#topnav li").hoverIntent(config);

});

function ReferFriend() {
    // Make sure all of the fields are cleared out
    $('#txtbxReferralUrl').val(location.href);
    $('#txtbxReferralName').val('');
    $('#txtbxReferralFromEmail').val('');
    $('#txtbxReferralEmail').val('');
    // Show the dialog
    $('#dlgReferFriend').dialog('open');
}

function SendReferralEmail() {
    if (Validate()) {
        // Get the values from the dialog
        var referralUrl = $('#txtbxReferralUrl').val();
        var name = escape($('#txtbxReferralName').val());
        var fromEmail = $('#txtbxReferralFromEmail').val();
        var referralEmail = $('#txtbxReferralEmail').val();
        // Call the controller to send the email
        $('#lblReferralEmailResponse').hide();

        $.ajax({
            type: "POST",
            url: "/Contact/SendReferralEmail",
            data: "url=" + referralUrl + "&name=" + name + "&referralEmail=" + referralEmail + "&fromEmail=" + fromEmail,
            dataType: "json",
            error: function (xhr, status, error) {
                // Show the error
                $('#lblReferralEmailResponse').attr('class', 'errormsg');
                $('#lblReferralEmailResponse').text(xhr.responseText);
                $('#lblReferralEmailResponse').attr('style', 'display:block;');
            },
            success: function (data, textSuccess) {
                // show the success message
                $('#lblReferralEmailResponse').attr('class', 'successmsg');
                $('#lblReferralEmailResponse').text("Referral email sent successfully.");
                $('#lblReferralEmailResponse').show();
                window.scrollTo(0, 0);
            }
        });
    }
}

function Validate() {
    $.validity.start();

    $("#txtbxReferralUrl").require();
    $("#txtbxReferralFromEmail")
        .require()
        .match('email');
    $("#txtbxReferralName").require();
    $("#txtbxReferralEmail")
        .require()
        .match('email');

    var result = $.validity.end();
    return result.valid;
}
