﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />

(function()
{
    BSC.UI.Ws.Profile = function()
    {
        // Private Properties
        var localdata = {};
        var currentProfile = null;
        var currentWindow = null;
        // Public Implementation
        /// <summary>
        /// @Author		: Danni Als (dals@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2009-04-01	: (DALS) Created.
        /// @Description
        ///				Callback for event TabsLoaded
        /// </summary>
        var tabsloaded = function(p1)
        {
            var guid = p1;
            basebind(guid);
        }

        /// <summary>
        /// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2008-11-10	: (RTG) Created.
        /// @Description
        ///				Callback for event Window Resized
        /// </summary>
        function onResize(p1)
        {
            var guid = p1;
        };

        /// <summary>
        /// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2008-11-10	: (RTG) Created.
        /// @Description
        ///				Callback for event Window Load
        /// </summary>
        var load = function(p1)
        {
            var guid = p1;
            BSC.D.AjaxAbort(guid);
            currentWindow = guid;


            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query); //, { id: 'string?' }
            onResize(guid);

            if (localdata[guid].data)
            {
                var name = localdata[guid].data.Name;
                $("ul#" + guid + "_tabs_content_ul li:first a span").html(name);
                $("h1#" + guid + "_title").html(name);
                $("#Taskbar_" + guid + " a span").html(name);
                $("#Taskbar_" + guid + " a").attr("title", name);

                localdata[guid].profile = localdata[guid].data;
            }

            BSC.UI.ShowStatus(guid);
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query);
            if (jQ)
                var feedid = jQ.feedid;
            // Load Feeds
            BSC.D.AjaxGet("/" + w.data.key + "/Stream", { feedid: feedid, reset: true }, function(res)
            {
                // Html
                if (getInternetExplorerVersion() >= 7)
                {
                    if (res.length < 100)
                        document.getElementById(guid + "_feeds").innerHTML = "<center>The content has either been deleted or is not available.</center>";
                    else
                        document.getElementById(guid + "_feeds").innerHTML = res;
                }
                else
                {
                    if (res.length < 100)
                        $("#" + guid + "_feeds").append("<center>The content has either been deleted or is not available.</center>");
                    else
                        $("#" + guid + "_feeds").append(res);
                }

                if (feedid != null)
                {
                    $("#" + guid + "_showentire").css("display", "");
                    $("#" + guid + "_seeolderboarditems").css("display", "none");
                }
                $("#" + guid + "_feedsloader").hide();
                // Event bind
                BSC.E.MapHrefs();
                // Hide Loading Status
                BSC.UI.HideStatus(guid, 200);

                try
                {
                    $("#" + guid + " textarea.expand").autogrow();
                }
                catch (e) { }
                $("#" + guid + "_showentire").bind("click", guid, function(e)
                {
                    var guid = e.data;
                    BSC.D.AjaxGet("/" + w.data.key + "/Stream", { reset: true }, function(res)
                    {
                        $("#" + guid + "_feeds").html(res);
                        $("#" + guid + "_showentire").css("display", "none");
                        $("#" + guid + "_seeolderboarditems").css("display", "");
                        BSC.UI.Ws.Get(guid).uri = "/" + w.data.key;
                        BSC.P.ChangeURI(BSC.UI.Ws.Get(guid).uri);
                        $("#" + guid + "_feedsloader").hide();
                        BSC.E.MapHrefs();
                        $("#" + guid + " textarea.expand").autogrow();
                        //            		var json = $("#" + guid + "_feed_json").val();
                        //            		var data = BSC.D.JSON.Parse(json);
                    }, "html", guid);
                });
            }, "html", guid);

            return;
        };


        function getInternetExplorerVersion()
        // Returns the version of Windows Internet Explorer or a -1
        // (indicating the use of another browser).
        {
            var rv = -1; // Return value assumes failure.
            if (navigator.appName == 'Microsoft Internet Explorer')
            {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null)
                    rv = parseFloat(RegExp.$1);
            }
            return rv;
        }

        /// <summary>
        /// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2008-11-10	: (RTG) Created.
        /// @Description
        ///				Callback for event Window binding events
        /// </summary>
        var bind = function(p1)
        {
            var guid = p1;
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query);
            if (jQ.login && !BSC.D.P.IsLoggedIn())
                BSC.UI.AlertLogin();

            localdata[guid] = { days: 3 };
            var json = $("#" + guid + "_json").getValue();
            var data = BSC.D.JSON.Parse(json);
            localdata[guid].data = data;

            baseActionMenuBind(guid);

            $("#" + guid + "_bookmark").bind("mousedown", guid,
		        function(e)
		        {
		            var guid = e.data;
		            var w = BSC.UI.Ws.Get(guid);
		            var jQ = BSC.U.ParseQuery(w.query);
		            BSC.UI.Bookmark.Add({ key: w.data.key, type: "profile", title: null, uri: null, callback: BSC.UI.Bookmark.Confirm, data: { guid: guid, h: "Profile is now bookmarked"} });
		            e.preventDefault(); e.stopPropagation(); return;
		        }
            );


            //
            /*
            var guid = p1;
            var callback = p2;
            var remove = p3;
            var picture = p4;
            
            picture.Rank
            picture.HasRated
            
            **/
            //            if (data && !data.hr)
            //            {
            //                BSC.UI.Ranking.Add(guid, function(guid, value)
            //                {
            //                    BSC.D.AjaxPost("/picture/rankingadd", { id: data.pic, value: value }, function(r)
            //                    {
            //                        $("#" + guid + "_profile_rating").html("<center style=\"font-weight:bold; font-size:14px;\">Thank you for rating this photo.</center>");
            //                    }, "json", guid);
            //                }, false, { Rank: data.s, HasRated: false });
            //            }
            //            else if (data && data.s > -1)
            //            {
            //                BSC.UI.Ranking.SetValue(guid, data.s);
            //            }

            $("#" + guid + "_ratingbuttons button").bind("click", guid, function(e)
            {

            });


            $("#" + guid + "_ratingbuttons button").bind("click", guid, function(e)
            {
                var guid = e.data;
                var value = $(this).text();
                var score = parseInt(value);
                var applicantID = localdata[guid].data.ID;  //jQ.id;
                var cc = localdata[guid].data.CC;  //jQ.id;

                $("#" + guid + "_ratingbuttons").html("<img src=\"/beautifulpeoplecdn/images/blueloader_small.gif\" />");

                $.post("/rating/givescore", { id: applicantID, value: score, cc: cc }, function(r)
                {
                    BSC.UI.W.Reload(guid);
                    BSC.UI.Ws.Rating.UpdateView(guid, value);
                }, "json");
            });



            // Hover menu on profile picture
            $("#" + guid + "_profilehover").hover(
	            function()
	            {
	                $(".footer").animate({
	                    marginBottom: "0"
	                }, 110);
	            },
                function()
                {
                    $(".footer").animate({
                        marginBottom: "-27"
                    }, 110);
                }
	        );

            $("#" + guid + "_url").click(
	            function()
	            {
	                $(this).trigger("focus").select();
	            }
	        );

            if (!BSC.D.P.IsLoggedIn() || !BSC.D.P.IsPaying())
            {
                $("#" + guid + "_comment").bind("focus", guid,
				function(e)
				{
				    //				    if (!localdata[guid].data.isfriends && localdata[guid].data.bsf > BSC.D.P.GetData("BeautyScore"))
				    //				    {
				    //				        BSC.UI.AlertBeautyFilter(localdata[guid].data.Name, localdata[guid].data.g, localdata[guid].data.bsfd, BSC.D.P.GetData("BeautyScoreDisplay"));
				    //				        return;
				    //				    }

				    $("#" + guid + "_comment").trigger("blur");
				    if (!BSC.D.P.IsLoggedIn())
				        BSC.UI.AlertLogin();
				    else
				        BSC.UI.AlertCurrentlyFREE();

				    return false;
				});
            }
            //            else if (!localdata[guid].data.isfriends && localdata[guid].data.bsf > BSC.D.P.GetData("BeautyScore"))
            //            {
            //                $("#" + guid + "_comment").bind("focus", guid,
            //				function(e)
            //				{
            //				    $(this).trigger("blur");
            //				    BSC.UI.AlertBeautyFilter(localdata[guid].data.Name, localdata[guid].data.g, localdata[guid].data.bsfd, BSC.D.P.GetData("BeautyScoreDisplay"));
            //				    return false;
            //				});
            //            }



            $("#" + guid + "_addcomment").bind("click", guid,
				function(e)
				{
				    var w = BSC.UI.Ws.Get(guid);

				    var comment = $("#" + guid + "_comment").getValue();
				    if (comment === "")
				    {
				        //ok_key, cancel_key, header, description, callback, data, icon
				        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", "No text inserted in comment field. Insert text.", null, null, "info");
				        return;
				    }
				    if (comment.length > 2249)
				    {
				        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", "Text in comment field too long.", null, null, "info");
				        return;
				    }
				    if (BSC.R.TextField.test(comment))
				    {
				        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", "Invalid text inserted.", null, null, "info");
				        return;
				    }
				    // Server
				    BSC.D.AjaxPost("/" + w.data.key + "/addcomment", { comment: comment }, function(res)
				    {
				        $("#" + guid + "_comment").val("");
				        if (!res.success) return;

				        $("#" + guid + "_feeds div.section.first").removeClass("first");
				        $("#" + guid + "_feeds div.iconwrap.first").removeClass("first");
				        $("#" + guid + "_emptyfeed").after(res.message);
				        BSC.E.MapHrefs();

				    }, "json", guid);
				}
			);

            // Status textarea and events
            $("#" + guid + "_statustext").bind("focus", guid,
	            function(e)
	            {

	                $("#" + guid + "_statustext").val("");
	                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;
	                }
	            }).bind("blur", guid,
	            function(e)
	            {
	                if (($("#" + guid + "_statustext").getValue()).length == 0)
	                    $("#" + guid + "_statustext").val($("#" + guid + "_statustext").attr("title"));
	            }
			);

            $("a#" + guid + "_setStatus").bind("click", guid,
	            function(e)
	            {

	                if (BSC.D.P.IsFullMember() && !BSC.D.P.IsPaying())
	                {
	                    BSC.UI.AlertCurrentlyFREE();
	                    return false;
	                }
	                var guid = e.data;
	                var w = BSC.UI.Ws.Get(guid);
	                if (!w.data.key) return;

	                var status = $("#" + guid + "_statustext").getValue();
	                if (status === "")
	                {
	                    //ok_key, cancel_key, header, description, callback, data, icon
	                    BSC.UI.Alert("Ok", null, "Info", "No text inserted in status field. Insert text.", null, null, "info");
	                    return;
	                }
	                if (status.length > 2249)
	                {
	                    BSC.UI.Alert("Ok", null, "Info", "Text in status field too long.", null, null, "info");
	                    return;
	                }
	                if (BSC.R.TextField.test(status))
	                {
	                    BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", "Invalid text inserted.", null, null, "info");
	                    return;
	                }
	                var p = new BSC.D.Param("BSC.WS.JS.Profile.ChangeStatus");
	                p.Add("status", status);
	                p.Add("windowID", guid);
	                BSC.D.Send(p, "Content.Profile", "ChangeStatus", function(res)
	                {
	                    if (res.d.success)
	                    {
	                        $("#" + guid + "_statustext").val("");
	                        $("#" + guid + "_feeds div.section.first").removeClass("first");
	                        $("#" + guid + "_feeds div.iconwrap.first").removeClass("first");

	                        $("#" + guid + "_emptyfeed").after(res.d.message);

	                        $("#" + guid + "_lateststatustext").html(status);

	                        //	                        if ($("#" + guid + "_feeds p").attr("id") != res.d.header)
	                        //	                        {
	                        //	                            if (getInternetExplorerVersion() >= 8)
	                        //	                                $("#" + guid + "_emptyfeed").appendChild("<p id=" + res.d.header + " class='date'><span>" + res.d.data + "</span></p>"); //.document.getElementById(guid + "_emptyfeed").appendChild(res.d.message);
	                        //	                            else
	                        //	                                $("#" + guid + "_emptyfeed").append(res.d.message);
	                        //	                        }
	                        //	                        if (getInternetExplorerVersion() >= 8)
	                        //	                            $("#" + guid + "_emptyfeed").appendChild(res.d.message); //.document.getElementById(guid + "_emptyfeed").appendChild(res.d.message);
	                        //	                        else
	                        //	                            $("#" + guid + "_emptyfeed").append(res.d.message);

	                    }
	                });
	            }
			);

            $("#" + guid + "_seeolderboarditems").bind("click", guid,
	            function(e)
	            {
	                var w = BSC.UI.Ws.Get(guid);
	                if (!w.data.key) return;

	                localdata[guid].days++;

	                $("#" + guid + "_feedsloader").show();
	                $("#" + guid + "_content").scrollTo({ top: '+=150px' }, 800);

	                BSC.D.AjaxGet("/" + w.data.key + "/Stream", { reset: false }, function(res)
	                {
	                    if (!localdata[guid].totalpages)
	                    {
	                        var json = $("#" + guid + "_feed_json").getValue();
	                        var data = BSC.D.JSON.Parse(json);
	                        if (data) localdata[guid].totalpages = data.TotalPages;
	                    }

	                    // Hide See Older button
	                    if (localdata[guid].page >= localdata[guid].totalpages)
	                        $("#" + guid + "_seeolderboarditems").hide();

	                    // Html
	                    $("#" + guid + "_feeds").append(res);
	                    $("#" + guid + "_feedsloader").hide();

	                    $("p.date + div").addClass("first");

	                    // Event bind
	                    BSC.E.MapHrefs();
	                    // Hide Loading Status
	                    BSC.UI.HideStatus(guid, 200);
	                }, "html", guid);
	            }
			);


            $("#" + guid + "_feeds").bind("click", guid,
	            function(e)
	            {
	                var guid = e.data;
	                var w = BSC.UI.Ws.Get(guid);

	                var inputs = BSC.E.GetTarget(e);
	                if (inputs && inputs.type && inputs.type == "textarea" && !BSC.D.P.IsLoggedIn())
	                {
	                    BSC.UI.AlertLogin();
	                    return;
	                }

	                var el = BSC.E.GetElement(e, "div", "a");
	                if (el.id)
	                {
	                    if ($(el).hasClass("menuaction"))
	                    {
	                        $(el).next().show();
	                    }
	                    else if ($(el).hasClass("menuaction_item"))
	                    {
	                        var guid = e.data;
	                        var t = (el.id).split("_");
	                        var feedID = t[0];
	                        var action = t[1];
	                        switch (action)
	                        {
	                            case "delete":
	                                $("#board_" + feedID).fadeOut(300, function(e) { $(this).remove(); });
	                                BSC.D.Send({ data: feedID }, "Content.Profile", "FeedEntry_Remove", function(res) { });
	                                break;
	                        }
	                    }
	                    else if ($(el).hasClass("addfeedcomment"))
	                    {
	                        var guid = e.data;
	                        var t = (el.id).split("_");
	                        var feedID = t[1];

	                        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;
	                        }

	                        $("#" + guid + "_" + feedID + "_addcomment").remove();
	                        $("#" + guid + "_" + feedID + "_comments").slideDown("fast");
	                    }
	                    else if ($(el).hasClass("savefeedcomment"))
	                    {
	                        var guid = e.data;
	                        var t = (el.id).split("_");
	                        var feedID = t[1];
	                        var comment = $("#" + guid + "_" + feedID + "_feedcomment").getValue();
	                        var commentsCount = $("#" + guid + "_" + feedID + "_comments div.entrycomment").length;

	                        if (comment.length == 0)
	                        {
	                            BSC.UI.Alert("OK", null, "Please add some text", "", null, null, "info");
	                            return false;
	                        }

	                        BSC.D.AjaxPost("/Stream/AddFeedComment", { comment: comment, feedID: feedID, profileKey: w.data.key, commentsCount: commentsCount }, function(res)
	                        {
	                            $("#" + guid + "_" + feedID + "_feedcomment").val("").css({ height: 0 });
	                            $("#" + guid + "_" + feedID + "_comments div.pointer").remove();
	                            $("#" + guid + "_" + feedID + "_addcomment").remove();
	                            $("#" + guid + "_" + feedID + "_newcomment").before(res.FeedHtml);
	                            //$("#" + guid + "_" + feedID + "_comments div.t").after(res.FeedHtml);
	                            $("#" + guid + "_" + res.ID + "_entry").fadeIn("slow");
	                        }, "json", guid);
	                    }
	                    else if ($(el).hasClass("removefeedcomment"))
	                    {
	                        var guid = e.data;
	                        var t = (el.id).split("_");
	                        var feedCommentID = t[1];
	                        var feedID = t[3];

	                        BSC.UI.Alert("Remove", "Cancel", "Are you sure ?", "You are about to remove a feedcomment. This cannot be undone.", function(e)
	                        {
	                            $("#" + guid + "_" + feedCommentID + "_entry").fadeOut("fast", function()
	                            {
	                                // Server call
	                                BSC.D.AjaxPost("/Stream/RemoveFeedComment", { feedCommentID: feedCommentID, feedID: feedID }, function(res) { }, "json", guid);
	                                $(this).remove();
	                            });
	                        }, null, "info");
	                    }

	                    e.preventDefault(); e.stopPropagation();
	                }
	            }
			);

            basebind(guid);
        };







        /// <summary>
        /// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2008-11-10	: (RTG) Created.
        /// @Description
        ///				Callback for event Window unbinding events
        /// </summary>
        var unbind = function(p1)
        {
            var guid = p1;
            $("#" + guid + " a").unbind();
            $("#" + guid + "_url").unbind();
            $("#" + guid + " .pagingactions").unbind();
            $("#" + guid + " textarea.expand").unbind();
            BSC.UI.Comment.Unbind(guid);
            delete localdata[guid];
        };

        /// <summary>
        /// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
        /// @Created	: 
        /// 
        /// @History
        ///				2008-11-10	: (RTG) Created.
        /// @Description
        ///				Binds ProfileID on tabs
        /// </summary>
        var basebind = function(p1)
        {
            var guid = p1;
            var w = BSC.UI.Ws.Get(guid);
            if (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(/profile/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 = newHref;
                });
            }
        };


        var baseActionMenuBind = function(p1)
        {
            var guid = p1;
            // Action menu
            $("#" + guid + " dl.actions").bind("click", guid, function(e)
            {
                var result = false;
                var el = BSC.E.GetElement(e, "dl", "a");
                if (!el.id) return true;
                var t = (el.id).split("_");
                var guid = e.data;
                var action = t[1];
                var profileid = t[2];
                var w = BSC.UI.Ws.Get(guid);
                var elm = $(el);

                //                alert(localdata[guid].data.isfriends);

                //                return;

                if (action == "bookmark")
                    BSC.UI.Bookmark.Add({ key: w.data.key, type: "profile", title: null, uri: null, callback: BSC.UI.Bookmark.Confirm, data: { guid: guid, h: BSC.T.Get("JS.BSC.UI.Ws.Profile.OnActionClick.Bookmark")} });
                else if (action == "reportprofile")
                    GO("/report/" + w.data.key);
                else if (action == "sendmail")
                {
                    if (BSC.D.P.IsApplicant())
                    {
                        BSC.UI.AlertCurrentlyApplicant();
                        return;
                    }
                    //                    if (!localdata[guid].data.isfriends && localdata[guid].data.bsf > BSC.D.P.GetData("BeautyScore"))
                    //                    {
                    //                        BSC.UI.AlertBeautyFilter(localdata[guid].data.Name, localdata[guid].data.g, localdata[guid].data.bsfd, BSC.D.P.GetData("BeautyScoreDisplay"));
                    //                        return;
                    //                    }
                    BSC.UI.Ws.Mail.Compose.AppendContact(profileid);
                }
                else if (action == "photoalbum")
                    GO("/" + w.data.key + "/photos/");
                else if (action == "startchat")
                {
                    if (BSC.D.P.IsApplicant())
                    {
                        BSC.UI.AlertCurrentlyApplicant();
                        return;
                    }
                    BSC.UI.Ws.Chat.Room.CreatePrivateChat(profileid);
                }
                else if (action == "requestfriendship")
                    RequestFriendship(el, guid, w.data.key);
                else if (action == "pendingfriendship")
                {
                    RequestFriendshipAgain(el, guid, w.data.key);
                }
                else if (action == "sendblink")
                {
                    //                    if (!localdata[guid].data.isfriends && localdata[guid].data.bsf > BSC.D.P.GetData("BeautyScore"))
                    //                    {
                    //                        BSC.UI.AlertBeautyFilter(localdata[guid].data.Name, localdata[guid].data.g, localdata[guid].data.bsfd, BSC.D.P.GetData("BeautyScoreDisplay"));
                    //                        return;
                    //                    }
                    $("#" + guid + "_sendblink").parent().css("height", 27);
                    $("#" + guid + "_flirtbox").toggle();
                    BSC.E.Stop(e);
                }
                return result;
            });

            //            $("#" + guid + "_sendblink").bind("click", guid, function(e) {
            //                
            //            });

            $("#" + guid + "_CancelFlirt").bind("click", guid, function(e)
            {
                var guid = e.data;
                $("#" + guid + "_flirtbox").hide();
                BSC.E.Stop(e);
            });

            $("#" + guid + "_flirtid").bind("change", guid, function(e)
            {
                var flirtid = $("#" + guid + "_flirtid").val();

                if (flirtid == "upgrade")
                {
                    GO("/subscription");
                    return false;
                }
            });

            $("#" + guid + "_SendFlirt").bind("click", guid, function(e)
            {
                if (!BSC.D.P.IsLoggedIn())
                    BSC.UI.AlertLogin();
                else
                {
                    BSC.E.Stop(e);
                    var guid = e.data;
                    var w = BSC.UI.Ws.Get(guid);
                    var flirtid = $("#" + guid + "_flirtid").val();

                    if (flirtid == "upgrade")
                    {
                        GO("/subscription");
                        return false;
                    }

                    var privacy = $("#" + guid + "_public").is(':checked');
                    BSC.D.AjaxPost("/" + w.data.key + "/GiveFlirt", { flirtID: flirtid, privacy: privacy }, function(res)
                    {
                        if (res.success)
                        {
                            $("#" + guid + "_flirtbox").hide();
                            if (privacy)
                                BSC.UI.W.Reload(guid);
                            else
                                BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, BSC.T.Get("JS.BSC.UI.Ws.Profile.BlinkSent.H"), BSC.T.Get("JS.BSC.UI.Ws.Profile.BlinkSent.M"), null, null, "info");
                        }
                        else
                        {
                            BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", res.message, null, null, "info");
                            $("#" + guid + "_flirtbox").hide();
                        }


                    }, "json", guid);
                }


            });
        };
        var RequestFriendshipAgain = function(el, p1, p2)
        {
            var guid = p1;
            var profileKey = p2;
            if (!BSC.D.P.IsLoggedIn())
            {
                BSC.UI.AlertLogin();
                return;
            }

            var isFriends = $(el).hasClass("removefriendship");

            var icon = (isFriends) ? "removefriend" : "friend";
            var header = (isFriends) ? "Remove friendship" : "Resend friends request?";
            var description = (isFriends) ? "Remove this profile from your friends?" : "You already requested friendship<br /> with this person.<br /><br />Are you sure you want to<br /> send the request again?";

            BSC.UI.Alert("Ok", "Cancel", header, description, function(e)
            {
                if (!e.profileKey) return;

                // UI
                if (isFriends)
                    $(el).removeClass().addClass("profileaction requestfriendship action").html("<span>Request friendship</span>");

                $.post("/" + profileKey + "/RequestFriendship", null, function(res)
                {
                    //BSC.UI.W.Reload(guid);
                }, "json");
            }, { guid: guid, profileKey: profileKey }, icon);
        };

        var RequestFriendship = function(el, p1, p2)
        {
            var guid = p1;
            var profileKey = p2;
            if (!BSC.D.P.IsLoggedIn())
            {
                BSC.UI.AlertLogin();
                return;
            }

            if (!profileKey) return;

            var isFriends = $(el).hasClass("removefriendship");

            var icon = (isFriends) ? "removefriend" : "friend";
            var header = (isFriends) ? "Remove friendship" : "Request friendship";
            var description = (isFriends) ? "Remove this profile from your friends?" : "Add this profile as a friend?";

            BSC.UI.Alert("Ok", "Cancel", header, description, function(e)
            {
                // UI
                if (isFriends)
                    $(el).removeClass().addClass("profileaction requestfriendship action").html("<span>Request friendship</span>");
                else
                    $(el).removeClass().addClass("profileaction pendingfriendship action").html("<span>Friends request pending</span>");

                $.post("/" + profileKey + "/RequestFriendship", null, function(res)
                {
                    //BSC.UI.W.Reload(guid);
                }, "json");
            }, null, icon);
        };


        // Private Methods


        //---------------------------
        /// <summary>
        /// @Author		: Danni Als (dals@beautifulpeople.com)
        /// @Created	: 19.05.2009
        ///
        /// @History
        ///             25.05.2009	: (DALS) Modified.
        /// @Description
        ///				Give applicant score.
        /// </summary>
        var giveScore = function(p1)
        {
            var value = p1;
            var guid = currentWindow;

            if (!BSC.D.P.IsFullMember())
            {
                BSC.UI.AlertLogin();
                return;
            }

            // No value
            if (value == 0)
            {
                BSC.UI.Alert(BSC.T.Get("alert", "rating_suspendprofile_yes"), BSC.T.Get("alert", "rating_suspendprofile_no"), BSC.T.Get("alert", "rating_suspendprofile_header"), BSC.T.Get("alert", "rating_suspendprofile_msg"), BSC.UI.Ws.Rating.Givescore, v, "info");
                return false;
            }

            var w = BSC.UI.Ws.Get(guid);

            // Server call
            // Load Give Score
            var p = new BSC.D.Param("BSC.WS.JS.Rating.GiveScoreKey");
            p.Add("key", w.data.key);
            p.Add("score", value);
            BSC.D.Send(p, "Content.Rating", "GiveScore", function()
            {
                var pdata = localdata[guid].profile;
                // IE friendly
                $("#" + guid + "_profilerating").replaceWith("<b id=\"" + guid + "_profilerating2\"></b>");
                var flashvars = { ScoreText_1: pdata.v1, ScoreText_2: pdata.v2, ScoreText_3: pdata.v3, ScoreText_4: pdata.v4 };
                var params = { id: "flashbg", name: "flashbg", wmode: 'transparent', allowScriptAccess: 'always', allowFullScreen: 'false', quality: 'high' };
                var attributes = { id: "flashbg", name: "flashbg", wmode: 'transparent', allowScriptAccess: 'always', allowFullScreen: 'false', quality: 'high' };
                swfobject.embedSWF("/Content/Flash/Rating/see_result_v2b.swf", guid + "_profilerating2", "400", "180", "9.0.0", attributes, flashvars, params);
                $("#" + guid + "_ratingheadlines h2").html(BSC.T.Get("Profile.Main.JSHeadline")); //"Thank you for rating");
                var text = BSC.T.Get("Profile.Main.JSText").replace(/\{username\}/i, pdata.Name).replace(/\{hours\}/i, pdata.Hours);
                $("#" + guid + "_ratingheadlines p").html(text); //"Your vote for {username} was registered");

                /**TODO: Consider the case where rating window is open. Needs updating. **/

                BSC.UI.Ws.Rating.UpdateView(pdata.ID, value);
            });
        };
        //---------------------------

        return {
            TabsLoaded: tabsloaded,
            Basebind: basebind,
            BaseActionMenuBind: baseActionMenuBind,
            //			LoadFeedList: loadFeedList,
            //			LoadFriendsPreview: loadFriendsPreview,
            OnResize: onResize,
            Load: load,
            Bind: bind,
            Unbind: unbind,
            Givescore: giveScore
        };
    } ();
})();
// Subscribe to events
BSC.E.Subscribe("load", BSC.UI.Ws.Profile.Load, null, "profile");
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.Bind, null, "profile");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Profile.Unbind, null, "profile");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Profile.TabsLoaded, null, "profile");



/// BSC.UI.Ws.Profile.About
(function()
{
	BSC.UI.Ws.Profile.About = function()
	{
		var bind = function(p1)
		{
			var guid = p1;
			BSC.UI.Ws.Profile.BaseActionMenuBind(guid);

			// Hover menu on profile picture
			$("#" + guid + "_profilehover").hover(
	            function()
	            {
	            	$(".footer").animate({
	            		marginBottom: "0"
	            	}, 110);
	            },
                function()
                {
                	$(".footer").animate({
                		marginBottom: "-27"
                	}, 110);
                }
	        );

			BSC.UI.Ws.Profile.Basebind(guid);
		};

		var unbind = function(p1)
		{
			var guid = p1;
		};
		return {
			Bind: bind,
			unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.About.Bind, null, "profile.about");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Profile.About.Unbind, null, "profile.about");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Profile.TabsLoaded, null, "profile.about");




/// BSC.UI.Ws.Profile.Photos
(function()
{
    BSC.UI.Ws.Profile.Photos = function()
    {
        var bind = function(p1)
        {
            var guid = p1;
        };

        return {
            Bind: bind
        };
    } ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.Photos.Bind, null, "profile.photos");
BSC.E.Subscribe("tabsloaded", BSC.UI.Ws.Profile.TabsLoaded, null, "profile.photos");



/// BSC.UI.Ws.Profile.Photos
(function()
{
	BSC.UI.Ws.Profile.Photos = function()
	{
		var localdata = {};
		var page = 1;
		var pagesize = 20;
		var currentPage = 1;
		var numberOfPages = 0;

		var load = function(p1)
		{
			var guid = p1;
			var w = BSC.UI.Ws.Get(guid);
			var data = BSC.U.ParseQuery(w.query);
			
			if(!w.data.albumkey) return;

			$("#" + guid + "_loader").show();
			BSC.UI.ShowStatus(guid, "Loading...");
			data.view = (data.view) ? data.view : (BSC.D.P.Setting("profile.main.viewphotos")) ? BSC.D.P.Setting("profile.main.viewphotos") : "small";
			BSC.D.AjaxGet("/" + w.data.key + "/PhotoList/" + w.data.albumkey, data, function(res)
			{
				// Html
				$("#" + guid + "_loader").hide();
				$("#" + guid + "_result").html(res).show();
				// Event bind
				BSC.E.MapHrefs();
				var json = $("#" + guid + "_pagejson").getValue();
				var pagingData = BSC.D.JSON.Parse(json);
				BSC.UI.ProfilePaging.Bind("/" + w.data.key + "/PhotoList/" + w.data.albumkey, guid, data, pagingData, "profile.main.viewphotos", { view: "small" });
				BSC.UI.HideStatus(guid, 200);
			}, "html", guid);
		};

		var bind = function(p1)
		{
			var guid = p1;
			BSC.UI.Ws.Profile.Basebind(guid);
		};

		var unbind = function(p1)
		{
			var guid = p1;
			$("#" + guid + "_topbar").unbind();
			localdata = {};
		};

		return {
			Load: load,
			Bind: bind,
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Profile.Photos.Load, null, "profile.photos");
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.Photos.Bind, null, "profile.viewphotos");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Profile.Photos.Unbind, null, "profile.viewphotos");


/**Friends**/
(function()
{
    BSC.UI.Ws.Profile.Admin = function()
    {
        var bind = function(p1)
        {
            var guid = p1;
            //$("#" + guid + "_accordion").accordion();

//            $('.accordion .head').click(function()
//            {
//                $(this).next().toggle();
//                return false;
//            }).next().hide();
        
        };

        var unbind = function(p1)
        {
            var guid = p1;
        };

        return {
            Bind: bind,
            Unbind: unbind
        };
    } ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.Admin.Bind, null, "profile.admin");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Profile.Admin.Unbind, null, "profile.admin");

/**Friends**/
(function()
{
	BSC.UI.Ws.Profile.Friends = function()
	{
		var load = function(p1)
		{
			var guid = p1;
			Loadfriends(guid);
		};

		var bind = function(p1)
		{
			var guid = p1;
			BSC.UI.Ws.Profile.Basebind(guid); 
			BSC.UI.Ws.Profile.BaseActionMenuBind(guid);
			

			$("#" + guid + "_friendselector").bind("click", guid, function(e)
			{
				var guid = e.data;
				var el = BSC.E.GetElement(e, "div", "a");
				if (!el.id) return;


				$("#" + guid + "_friendselector a").removeClass("active");
				$(el).addClass("active");

				var w = BSC.UI.Ws.Get(guid);
				var jQ = BSC.U.ParseQuery(w.query);

				delete jQ.type;
				if ($(el).hasClass("mutual"))
					jQ.type = "mutual";

				BSC.UI.Ws.Get(guid).query = BSC.U.QueryToString(jQ);
				BSC.UI.Ws.Get(guid).uri = w.cleanpath + "/?" + BSC.UI.Ws.Get(guid).query;
				BSC.P.ChangeURI(BSC.UI.Ws.Get(guid).uri);
				Loadfriends(guid);

				return false;
			});
		};

		var unbind = function(p1)
		{
			var guid = p1;
		};

		var Loadfriends = function(p1)
		{
			var guid = p1;
			var w = BSC.UI.Ws.Get(guid);
			var data = BSC.U.ParseQuery(w.query);

			$("#" + guid + "_loader").hide();
			BSC.UI.ShowStatus(guid, "Loading...");
			data.view = (data.view) ? data.view : (BSC.D.P.Setting("profile.main.friends")) ? BSC.D.P.Setting("profile.main.friends") : "big";
			BSC.D.AjaxGet("/" + w.data.key + "/FriendList", 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("/" + w.data.key + "/FriendList", guid, data, pagingData, "profile.main.friends", { view: "big" });
				BSC.UI.HideStatus(guid, 200);
			}, "html", guid);
		};

		return {
			Load: load,
			Bind: bind,
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Profile.Friends.Load, null, "profile.friends");
BSC.E.Subscribe("bind", BSC.UI.Ws.Profile.Friends.Bind, null, "profile.friends");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Profile.Friends.Unbind, null, "profile.friends");
