//显示用户的相关信息
(function() {
    $.ajax({
        type: "get",
        url: "/myjd_getUserInfo.action?t=" + new Date().getTime(),
        dataType: "html",
        timeout: 6000,
        success: function(r) {
            try {
                if (r == '{"error":"NotLogin"}') {
                    window.location.href = "http://passport.jd.com/new/login.aspx?ReturnUrl=http://home.jd.com";
                }
            } catch(err) {
            }
            if (r) {
                $(".acco-safe").html(r);

            }
        },
        error:function(request, status, error) {
        }
    });
})();

if ($("#memberInfo").html() != null && $("#memberInfo").html().replace(/\s/g, "") == "") {
    getHomeMemberInfo();
}

//显示用户会员级别及消费
function getHomeMemberInfo(){
	jQuery.ajax({
	    type: "GET",
	    url: "http://usergrade.jd.com/user/usergrade/getConsumeInfo.action?callback=?",
	    data: "",
	    dataType: "jsonp",
	    timeout: 6000,
	    success: function(r) {
                var html = "";
                if(r.uclassName == "一钻会员" || r.uclassName == "双钻会员" || r.uclassName == "三钻会员" || r.uclassName == "四钻会员"|| r.uclassName == "五钻会员") {
                    document.getElementById("double_href").style.display = "";
				}
                html = "<div class='memb-info' style='display: block;'>" + r.returnInfo + "</div><div class='close'></div>";
                //企业用户信息     
                $.ajax({
                    type: "get",
                    url: "/myjd_getBasicinfo.action?t=" + new Date().getTime(),
                    data : "yearPrice=" + r.consumeAmount,
                    success: function(info) {
                        if (info != "0") {
                            var returnInfo = info.split("!#!");
                            if (returnInfo[0] != null) {
                                r.uclassName = "企业用户";
                                html = "<div class='memb-info memb-info-02' style='display: block;'>" + returnInfo[1] + "</div><div class='close'></div>";
                            }
                        }
                        var show = "<div class='rank r" + r.ico + "' clstag='click|keycount|myhome|currentclass'><s></s><a target='_blank' href='http://usergrade.jd.com/user/grade'>" + r.uclassName + "</a></div>";
                        $("#memberInfo").html(show + html);
                        $(".member .close").toggle(function() {
                            $(".member .memb-info").hide().next().addClass("hover");
                        }, function() {
                            $(".member .memb-info").show().next().removeClass("hover");
                        });
                    }
                });
        }
    });
}

//获取用户头像
$.ajax({
    type : "GET",
    url : "http://i.jd.com/user/upload/showImg.action",
    dataType : "jsonp",
    timeout: 6000,
    success : function(result) {
        if (result && result.info && result.info[0].imgurl != "") {
            $("#userImg").attr('src', result.info[0].imgurl);
        }
    }
});

$.ajax({
    type : "GET",
    url : "http://qq.jd.com/new/checkPinHasBind.action",
    dataType : "jsonp",
    timeout: 6000,
    success : function(data) {
        var result = eval(data);
        if (result != null && result != "") {
            if (result.result == "0") {
                $("#jdTask").html("<a class='flk-01' href='http://qq.jd.com/new/regbind.aspx'>完善账户信息，随时随地享受快乐购物！</a>");
            }
        }
    }
});
//咨询回复数
$.ajax({
    type: "GET",
    url: "http://club.jd.com/MyJDIndexService.aspx",
    data: { method: "GetCount" },
    dataType: "jsonp",
    timeout: 6000,
    success: function(r) {
        if (r) {
            $("#reviewCount").html(r.ProductCount);
            $("#sdCount").html(r.OrderCount);
            try {
                if (parseInt(r.ProductCount) == 0) {
                    $("#reviewCountHref").addClass("flk-03");
                }
                if (parseInt(r.OrderCount) == 0) {
                    $("#sdCountHref").addClass("flk-03");
                }

            } catch(err) {
            }
        }
    }
});
//短消息
$.ajax({
    type: "GET",
    url: "http://s.jd.com/msg/index.php/msg/countmsg",
    dataType: "jsonp",
    timeout: 6000,
    success: function(r) {
        if (r) {
            $("#messageCount").html(r.MessageCount);
            try {
                if (parseInt(r.MessageCount) == 0) {
                    $("#messageCountHref").addClass("flk-03");
                }
            } catch(err) {
            }

        }
    }
});


//已回复的咨询
$.ajax({
    type: "GET",
    url: "http://club.jd.com/index.php?mod=Consultation&action=havingReplyCount",
    dataType: "jsonp",
    timeout: 6000,
    success: function(r) {
        if (r) {
            $("#notificationCount").html("" + r.cnt);
            try {
                if (parseInt(r.cnt) == 0) {
                    $("#notificationCountHref").addClass("flk-03");
                }
            } catch(err) {
            }

        }
    }
});

//我的关注
var cName = "ipLocation";
var iplocation = {
    "北京" : {
        id : "1"
    },
    "上海" : {
        id : "2"
    },
    "天津" : {
        id : "3"
    },
    "重庆" : {
        id : "4"
    },
    "河北" : {
        id : "5"
    },
    "山西" : {
        id : "6"
    },
    "河南" : {
        id : "7"
    },
    "辽宁" : {
        id : "8"
    },
    "吉林" : {
        id : "9"
    },
    "黑龙江" : {
        id : "10"
    },
    "内蒙古" : {
        id : "11"
    },
    "江苏" : {
        id : "12"
    },
    "山东" : {
        id : "13"
    },
    "安徽" : {
        id : "14"
    },
    "浙江" : {
        id : "15"
    },
    "福建" : {
        id : "16"
    },
    "湖北" : {
        id : "17"
    },
    "湖南" : {
        id : "18"
    },
    "广东" : {
        id : "19"
    },
    "广西" : {
        id : "20"
    },
    "江西" : {
        id : "21"
    },
    "四川" : {
        id : "22"
    },
    "海南" : {
        id : "23"
    },
    "贵州" : {
        id : "24"
    },
    "云南" : {
        id : "25"
    },
    "西藏" : {
        id : "26"
    },
    "陕西" : {
        id : "27"
    },
    "甘肃" : {
        id : "28"
    },
    "青海" : {
        id : "29"
    },
    "宁夏" : {
        id : "30"
    },
    "新疆" : {
        id : "31"
    },
    "台湾" : {
        id : "32"
    },
    "香港" : {
        id : "42"
    },
    "澳门" : {
        id : "43"
    }
};
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1)
        return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1)
        end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}
;

function getfollowCount() {
    var region = 1;
    var ck = getCookie(cName);
    if (ck) {
        var pid = iplocation[ck];
        if (pid)
            region = pid.id;
    }
    $("#arriveStockHref").attr("href", "http://t.jd.com/home/follow?page=1&a_a=1&r_r=" + region);
    $.ajax({
        type: "GET",
        url: "http://t.jd.com/follow/followProductCount.action",
        data: { method: "GetCount", r_r: region},
        dataType: "jsonp",
        timeout: 6000,
        success: function(r) {
            if (r) {
                $("#arriveStock").html(r.arriveStock + "");
                $("#priceReduction").html(r.priceReduction + "");
                $("#promotion").html(r.promotion + "");

                try {
                    if (parseInt(r.arriveStock) == 0) {
                        $("#arriveStockHref").addClass("flk-03");
                    }
                    if (parseInt(r.priceReduction) == 0) {
                        $("#priceReductionHref").addClass("flk-03");
                    }
                    if (parseInt(r.promotion) == 0) {
                        $("#promotionHref").addClass("flk-03");
                    }
                } catch(err) {
                }
            }
        }
    });
}
//getfollowCount();

//降价商品
getPriceReduction();
//促销商品
getPromotion();
//新到货商品
getArriveStock();
function getPriceReduction(){
	jQuery.ajax({
	    type: "GET",
	    url: "http://follow.soa.jd.com/product/queryForReduceProductCount.action",
	    dataType: "jsonp",
	    cache: false,
	    timeout: 6000,
	    success: function(r) {
	    	 if (r.success) {
	                $("#priceReduction").html(r.data + "");
	                try {
	                    if (parseInt(r.data) == 0) {
	                        $("#priceReductionHref").addClass("flk-03");
	                    }
	                } catch(err) {
	                }
	         }
	    }
	});
}


function getPromotion(){
	jQuery.ajax({
	    type: "GET",
	    url: "http://follow.soa.jd.com/product/queryForSaleProductCount.action",
	    dataType: "jsonp",
	    cache: false,
	    timeout: 6000,
	    success: function(r) {
	    	 if (r.success) {
	                $("#promotion").html(r.data + "");
	                try {
	                    if (parseInt(r.data) == 0) {
	                        $("#promotionHref").addClass("flk-03");
	                    }
	                } catch(err) {
	                }
	         }
	    }
	});
}


function getArriveStock(){
	jQuery.ajax({
	    type: "GET",
	    url: "http://follow.soa.jd.com/product/queryForStockProductCount.action",
	    dataType: "jsonp",
	    cache: false,
	    timeout: 6000,
	    success: function(r) {
	    	 if (r.success) {
	                $("#arriveStock").html(r.data + "");
	                try {
	                    if (parseInt(r.data) == 0) {
	                        $("#arriveStockHref").addClass("flk-03");
	                    }
	                } catch(err) {
	                }
	         }
	    }
	});
}


 var ajaxJd2008Path = "http://jd2008.jd.com/JdHome/AjaxService.aspx";
 if (umsMsgFlag == '1') {
	 ajaxJd2008Path = "http://order.jd.com/lazy/getOrderTrackShowJson.action";
 }

//订单中心---消息
function ShowUmsMessage(obj, orderId, parentId, paymentType, orgId, shipment, storeid, ordertype) {
    var curr = $(obj);
    var wrap = "<div class='prompt-01'><div class='pc loadings' id='tracker" + orderId + "'>加载中，请稍候...</div><div class='pb pb-r'><b class='b1'></b><b class='b2'></b><b class='b3'></b></div></div>";
    if (curr.find(".prompt-01").length == 0) {
        curr.append(wrap);
        var para = { action: "ShowUmsMessage", orderid: orderId, parentid: parentId, payment: paymentType, orgid: orgId, shipment: shipment, storeid: storeid, ordertype: ordertype };
        GetUmsMessage(para);
    }
    else {
        curr.find(".prompt-01").show();
    }
}
;

function GetUmsMessage(para) {
    $.ajax({
        type: "GET",
        url: ajaxJd2008Path,
        data: para,
        dataType: "jsonp",
        cache: false,
        success: function(result) {
            if (result.info) {
                var html = "<div onclick=\"$(this).parent().parent().hide()\" class='close'></div><dl><dt>处理时间</dt><dd>处理信息</dd></dl><ul>";
                for (var i = 0; i < result.info.length; i++) {
                    if (result.info[i].CreationTime == "") {
                        html += "<li><div class='fore1'></div><div class='fore2'>" + result.info[i].Content + "</div></li>";
                    }
                    else {
                        html += "<li><div class='fore1'>" + result.info[i].CreationTime + "</div><div class='fore2'>" + result.info[i].Content + "</div></li>";
                    }
                }
                html += "</ul>";
                $("#tracker" + para.orderid).html(html);
            }
        }
    });
}
;

function CloseUmsMessage() {
    $(".prompt-01").hide();
}
;

function ShowOneMonthOrder() {
    var para = { action: "GetBeforeOneMonthOrderList" };
    GetOneMonthOrder(para);
}
;

var flag = false;

var resultstr = "";
// 最近一个月订单开始
var ajaxNewPath = "http://order.jd.com/lazy/getOrderListJson.action";
if (newOrOld == '1') {
    ajaxNewPath = "http://jd2008.jd.com/JdHome/AjaxService.aspx";
}
$.ajax({
    type: "GET",
    url: ajaxNewPath,
    data: { action: "GetBeforeOneMonthOrderList"},
    dataType: "jsonp",
    cache: false,
    async:false,
    success: function(result) {
        if (result.info && result.info.length > 0) {
            flag = true;
            $("#latelyOrderListLi").removeClass("hide");
            $("#order-list").removeClass("hide");
            $("#reviewProductListLi").removeClass("curr");
            $("#stay-eval").addClass("hide");
            var wrap = "<div class='mc'><div id='order-list' class='tabcon'><div id='latelyOrderListDiv' class='tb-void tb-rows'><table cellspacing='0' cellpadding='0' border='0' width='100%'><tbody><tr><th width='90'>订单编号</th><th>订单商品</th><th width='90'>收货人</th><th width='90'>订单金额</th><th width='90'>下单时间</th><th width='90'>订单状态</th><th width='120'>操作</th></tr>";
            for (var i = 0; i < result.info.length; i++) {
                var temp = result.info[i];
                resultstr = resultstr + "<tr><td>" + temp.OrderId + "</td><td><div class='img-list'>" + temp.WareImgUrl + "</div></td><td>" + temp.CustomerName + "</td><td><div class='ftx-01'>" + temp.ShouldPay + "</div>" + temp.IdPaymentTypeName + "</td><td><div class='ftx-03'>" + temp.DateSubmit + "</div></td>" + temp.StateMsg + "<td clstag='click|keycount|myhome|ordercheck'>" + temp.StateBtn + "</td></tr>";
            }
            var wrapend = "</tbody></table></div><div class='extra'><a target='_blank' href='http://jd2008.jd.com/JdHome/OrderList.aspx?jdhome=myjd'>查看更多订单>></a></div></div></div>";
            $("#order-list").append(wrap + resultstr + wrapend);
        } else {
            flag = false;
        }
    }
});


//优惠卷，礼品卡
$.ajax({
    type: "GET",
    url: "http://coupon.jd.com/service.ashx",
    data: { m: "getcouponcount" },
    dataType: "jsonp",
    timeout: 6000,
    success: function(r) {
        if (r) {
            $("#CouponCount").html(r.CouponCount + "");
            $("#LipinkaCount").html(r.LipinkaCount + "");
            try {
                if (parseInt(r.CouponCount) == 0) {
                    $("#CouponCountHref").addClass("flk-03");
                } else {
                    $("#CouponCountHref").addClass("ftx-04");
                }
                if (parseInt(r.LipinkaCount) == 0) {
                    $("#LipinkaCountHref").addClass("flk-03");
                } else {
                    $("#LipinkaCountHref").addClass("ftx-04");
                }
            } catch(err) {
            }
        }
    }
});


//关注商品动态
/*
$.ajax({
    type : "GET",
    url : "http://t.jd.com/feed/myJdFeeds.action",
    dataType : "jsonp",
    timeout: 6000,
    success : function(result) {
        if (result && result.length > 0) {
            var html = "<div class=\"mt\"><h3>您关注的商品动态</h3><div class=\"extra\"><a href=\"http://t.jd.com/home/follow\">管理我关注的商品&raquo;</a></div></div>	<div class=\"mc\"><ul>";
            for (var i = 0, length = result.length; i < length; i++) {
                var proImg, proUrl, productUrl;
                if (result[i].type > 0 && result[i].type <= 5) {
                    proUrl = "http://t.jd.com/" + result[i].u;
                    if (result[i].proImg.length < 5) {
                        proImg = 'src="http://club.jd.com/images/'
                                + result[i].proImg + '.gif"';
                    } else {
                        proImg = 'src="http://snspic.jd.com'
                                + result[i].proImg
                                + '_sma_.jpg" class="lx"';
                    }
                    productUrl = "http://www.jd.com/product/"
                            + result[i].promulgator + ".html";
                } else {
                    proUrl = "http://www.jd.com/product/"
                            + result[i].promulgator + ".html";
                    productUrl = "http://www.jd.com/product/"
                            + result[i].promulgator + ".html";
                    proImg = 'src="http://img10.360buyimg.com/n5/'
                            + result[i].proImg + '" class="lx"';
                }
                html += '<li><div class="u-icon"><a href="' + proUrl + '"><img ' + proImg + '></a></div><div class="t-info"><a clstag="click|keycount|myhome|personname"  href="' + proUrl + '" class="u-name" target="_blank">' + result[i].unickname + '</a>' + result[i].echoText + '<a clstag="click|keycount|myhome|productname" href="' + productUrl + '" target="_blank" class="p-name">' + result[i].longTitle + '</a>' + result[i].econtent + '</div><div class="t-time">' + result[i].time + '</div></li>';

            }
            html += '</ul></div>';
            $("#atte-area").html(html);


            //可能感兴趣的人
            $.ajax({
                type : "GET",
                url : "http://t.jd.com/homepage/possibleInterestedPersonJson.action",
                dataType : "jsonp",
                success : function(result) {
                    if (result && result.length > 0) {
                        var html = '<div class="mt"><h3>您可能感兴趣的人…</h3></div><div class="mc"><ul class="uic-list">';
                        for (var i = 0, length = result.length; i < length; i++) {
                            html += '<li><a clstag="click|keycount|myhome|persontouxiang" href="http://t.jd.com/' + result[i].userId + '" target="_blank" title="' + result[i].name + '"><img src="' + result[i].imgUrl + '"></a></li>';
                        }
                        html += '</ul></div>';
                        $("#prod-area").html(html);

                    }
                }
            });
        }
    }
});
*/

//加载关注的商品
function followProductList(object) {
    //可能感兴趣的人
    $.ajax({
        type : "GET",
        url : "http://t.jd.com/follow/followProductList.action",
        dataType : "jsonp",
        timeout: 6000,
        success : function(result) {
            if (!result || result.length < 1) {
                html = "";
                object.find(".list").html(html);
            }
            if (result && result.length > 0) {
                var html = '<ul class="reco-list">';
                for (var i = 0, length = result.length; i < length; i++) {
                    html += '<li><div class="p-img"><a target="_blank" clstag="click|keycount|myhome|guanzhuimg" href="http://www.jd.com/product/' + result[i].id + '.html"><img src="' + (result[i].imgUrl).replace(/\/n5\//g, "/n4/") + '"></a></div>';
                    html += '<div class="p-name"><a clstag="click|keycount|myhome|guanzhuname" href="http://www.jd.com/product/' + result[i].id + '.html" target="_blank">' + result[i].name + '</a></div>';
                    html += '<div class="p-price"> 京东价：<strong>';
                    html += '<img src="http://price.jd.com/gp' + result[i].id + ',1.png" onerror="this.src=\'http://www.jd.com/images/no2.gif\'">';
                    html += '</strong></div></li>';
                }
                html += '</ul>';
                object.find(".list").html(html);
                object.find(".list").jdMarquee({
                    deriction: "right",
                    width:548,
                    height:183,
                    step: 3,
                    speed: 4,
                    delay: 10,
                    control: true,
                    _front:"#recoright03",
                    _back:"#recoleft03"
                });

            }
        }
    });
}


(function() {
    $("#reco-area").jdTab({
        event:"click",
        index:0,
        type:"dynamic"
    }, function(data, object, n) {
        if (!data) {
            return;
        }
        var url,objectFront,objectBack;
        switch (data) {
            case "1":

                return;
            case "2":
                url = "/myjd_latelyBrowse.action";
                objectFront = "#recoright02";
                objectBack = "#recoleft02";
                break;
            case "3":
                followProductList(object);
                return;
            case "4":
                url = "/myjd_teamList.action";
                objectFront = "#recoright04";
                objectBack = "#recoleft04";
                $.get(url, null, function(result) {

                    object.find("#mlist03").html(result);
                    object.find("#mlist03").jdMarquee({
                        deriction: "right",
                        width:548,
                        height:183,
                        step: 3,
                        speed: 4,
                        delay: 10,
                        control: true,
                        _front:objectBack,
                        _back:objectFront
                    });
                });
                return;
            case "5":
                url = "/myjd_robbuyList.action";
                objectFront = "#recoright05";
                objectBack = "#recoleft05";
                break;
        }
        $.get(url, null, function(result) {
            object.find(".list").html(result);
            object.find(".list").jdMarquee({
                deriction: "right",
                width:548,
                height:183,
                step: 3,
                speed: 4,
                delay: 10,
                control: true,
                _front:objectBack,
                _back:objectFront
            });
        });
    });


    $("#con-area").jdTab({
        event:"click",
        type:"dynamic"
    }, function(data, object, n) {
        if (!data)return;
        object.html("<iframe scrolling=\"no\" height=\"210px\" frameborder=\"0\" width=\"100%\" src=\"" + data + "\"></iframe>");
    });
})();
$("#buy-area").jdTab({event:"click"});
//显示订单状态的

$(".picture").hover(
        function() {
            $("#modifyheader").show();
        }, function() {
    $("#modifyheader").hide();
});

function CloseUmsMessage() {
    $(".prompt-01").hide();
}
;

$("#modifyheader").jdThickBox({
    type:"iframe",
    source:"http://i.jd.com/user/upload/show.action",
    width:600,
    height:580,
    _titleOn:false,
    _close:"headcolse",
    _con:"headcon",
    _autoReposi:false
});

function refurbish(str) {
    $("#userImg").attr('src', str);
}
;


jQuery.ajax({
    type: "GET",
    url: "http://club.jd.com/MyJDIndexService.aspx?method=AjaxWaitComment",
    dataType: "jsonp",
    cache: false,
    timeout: 6000,
    success: function(result) {
        if (result && result.length > 0) {
            $("#reviewProductListLi").removeClass("hide");
            if (flag) { //如果有一个月订单
            } else {
                $("#reviewProductListLi").addClass("curr");
                $("#stay-eval").removeClass("hide");
            }
            var wrap = "<div class='tb-void tb-rows'><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><th width='105'>&nbsp;</th><th>商品名称</th><th width='180'>购买时间</th><th width='160'>操作</th></tr>";
            for (var i = 0; i < result.length; i++) {
                var prodName = unescape(result[i].productName);
                wrap = wrap + "<tr><td><div class='img-list'><a clstag='click|keycount|myhome|commentqian' target='_blank' href='http://www.jd.com/product/" + result[i].productId + ".html'><img title='" + prodName + "' src='http://img10.360buyimg.com/N5/" + result[i].productImage + "'>";
                wrap = wrap + "</a></div></td><td><div class='p-name'><a href='http://www.jd.com/product/" + result[i].productId + ".html'>" + prodName + "</a></div></td><td>";
                wrap = wrap + "<div class='ftx-03'>" + result[i].createDate + "</div></td><td><a clstag='click|keycount|myhome|commentbt' href='http://club.jd.com/Simplereview/" + result[i].productId + ".html' class='btn btn-8'><s></s>我要评价</a><br/>";
                wrap = wrap + "<a clstag='click|keycount|myhome|commentcount' href='http://club.jd.com/review/" + result[i].productId + "-1-1.html'>查看所有" + result[i].commentCount + "个评价</a></td></tr>";
            }
            var wrapend = "</table></div><div class='extra'><a href='http://club.jd.com/mycomments.aspx' target='_blank'>查看更多待评价商品&raquo;</a></div></div>";

            $("#stay-eval").append(wrap).append(wrapend);

        }
    }
});
