﻿/// <reference path="SystemUser.js" />

function redirectDefaultPage()
{
    window.location.href = "http://" + window.location.host;
}

reipBasePage =
{
    sckcdUri: "http://172.16.40.221",
    sckcdSessionId: null,
    topPageRedirect: function(url)
    {
        if (url != null)
        {
            var parentPage = parent;
            while (parentPage.parent != parentPage)
            {
                parentPage = parentPage.parent;
            }
            parentPage.location.href = url;
        }
    },
    redirectSCKCD: function()
    {
        reipBasePage.topPageRedirect(reipBasePage.sckcdUri);
    },
    setIframeSize: function(iframe)
    {
        function autoSetSize()
        {
            if (iframe.contentWindow.document.documentElement != null)
            {
                var height = iframe.contentWindow.document.documentElement.scrollHeight;
                var iframeHeight = iframe.offsetHeight;
                if (height != iframeHeight)
                {
                    iframe.style.height = height.toString() + "px";
                }
                iframe.style.height = height.toString() + "px";
            }
            setTimeout(autoSetSize, 250);
        }
        autoSetSize();
    },
    autoRedirectSCKCD: function(parentNode)
    {
        parentNode = parentNode == null ? document.body : parentNode;
        var clockPanel = document.createElement("div");
        clockPanel.appendChild(document.createTextNode("页面"));
        var lblTimeClock = document.createElement("span");
        lblTimeClock.innerHTML = "5";
        lblTimeClock.style.color = "Red";
        lblTimeClock.style.fontWeight = "bold";
        lblTimeClock.style.marginLeft = lblTimeClock.style.marginRight = "3px";
        clockPanel.appendChild(lblTimeClock);
        clockPanel.appendChild(document.createTextNode("秒种后自动跳转到商超快车道首页"));
        clockPanel.appendChild(document.createElement("br"));
        clockPanel.appendChild(document.createTextNode("如果没有跳转请手动点击网址："));
        var hlSCKCD = document.createElement("a");
        hlSCKCD.href = "javascript:reipBasePage.redirectSCKCD();"
        hlSCKCD.innerHTML = reipBasePage.sckcdUri;
        clockPanel.appendChild(hlSCKCD);

        parentNode.appendChild(clockPanel)
        var i = 5;
        function __autoRedirect()
        {
            lblTimeClock.innerHTML = i--;
            if (i > 0)
            {
                setTimeout(__autoRedirect, 1000);
            }
            else
            {
                reipBasePage.redirectSCKCD();
            }
        }
        __autoRedirect();
    },
    userOperator: null,
    rb2bUserOperator: null,
    siteOperator: null,
    initialize: function(rb2bDomain)
    {
        reipBasePage.sckcdUri = rb2bDomain;
        if (reipBasePage.userOperator == null && typeof (sysetemUserOperator) != "undefined") reipBasePage.userOperator = new sysetemUserOperator();
        if (reipBasePage.siteOperator == null && typeof (siteOperator) != "undefined") reipBasePage.siteOperator = new siteOperator();
        if (rb2bDomain != null || rb2bDomain != "")
        {
            if (reipBasePage.rb2bUserOperator == null && typeof (rb2bUserOperator) != "undefined") reipBasePage.rb2bUserOperator = new rb2bUserOperator(rb2bDomain);
        }
    },
    isCheckLogin: false,
    checkLogin: function()
    {
        if (!reipBasePage.isCheckLogin)
        {
            reipBasePage.isCheckLogin = true;
            addEventHandle(window, "load", function() { reipBasePage.userOperator.checkIsLogin(); });
        }
    },
    checkRB2BLoginCompletedHandlers: [],
    addCheckRB2BLoginCompleted: function(handler)
    {
        if (handler != null && typeof (handler) == "function")
        {
            for (var i = 0; i < reipBasePage.checkRB2BLoginCompletedHandlers.length; i++)
            {
                if (reipBasePage.checkRB2BLoginCompletedHandlers[i] == handler)
                {
                    break;
                }
            }
            reipBasePage.checkRB2BLoginCompletedHandlers.push(handler);
        }
    },
    checkRB2BLogin_Completed: function(r)
    {
        for (var i = 0; i < reipBasePage.checkRB2BLoginCompletedHandlers.length; i++)
        {
            function t(h)
            {
                h(r)
            };
            t(reipBasePage.checkRB2BLoginCompletedHandlers[i]);
        }
        if (r.status != 1)
        {
            reipBasePage.userOperator.logout(function() { return false; });
        }
        else
        {
            reipBasePage.userOperator.loginBySessionId({ userId: r.userId, sessionId: r.sessionId, companyId: r.companyId, companyRole: r.companyRole });
        }
    },
    checkRB2BLogin: function(companyId)
    {
        if (!reipBasePage.isCheckLogin && reipBasePage.rb2bUserOperator != null)
        {
            reipBasePage.isCheckLogin = true;
            var searchP = $getUrlSearchParamters();
            var isLogout = searchP["out"] == "1";
            addEventHandle(window, "load", function() { reipBasePage.rb2bUserOperator.checkLogin(companyId, isLogout, "reipBasePage.checkRB2BLogin_Completed"); });
        }
    },
    logoutRB2B: function()
    {
        function completed(result)
        {
            reipBasePage.rb2bUserOperator.logout();
            return false;
        }
        reipBasePage.userOperator.logout(completed)
    },
    isLoading: false,
    loading: function()
    {
        if (!reipBasePage.isLoading && reipBasePage.rb2bUserOperator != null)
        {
            reipBasePage.isLoading = true;
        }
    }
}
