﻿//Define Kpw object only if it's not already defined
if ("undefined" == typeof Kpw) {
    Kpw = {};
}

$(document).ready(function(){Kpw.ProfileControl.init();});

//namespace...
Kpw.ProfileControl = {
    init: function () {

        Kpw.ProfileControl.InitializeUserProperties();

        //hide elements based upon edit mode.
        var hdnvar = $(".hdnEditMode");

        if (hdnvar.val() == 0) {
            $(".editUser").addClass("hide");
        }

        $(".submit-button").button();
        $(".weddingDate").datepicker();
        $(".phone").mask("(999) 999-9999");

        $(".userType").bind("change", function (e) {
            Kpw.ProfileControl.InitializeUserProperties();
        });

        $("div#Profile div.bride .PaletteSelect").multiSelect({
            selectAll: false,
            noneSelected: '',
            oneOrMoreSelected: '*'
        });

        $('div.bride div.multiSelectOptions label input').each(function () {
            var color = $(this).val();
            $(this).parent().addClass(color);
        });

        Kpw.ProfileControl.ProfilePic.init();

        $('.ddlBridalWeddingState').bind('change', Kpw.ProfileControl.StateChanged);

        //set intitial value
        Kpw.ProfileControl.StateChanged();

    }, //init
    InitializeUserProperties: function () {
        //hide elements based upon profile mode.
        var userType = $(".hdnEditorType");
        var hdnvar = $(".hdnUserType");

        if ($(".userType").length > 0) {
            hdnvar = $(".userType");
        }

        Kpw.ProfileControl.ShowAllProperties();

        if (userType.val() != "Admin") {
            Kpw.ProfileControl.HideAdminProperties();
        }

        switch ($(hdnvar).val()) {
            case "Admin":
                Kpw.ProfileControl.HideVendorProperties();
                Kpw.ProfileControl.HideBrideProperties()
                $(".user").addClass("hide");
                break;
            case "Vendor":
                Kpw.ProfileControl.HideBrideProperties();
                $(".vendor").removeClass("hide");
                break;
            case "Facebook":
            case "Bride":
                Kpw.ProfileControl.HideVendorProperties();
                break;
            case "BrideQuickEdit":
                Kpw.ProfileControl.HideVendorProperties();
                Kpw.ProfileControl.HideBrideProperties();
                Kpw.ProfileControl.ShowBrideQuickEditProperties();
                break;
        }

    },
    ShowAllProperties: function () {
        $(".vendor").removeClass("hide");
        $(".bride").removeClass("hide");
        $(".user").removeClass("hide");
    },

    HideVendorProperties: function () {
        $(".vendor").addClass("hide");
    },

    HideBrideProperties: function () {
        $(".bride").addClass("hide");
    },

    HideAdminProperties: function () {
        $(".admin").addClass('hide')
    },
    ShowBrideQuickEditProperties: function () {
        $(".brideQuickEdit").removeClass("hide");
    },
    addFormField: function () {
        var id = $("#nextId").val();

        var clone = $("#target0").clone()
        clone[0].id = "target" + id;
        $("#WeddingTargets").append(clone);

        id = (id - 1) + 2;
        $("#nextId").val(id);
    },
    removeFormField: function (id) {

        if (id !== "#target0") {
            $(id).remove();
        }
    },
    brideRequired: function (source, clientside_arguments) {

        if ($(".userType").val() != "Bride") {
            clientside_arguments.IsValid = true;
        }
        else {
            if (clientside_arguments.Value == "" || clientside_arguments.Value == "None") {
                clientside_arguments.IsValid = false;
            } else {
                clientside_arguments.IsValid = true;
            }
        }
    },
    StateChanged: function () {
        var location = $('.ddlBridalWeddingState').val();
        var disableBudget = false;
        if (location == 'CostaRica'
            || location == 'Mexico') {

            disableBudget = true;
        }

        if (location == 'NY') {
            $(".ddlBridalBudget option:first-child").attr("disabled", "disabled");
        } else {
            $(".ddlBridalBudget option:first-child").removeAttr("disabled");
        }

        $('.ddlBridalBudget').attr("disabled", disableBudget);
    },
    ProfilePic: {
        click: function () {
            Kpw.ProfilePic.init();
            $("div.profilePicTool").dialog('open');

        },
        init: function () {

            //init dialog
            $("div.profilePicTool").dialog({
                title: 'Upload Your Profile Picture',
                autoOpen: false,
                resizable: false,
                height: 600,
                width: 700,
                modal: true,
                overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
                },
                open: function () {
                    $(this).parent().appendTo($("form:first"));
                },
                closeOnEscape: true
            });

            $("div.profilePicTool").prev().addClass("profilePicToolHeader");
            $("div.profilePicTool").next().addClass("profilePicToolFooter");
            $("div.profilePicTool").parent().addClass("profilePicToolWrapper");
            $("div.profilePicToolWrapper span.ui-dialog-title").after('<span class="caption">Crop Your Profile Picture</span>');

        }
    }
} //Kpw.ProfileControl
    
