﻿var background = {
    show: function() {//显示遮罩层 显示时候这个最早，hide时候这个最晚
        if (!$('#backgroundlayer').length) {
            var ele = $('<div style="position:absolute; left:0; top:0; background-color:#000;text-align:center; vertical-align:middle" id="backgroundlayer"></div>'); //回调后处理掉loading界面
            var bodyer = document.getElementsByTagName('body')[0];
            var height = parseInt(bodyer.scrollHeight) > parseInt(bodyer.clientHeight) ? bodyer.scrollHeight : bodyer.clientHeight;
            var width = parseInt(bodyer.scrollWidht) > parseInt(bodyer.clientWidth) ? bodyer.scrollWidht : bodyer.clientWidth;
            ele.dblclick(function() { background.msgbox.close(); });
            ele.css("width", width + "px").css("height", height + "px").css("opacity", 0.75).appendTo("body");
        } else {
        $('#backgroundlayer').dblclick(function() { background.msgbox.close(); });
        $('#backgroundlayer').show(); //css('display', '');
        }
    },
    hide: function() {//隐藏遮罩层
        if ($('#backgroundlayer').length) {
            $('#backgroundlayer').hide(); //css('display', 'none');
        }
    },
    load: function(type, url, data, callback, erro, beforesend)//外部载入
    {
        $.ajax({
            type: type,
            url: url,
            data: data,
            success: function(obj) {
                callback.call(this, obj);
            },
            erro: function(xml, status, e) { },
            beforesend: null
        })

    },
    loadSuccess: function() {
        background.show();
        var body = document.getElementsByTagName('body')[0];
        if (!$('#loadBox').length > 0) {
            //simple msg,just for redirecting
            var ele = $('<div id="loadBox"></div>');
            ele.css('border', '1px #000 solid');
            ele.css('padding', '12px');
            ele.css('position', 'absolute');
            ele.css('background-color', '#fff');
            ele.css('overflow', 'visible');
            ele.html(arguments[0]);
            ele.appendTo($('body'));
            ele = null;
            ele = $('#loadBox');
            $('<div onclick="background.loaded()" align="right" style=" cursor:pointer; font-weight:700; margin-top:8px">[CLOSE]</div>').appendTo(ele);
            ele = null;
        } else {
            $('#loadBox').html(arguments[0]);
            $('<div onclick="background.loaded()" align="right" style=" cursor:pointer; font-weight:700; margin-top:8px">[CLOSE]</div>').appendTo($('#loadBox'));
            $('#loadBox').show();
        }
        background.setPositon(body, $('#loadBox'), 250);
    },
    loading: function() {//loading过程中
        background.show();
        if (!$('#backgroundloadinglayer').length) {
            $('#backgroundlayer').html('<div id="backgroundloadinglayer" style="height:30px; line-height:30px; display:inline; background-color:#fff; position:absolute"><img src="images/loading.gif" /> 加载中，请稍候...</div>');

        } else {
            $('#backgroundlayer').show(); //css('display', '');
        }
        background.setPositon(document.getElementsByTagName('body')[0], $('#backgroundloadinglayer'), 300);

    },
    loaded: function(fun) {//load完成
        if ($('#backgroundloadinglayer').length > 0) {
            $('#backgroundloadinglayer').hide();
        }
        $('#backgroundlayer').hide();
        $('#loadBox').hide();
        if (fun) {
            fun.call(window, null);
        }
    },
    setPositon: function(body, ele, top) {//设置元素坐标，参数分别为body,当前节点，距顶边距
        var scrollTop = document.documentElement ? document.documentElement.scrollTop : body.scrollTop;
        var scrollLeft = document.documentElement ? document.documentElement.scrollLeft : body.scrollLeft;
        scrollLeft = (parseInt(body.clientWidth) / 2) - ele.width() / 2;
        scrollTop += top;
        scrollTop += 'px';
        scrollLeft += 'px';
        ele.css('left', scrollLeft);
        ele.css('top', scrollTop);

    },
    msgbox: {//对话框，目前还没有完善，只有normal版，整体而言包括提示信息，回调函数，处理时间，以及对话框版本 如alert,msg,confirm==
        Init: function() {//初始化
            background.show();
            var body = document.getElementsByTagName('body')[0]; //获取原生对象          
            if (arguments.length < 3) {
                alert('invalied paramters');
            }
            if (arguments.length == 3) {
                if (!$('#msgbox').length > 0) {
                    //simple msg,just for redirecting
                    var ele = $('<div id="msgbox"></div>');
                    //ele.css('border', '1px #000 solid');
                    //ele.css('padding', '12px');
                    ele.css('position', 'absolute');
                    //ele.css('background-color', '#fff');
                    ele.css('overflow', 'visible');
                    ele.html(arguments[0]);
                    ele.appendTo($('body'));
                    ele = null;
                    ele = $('#msgbox');
                    //$('<div onclick="background.msgbox.close(rld,window)" align="right" style=" cursor:pointer; font-weight:700; margin-top:8px">[X]</div>').appendTo(ele);
                    ele = null;
                } else {
                    $('#msgbox').show();
                }
                background.setPositon(body, $('#msgbox'), 200);
                setTimeout(arguments[1], arguments[2]);

            } else {
                switch (arguments[0]) {//以后要扩展的各个类型
                    case 'Alert': (function() { })(); break;
                    case 'Confirm': (function() { })(); break;
                    case 'msg': (function() { })(); break;
                    default: (function() { })(); break
                }
            }
        },
        close: function() {//关闭对话
            if ($('#msgbox').length > 0) {
                $('#msgbox').hide(); //css('display','none');
                background.hide();
            }
            if (arguments.length > 1) {
                arguments[0].call(arguments[1], null)
            }
        }
    }
}
var cdkey = '';

function rld() {
    window.location.reload();
}

function showKey(msg) {
    background.msgbox.Init(msg, function() { background.show(); $('#sntext').val(cdkey); }, 50);
}

function showActorLoginForm() {
    background.load("get", "showkey.htm", null, showKey, null, null);
}

var actorName;

var LoginShow;
// 显示新建窗口
function showLoginShow() {
    $.ajax({
        type: "post",
        url: "handler/GetKey.ashx?rd=" + Math.random(),
        data: null,
        dataType: "json",
        success: function(msg) {
            /*
            $('#body').hide("");
            new Boxy.load("showkey.htm", { title: '', draggable: true, closeable: true, fixed: false, center: true, oninit: function(tform) {
            LoginShow = tform;
            $('#sntext').val(msg.cdkey);
            }
            });
            */
            cdkey = msg.cdkey;
            showActorLoginForm();
        },
        async: false

    })
}

// 退出登陆
function logOut() {
    $.ajax({ type: "POST", url: "handler/lib/LogOut.ashx",
        success: function(msg) {
            $("#login1").css("display", "none");
            $("#login").css("display", "block");
            return false;
        }, async: false
    });
}
function showMaskAuto() {
    $.ajax({ type: "POST", url: "handler/loginCk.ashx", dataType: 'json',
        success: function(msg) {
            if (msg.status) {
                showLoginShow();
            }
        }, async: false
    });

}


function showmask() {
    $.ajax({ type: "POST", url: "handler/loginCk.ashx", dataType: 'json',
        success: function(msg) {
            if (msg.status) {
                showLoginShow();
            } else {
                ifQQLogin();
            }
        }, async: false
    });
}


function copyCode() {
    var value = document.getElementById("sntext").value;
    if (window.clipboardData) {
        event.returnValue = false;
        window.clipboardData.setData("Text", value);
    }

}

/*-判断是否QQ登录-*/
function ifQQLogin() {
    $.ajax({ type: "GET", url: "handler/Login.ashx?rand=" + Math.random(), dataType: "json",
        error: function(xml, status, e) {
        },
        success: function(response) {
            if (response.QQ == "-1") {
                location.href = response.Url;
                return false;
            }
        }, async: true
    });


}
