﻿$(function () {

    if (typeof (SVG) == 'undefined') { SVG = {}; }
    if (typeof (SVG.Group) == 'undefined') { SVG.Group = {}; }
    else { return; }

    SVG.Group.ID = null;
    SVG.Group.ProfilePictureIcon = null;
    SVG.Group.EmailInviteTextArea = null;
    SVG.Group.MemberManagementTabs = [];
    SVG.Group.InvitedUserContainer = null;
    SVG.Group.WallContainer = null;

    SVG.Group.BlockUI = {
        message: '<img src="' + SVG.URLS.LoadingIcon + '" alt="" />',
        overlayCSS: {
            backgroundColor: '#000',
            opacity: 0.3,
            width: '100%'
        },
        css: { width: '100%' },
        centerX: true,
        centerY: true
    };

    SVG.Group.Init = function (groupID, options, emailTextArea, invitedUserContainer, uninvitedUserContainer, tabDiv, tabClickMap, wallContainer) {

        $.extend(SVG.Group.BlockUI, options);
        $.ajaxSettings.traditional = true;

        SVG.Group.ID = groupID;
        SVG.Group.EmailInviteTextArea = emailTextArea;
        SVG.Group.InvitedUserContainer = invitedUserContainer;
        SVG.Group.UninvitedUserContainer = uninvitedUserContainer;
        SVG.Group.WallContainer = wallContainer;
        SVG.Group.InitTabs(tabDiv, tabClickMap);
        if (wallContainer) {
            SVG.Wall.Init(wallContainer);
            SVG.Group.LoadWallPosts();
        }

    };


    SVG.Group.GroupDeleteFromGrid = function () {
        var row = $("input[val==" + SVG.Group.ID + "]").closest("tr");

        var callBackFunction = function () {
            SVG.DataGrid.MainContainer.fnDeleteRow(row[0]);
            SVG.DataGrid.MainContainer.fnDraw();
        };

        SVG.Group.Delete(callBackFunction);
    };

    SVG.Group.Delete = function (callBackFunction) {

        var group = { id: SVG.Group.ID };

        var result = confirm("Delete group?");
        if (!result) { return; }

        $.post(SVG.URLS.DeleteGroupForAdmin, group, function (result) {
            if ($.isFunction(callBackFunction)) { callBackFunction(); }
        });

    }

    SVG.Group.Submit = function () {

        if (!SVG.Group.HasInvitedFriendsSelected) {
            alert("No friends selected");
            return;
        }

        var messageContainer = $("<div class='FriendInvitationMessageContainer' />");
        var messageLabel = $("<div />").text("Enter an invitation message");
        var textArea = $("<textarea id='InvitationMessage' />").attr("value", "Welcome to Student Voice Groups!");
        var button = $("<div />").attr("class", "FriendInviteSubmitMessageButton").text("Invite");
        button.click(function () {
            SVG.Group.SubmitInvitations($("#InvitationMessage")[0].value);
            $.unblockUI();
        });

        messageContainer.append(messageLabel);
        messageContainer.append(textArea);
        messageContainer.append(button);


    };
  
    SVG.Group.LoadWallPosts = function () {

        $.getJSON(SVG.URLS.GetGroupWallPosts, function (posts) {
            var postCount = posts.length;
            for (var i = 0; i < postCount; i++) {
                SVG.Wall.AddPost(posts[i]);
            }
        });

    };

    SVG.Group.AddWallPost = function (message) {

        message = SVG.StripHtml(message);

        var post = {
            shortMessage: message,
            groupId: SVG.Group.ID
        };

        $.post(SVG.URLS.CreateNewGroupWallPost, post, function () {

            var d = new Date();
            var dateCreated = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
            dateCreated += " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
            dateCreated += " " + (d.getHours() < 12 ? "AM" : "PM");

            post.DateCreated = dateCreated;
            post.OwnerPicture = SVG.Group.ProfilePictureIcon;
            post.OwnerName = SVG.CurrentMember.Name;
            post.ShortMessage = post.shortMessage;
            var postEl = SVG.Wall.AddPost(post, true);

            postEl.effect("highlight", { color: "#FFF3CB" }, 3000);

            setTimeout(function () {
                postEl.effect("highlight", { color: "white" }, 3000);
            }, 3000);

        });

    };

});

