﻿//省市分类联动
function onAreaChange(url, cid, province, city) {
    var ddl = $(city)
    ddl[0].length = 0;
    ddl[0].options.add(new Option("--", "0"));
    var pid = $(province).val();
    if (pid == 0) return false;
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        data: { pid: pid },
        success: function(json) {
            $.each(json, function(i, item) {
                ddl[0].options.add(new Option(item.AreaName, item.ID));
            });
            for (var i = 0; i < ddl[0].options.length; i++) {
                if (ddl[0].options[i].value == cid) {
                    ddl[0].options[i].selected = true;
                    break;
                }
            }
        }
    });
} //省市分类联动结束
function inputblur(obj) {
    $(obj).focus(function() {
        if ($(this).val() == this.defaultValue)
            $(this).val('');
    }).blur(function() {
        if ($(this).val() == '')
            $(this).val(this.defaultValue);
    });
}
(function(JQ) {
    JQ.fn.textEdit = function(obj, pobj, editClass, startClass, inputStyle, url) {
        var id = $(pobj).attr("link");
        //监听鼠标经过事件   
        $(obj).mouseover(function() {
            $(obj).addClass(editClass);
        });
        $(obj).mouseout(function() {
            $(obj).removeClass(editClass);
        });

        $(obj).click(function() {
            var textarea = $('<input style="' + inputStyle + '" type="text" value="' + $(obj).html() + '" ></input>').blur(
                            function() {
                                var spanValue = $(this).val();
                                if (spanValue === '') {
                                    jAlert('没有填写内容');
                                    return;
                                }
                                if (spanValue.length > 50) {
                                    jAlert('填写内容超过50字');
                                    return;
                                }
                                if (spanValue != $(obj).html()) {
                                    $.ajax({
                                        type: "POST",
                                        url: url,
                                        dataType: "json",
                                        data: { pid: id, title: spanValue },
                                        success: function(json) {
                                            json = eval('(' + json + ')');
                                            jAlert(json.msg);
                                        }
                                    });
                                }
                                var spanText = $('<em class="' + startClass + '"  title="点击可以编辑">' + spanValue + '</em>');
                                spanText.textEdit(spanText, $(pobj), editClass, startClass, inputStyle, url);
                                $(this).after(spanText).remove();
                                spanText.siblings().val(spanValue);
                            }).focus(function() { this.select(); });
            $(obj).after(textarea).remove();
            textarea[0].focus();
        });
    }
} (jQuery));
function mouseCoords(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    return {
        x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.scrollTop - document.body.clientTop
    };
}

$(function() {
    loadattention();
    $('#fattention').dialog({ autoOpen: false, width: 400 });
    $(".ulist").each(function() {
        var uid = $(this).attr("link");
        $(this).find(".huifilter").mouseover(function(event) {
            $(this).removeClass("huifilter");
            $(this).addClass("yell");
            var event = event || window.event;
            var mousePos = mouseCoords(event);
            var oleft = mousePos.x;
            var otop = mousePos.y;
            var left = oleft;
            var top = otop;
            if (left + 270 >= window.screen.availWidth - 45)
                left = left - 270;
            if (top + 240 >= document.body.clientHeight)
                top = top - 180;
            if (utdiv) clearTimeout(utdiv);
            utdiv = setTimeout("createuserinfofilter('" + uid + "','" + top + "','" + left + "','" + otop + "','" + oleft + "');", 500);
        }).mouseout(function() {
            $(this).addClass("huifilter");
            $(this).removeClass("yell");
            utdiv1 = setTimeout("removeuserinfofilter();", 500);
        });
    });
    $(":input").each(function() {
        $(this).mouseover(function() {
            switch ($(this).attr("type")) {
                case 'button':
                    $(this).removeClass("button");
                    $(this).addClass("buttonhover");
                    break;
                case 'submit':
                    $(this).removeClass("button");
                    $(this).addClass("buttonhover");
                    break;
                case 'text':
                    $(this).addClass("inputhover");
                    break;
                case 'file':
                    $(this).addClass("inputhover");
                    break;
                case 'password':
                    $(this).addClass("inputhover");
                    break;
            }
        }).mouseout(function() {
            switch ($(this).attr("type")) {
                case 'button':
                    $(this).addClass("button");
                    $(this).removeClass("buttonhover");
                    break;
                case 'submit':
                    $(this).addClass("button");
                    $(this).removeClass("buttonhover");
                    break;
                case 'text':
                    $(this).removeClass("inputhover");
                    break;
                case 'file':
                    $(this).removeClass("inputhover");
                    break;
                case 'password':
                    $(this).removeClass("inputhover");
                    break;
            }
        });
    });
    $("textarea").each(function() {
        $(this).mouseover(function() {
            $(this).addClass("inputhover");
        }).mouseout(function() {
            $(this).removeClass("inputhover");
        });
    });
});
var utdiv = null;
var utdiv1 = null;
function createuserinfofilter(id, top, left, otop, oleft) {
    if (left == oleft) left = oleft;
    if (top == otop) top = otop;
    var boardDiv = "<div id='userinfofilter'style='background-color:#fff; border:solid 5px #A8A879;max-width:270px;max-height:180px;z-index:999;position:absolute;top:" + otop + "px;left:" + oleft + "px;'>加载中...</div>";
    $(document.body).append(boardDiv);
    $('#userinfofilter').mouseover(function() {
        clearTimeout(utdiv1);
    }).mouseout(function() {
        utdiv1 = setTimeout("removeuserinfofilter();", 500);
    });
    setTimeout('loaduserinfo(' + id + ',' + top + ',' + left + ');', 500);
}
function removeuserinfofilter() {
    if ($('#userinfofilter') != null) {
        $('#userinfofilter').remove();
    }
}
function loaduserinfo(id, top, left) {
    $.ajax({
        type: "POST",
        url: "/member/loaduserinfo",
        dataType: "json",
        data: { uid: id },
        success: function(json) {
            json = eval('(' + json + ')');
            loaduserinforeturn(json, top, left);
        }
    }); return false;
}
function loaduserinforeturn(json, top, left) {
    $("#userinfofilter").css("left", left + "px");
    $("#userinfofilter").css("top", top + "px");
    $("#userinfofilter").html('\
        <div class="userinfofilter">\
            <div class="userinfofiltertitle clearfix">\
                <p>\
                    <a href="/i/u/' + json.UserCode + '">' + json.UserName + '</a></p>\
                <div class="userinfofiltertitleright">\
                    <a href="/publish-item-' + json.ID + '"><span class="datingicon" title="约会">约会</span></a>\
                    <a href="javascript:;" onclick="onAttentionIcon(' + json.ID + ');"><span class="attentionicon" title="招呼">招呼</span></a>\
                    <a href="javascript:;" onclick="onFavoriteIcon(' + json.ID + ');"><span class="favoriteicon" title="关注">关注</span></a></div>\
            </div>\
            <div class="userinfofiltercontent clearfix">\
                <div class="userinfofiltercontentleft">\
                    <div class="useritem">\
                        <img src="' + json.Avatar + '" alt="' + json.UserName + '" />\
                    </div>\
                    <p>\
                        ' + json.Sex + '(' + json.Age + ')' + json.Area + '</p>\
                    <p>\
                        ' + json.Work + '</p>\
                </div>\
                <div class="userinfofiltercontentright">\
                    <p>交友对象：' + json.DatingObj + '</p>' + json.Sincere + '\
                    <p>我的约会方式：' + json.DatingType + '</p>\
                </div>\
            </div>\
        </div>');
}
//<a href="javascript:;" onclick="onAttentionIcon(' + json.ID + ');"><span class="attentionicon" title="关注">关注</span></a>\

function loadmsgfrom(vid, tid) {
    $(document.body).append('<div id="' + vid + '" title="发送留言" style="display: none;">\
        <fieldset>\
            <form id="frmmsg" name="frmmsg">\
            <div style="padding:5px">\
                <label for="title">\
                    标题：</label>\
                <input id="title" name="title" value="" type="text" />\
                <p id="titleTip">\
                </p>\
            </div>\
            <div style="padding:5px">\
                <label for="content">\
                    内容：</label>\
                <textarea id="content" name="content" rows="8" cols="50"></textarea>\
                <p id="contentTip">\
                </p>\
            </div>\
            <div style="padding:5px">\
                <label style="padding-left: 45px">\
                </label>\
                <input name="mid" id="mid" type="hidden" value="" />\
                <input name="uid" id="uid" type="hidden" value="" />\
                <input name="tid" id="tid" type="hidden" value="' + tid + '" />\
                <input id="btnmsg" type="submit" value="发送" class="button" />\
            </div>\
            </form>\
        </fieldset>\
    </div>');
}
function loadattention() {
    $(document.body).append('<div id="fattention" title="打招呼" style="display: none;">\
        <fieldset>\
            <div id="attentionraidos" style="width:360px;">\
            </div>\
            <div>\
                <input id="btnattention" type="button" value="发送" class="button" />\
            </div>\
        </fieldset>\
    </div>');
}
function onAttentionIcon(uid) {
    getAttention();
    $('#fattention').show();
    $('#fattention').dialog('open');
    $('#btnattention').click(function() {
        $.ajax({
            type: "POST",
            url: '/member/onattention',
            dataType: "json",
            data: { uid: uid, sid: $('input:radio[name=sid][checked=true]').val() },
            success: function(json) {
                json = eval('(' + json + ')');
                jAlert(json.msg, '温馨提示', function() { $('#fattention').dialog('close'); });
            }
        });
    });
    return false;
}
function onFavoriteIcon(uid) {
    $.getJSON('/member/onfavorite', { uid: uid }, function(json) {
        json = eval('(' + json + ')');
        jAlert(json.msg);
    });
}
function getAttention() {
    $.getJSON("/member/loadsalutation", null, function(json) {
        var raidos = '';
        $.each(json, function(i, item) {
            raidos += '<input type="radio" name="sid" value="' + item.ID + '" ' + (i == 0 ? 'checked="checked"' : '') + '/>' + item.Content;
            //if (i % 5 == 0 && i != 0)
                raidos += '<br />';
            $('#attentionraidos').html(raidos);
        });
    });
}