﻿TwentyQuestions.Profile = {};

$(document).ready(function () {
    TwentyQuestions.Common.SetupTabs('.tabs-followers');
    TwentyQuestions.Common.SetupTabs('.tabs-achievements');
    TwentyQuestions.Profile.AddToolTips();
    $(".edit-profile").fancybox({
        'hideOnContentClick': false,
        'titleShow': false,
        'scrolling': 'no',
        'onComplete': function () {
            TwentyQuestions.Common.SetupTabs('.modal-profile-column');
            TwentyQuestions.Profile.AddWatermarks();
            TwentyQuestions.Profile.AddToolTips();
            TwentyQuestions.Profile.AddUploadAvatarBehavior();
        }
    });    
});

TwentyQuestions.Profile.AddWatermarks = function () {
    $('#UserName').watermark("Username");
    $('#Email').watermark("Email Address");
    $('#OldPassword').watermark("Old Password");
    $('#NewPassword').watermark("New Password");
    $('#ConfirmPassword').watermark("Repeat Password");
    $('#FullName').watermark("Full Name");
};

TwentyQuestions.Profile.AddToolTips = function () {
    $('#FullName').tipTip({ activation: "focus", defaultPosition: "right" });
    $('#Email').tipTip({ activation: "focus", defaultPosition: "right" });
    $('#OldPassword').tipTip({ activation: "focus", defaultPosition: "right" });
    $('#NewPassword').tipTip({ activation: "focus", defaultPosition: "right" });
    $('#ConfirmPassword').tipTip({ activation: "focus", defaultPosition: "right" });
    $('.achievement').tipTip({ activation: "hover", defaultPosition: "right" });
    $('.achievements-thinker').tipTip({ activation: "hover", defaultPosition: "top" });
    $('.achievements-guesser').tipTip({ activation: "hover", defaultPosition: "top" });
};

TwentyQuestions.Profile.SaveProfile = function (sender) {
    var form = $(sender).parent().parent();
    var data = form.serialize();

    var url = form.attr('action');
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (response) {  
            TwentyQuestions.Common.ShowMessageBar(response);
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
        }
    });
    return false;
};

TwentyQuestions.Profile.Follow = function (sender, userName) {
    $(sender).val("Following");
    $(sender).addClass("disabled");
    $(sender).attr("disabled", "disabled");

    $.ajax({
        type: "POST",
        url: root + "/" + userName + "/Follow",
        dataType: "json",
        success: function (response) {
            if (response.Success) {
                $(sender).parent().html(response.Html);
            }
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
            $(sender).val("Follow");
            $(sender).removeAttr('disabled');
            $(sender).removeClass("disabled");
        }
    });
    return false;
};

TwentyQuestions.Profile.Unfollow = function (sender, userName) {
    $(sender).val("Unfollowing...");
    $(sender).attr("disabled", "disabled");

    $.ajax({
        type: "POST",
        url: root + "/" + userName + "/Unfollow",
        dataType: "json",
        success: function (response) {
            if (response.Success) {
                $(sender).parent().html(response.Html);
            }
        },
        error: function (xhr, status, error) {
            TwentyQuestions.Common.ShowMessageBar(error);
            $(sender).val("Unfollow");
            $(sender).removeAttr('disabled');
        }
    });
    return false;
};


TwentyQuestions.Profile.AddUploadAvatarBehavior = function () {
    var $popup = $("#fancybox-outer");
    $popup.find('#fileInput').uploadify({
        'uploader': root + '/Content/Flash/uploadify.swf',
        'script': root + '/Profile/ChangeAvatar',
        'scriptData': { 'userName': $popup.find('#Who').val(), 'token': $popup.find('#Auth').val() },
        'cancelImg': root + '/Content/Images/trashHover.png',
        'auto': true,
        'multi': true,
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
        'queueSizeLimit': 90,
        'sizeLimit': 4000000,
        'buttonText': 'Change Avatar',
        'width': 200,
        'folder': root + '/uploads',
        'onComplete': function (event, queueID, fileObj, response, data) {
            //TwentyQuestions.Common.ShowMessageBar(response);
            TwentyQuestions.Common.ShowMessageBar("Your avatar has been updated. Refresh your browser to see the change.");
        },
        'onError': function (event, ID, fileObj, errorObj) {
            var msg;
            if (errorObj.type === "File Size")
                msg = 'File size cannot exceed 4MB';
            else
                msg = "An error occured while attempting to upload your avatar."

            TwentyQuestions.Common.ShowMessageBar(msg);
            this.hide();
        }
    });
};
