﻿MplogSSO = function() {
    this._eventElement = null;
    
    this._scrollActionlayerHandler = null;
    this._resizeActionlayerHandler = null;
    this._windowHandlersActionlayerAttached = false;
    
    this._scrollAddFriendHandler = null;
    this._resizeAddFriendHandler = null;
    this._windowHandlersAddFriendAttached = false;
    
    this._scrollMessegeHandler = null;
    this._resizeMessegeHandler = null;
    this._windowHandlersMessegeAttached = false;
}

MplogSSO.prototype = {
    Initialize: function(isAuthenticated, ViewMemberNo, Hash, memberNo) {
        if (document.getElementById("frmMplog") != null) {
            document.getElementById("ssoMemberno").value = document.getElementById("hdnMemberNo").value;
            document.getElementById("ssoHash").value = document.getElementById("hdnHash").value;
            document.getElementById("ssoIsSession").value = document.getElementById("hdnIsSession").value;
            document.getElementById("ssoViewMemberNo").value = document.getElementById("hdnViewMemberNo").value;
            document.getElementById("frmSSO").submit();

        }
    },

    ViewActionLayer: function(isAuthenticated, e, imgServer, csViewID, siteRoot, identityName, memberNo, gameNo, leagueNo) {
        if (parseInt(memberNo) < 1)
            return;

        if (!e) var e = window.event;

        var eventElement = (e) ? e.srcElement : e.target;

        if (!eventElement)
            eventElement = e.currentTarget;

        var el = $(eventElement).offset();

        var xfrom = el.left;
        var yfrom = el.top + $(eventElement).height();

        var oActionLayer = document.getElementById("oActionLayer");

        if (oActionLayer != null) {
            oActionLayer.parentNode.removeChild(oActionLayer);

            var oActionLayerSendMessages = document.getElementById("oActionLayerSendMessages");

            if (oActionLayerSendMessages != null) {
                oActionLayerSendMessages.parentNode.removeChild(oActionLayerSendMessages);
            }

            var oActionLayerAddFriend = document.getElementById("oActionLayerAddFriend");

            if (oActionLayerAddFriend != null) {
                oActionLayerAddFriend.parentNode.removeChild(oActionLayerAddFriend);
            }
        }

        var url = siteRoot + "/javascript/template/tmpl_ActionLayer.html";
        $.get(url, function(html) {
            var data = {
                ImgServer: imgServer,
                SiteRoot: siteRoot,
                IsAuthenticated: isAuthenticated,
                IdentityName: identityName,
                MemberNo: memberNo,
                GameNo: gameNo,
                LeagueNo: leagueNo,
                CSViewID: csViewID
            };

            var tmpl = parseTemplate(html, data);

            $(document.body).append(tmpl);

            $("#oActionLayer").css({
                "position": "absolute",
                "left": xfrom + "px",
                "top": yfrom + "px",
                "width": "100px",
                "height": "52px",
                "zIndex": "10000001"
            });
        });

        this._scrollActionlayerHandler = Function.createDelegate(this, this._onLayout);
        this._resizeActionlayerHandler = Function.createDelegate(this, this._onLayout);

        this._attachActionlayerPopup();
    },

    CloseActionLayer: function() {
        var oActionLayer = document.getElementById("oActionLayer");

        if (oActionLayer) {

            var oActionLayerSendMessages = document.getElementById("oActionLayerSendMessages");

            if (oActionLayerSendMessages) {
                oActionLayerSendMessages.parentNode.removeChild(oActionLayerSendMessages);
            }

            var oActionLayerAddFriend = document.getElementById("oActionLayerAddFriend");

            if (oActionLayerAddFriend) {
                oActionLayerAddFriend.parentNode.removeChild(oActionLayerAddFriend);
            }

            oActionLayer.parentNode.removeChild(oActionLayer);

            this._detachActionlayerPopup();
        }
    },

    RunViewMplog: function(siteRoot, memberNo) {
        //alert(siteRoot + ", " + memberNo);
        window.location = siteRoot + "/mplog/MP_Post_View.aspx?memberNo=" + memberNo;
    },

    ViewSendMessages: function(imgServer, sendID, receiveID) {

        var oActionLayerSendMessages = document.getElementById("oActionLayerSendMessages");
        //oActionLayer.parentNode.removeChild(oActionLayer);
        //alert(oActionLayer);
        if (oActionLayerSendMessages != null) {
            oActionLayerSendMessages.parentNode.removeChild(oActionLayerSendMessages);
        }

        var url = "/wcgzone/javascript/template/tmpl_SendMessages.html";
        $.get(url, function(html) {
            var data = {
                ImgServer: imgServer,
                SendID: sendID,
                ReceiveID: receiveID
            };

            var tmpl = parseTemplate(html, data);

            $(document.body).append(tmpl);

            var Coordinates = $("#oActionLayer").position();

            $("#oActionLayerSendMessages").css({
                "position": "absolute",
                "left": Coordinates.left + "px",
                "top": Coordinates.top + "px",
                "width": "220px",
                "height": "52px",
                "zIndex": "10000001"
            });
        });

        this._scrollMessegeHandler = Function.createDelegate(this, this._onLayout);
        this._resizeMessegeHandler = Function.createDelegate(this, this._onLayout);

        this._attachMessegePopup();
    },

    CloseViewSendMessages: function() {
        var oActionLayerSendMessages = document.getElementById("oActionLayerSendMessages");
        oActionLayerSendMessages.parentNode.removeChild(oActionLayerSendMessages);

        this._detachMessegePopup();
    },

    RunSendMessages: function(sendID, receiveID, Message) {

        var oIndexScript = new IndexScript();

        //        try {
        //            oUserManageScript = new UserManageScript();
        //        }
        //        catch (Exception) {
        //            oUserManageScript = new IndexScript();
        //        }
        //        //alert(oUserManageScript);

        oIndexScript.SendMessage(sendID, receiveID, Message.value, function(result) {
            if (result == "") {
                //var oActionLayerSendMessages = document.getElementById("oActionLayerSendMessages");
                //oActionLayerSendMessages.parentNode.removeChild(oActionLayerSendMessages);
                var oActionLayerSendMessages = $("#oActionLayerSendMessages");
                oActionLayerSendMessages.css("width", "150px");
                oActionLayerSendMessages.find("tr:first > th").css("text-align", "left");
                oActionLayerSendMessages.find("tr:first > th > span").css({"padding-left": "", "float": "right"});
                oActionLayerSendMessages.find("tr:eq(1)").css("display", "none");
                oActionLayerSendMessages.find("tr:last").css("display", "");
            }
            else {
                getMultiLang(result.replace("|||error|", "").replace("|", "_"), MLCallMsg);
            }
        }, function(result) {
            //            alert(result);
        });
    },

    AddFriend: function(sendID, receiveID, imgServer) {

        //        var oUserManageScript = "";

        //        try {
        //            oUserManageScript = new UserManageScript();
        //        }
        //        catch (Exception) {
        //            oUserManageScript = new IndexScript();
        //        }

        if ($("#oActionLayerAddFriend")[0]) {
            return;
        }

        var data = {
            ImgServer: imgServer,
            ResultMsg: ""
        };

        var oIndexScript = new IndexScript();
        oIndexScript.AddFriend(sendID, receiveID, function(result) {
            data.ResultMsg = "Success";

            fnAddFriendShow(data);

        }, function(result) {
            data.ResultMsg = "You've already added.";

            fnAddFriendShow(data);
        });

        this._scrollAddFriendHandler = Function.createDelegate(this, this._onLayout);
        this._resizeAddFriendHandler = Function.createDelegate(this, this._onLayout);

        this._attachAddFriendPopup();

        function fnAddFriendShow(data) {
            var url = "/wcgzone/javascript/template/tmpl_AddFriend.html";
            $.get(url, function(html) {

                var tmpl = parseTemplate(html, data);

                $(document.body).append(tmpl);

                var Coordinates = $("#oActionLayer").position();

                $("#oActionLayerAddFriend").css({
                    "position": "absolute",
                    "left": Coordinates.left + "px",
                    "top": Coordinates.top + "px",
                    "width": "150px",
                    "height": "52px",
                    "zIndex": "10000001"
                });
            });

        }
    },

    CloseMessagesBox: function() {
        var ooActionLayerMessagesBox = document.getElementById("oActionLayerAddFriend");
        ooActionLayerMessagesBox.parentNode.removeChild(ooActionLayerMessagesBox);

        this._detachAddFriendPopup();
    },

    _onLayout: function() {
        this._layout();
    },

    _layout: function() {
        var el = null;
        if (this._eventElement) {
            el = $(this._eventElement).position();

            if (this._windowHandlersActionlayerAttached) {
                var oActionLayer = $("#oActionLayer");
                oActionLayer.css("left", el.left + "px");
                oActionLayer.css("top", el.bottom + "px");
            }

            if (this._windowHandlersAddFriendAttached) {
                var oActionLayerAddFriend = $("#oActionLayerAddFriend");
                oActionLayerAddFriend.css("left", el.left + "px");
                oActionLayerAddFriend.css("top", el.bottom + "px");
            }

            if (this._windowHandlersMessegeAttached) {
                var oActionLayerSendMessages = $("#oActionLayerSendMessages");
                oActionLayerSendMessages.css("left", el.left + "px");
                oActionLayerSendMessages.css("top", el.bottom + "px");
            }
        }
    },

    _attachActionlayerPopup: function() {
        /// <summary>
        /// Attach the event handlers for the popup
        /// </summary>

        $addHandler(window, 'resize', this._resizeActionlayerHandler);
        $addHandler(window, 'scroll', this._scrollActionlayerHandler);
        this._windowHandlersActionlayerAttached = true;
    },

    _detachActionlayerPopup: function() {
        /// <summary>
        /// Detach the event handlers for the popup
        /// </summary>

        if (this._windowHandlersActionlayerAttached) {
            if (this._scrollActionlayerHandler) {
                $removeHandler(window, 'scroll', this._scrollActionlayerHandler);
            }
            if (this._resizeActionlayerHandler) {
                $removeHandler(window, 'resize', this._resizeActionlayerHandler);
            }

            this._windowHandlersActionlayerAttached = false;
        }

    },

    _attachAddFriendPopup: function() {
        /// <summary>
        /// Attach the event handlers for the popup
        /// </summary>

        $addHandler(window, 'resize', this._resizeAddFriendHandler);
        $addHandler(window, 'scroll', this._scrollAddFriendHandler);
        this._windowHandlersAddFriendAttached = true;
    },

    _detachAddFriendPopup: function() {
        /// <summary>
        /// Detach the event handlers for the popup
        /// </summary>

        if (this._windowHandlersAddFriendAttached) {
            if (this._scrollAddFriendHandler) {
                $removeHandler(window, 'scroll', this._scrollAddFriendHandler);
            }
            if (this._resizeAddFriendHandler) {
                $removeHandler(window, 'resize', this._resizeAddFriendHandler);
            }

            this._windowHandlersAddFriendAttached = false;
        }

    },

    _attachMessegePopup: function() {
        /// <summary>
        /// Attach the event handlers for the popup
        /// </summary>

        $addHandler(window, 'resize', this._resizeMessegeHandler);
        $addHandler(window, 'scroll', this._scrollMessegeHandler);
        this._windowHandlersMessegeAttached = true;
    },

    _detachMessegePopup: function() {
        /// <summary>
        /// Detach the event handlers for the popup
        /// </summary>

        if (this._windowHandlersMessegeAttached) {
            if (this._scrollMessegeHandler) {
                $removeHandler(window, 'scroll', this._scrollMessegeHandler);
            }
            if (this._resizeMessegeHandler) {
                $removeHandler(window, 'resize', this._resizeMessegeHandler);
            }

            this._windowHandlersMessegeAttached = false;
        }

    }
}


