﻿$(function() {
    if (loaded)
        return;
    loaded = true;

    $(sendFriend_scriptProperties.dialogId).dialog(
    {
        autoOpen: false,
        buttons: { "Cancel": function() { $(this).dialog('close'); },
        "Submit": function() {
            $.getJSON('/sendfriend.json',
                { name: $(sendFriend_scriptProperties.nameControl).attr('value'),
                    from: $(sendFriend_scriptProperties.fromControl).attr('value'),
                    to: $(sendFriend_scriptProperties.toControl).attr('value'),
                    url: document.location.href,
                    message: $(sendFriend_scriptProperties.messageControl).attr('value'),
                    codeNumber: $(sendFriend_scriptProperties.codeControl).attr('value')
                },
                function(data, status) {
                    if (data.result) {
                        var fb = $('<div>' + data.message + '</div>');
                        fb.prependTo('body');
                        fb.dialog({
                            buttons: { 'OK': function() { $(this).dialog('close'); $(sendFriend_scriptProperties.dialogId).dialog('close'); } },
                            modal: true,
                            title: 'Mail has been sent.'
                        });
                    }
                    else {
                        var fb = $('<div>' + data.message + '</div>');
                        fb.prependTo('body');
                        fb.dialog({
                            buttons: { 'OK': function() { $(this).dialog('close'); $(this).remove(); } },
                            modal: true,
                            title: 'Mail was not sent.'
                        });
                    }
                });
            } },
        closeOnEscape: true,
        modal: true,
        title: 'Send to a Friend',
        width: 400
    });

    $(sendFriend_scriptProperties.buttonId).click(function(e) {
        $(sendFriend_scriptProperties.dialogId).dialog('open');
    });
});