﻿function g_createNewGroup() {
    openGroupDialog();
}

function g_loadAllGroups(){
   g_showGroupPanel("groups_all_groups");
   addAjaxLoader("groups_all_groups");
   con.groupsManager.loadAllGroups();
}

function g_loadMyGroups(){
   g_showGroupPanel("groups_my_groups");
   addAjaxLoader("groups_my_groups");
   con.groupsManager.loadMyGroups();
}

function g_backToGroupsMenu(){
    var menu_general = j$("#groups_menu_general");
    var menu_specific= j$("#groups_menu_specific");

    menu_specific.hide();
    menu_general.show();
    g_showGroupPanel("groups_all_groups");
    con.groupsManager.closeSelectedGroup();
    g_loadAllGroups();

}

function g_openGroup(groupid){
    var menu_general = j$("#groups_menu_general");
    var menu_specific= j$("#groups_menu_specific");

    menu_general.hide();
    menu_specific.show();
    
    g_showGroupPanel("groups_selected_group");
    addAjaxLoader("groups_selected_group");
    con.groupsManager.loadCompleteGroup(groupid);
}

function g_groups_write_message(){
    j$("#groups_group_post_div").show();
    j$("#groups_group_post_lnk").hide();
    var txtarea = j$("#groups_group_post_textarea");
    txtarea.val("");
    g_groups_post_count_chars(txtarea);
}

function g_groups_post_count_chars(elem){
 var jelem = j$(elem);
 var elemCharsLeft = j$("#groups_chars_left");
 var countLeft = 500 - jelem.val().length;
 elemCharsLeft.text(g_tr.group_chars_left + countLeft);
 if(countLeft < 0){
    elemCharsLeft[0].style.color = "red";
 }else{ 
    elemCharsLeft[0].style.color = "black";
 }
}

function g_groups_post_message(){
    var txtarea = j$("#groups_group_post_textarea");
    var txt = txtarea.val();
    var countLeft = 500 - txtarea.val().length;
    if(countLeft < 0){
        alert(g_tr.group_post_limit);
        return;
    }
    j$("#groups_group_post_textarea").val("");
    j$("#groups_group_post_div").hide();
    j$("#groups_group_post_lnk").show();
    addAjaxLoader("groups_selected_group");
        
    con.groupsManager.postMessage(txt);
}



function g_joinGroup(){
    if(confirm(g_tr.group_confirm_join)){
        addAjaxLoader("groups_selected_group");
        con.groupsManager.joinGroup();
    }
}

function g_leaveGroup(){
    if(confirm(g_tr.group_confirm_leave)){
        addAjaxLoader("groups_selected_group");
        con.groupsManager.leaveGroup();
    }
}

function g_showGroupPanel(divid){
    if("groups_all_groups" == divid){
        j$("#groups_all_groups").show();
    }else{
        j$("#groups_all_groups").hide();
    }

    if("groups_my_groups" == divid){
        j$("#groups_my_groups").show();
    }else{
        j$("#groups_my_groups").hide();
    }

    if("groups_selected_group" == divid){
        j$("#groups_selected_group").show();
    }else{
        j$("#groups_selected_group").hide();
    }
}



function GroupsManager(con) {
    this.connection = con;
    this.groups = new Array();
    this.mygroups = new Array();
    this.groupToCreate = null; // group sent to the server for creation
    this.selectedGroup = null; // group selected to be displayed
    this.selectedGroupDiscussion = new Array(); // discussion (wall) of the selected group

    // add listeners
    con.messageListeners.addListener(new MessageListener("msg2", "response", "group", global_DefaultSchema, internalHandleGroupResponseMessage, this));
    con.messageListeners.addListener(new MessageListener("msg2", "notify", "group", global_DefaultSchema, internalHandleGroupNotifyMessage, this));
}

GroupsManager.prototype = {
    createGroup : GroupsManager_CreateGroup,
    displayGroups: GroupsManager_DisplayGroups,
    loadAllGroups: GroupsManager_LoadAllGroups,
    loadMyGroups: GroupsManager_LoadMyGroups,
    closeSelectedGroup: GroupsManager_CloseSelectedGroup,
    loadCompleteGroup: GroupsManager_LoadCompleteGroup,
    displaySelectedGroup: GroupsManager_DisplaySelectedGroup,
    postMessage: GroupsManager_PostMessage,
    joinGroup: GroupsManager_JoinGroup,
    leaveGroup: GroupsManager_LeaveGroup,
}



function internalHandleGroupResponseMessage(sender, message, targetObject) {
    if (isSet(message.success) && message.success == "false") {
        alert(message.errcode + " - " + message.errmsg);
        return;
    }

    // remove waiting-to-load animated icon from all panels related to group
    removeAjaxLoader("groups_all_groups");
    removeAjaxLoader("groups_my_groups");
    removeAjaxLoader("groups_selected_group");

    if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "create")) {
        if(targetObject.groupToCreate != null){
            targetObject.groupToCreate.groupid = message.properties.groupid;
            targetObject.groups.splice(0, 0, targetObject.groupToCreate);
            targetObject.mygroups.splice(0, 0, targetObject.groupToCreate);
            
        }
        targetObject.displayGroups("groups_all_groups", targetObject.groups);
        targetObject.displayGroups("groups_my_groups", targetObject.mygroups);
        targetObject.groupToCreate = null;
    }
    else     if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "LoadAllGroups") && isSet(message.groups)) {
        targetObject.groups = message.groups;
        targetObject.displayGroups("groups_all_groups", targetObject.groups);
        
    }
    else     if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "LoadMyGroups") && isSet(message.groups)) {
        targetObject.mygroups = message.groups;
        targetObject.displayGroups("groups_my_groups", targetObject.mygroups);
        
    } else if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "LoadGroup") && isSet(message.groups)) {
        targetObject.selectedGroup = message.groups[0];
        targetObject.selectedGroup.ismember = message.properties.ismember == "true";
        targetObject.selectedGroup.membersCount = message.properties.membersCount;
        targetObject.selectedGroup.postsCount = message.properties.postsCount;
        if (isSet(message.posts)) {
            targetObject.selectedGroupDiscussion = message.posts;
        }
        targetObject.displaySelectedGroup("groups_selected_group");
    } else if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "AddMessage") && isSet(message.posts) && targetObject.selectedGroup != null) {
        targetObject.selectedGroup.ismember = message.properties.ismember == "true";
        targetObject.selectedGroup.membersCount = message.properties.membersCount;
        targetObject.selectedGroup.postsCount = message.properties.postsCount;
        if(message.posts.length > 0){
            targetObject.selectedGroupDiscussion.splice(0, 0, message.posts[0]);
            targetObject.displaySelectedGroup("groups_selected_group");
        }
    } else if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "Join") && isSet(message.groups)) {
        targetObject.selectedGroup = message.groups[0];
        targetObject.selectedGroup.ismember = message.properties.ismember == "true";
        targetObject.selectedGroup.membersCount = message.properties.membersCount;
        targetObject.selectedGroup.postsCount = message.properties.postsCount;
        if (isSet(message.posts)) {
            targetObject.selectedGroupDiscussion = message.posts;
        }
        targetObject.displaySelectedGroup("groups_selected_group");
    } else if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "Leave") && isSet(message.groups)) {
        targetObject.selectedGroup = message.groups[0];
        targetObject.selectedGroup.ismember = message.properties.ismember == "true";
        targetObject.selectedGroup.membersCount = message.properties.membersCount;
        targetObject.selectedGroup.postsCount = message.properties.postsCount;
        if (isSet(message.posts)) {
            targetObject.selectedGroupDiscussion = message.posts;
        }
        targetObject.displaySelectedGroup("groups_selected_group");
    }

    
}

function internalHandleGroupNotifyMessage(sender, message, targetObject) {
if (isSet(targetObject) && isSet(message.properties.action)
        && (message.properties.action == "AddMessage") && isSet(message.posts) && targetObject.selectedGroup != null) {
        targetObject.selectedGroup.membersCount = message.properties.membersCount;
        targetObject.selectedGroup.postsCount = message.properties.postsCount;
        if(message.posts.length > 0){
            targetObject.selectedGroupDiscussion.splice(0, 0, message.posts[0]);
            targetObject.displaySelectedGroup("groups_selected_group");
        }
    }
}


function GroupsManager_DisplayGroups(divId, groupsToDisplay) {
    var container = j$("#" + divId);
    if(!container.is(":visible")){
        return;
    }
    var strHtml = "";

    if(typeof(groupsToDisplay) != "undefined" && groupsToDisplay != null){
        for (var i = 0; i < groupsToDisplay.length; i++) {
            strHtml += GetGroupHtml(groupsToDisplay[i]);
        }
    }

    container.html(strHtml);
}


function GetGroupHtml(group) {
    var html = "<div><div class = 'group' onmouseover=\"changeColors(this, '#ffffff','#CA0002')\" onmouseout=\"changeColors(this, 'Black','#ffffff')\" onclick=\"g_openGroup('" +  group.groupid +"');\" >";
    html += "<span class='group_title'>" + encodeHtml(group.name) + "</span>";
    html += "<div class='group_description'>" + encodeHtml(group.description) + "</span>";
    html += "</div></div>";

    return html;
}

function GroupsManager_DisplaySelectedGroup() {
    if (this.selectedGroup == null) {
        return;
    }
    var elemTitle = j$("#groups_group_title");
    var elemMembersCount = j$("#groups_group_members_count");
    var elemPostsCount = j$("#groups_group_posts_count");
    elemTitle.text(this.selectedGroup.name);
    elemMembersCount.text(this.selectedGroup.membersCount + g_tr.group_members);
    elemPostsCount.text(this.selectedGroup.postsCount + g_tr.group_posts);
    if (this.selectedGroup.ismember) {
        j$("#groups_joinGroup").hide();
        j$("#groups_leaveGroup").show();
        j$("#groups_group_post_lnk").show();
    } else {
        j$("#groups_joinGroup").show();
        j$("#groups_leaveGroup").hide();
        j$("#groups_group_post_lnk").hide();
        j$("#groups_group_post_div").hide();
    }


    var wall = j$("#groups_group_wall");
    var strHtml = "";

    for (var i = 0; i < this.selectedGroupDiscussion.length; i++) {
        strHtml += GetGroupPostHtml(this.selectedGroupDiscussion[i]);
    }

    wall.html(strHtml);

}

function GetGroupPostHtml(post) {
    var html = "<div><div class = 'group_post' >";
    html += "<img src = '" + post.smallProfileImageUrl + "' class='group_post_image' />" ;
    html += "<span class='group_post_nickname'>" + encodeHtml(post.nickname) + " </span>"
    html += encodeHtml(post.text);
    html += "<br/><span class='group_post_footer'>" + post.time + "</span><br/><br/>";
    html += "</div>";
    html += "</div>";

    return html;
}

function GroupsManager_CreateGroup(name, description, privacy){
        this.groupToCreate = {"name" : name, "description": description, "privacy":privacy};
	    var msg = createGroupMessage(this.connection.nextMessageID(), "create", name, description, privacy);
        this.connection.postMessageObject(msg);
}

function GroupsManager_LoadAllGroups(){
	    var msg = createLoadGroupsMessage(this.connection.nextMessageID(), false);
        this.connection.postMessageObject(msg);
}


function GroupsManager_LoadMyGroups(){
	    var msg = createLoadGroupsMessage(this.connection.nextMessageID(), true);
        this.connection.postMessageObject(msg);
}

function GroupsManager_CloseSelectedGroup(){
    if(this.selectedGroup != null){
        var msg = createExitViewingGroupMessage(this.connection.nextMessageID(), this.selectedGroup.groupid);
        this.connection.postMessageObject(msg);
        this.selectedGroup = null;
        this.selectedGroupDiscussion = new Array();
    }
}

function GroupsManager_LoadCompleteGroup(groupid) {
    var msg = createLoadGroupMessage(this.connection.nextMessageID(), groupid, true);
    this.connection.postMessageObject(msg);
}

function GroupsManager_PostMessage(text){
    if(this.selectedGroup != null && this.selectedGroup.ismember){
        var msg = createPostMessage(this.connection.nextMessageID(), this.selectedGroup.groupid, text);
        this.connection.postMessageObject(msg);
    }
}

function GroupsManager_JoinGroup(){
    if(this.selectedGroup != null && !this.selectedGroup.ismember){
        var msg = createJoinGroupMessage(this.connection.nextMessageID(), this.selectedGroup.groupid);
        this.connection.postMessageObject(msg);
    }
}

function GroupsManager_LeaveGroup(){
    if(this.selectedGroup != null && this.selectedGroup.ismember){
        var msg = createLeaveGroupMessage(this.connection.nextMessageID(), this.selectedGroup.groupid);
        this.connection.postMessageObject(msg);
    }
}

function createExitViewingGroupMessage(id, groupid) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", "ExitViewing");
    m.attributes.set("groupid", groupid);
    return m;
}

function createLeaveGroupMessage(id, groupid) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", "Leave");
    m.attributes.set("groupid", groupid);
    return m;
}

function createJoinGroupMessage(id, groupid) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", "Join");
    m.attributes.set("groupid", groupid);
    return m;
}

function createPostMessage(id, groupid, text) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", "AddMessage");
    m.attributes.set("groupid", groupid);
    m.text = text;
    return m;
}


function createLoadGroupMessage(id, groupid, complete) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", "LoadGroup");
    m.attributes.set("complete", "" + complete);
    m.attributes.set("groupid", groupid);
    return m;
}


function createLoadGroupsMessage(id, mygroupsOnly) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    if(mygroupsOnly){
        m.attributes.set("action", "LoadMyGroups");
    }else{
        m.attributes.set("action", "LoadAllGroups");
    }
    return m;
}

function createGroupMessage(id, action, name, description, privacy) {
    var m = new Message();
    m.id = id;
    m.command = "group";
    m.attributes.set("action", action);
    m.attributes.set("name", name);
    m.attributes.set("privacy", privacy);
    if (isSet(description)) {
        m.attributes.set("description", description);
    }
    return m;

}



