﻿// ==UserScript==
// @name                登录弹出框
// @description         这里是完成“登录”弹出框的功能
// @created date        2009/7/13   8:44
// @created author      JianZi/Tiger
// @version             1.0.1
// @version owner       Copyright (C) 2009-2010 keerr.com All rights reserved.
// ==/UserScript==

/*************************************************
verified time 	:	2009/9/15   14:35
verified author	:	Tiger
verified purpose:	修改了setHomepage的一个小bug，两次执行的bug
**************************************************/
/*************************************************
verified time 	:	2009/9/25   13:37
verified author	:	Tiger
verified purpose:	修改了“设为首页”的bug
**************************************************/
/*************************************************
verified time 	:	2009/9/25   16:38
verified author	:	Tiger
verified purpose:	添加了“加入收藏夹”的js功能
**************************************************/
/********************************************************************
verified time   : 2009/11/6   10:52
verified author : JianZi
verified purpose: 获取登录状态方法getLoginStates
*********************************************************************/
/*************************************************
verified time 	:	2010/1/29   12:21
verified author	:	Tiger
verified purpose:	将headlogin方法统一方法格式
**************************************************/
/********************************************************************
verified time 	:	2010/4/6   17:15
verified author	:	Tiger
verified purpose:	修改了头部登录的处理，以js来处理，放弃了cs处理
*********************************************************************/
/********************************************************************
verified time 	:	2010/5/5   14:48
verified author	:	Tiger
verified purpose:	为静态页面做了headlogin的修订，特别是F5后只显示“欢迎您。。。”
*********************************************************************/
var _reg_checkCode;
$(document).ready(function() {
    //getLoginStates(); // 移到了login.ascx的控件中去了
    bylouLogin.initial({ uidSel: "#headeruid", pswSel: "#headerpwd", btnSel: "#btlogin", callBack: bylouLogin.headLoginCallBack });

});

/// <summary>
/// 
/// </summary>
/// <param name="opt">传入的参数</param>
/// { uidSel: "#headeruid", pswSel: "#headerpwd", btnSel: "#btlogin", callBack: headLoginCallBack }
var bylouLogin = {
    initial: function(opt) {
        if ($.cookie("userEmail") != null) {
            $(opt["uidSel"]).val($.cookie("userEmail"));
            $(opt["pswSel"]).focus();
        }
        // enter事件
        $("body").keydown(function(event) {
            return bylouLogin.enterSubmit(event);
        });
        $(opt["btnSel"]).click(function() {
            bylouLogin.sentAjax(opt);
        });
        //$("#headerpwd").isEmail();

    },
    sentAjax: function(opt) {
        if ($(opt["uidSel"]).val() === "") {
            ShowMsgbox({ errmsg: "请输入账号", cssOptions: { top: "130px", left: "280px", opacity: "0.8" }, top: 30, left: 50, delay: 5000, clsName: "userNameErrMsg" }, $(opt["uidSel"]));
            $(opt["uidSel"]).select();
            bylouLogin.resetBt();
            return;
        }
        if ($(opt["pswSel"]).val() === "") {
            ShowMsgbox({ errmsg: "请输入密码。", cssOptions: { top: "190px", left: "280px", opacity: "0.8" }, top: 30, left: 50, delay: 5000, clsName: "pswErrMsg" }, $(opt["pswSel"]));
            $(opt["pswSel"]).select();
            bylouLogin.resetBt();
            return;
        }
        $.ajax({
            url: '/ajax/GetJson.aspx',
            type: 'POST',
            dataType: 'json',
            timeout: 5000,
            data: 'type=Login&txtU=' + $(opt["uidSel"]).val() + '&txtP=' + $(opt["pswSel"]).val(),
            cache: false,
            error: function() {
                window.location.href = "http://linsuo.bylou.com?url=" + document.location.href;
            },
            success: function(data) {
                switch (data["result"]) {
                    case "1":
                    case "2":
                        //redirectIndex(_s);
                        var Cookieopt = {// cookie的统一参数集
                            path: '/',
                            domain: '',
                            expires: 365,
                            secure: false
                        };
                        opt.callBack(data);
                        //$("#nologul").empty().html('<li>欢迎 ' + data["username"] + ' 登陆百楼网</li><li>| <a href="http://linsuo.bylou.com/admin/index.aspx">后台管理</a></li><li>| <a href="/logout.aspx">退出</a></li>');
                        $.cookie("userEmail", $(opt["uidSel"]).val(), Cookieopt);
                        _UserOrCustomer = data["result"];
                        _OnUserIDOrCustomerID = data["id"];
                        break;
                    case "0":
                    case "-2":
                    case "-1":
                        window.location.href = "http://linsuo.bylou.com?url=" + document.location.href;
                        break;
                    default:
                }
            }
        });
    },
    /// <summary>
    /// 回车登录
    /// </summary>
    enterSubmit: function(e) {
        if ($("#btlogin").attr("disabled") == "disabled") {
            return false;
        }
        if (window.event)
            keyPressed = window.event.keyCode; // IE
        else
            keyPressed = e.which; // Firefox
        if (keyPressed === 13) {
            //login();
            $("#btlogin").click();
            return false;
        }

    },
    /// <summary>
    /// 激活回车及按钮事件
    /// </summary>
    resetBt: function() {
        $("#btlogin").attr("disabled", "");
        $("body").keydown(function(event) {
            return bylouLogin.enterSubmit(event);
        });
    },
    /// <summary>
    /// headlogin的callback事件
    /// </summary>
    /// <param name="data">ajax返回的数据</param>
    headLoginCallBack: function(data) {
        $("#nologul").empty().html('<li>欢迎 ' + data["username"] + ' 登陆百楼网</li><li>[<a href="http://linsuo.bylou.com/admin/index.aspx">后台管理</a>]</li><li>[ <a href="/logout.aspx">退出</a>]</li>');
    }
};



/// <summary>
/// 获取登录状态
/// </summary>
function getLoginStates() {
    $.ajax({
        url: '/ajax/GetJson.aspx',
        type: 'POST',
        dataType: 'json',
        timeout: 3000,
        data: { type: "getSession" },
        cache: false,
        error: function() {
        },
        success: function(str) {
            if (typeof (str.result) != 'undefined') {
                if (str.result !== "0") {
                    if (str.result === "1") {
                        _OnUserIDOrCustomerID = Number(str.id);
                        _UserOrCustomer = 2;
                        
                    }
                    else if (str.result === "2") {
                        _OnUserIDOrCustomerID = Number(str.id);
                        _UserOrCustomer = 1;
                    }
                    $("#logul li:first").text("欢迎 " + str["name"] + " 登陆百楼网");
                    $("#logul").show();
                    $("#nologul").remove();
                }
                else {
                    $("#logul").remove();
                    $("#nologul").show();
                }
            }
        }
    });
}


/// <summary>
/// 设置为首页js
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
function setHomepage(obj) {
    var x = obj.offsetLeft - 5;
    var y = obj.offsetTop + obj.offsetHeight + 3;
    var o = document.getElementById('fp_set_pt');
    if ($.browser.msie) {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage('http://www.bylou.com');
    }

    else {
        //if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
        if ($.browser.mozilla) {
            o.style.ZIndex = '1000';
            o.style.left = x + 'px';
            o.style.top = y + 'px';
            o.style.display = 'block';
        }
        else {
            document.getElementById('sethomepagediv').style.display = 'none';
            alert("抱歉，您的浏览器已经超越了我们支持的范畴！");
        }
    }
}

/// <summary>
/// 点击首页的“更多”后的js效果
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
function show_link_more(obj) {
    var x = obj.offsetLeft - 1,
        y = obj.offsetTop + obj.offsetHeight,
        o = document.getElementById('link_more'),
        flagTime,
        popupStatus = 1;
    o.style.zindex = '1001';
    o.style.left = x + 'px';
    o.style.top = y + 'px';
    o.style.display = 'block';

    $("#link_more")
    .bind("mouseleave", function() {
        flagTime = setTimeout(function() {
            $("#link_more").hide();
        }, 1000);
        popupStatus = 0;
    })
    .mouseover(function() {
        clearTimeout(flagTime);
    });

    setTimeout(function() {
        $("body").click(function() {
            if (popupStatus == 1) {
                $("#link_more").fadeOut("slow");
                popupStatus = 0;
            }
        });
    }, 1000);

}

/// <summary>
/// 添加收藏夹
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}