﻿//////////////// SINGLETON ///////////////

var Help = new function ()
{

    this.rolling = false;

    this.onInit = function ()
    {
        $('#Help #HelpName').blur(function ()
        {
            if ($(this).val() == '')
            {
                $(this).addClass('errorIcon');
                $('#Help #ErrorMessage').fadeIn();
            } else
            {
                $(this).removeClass('errorIcon');
                $('#Help #ErrorMessage').fadeOut();
            }
        });

        $('#Help #HelpTelephone').blur(function ()
        {
            if ($(this).val() == '')
            {
                $(this).addClass('errorIcon');
                $('#Help #ErrorMessage').fadeIn();
            } else
            {
                $(this).removeClass('errorIcon');
                $('#Help #ErrorMessage').fadeOut();
            }
        });

        $('#Help #HelpEmail').blur(function ()
        {
            if ($(this).val() == '' || !isValidEmailAddress($(this).val()))
            {
                $(this).addClass('errorIcon');
                $('#Help #ErrorMessage').fadeIn();
            } else
            {
                $(this).removeClass('errorIcon');
                $('#Help #ErrorMessage').fadeOut();

            }
        });


        $('#Help').hoverIntent({
            over: this.onRollOver, 
            timeout: 100, 
            out: this.onRollOut 
        });

        $('#Help .close').click(this.onRollOut);

        $('#Help .request').click(this.onRequest);
        $('#Help .adapt').click(this.onAdapt);
        $('#Help .advice').click(this.onAdvice);
    }

    this.onRollOver = function ()
    {
        Help.rolling = false;

        $('#Help-Overlay').fadeIn();

        if ($('#Help-Sandbox').css('width') == '28px')
        {
            $('#Help-Sandbox').css('width', '483px');

            $('#Help').css('margin-left', '455px');

            $('#Help').animate({
                marginLeft: 0
            });
        }
    }

    this.onRollOut = function ()
    {
        if ($('#Help-Sandbox').css('width') == '483px' && !this.rolling)
        {
            Help.rolling = true;

            $('#Help-Overlay').fadeOut();

            $('#Help').animate({ marginLeft: 455 }, function () { Help.onComplete(); });
        }
    }

    this.onComplete = function ()
    {
        $('#Help-Sandbox').css('width', '28px');

        $('#Help').css('margin-left', '0px');

        Help.rolling = false;
    }

    this.onRequest = function ()
    {
        if ($('#Help #HelpName').val() != '' && $('#Help #HelpTelephone').val() != '' && $('#Help #HelpEmail').val() != '' && isValidEmailAddress($('#Help #HelpEmail').val()))
        {
            var mb = {
                email: $('#Help #HelpEmail').val(),
                name: $('#Help #HelpName').val(),
                telephone: $('#Help #HelpTelephone').val(),
                noMail: true
            };

            var mbData = JSON.stringify(mb);

            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: '/webservices/stairplanner.svc/project/save',
                dataType: 'json',
                success: function (data, success) { Help.onSaveComplete(data, success); },
                data: mbData
            });
        }
    }

    this.onAdapt = function ()
    {
        window.location = $('#Help #AdaptRedirect').val();
    }

    this.onAdvice = function ()
    {
        window.location = 'http://www.richardburbidge.com/help-advice/';
    }

    this.onSaveComplete = function (data, success)
    {
        if (success == 'success')
        {
            var obj = {
                member: data.memberId,
                email: 'Request Help'
            };

            var objData = JSON.stringify(obj);

            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: '/webservices/stairplanner.svc/project/sendmail',
                dataType: 'json',
                success: function (data, success)
                {
                    Help.onSendComplete(data, success);
                },
                data: objData
            });
        }
    }

    this.onSendComplete = function (data, success)
    {
        if (success == 'success')
        {
            $('#Help #RequestForm').hide();

            $('#Help #Success').show();
        }
    }

}

//////// INIT CALL /////////

$(function ()
{
    Help.onInit();
});
