﻿$(function() {
    $(".index_top_navshowspan").mouseover(function() {
        $(this).removeClass(); $(this).addClass("index_top_navhidespan");
    });
    $(".index_top_navshowspan").mouseout(function() {
        $(this).removeClass(); $(this).addClass("index_top_navshowspan");
        MPSwitchTab();
    });
    $(".index_top_navnormalspan").mouseover(function() {
        $(this).removeClass(); $(this).addClass("index_top_navhidespan");
    });
    $(".index_top_navnormalspan").mouseout(function() {
        $(this).removeClass(); $(this).addClass("index_top_navnormalspan");
        MPSwitchTab();
    });

    MPSwitchTab();

    $("#dialog_userloginbg").width(window.screen.width - 20);
    $("#dialog_userloginbg").height($("body").height());
    $("#dialog_frame").width(window.screen.width - 20);
    $("#dialog_frame").height($("body").height());

    GetUserStatusMP();
});

var uname = "";
function MPSetUserStatus() {
    $("#ctl00_dialogUserName").val(uname);
    if (uname != undefined && uname != "") {
        //usertype = 1;
        //用户没有登录成功
        if (usertype == undefined || usertype == "") {
            $("#liLogin").show();
            $("#liRegister").show();
            $("#liLogOut").hide();
            $("#spanTip").text("您好，欢迎来到家和网");
        }
        //用户登录成功，类型为普通用户
        else if (usertype == "1") {
            $("#spanTip").text(uname + ", 您好！");
            $("#liLogin").hide();
            $("#liRegister").hide();
            $("#liLogOut").show();
        }
        //用户登录成功，类型为经纪人
        else if (usertype == "2") {
            $("#spanTip").text(uname + ", 您好！");
            $("#liLogin").hide();
            $("#liRegister").hide();
            $("#liLogOut").show();
        }
        else {
            $("#liLogin").show();
            $("#liRegister").show();
            $("#liLogOut").hide();
        }
    }
    else {
    }
}

var mp_toptab_stitch = 1;

function MPSwitchTab() {
    switch (mp_toptab_stitch) {
        case 1:
            $("#index_top_index").removeClass();
            $("#index_top_index").addClass("index_top_navshowspan");
            break;
        case 2:
            $("#index_top_sale").removeClass();
            $("#index_top_sale").addClass("index_top_navshowspan");        
            break;
        case 3:
            $("#index_top_rent").removeClass();
            $("#index_top_rent").addClass("index_top_navshowspan");
            break;
        case 4:
            $("#index_top_map").removeClass();
            $("#index_top_map").addClass("index_top_navshowspan");
            break;
        case 5:
            $("#index_top_cp").removeClass();
            $("#index_top_cp").addClass("index_top_navshowspan");
            break;
        case 6:
            $("#index_top_agent").removeClass();
            $("#index_top_agent").addClass("index_top_navshowspan");
            break;
        case 7:
            $("#index_top_rop").removeClass();
            $("#index_top_rop").addClass("index_top_navshowspan");
            break;
        default:
            $("#index_top_index").removeClass();
            $("#index_top_index").addClass("index_top_navshowspan");
            break;
    }
}

function ShowCity() {
    $("#switchCity").show();
}

function HideCity() {
    $("#switchCity").hide();
}

function ShowLogin() {
    $("#dialog_userloginbg").show();
    if ($("#dialog_userlogin").dialog('open')){
        $("#dialog_userlogin").dialog({
            autoOpen: true,
            draggable: true,
            hide: 'slide',
            close: CloseDialog
        });
    }
}

function CloseDialog() {
    $("#dialog_userloginbg").hide();
}

//获取event,解决IE与Frefox下的差异
function MPSearchEvent()
{
    func = MPSearchEvent.caller;
    while (func != null)
    {
        var arg0 = func.arguments[0];
        if (arg0 != undefined && arg0 != "") {
            if (arg0.constructor == "[object KeyboardEvent]") {
                return arg0;
            }
        }
        func = func.caller;
    }
    return window.event;
}

function MPToDialogPassword() {
    evt = MPSearchEvent();
    if (evt.keyCode == 13) {
        $("#dialogPassword").focus();
    }
}

function MPToDialogLogin() {
    evt = MPSearchEvent();
    if (evt.keyCode == 13) {
        DialogLogin();
    }
}

function DialogLogin() {
    var uname = $("#ctl00_dialogUserName").val();
    if (uname == undefined || uname == "") {
        alert("您好，用户名不能为空！");
        $("#ctl00_dialogUserName").focus();
        return;
    }
    var upwd = $("#dialogPassword").val();
    if (upwd == undefined || upwd == "") {
        alert("您好，密码不能为空！");
        $("#dialogPassword").focus();
        return;
    }
    $("#divMsg").show();
    $("#divMsg").text("登录中,请稍后...");
    var rurl = "";
    var s = "un=" + escape(uname) + "&pwd=" + escape(upwd) + "&rurl=" + escape(rurl);
    $.ajax({
        type: "post",
        url: "/user/login/Login.aspx",
        data: s,
        success: CompleteMP
    });
}

function CompleteMP(o) {
try {
    if (o != false) {
        var uname = $("#ctl00_dialogUserName").val();
        $("#divMsg").text("登录成功！");
        if (o.indexOf("index.aspx")>-1) {
            //用户登录成功，类型为普通用户
            if (o.indexOf("ok|*$|*%") > -1) {
                usertype = "1";
                $("#spanTip").text(uname + ", 您好！");
                $("#liLogin").hide();
                $("#liRegister").hide();
                $("#liLogOut").show();
            }
            //用户登录成功，类型为经纪人
            else if (o.indexOf("ok|+&|&+") > -1) {
                usertype = "2";
                $("#spanTip").text(uname + ", 您好！");
                $("#liLogin").hide();
                $("#liRegister").hide();
                $("#liLogOut").show();
            }
            $("#dialog_userlogin").dialog("close");
            $("#ctl00_dialogUserName").val("");
            $("#dialogPassword").val("");
            $("#divMsg").text("");
            $("#divMsg").hide();
            
            var toUrl = top.location.href;
            if (toUrl.indexOf('.com/index.aspx') > -1 || toUrl.lastIndexOf('.com') == toUrl.length - 4 || toUrl.lastIndexOf('.com/') == toUrl.length - 5 || toUrl.lastIndexOf('.com/#') == toUrl.length - 6) {
                top.location.href = "/index.aspx";
            }
            else {
                top.location.href = toUrl;
            }
        }
        else {
            $("#divMsg").text("登录失败！");
            alert("您好，用户名或密码错误，请重试！");
            $("#dialogPassword").focus();
            $("#divMsg").text("");
            $("#divMsg").hide();
            //刷新页面
        }
    }
    else {
        $("#divMsg").text("登录失败！");
    }
    }
    catch(ex) {
        $("#divMsg").text("");
        $("#divMsg").hide();
        $("#dialog_userlogin").dialog("close");
    }
}

function GetUserStatusMP() {
    $.ajax({
        type: "get",
        url: "/Action/GetStatusHandler.ashx?" + Math.random(),
        success: CompleteGetUserStatusMP
    });
}

function CompleteGetUserStatusMP(o) {
    var itemAry = o.split(",");
    usertype = itemAry[0];
    uname = itemAry[1]
    MPSetUserStatus();
}

function LogOutMP() {
    $.ajax({
        type: "get",
        url: "/Action/LogOutHandler.ashx?" + Math.random(),
        success: CompleteLogOutMP
    });
}

function CompleteLogOutMP(o) {
    if (o) {
        document.location.reload();
//        if (mp_toptab_stitch == 1) {
//            document.location.reload();
//        }
//        else {
//            usertype = 0;
//            MPSetUserStatus();
//        }
    }
}

function MPGetCityName() {
    $.ajax({
        type: "get",
        url: "/Action/GetCityNameHandler.ashx?" + Math.random(),
        success: CompleteMPGetCityName
    });
}

function CompleteMPGetCityName(o) {
    if (o != "") {
        $("#index_top_cityName").text(o);
    }
    else {
        $("#index_top_cityName").text("北京");
    }
}

