﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />

(function()
{
	BSC.UI.Ws.Group = function()
	{
		var feedentries = 10;
		var basebind = function(p1)
		{
		    var guid = p1;
		    var w = BSC.UI.Ws.Get(guid);
		    if (w.data && w.data.key)
		    {
		        $("#" + guid + "_tabs_content_ul li a").map(function()
		        {
		            var orghref = $(this).attr("orghref");
		            if (orghref.indexOf(w.data.key) > 0) return;
		            var h = orghref.replace(/http:\/\/[^\/]+/i, "");

		            var newHref = h.replace(/\/group/i, w.data.key);
		            //					var t = h.split("/");
		            //					var action = (t.length > 2) ? "/" + t[2] : "";
		            //					var newHref = "/" + t[1] + "/" + w.data.key + action;
		            this.href = "/group/" + newHref;
		        });
		    }
//			var guid = p1;
//			var w = BSC.UI.Ws.Get(guid);
//			//var jQ = BSC.U.ParseQuery(w.query);
//			if (w.data && w.data.key)
//			{
//				//   BSC.UI.Ws.Get(guid).query = "groupid=" + jQ.groupid;
//				$("#" + guid + "_tabs_content_ul li a").map(function()
//				{
//					var h = (this.href).replace(/http:\/\/[^\/]+/i, "");
//					var t = h.split("/");
//					var action = (t.length > 2) ? "/" + t[2] : "";
//					var newHref = "/" + t[1] + "/" + w.data.key + action;
//					this.href = newHref;
//				});
//			}
		};

		var tabsloaded = function(p1)
		{
			var guid = p1;
			basebind(guid);
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1)
		{
			var guid = p1;


			$("#" + guid + "_feedentry").bind("click", { guid: guid }, function(e)
			{

				var data = { feedcount: feedentries };
				BSC.D.AjaxGet("/Groups/Stream/", null, function(res)
				{
					// Html
					$("#" + guid + "_groupItems").html(res).show();
					BSC.E.MapHrefs();
					BSC.UI.HideStatus(guid, 200);
				}, "html", guid);
			});
			$("#" + guid + "_grouptabs").bind("click", guid, function(e)
			{
				var el = BSC.E.GetElement(e, "ul", "li");
				if (!el.id) return;
				var t = (el.id).split("_");
				var name = t[1];
				var guid = e.data;

				$("#" + guid + "_grouptabs li").removeClass("selected");
				$("#" + guid + "_" + name).addClass("selected");

				BSC.UI.ShowStatus(guid, "Loading...");
				$("#" + guid + "_button").hide();

				if ($("#" + guid + "_groupfeed").hasClass('selected'))
				{
				    BSC.D.AjaxGet("/Groups/Stream/", null, function(res)
					{
						// Html
						$("#" + guid + "_groupItems").html(res).show();
						$("#" + guid + "_feedentry").bind("click", { guid: guid }, BSC.UI.Ws.Group.SeeOlderFeed);
						BSC.E.MapHrefs();
						BSC.UI.HideStatus(guid, 200);
					}, "html", guid);
				}
				else
				{

					BSC.D.AjaxGet("/Groups/FeaturedList/" + name, null, function(res)
					{
						if (name != "staffpicks")
						{
							//$("#" + guid + "_button").attr("href", "/groups/search/?list=" + name);
							$("#" + guid + "_button").show().unbind().click(function(e)
							{
								GO("/groups/search/?list=" + name);
								return false;
							});
						}

						// Html
						$("#" + guid + "_groupItems").html(res).show();
						BSC.E.MapHrefs();
						BSC.UI.HideStatus(guid, 200);
					}, "html", guid);
				}
			});


			$("#" + guid + "_editGroup").bind("click", { guid: guid }, BSC.UI.Ws.Group.OnEditGroupClick);
			$("#" + guid + "_feedentry").bind("click", { guid: guid }, BSC.UI.Ws.Group.SeeOlderFeed);


			$("#" + guid + "_groupsearchform").bind("submit", guid, function(e)
			{
				var guid = e.data;
				var value = $("#" + guid + "_searchInput").val();
				GO("/groups/search/" + value);
				return false;
			});

			basebind(guid);

			BSC.D.AjaxGet("/Groups/Stream", null, function(res)
			{
				if (!$("#" + guid + "_groupfeed")[0].className == 'selected')
					alert(res);
				//				// Html
				$("#" + guid + "_loader").hide();
				$("#" + guid + "_groupItems").html(res).show();
				BSC.E.MapHrefs();
				BSC.UI.HideStatus(guid, 200);
				$("#" + guid + "_feedentry").bind("click", { guid: guid }, BSC.UI.Ws.Group.SeeOlderFeed);
			}, "html", guid);
		};


		var seeOlderFeed = function(e)
		{
			var guid = e.data.guid;
			feedentries += 10;
			var data = { feedentries: feedentries };
			BSC.D.AjaxGet("/Groups/Stream/", data, function(res)
			{
				if (!$("#" + guid + "_groupfeed").hasClass('selected'))
					$("#" + guid + "_button").css("display", "block")
				else
					$("#" + guid + "_button").css("display", "none")
				// Html
				$("#" + guid + "_groupItems").html(res).show();
				BSC.E.MapHrefs();
				BSC.UI.HideStatus(guid, 200);

				$("#" + guid + "_feedentry").bind("click", { guid: guid }, BSC.UI.Ws.Group.SeeOlderFeed);
			}, "html", guid);
		};
		/// <summary>
		/// Unbind events to ui elements
		/// </summary>
		var unbind = function(p1)
		{
			var guid = p1;
			//if ($("#" + guid + "_mygroups").length>0)
			$("#" + guid + "_mygroups").unbind();
			$("#" + guid + "_categories").unbind();
		};

		/// <summary>
		/// 
		/// </summary>
		var onMemberTabClick = function(e)
		{
			var el = BSC.E.GetElement(e, "ul", "li");
			if (!el.id) return;
			var t = (el.id).split("_");
			var guid = t[0];
			var name = t[1];
			//if ($("#" + guid + "_created li.selected"))
			$("#" + guid + "_created").removeClass("selected");
			$("#" + guid + "_member").addClass("selected");
		};


		return {

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Basebind: basebind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			TabsLoaded: tabsloaded,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			SeeOlderFeed: seeOlderFeed,
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			OnMemberTabClick: onMemberTabClick
		};
	} ();
})();
//BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Bind, null, "group");
//BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Unbind, null, "group");

BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Bind, null, "groups");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Unbind, null, "groups");
//BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Group.TabsLoaded, null, "group.main.main");

(function()
{
    BSC.UI.Ws.Group.Create = function()
    {

        var localdata = {};

        var AddProfilesToLocaldata = function(guid, data)
        {
            localdata[guid].profiles = {};
            var l = data.length;
            for (i = 0; i < l; i++)
            {
                if (!localdata[guid].addedProfiles[data[i].ID])
                    localdata[guid].profiles[data[i].ID] = data[i].IsMember;
            }
        }

        /// <summary>
        /// Bind events to ui elements
        /// </summary>
        var load = function(p1)
        {
            var guid = p1;
            localdata[guid] = { profiles: {}, addedProfiles: {} };
        };

        /// Private End
        var bind = function(p1)
        {
            var guid = p1;

            $("#" + guid + "_save").bind("click", guid, function(e)
            {
                var grouptitle = "" + $("#" + guid + "_grouptitle").val();
                if (BSC.R.TextField.test(grouptitle) || grouptitle.length == 0)
                {
                    BSC.UI.ShowHelpBubble(guid + "_grouptitle", "info", BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertTitle"), BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertTitleDesc"));
                    return;
                }
                var categoryid = $("#" + guid + "_category").val();
                if (categoryid == "")
                {
                    BSC.UI.ShowHelpBubble(guid + "_category", "info", BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertCat"), BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertCatDesc"));
                    return;
                }
                var description = "" + $("#" + guid + "_description").val();
                if (description.length == 0)
                    BSC.UI.ShowHelpBubble(guid + "_description", "info", BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertDesc"), BSC.T.Get("JS.BSC.UI.Ws.Groups.InsertDescDesc"));
                var pictureid = $("#" + guid + "_pictureid").val();

                var w = BSC.UI.Ws.Get(guid);
                var groupKey = w.data.key;
                if (groupKey == "create")
                    groupKey = "";
                //Title, CategoryID, Description, Type, ID, PictureID
                var data = {
                    Title: grouptitle,
                    CategoryID: categoryid,
                    Description: description,
                    Type: 0,
                    PictureID: pictureid,
                    Key: groupKey
                };
                BSC.D.AjaxPost("/group/save", data, function(res)
                {
                    // Html
                    if (res.success && groupKey != "")
                        GO("/group/" + res.header);
                    else if (res.success)
                    {
                        groupKey = res.header;
                        BSC.D.AjaxGet("/group/" + groupKey + "/invite", { groupid: res.id }, function(res)
                        {
                            $("#" + guid + "_body").html(res); //
                            $("#" + guid + "_skip").bind("click", guid, function(e)
                            {
                                GO("/group/" + groupKey + "/summary");
                            });
                            $("#" + guid + "_sendit").bind("click", guid, function(e)
                            {
                                var guid = e.data;
                                BSC.UI.Loader(guid);
                                //var profileids = ($("#" + guid + " input[name=" + guid + "_profileID]").getValue()).split(",");
                                var addedprofiles = [];
                                var dl = $("#" + guid + "_profilelistremove");
                                for (x = 0; x < dl[0].children.length; x++)
                                {

                                    var element = dl[0];
                                    var p = element.children[x];
                                    var pid = p.id.split('_');
                                    var profileID = pid[0];
                                    addedprofiles.push(profileID);
                                    //x--;
                                }
                                var message = $("#" + guid + "_personalmessage").val();
                                var groupkey = $("h3#").attr("id");

                                var invitedata = { profilesInvited: addedprofiles, personalmessage: message };

                                BSC.D.AjaxPost("/group/" + groupKey + "/invitationsend", invitedata, function(res)
                                {
                                    //do callback logic here!!!
                                    if (res.success)
                                        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Invitation(s) sent", "You are still able to invite the forgotten from the group.", GO("/group/" + res.header), null, "info")
                                    else
                                        alert("error");
                                    BSC.UI.HideStatus(guid, 200);
                                }, "json", guid);
                            });
                            // Add All Profiles
                            // -------------------------------------------------
                            $("#" + guid + "_addall").bind("click", guid, function(e)
                            {
                                var guid = e.data;
                                if ($("#" + guid + "_profilelistadd dd").length == 0) return;

                                var html = $("#" + guid + "_profilelistadd").html();
                                html = html.replace(/_dd_pid/g, '_dd_pid_added');
                                html = html.replace(/add_ico/g, 'remove_ico');
                                html = html.replace(/friendadd/g, 'friendremove');
                                $("#" + guid + "_profilelistadd dd").remove();
                                $("#" + guid + "_profilelistremove").append(html);

                            });
                            // Remove All Profiles
                            // -------------------------------------------------
                            $("#" + guid + "_removeall").bind("click", guid, function(e)
                            {

                                var guid = e.data;
                                if ($("#" + guid + "_profilelistremove dd").length == 0) return;

                                var html = $("#" + guid + "_profilelistremove").html();
                                html = html.replace(/_dd_pid_added/g, '_dd_pid');
                                html = html.replace(/remove_ico/g, 'add_ico');
                                html = html.replace(/friendremove/g, 'friendadd');
                                $("#" + guid + "_profilelistremove dd").remove();
                                $("#" + guid + "_profilelistadd").append(html);
                            });
                            // Add/Remove profile in lists
                            // -------------------------------------------------
                            $("#" + guid + "_profilelists").bind("click", guid, function(e)
                            {
                                var guid = e.data;
                                // A Tags
                                var el = BSC.E.GetElement(e, "dl", "a");
                                if (el.id)
                                {
                                    var t = (el.id).split("_");
                                    var profileID = t[0];

                                    if ($(el).hasClass("add_ico"))
                                    {
                                        AddProfileToInvitation(profileID, guid);
                                    }
                                    else if ($(el).hasClass("remove_ico"))
                                    {
                                        RemoveProfileFromInvitation(profileID, guid);
                                    }
                                }
                                BSC.E.Stop(e);
                            });

                        }, "html", guid);
                    }
                    else
                        alert("Failed creating group : " + res.message);

                    BSC.UI.HideStatus(guid, 200);
                }, "json", guid);
            });

            //			$("#" + guid + "_cancel").bind("click", guid, BSC.UI.Ws.Group.Create.onCancelEditGroupClick);
            //			BSC.UI.HideStatus(guid, 200);

            var settings = {
                button_image_url: BSC.BASE_STATIC_PATH + "beautifulpeoplecdn/images/buttons/blue/flash_btn.png",
                button_placeholder_id: guid + "_selectpicture",
                button_width: 115,
                button_height: 18,
                button_text_top_padding: 0,
                button_text: '<span class="btnupl">' + BSC.T.Get("JS.BSC.UI.Ws.Groups.Createphoto") + '</span>',
                button_text_style: '.btnupl { font-family: Verdana; font-weight:bold; color:#ffffff; font-size: 11px; line-height:18px; text-align:center; }',
                file_types_description: "Select group picture file",
                file_upload_limit: 0,
                file_queue_limit: 0,
                file_types: "*.jpg;*.gif;*.png;*.bmp",
                button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
                upload_success_handler: function(p1, p2)
                {
                    setTimeout(function()
                    {
                        var file = p1;
                        var data = BSC.D.JSON.Parse(p2);
                        var img = $("#" + data.guid + "_grouppicture");
                        img.attr("src", BSC.PicturePath(data.id, "medium"));
                        $("#" + data.guid + "_grouppicture_loader").hide();
                        img.fadeIn("fast", function() { });
                        $("#" + data.guid + "_pictureid").val(data.id);
                    }, 100);
//                    try
//                    {
//                        

//                    } catch (ex)
//                    {
//                        this.debug(ex);
//                    }
                    this.setButtonDisabled(false);

                },
                file_queued_handler: function(p1)
                {
                    var file = p1;
                    $("#" + guid + "_grouppicture").hide().attr("src", "");
                    $("#" + guid + "_pictureid").val("");
                    $("#" + guid + "_grouppicture_loader").show();
                },
                upload_start_handler: function(p1, p2)
                {
                    // PostParams
                    var postParams = {
                        //p1: null,
                        p2: (BSC.D.P.GetData()).TicketID,
                        //p3: null,
                        p4: guid,
                        p5 : 7
                    };
                    this.setPostParams(postParams);
                    this.setButtonDisabled(true);
                }
            };

            BSC.F.GetUpload(guid, settings);
        };

        var AddProfileToInvitation = function(p1, p2)
        {
            var profileID = p1;
            var guid = p2;

            var html = $("#" + profileID + "_dd_pid").html();

            // Remove from Not selected list
            $("#" + profileID + "_dd_pid").remove();

            // Add to selected list
            html = " <dd class='friend' id='" + profileID + "_dd_pid_added' title='Added'>" + html + "</dd>";
            html = html.replace(/add_ico/i, 'remove_ico');
            html = html.replace(/friendadd/i, 'friendremove');
            $("#" + guid + "_profilelistremove").append(html);

            localdata[guid].addedProfiles[profileID] = true;
        };

        var RemoveProfileFromInvitation = function(p1, p2)
        {
            var profileID = p1;
            var guid = p2;

            var html = $("#" + profileID + "_dd_pid_added").html();

            // Remove from Not selected list
            $("#" + profileID + "_dd_pid_added").remove();

            // Add to selected list
            html = " <dd class='friend' id='" + profileID + "_dd_pid' title='Added'>" + html + "</dd>";
            html = html.replace(/remove_ico/i, 'add_ico');
            html = html.replace(/friendremove/i, 'friendadd');
            html = html.replace(/_dd_pid_added/i, '_dd_pid').replace(/Added/i, 'Add by clicking the icon');
            $("#" + guid + "_profilelistadd").append(html);

            delete localdata[guid].addedProfiles[profileID];
        };

        var unbind = function(p1)
        {
            var guid = p1;
            BSC.F.DisposeUpload(guid);
        };

        var onSearchGroupClick = function(e)
        {
            var el = BSC.E.GetElement(e, "ul", "a");
            if (el == null) return;
            var t = (el.id).split("_");
            var guid = t[0];
            var name = t[1];
            var value = $("#" + guid + "_searchInput").val();
            alert("Search group not implemented!");
        };

        var onCancelEditGroupClick = function(e)
        {
            onCancelEditGroupClick();
        };

        return {
            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Load: load,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Bind: bind,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Unbind: unbind,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            OnSearchGroupClick: onSearchGroupClick,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            OnCancelEditGroupClick: onCancelEditGroupClick
        };
    } ();
})();
//BSC.E.Subscribe("load", BSC.UI.Ws.Group.Create.Loaddata, null, "group.create.create");
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Create.Bind, null, "group.create");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Create.Unbind, null, "group.create");
BSC.E.Subscribe("load", BSC.UI.Ws.Group.Create.Load, null, "group.create");

BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Create.Bind, null, "group.edit");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Create.Unbind, null, "group.edit");
BSC.E.Subscribe("load", BSC.UI.Ws.Group.Create.Load, null, "group.edit");



(function()
{
    BSC.UI.Ws.Group.Summary = function()
    {
        var JoinLeaveGroup = function(p1)
        {

            var guid = p1;
            var w = BSC.UI.Ws.Get(guid);

            if (!BSC.D.P.IsLoggedIn())
            {
                BSC.UI.AlertLogin();
                return false;
            }

            BSC.D.AjaxPost("/group/" + w.data.key + "/JoinLeave", null, function(res)
            {
                if (res.success)
                    BSC.UI.W.Reload(guid); //GO("/group/" + w.data.key);
                else
                    BSC.UI.AlertOnlyFullMembers();
            }, "json", guid);

            return false;
        };

        var BecomeAdmin = function(p1)
        {
            var guid = p1;
            if (!BSC.D.P.IsLoggedIn())
            {
                BSC.UI.AlertLogin();
                return false;
            }

            var w = BSC.UI.Ws.Get(guid);
            BSC.D.AjaxPost("/Group/" + w.data.key + "/BecomeAdmin", null, function(res)
            {
                if (res.success)
                    BSC.UI.W.Reload(guid);
                else
                    BSC.UI.AlertOnlyFullMembers();
            }, "json", guid);
        };


        var bind = function(guid)
        {
            // New Discussion Button click
            $("#" + guid + "_newtopic").bind("click", guid, function(e)
            {
                var guid = e.data;

                if (!BSC.D.P.IsLoggedIn())
                {
                    BSC.UI.AlertLogin();
                    return false;
                }
                else if (BSC.D.P.IsFullMember() && !BSC.D.P.IsPaying())
                {
                    BSC.UI.AlertCurrentlyFREE();
                    return false;
                }

                $("#" + guid + "_additem").slideToggle("fast", function(p1, p2)
                {

                    if ($(this).css("display") == "none")
                    {
                        $("#" + guid + "_activate").removeClass("activated");
                        $("#" + guid + "_discussionsection").removeClass("activated");
                        //						$("#" + guid + "_textarea").unbind();
                        //						$("#" + guid + "_input").unbind();
                    }
                    else
                    {
                        $("#" + guid + "_activate").addClass("activated");
                        $("#" + guid + "_discussionsection").addClass("activated");
                        // scroll here
                        $('#' + guid + '_content').scrollTo($("#" + guid + "_newtopic"), 800);
                    }
                });
            });

            // Join/Leave Button Click
            $("#" + guid + "_joinleave").bind("click", guid, function(e)
            {
                var guid = e.data;
                var w = BSC.UI.Ws.Get(guid);
                if (!w.data.key) return;

                // disable button+change to red
                $("#" + guid + "_joinleave").attr("disabled", "true");

                JoinLeaveGroup(guid);
            });

            // Save new discussion and post
            $("#" + guid + "_additem").click(function(e)
            {
                if (!BSC.D.P.IsFullMember())
                {
                    BSC.UI.AlertOnlyFullMembers();
                    return;
                }
                BSC.UI.ShowStatus(guid, "Saving...");

                var el = BSC.E.GetElement(e, "div", "a");
                if (!el.id) return;
                var t = (el.id).split("_");
                var guid = t[0];
                var action = t[1].toLowerCase();

                var w = BSC.UI.Ws.Get(guid);

                if (action == "saveandupdatebutton")
                {
                    var header = "" + $("#" + guid + "_discussiontitle").val();
                    var text = "" + $("#" + guid + "_discussionpost").val();
                    if (header.length == 0 || BSC.R.TextField.test(header))
                    {
                        BSC.UI.ShowHelpBubble(guid + "_discussiontitle", "info", "Please enter a headline", BSC.T.Get("JS.BSC.UI.Ws.Group.Add.Desc"), { top: -10, left: 400 });
                        $("#" + guid + "_discussiontitle").trigger("focus");
                        return;
                    }
                    if (text.length == 0)
                    {
                        BSC.UI.ShowHelpBubble(guid + "_discussionpost", "info", "Please enter text", BSC.T.Get("JS.BSC.UI.Ws.Group.Add.Desc"), { top: -10, left: 400 });
                        $("#" + guid + "_discussionpost").trigger("focus");
                        return;
                    }

                    var data = {
                        Title: header,
                        PostText: text
                    };

                    BSC.D.AjaxPost("/Group/" + w.data.key + "/DiscussionAdd", data, function(res)
                    {
                        // Html
                        $("#" + guid + "_posttextarea").val("");
                        if (res.success)
                            GO("/group/" + w.data.key + "/discussion/" + res.header);
                        else
                            BSC.UI.AlertOnlyFullMembers();

                        BSC.UI.HideStatus(guid, 200);
                    }, "json", "guid");

                }
            });

            // Action Menu Bind
            $("#" + guid + "_actionbuttons").bind("click", guid, function(e)
            {
                var guid = e.data;
                var el = BSC.E.GetElement(e, "dl", "dd");

                var t = (el.id).split("_");
                var action = t[1];

                var w = BSC.UI.Ws.Get(guid);
                if (!w.data.key) return;

                switch (action)
                {
                    case "leave":
                    case "join":
                        JoinLeaveGroup(guid);
                        break;
                }
            });

            $("#" + guid + "_becomeadmin").bind("click", guid, function(e)
            {
                var guid = e.data;
                var w = BSC.UI.Ws.Get(guid);
                if (!w.data.key) return;
                BecomeAdmin(guid);
            });

        };
        var unbind = function(guid)
        {
            $("#" + guid + " a").unbind();
            $("#" + guid + " ul").unbind();
            $("#" + guid + " dl").unbind();
            $("#" + guid + " div").unbind();
        };



        return {
            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Bind: bind,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Unbind: unbind,

            JoinLeaveGroup: JoinLeaveGroup
        };
    } ();
})();

BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Summary.Bind, null, "group.summary");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Summary.Unbind, null, "group.summary");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Group.TabsLoaded, null, "group.summary");

(function()
{
	BSC.UI.Ws.Group.Members = function()
	{

		var localcache = {};
		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var loaddata = function(p1)
		{
			var guid = p1;
			localcache[guid] = {};
			BSC.UI.ShowStatus(guid, "Loading...");
			var w = BSC.UI.Ws.Get(guid);
			var data = BSC.U.ParseQuery(w.query);

			// Load Attendees
			$("#" + guid + "_loader").hide();
			data.view = (data.view) ? data.view : (BSC.D.P.Setting("group.memberframe.view")) ? BSC.D.P.Setting("group.memberframe.view") : "big";
			BSC.D.AjaxGet("/Group/"+w.data.key+"/MemberList", data, function(res)
			{
				// Html
				$("#" + guid + "_result").html(res);
				$("#" + guid + "_loader").hide();
				$("#" + guid + "_result").show();
				// Event bind
				BSC.E.MapHrefs();
				var json = $("#" + guid + "_pagejson").getValue();
				var pagingData = BSC.D.JSON.Parse(json);
				BSC.UI.ProfilePaging.Bind("/Group/" + w.data.key + "/MemberList", guid, data, pagingData, "group.memberframe.view", { view: "big" });
				BSC.UI.HideStatus(guid, 200);
			}, "html", guid);

		};

		var addReceipient = function(e)
		{
			e.preventDefault(); e.stopPropagation();
			var el = BSC.E.GetElement(e, "dl", "dd");
			if (!el.id) return;
			var t = (el.id).split("_");
			var profileID = t[0];
			BSC.UI.Ws.Mail.Compose.AppendContact(profileID);

			//alert(profileID);
			return false;
		};



		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1)
		{
			var guid = p1;
		};

		/// <summary>
		/// Unbind events from ui elements
		/// </summary>
		var unbind = function(p1)
		{
			var guid = p1;
		};

		return {
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Loaddata: loaddata,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			AddReceipient: addReceipient,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind
		};
	} ();
})();


BSC.E.Subscribe("load", BSC.UI.Ws.Group.Members.Loaddata, null, "group.members");
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Members.Bind, null, "group.members");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Members.Unbind, null, "group.members");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Group.TabsLoaded, null, "group.members");







(function()
{
	BSC.UI.Ws.Group.Discussions = function()
	{
		return {
			Bind: function(guid)
			{
				
			},

			Unbind: function(guid)
			{

			}
		};
	} ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Discussions.Bind, null, "group.discussions");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Discussions.Unbind, null, "group.discussions");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Group.TabsLoaded, null, "group.discussions");





(function()
{
    BSC.UI.Ws.Group.Discussion = function()
    {
        return {
            Bind: function(guid)
            {
                $("#" + guid + "_joinaction").bind("click", guid, function(e)
                {
                    var guid = e.data;

//                    var el = BSC.E.GetElement(e, "div", "button");

//                    if (!el.id) return;

//                    var action = (el.id).split("_")[1];

                    BSC.UI.Ws.Group.Summary.JoinLeaveGroup(guid);

//                    alert(action);
//                    return false;
                });

                // Add Post Entry
                $("#" + guid + "_addPost").bind("click", guid, function(e)
                {
                    var guid = e.data;
                    var postText = "" + $("#" + guid + "_posttextarea").val();
                    if (postText.length == 0 || BSC.R.TextField.test(postText))
                    {
                        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Group.Add.OK"), null, BSC.T.Get("JS.BSC.UI.Ws.Group.Add.Wait"), BSC.T.Get("JS.BSC.UI.Ws.Group.Add.Desc"), null, null, "info");
                        return false;
                    }

                    var w = BSC.UI.Ws.Get(guid);
                    var data = {
                        Body: postText
                    };

                    BSC.D.AjaxPost("/Group/" + w.data.key + "/discussion/" + w.data.discussionkey + "/PostAdd", data, function(res)
                    {
                        // Html
                        BSC.UI.HideStatus(guid, 200);
                        $("#" + guid + "_noposts").remove();
                        $("#" + guid + "_posttextarea").val("")
                        if (res.success)
                        {
                            //$('#' + guid + '_content').scrollTo({ top: 0 }, 200);
                            $("#" + guid + "_bottompost").before(res.message);
                            // Appens new entry & scroll to top
                        }
                        else
                            BSC.UI.AlertOnlyFullMembers();
                    }, "json", guid);
                });

                // Remove post
                $("#" + guid + "_postlist").bind("click", guid, function(e)
                {
                    var el = BSC.E.GetElement(e, "div", "a");
                    if (!el.id) return;

                    if ($(el).hasClass("removepost"))
                    {
                        BSC.UI.Alert("Remove", "Cancel", "Are you sure?", "<br/>", function(e)
                        {
                            var guid = e.guid;
                            var id = el.id;
                            var w = BSC.UI.Ws.Get(guid);
                            var data = {
                                id: id
                            };

                            BSC.D.AjaxPost("/Group/" + w.data.key + "/discussion/" + w.data.discussionkey + "/PostRemove", data, function(res)
                            {
                                if (res.success)
                                {
                                    $("#" + id + "_entirepost").fadeOut(300, function() { $(this).remove(); });
                                }
                            }, "json", guid);
                        }, { guid: e.data, id: this.id }, "info");

                        BSC.E.Stop(e);
                        return false;
                    }
                });

                // Scroll to input field
                $("#" + guid + "_writepost").bind("click", guid, function(e)
                {
                    var guid = e.data;
                    $('#' + guid + '_content').scrollTo($("#" + guid + "_addPost"), 400);
                });

                $("#" + guid + "_posttextarea").bind("focus", guid, function(e)
                {
                    if (!BSC.D.P.IsLoggedIn())
                    {
                        $(this).trigger("blur");
                        BSC.UI.AlertLogin();
                        return false;
                    }
                    else if (BSC.D.P.IsFullMember() && !BSC.D.P.IsPaying())
                    {
                        $(this).trigger("blur");
                        BSC.UI.AlertCurrentlyFREE();
                        return false;
                    }
                });
            },

            Unbind: function(guid)
            {
                $("#" + guid + "_writepost").unbind();
                $("#" + guid + "_back").unbind();
                $("#" + guid + "_addPost").unbind();
                $('#' + guid + '_content').unbind();
                $("#" + guid + "_posttextarea").unbind();
                $("#" + guid + " div.postlist").unbind();
            }
        };
    } ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Discussion.Bind, null, "group.discussion");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Discussion.Unbind, null, "group.discussion");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Group.TabsLoaded, null, "group.discussion");























(function() {
	BSC.UI.Ws.Group.Search = function() {
		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var load = function(p1) {
			var guid = p1;
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1) {
			var guid = p1;
			$("#" + guid + "_searchGroups").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.OnSearchGroupClick);
			$("#" + guid + "_searchGroups1").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.OnSearchGroupClick);

			$("#" + guid + " div.searchoptions input").focus(function() { var el = $(this); if (el.val() == el.attr("title")) { el.val(""); } });
			$("#" + guid + "_searchGroups2").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.OnSearchGroupClick);
			$("#" + guid + "_category").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.BrowseGroupCategories);
			$("#" + guid + "_searchInput").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.EmptyInput);
			$("#" + guid + "_searchInput1").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.EmptyInput);
			$("#" + guid + "_searchInput2").bind("click", { guid: guid }, BSC.UI.Ws.Group.Search.EmptyInput);

			BSC.UI.Ws.Group.Basebind(guid);
		};


		/// <summary>
		/// Unbind events from ui elements
		/// </summary>
		var unbind = function(p1) {
			var guid = p1;
			$("#" + guid + " a").unbind();
			$("#" + guid + " ul").unbind();
			$("#" + guid + " dl").unbind();
			$("#" + guid + " div").unbind();
		};

		var onSearchGroupClick = function(e) {
			var el = BSC.E.GetElement(e, "a", "span");
			if (el == null) return;
			var t = (el.id).split("_");
			var guid = t[0];
			var name = t[1];
			var value = "";
			if (el.className == "title")
				value = $("#" + guid + "_searchInput").val();
			if (el.className == "creator")
				value = $("#" + guid + "_searchInput1").val();
			if (el.className == "keyword")
				value = $("#" + guid + "_searchInput2").val();
			var category = $("#" + guid + "_default").val();
			GO("/groups/search/" + value);
			return;

		};
		var browseGroupCategories = function(e) {
			var el = BSC.E.GetElement(e, "ul", "li");
			if (el == null) return;
			var t = (el.id).split("_");
			var id = t[0];

			GO("/groups/search/?category=" + id);

			return;
		};

		var emptyInput = function(e) {
			var el = BSC.E.GetElement(e, "div", "input");
			if (el == null) return;
			var t = (el.id).split("_");
			var guid = t[0];
			var name = t[1];
			if (name == "searchInput")
				$("#" + guid + "_searchInput").val("");
			if (name == "searchInput1")
				$("#" + guid + "_searchInput1").val("");
			if (name == "searchInput2")
				$("#" + guid + "_searchInput2").val("");
			return;

		};


		return {
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Loaddata: load,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			OnSearchGroupClick: onSearchGroupClick,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			BrowseGroupCategories: browseGroupCategories,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			EmptyInput: emptyInput,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Group.Search.Loaddata, null, "groups.search");
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Search.Bind, null, "groups.search");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Search.Unbind, null, "groups.search");


(function()
{
	BSC.UI.Ws.Group.Search.Result = function()
	{
		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var load = function(p1)
		{
			var guid = p1;
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1)
		{
			var guid = p1;
			BSC.UI.Ws.Group.Basebind(guid);
		};

		/// <summary>
		/// Unbind events from ui elements
		/// </summary>
		var unbind = function(p1)
		{
			var guid = p1;
		};


		return {
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Loaddata: load,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Group.Search.Result.Loaddata, null, "group.searchresult");
BSC.E.Subscribe("bind", BSC.UI.Ws.Group.Search.Result.Bind, null, "group.searchresult");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Group.Search.Result.Unbind, null, "group.searchresult");
