// JavaScript Document
// 初始化用户状态
var friend = null;

function buildCommentList(item, lastChildClass, userid) {
    var comment;
    if (friend) {
        if (friend['' + userid + '']) {
            comment = buildCommentItem(item, lastChildClass, true);
        } else {
            comment = "";
        }
    } else {
        comment = buildCommentItem(item, lastChildClass);
    }
    return comment;
}

function buildCommentItem(item, lastChildClass, flag) {
    var alias = item.customer.alias;
    var userid = item.customer.id;
    var displayName = item.customer.displayName;
    var commenttext = item.text;
    var created = item.created;
    // var date = new Date();
    // var currentDate = date.getTime();
    // var minute = new String((currentDate - created) / 1000 / 60);
    var time = getMinuteToTimes(created, true);
    var imgurl = "/uc/ws/0.1/user/image/small/" + userid;
    var lnglat = item.poiLon + "," + item.poiLat;
    var id = item.id;
    var poiKeyword = item.poiKeyword;
    var poiId = item.poiId;
    if (!poiId) {
        poiId = "null";
    }
    if (displayName) {
        alias = displayName;
    }
    if (alias.lenght >= 5) {
        alias = substringbyte(alias, 8);
    }
    if (commenttext.length > 140) {
        commenttext = commenttext.substring(0, 140) + "...";
    }
    var comment = "<li id=\"" + id + "\" class='" + lastChildClass + "'>";
    if (flag) {
        comment += "<div class='user_pic'><a href='space-home.jsp?customer="
                + userid
                + "'  style='cursor:pointer;text-decoration:none' asyncHtml='asyncHtml'><img src="
                + imgurl
                + " height=50 width=50 title="
                + alias
                + "></a></div>"
                + "<div class=\"msg\">"
                + "<div><span style='color:#254b78;'><a href='space-home.jsp?customer="
                + userid
                + "' style='cursor:pointer;text-decoration:none' asyncHtml='asyncHtml'>"
                + alias
                + "</a></span>: <span style='color:#666666;line-height:18px'>"
                + time
                + "</span><span class='commenting-label'>评论了</a><a href='javascript:void(0)' onclick=\"poiCommnet('"
                + poiId + "','" + lnglat + "','" + poiKeyword + "',true);return false\">" + poiKeyword
                + "</a></div>" + "<div class=\"msg\">" + " <div class=\"msg_header_bg\"></div>"
                + " <div class=\"msg_body_bg\">" + commenttext + "</div>"
                + " <div class=\"msg_tail_bg\"></div>" + "</div>" + "</div>" + " </li>";
    } else {
        comment += "<div class='user_pic'><a href='home.jsp#space-home.jsp?customer="
                + userid
                + "'  style='cursor:pointer;text-decoration:none'><img src="
                + imgurl
                + " height=50 width=50 title="
                + alias
                + "></a></div>"
                + "<div class=\"msg\">"
                + "<div><span style='color:#254b78;'><a href='home.jsp#space-home.jsp?customer="
                + userid
                + "' style='cursor:pointer;text-decoration:none'>"
                + alias
                + "</a></span>: <span style='color:#666666;line-height:18px'>"
                + time
                + "</span><span class='commenting-label'>评论了</a><a href='javascript:void(0)' onclick=\"poiCommnet('"
                + poiId + "','" + lnglat + "','" + poiKeyword + "');return false\">" + poiKeyword
                + "</a></div>" + "<div class=\"msg\">" + " <div class=\"msg_header_bg\"></div>"
                + " <div class=\"msg_body_bg\">" + commenttext + "</div>"
                + " <div class=\"msg_tail_bg\"></div>" + "</div>" + "</div>" + " </li>";
    }

    return comment;
}

function initUserStatus(friendList) {
    var comment = "";
    if (friendList) {
        friend = friendList;
    }
    $.ajax({
        url : "/poicomment/ws/0.1/comment/review_all",
        dataType : "json",
        cache : true,
        success : function(data) {
            var $ul = $("#comment");
            var items = data.items;
            if (items.length > 0) {
                for ( var i = 0; i < 20 && i < items.length; i++) {
                    var lastChildClass = (i + 1 == 20 || i + 1 == items.length) ? "last-child" : "";
                    var itemComment = buildCommentList(items[i], lastChildClass, items[i].customer.id);
                    if (itemComment) {
                        comment += itemComment;
                    }
                }
                if (comment == "") {
                    comment = "<li class='no-message'>暂无好友动态</li>";
                }
                $ul.html(comment);
            }
        },
        error : function(xhr) {
            common401Handler(xhr);
            var $ul = $("#comment");
            var comment = "<li class='no-message'>暂无好友动态</li>";
            $ul.html(comment);
        }
    });
};

function getOneMoreComment() {
    $.ajax({
        url : "/poicomment/ws/0.1/comment/review_all",
        dataType : "json",
        cache : true,
        success : function(data) {
            var $ul = $("#comment");
            var $li = $("#comment li");
            var items = data.items;
            if (items.length > 0) {
                var item = items[0];
                var poi = $("#" + item.id);
                if (poi.length == 0) {
                    var comment = buildCommentList(item, "");
                    if ($li.length < 5) {
                        $ul.prepend(comment);
                    } else {
                        $ul.find("li:last").html(comment);
                    }
                }
            }
        },
        error : function(xhr) {
            common401Handler(xhr);
        }
    });
}

function homePageReady() {
    // setInterval(function() {
    // getOneMoreComment();
    // }, 24000);

    $(".action").mouseover(function() {
        $(".action").css("cursor", "pointer");
    });
    $(".action").click(function() {
        getUserList();
    });
    initUserStatus();
    getUserList();
}

// 获取推荐用户
function getUserList() {
    $.ajax({
        url : "/uc/ws/0.1/user/view/new",
        dataType : "json",
        type : "get",
        cache : true,
        success : function(data) {
            var $userList = $("#userList");
            var comment = "";
            if (data.length > 0) {
                for ( var i = 0; i < data.length; i++) {
                    var userId = data[i].id;
                    var alias = data[i].displayName;
                    if (alias == null || alias == undefined || alias == "") {
                        alias = data[i].alias;
                    }

                    var imgurl = "/uc/ws/0.1/user/image/small/" + userId;
                    comment += "<li id=" + userId
                            + "><p class='user_pic'><a href='home.jsp#space-home.jsp?customer=" + data[i].id
                            + "'><img src='" + imgurl + "' width='50px' height='50px' alt='" + alias
                            + "' title='" + alias + "'></a></p>";
                    comment += "<p class='last-child'><a href='home.jsp#space-home.jsp?customer="
                            + data[i].id + "' class='short' title='" + alias + "'>" + substringbyte(alias, 8)
                            + "</a></p></li>";

                }
            }
            $userList.html(comment);
        },
        error : function(xhr) {
            common401Handler(xhr);
        }
    });
}
function getCompanyInfo() {
    $.ajax({
        url : "/cms/ws/0.1/content/list?name=companyStatus",
        dataType : "json",
        success : function() {
        },
        error : function(xhr) {
            common401Handler(xhr);
        }
    });
}

