var PopUI = {
    is_active: 1,
    cache_list: [],
    test_url: "",
    url: "http://www.chinaaet.com/tools/ajax.aspx",
    a_mouseover: null,
    defer_show: 0,
    defer_hide: 0,
    timeout: 500,
    id: "#popup",
    movable: true,
    draging: false,
    is_draged: false,
    is_show: false,
    loading: "http://s1.chinaaet.com/images/loading.gif",
    onloading: false,
    win: null,
    dom: null,
    div: null,
    scroll_hide: false,
    getPos: function (n) {
        var r = this;
        var e, q, w = $(n),
            p = r.div,
            t = r.dom,
            f = r.win;
        var d = p.width(),
            m = p.height();
        var g = w.offset(),
            l = w.height(),
            b = w.width();
        var v = f.width(),
            c = f.height(),
            o = t.scrollLeft(),
            h = t.scrollTop();
        var u = (document.documentElement.clientWidth - document.body.clientWidth) / 2;
        var k = g.left + d < v * 0.8 + o;
        var s = g.top + m < c * 0.9 + h;
        e = k ? g.left : g.left - d + b;
        q = s ? g.top + l + 2 : g.top - m - l;
        e = e - u;
        return {
            left: e,
            top: q
        }
    },
    getData: function (d) {
        var b = this;
        var e = $(d).text();
        var a = (e.length > 10) ? (e.substring(0, 10) + "...") : e;
        $("#headp a").html(a).attr("href", "http://www.chinaaet.com/search/?q=" + encodeURI(e));
        if (b.cache_list[e]) {
            b.setData(b.cache_list[e]);
            b.show(b.getPos(d))
        } else {
            var c = b.url + "?otype=innerlink&innertitle=" + encodeURI(e);
            if (b.test_url) {
                c = b.test_url
            }
            $("#summaryDataBody").html('<div style="height:90px;"><img src="' + b.loading + '" border="0" /> 正在加载内容...</div>');
            b.show(b.getPos(d));
            b.onloading = true;
            $.ajax({ url: c, success: function (g, f) {
                if (b.a_mouseover != d || !b.is_show) {
                    return
                }
                if (f != "success") {
                    return alert(f)
                }
                b.cache_list[e] = g;
                b.setData(g);
                if (!b.is_draged) {
                    b.show(b.getPos(d))
                }
                b.onloading = false
            }, dataType: "json"
            });
        }
    },
    setData: function (e) {
        var b = e.isok;
        var f = "";
        if (r == "") {
            r = "http://www.chinaaet.com/search/?q=" + encodeURI(a);
        }
        if (b == false) {
            f = "<p style='padding:5px; margin:0;'><font size='2' color='red' >关键词尚无人购买,请点击购买!</font></p>"
        } else {
            var a = e.data[0].InnerTitle;
            var c = e.data[0].ID;
            var r = e.data[0].InnerLink;
            var d = e.data[0].InnerDescription;
            var g = e.data[0].InnerImage;
            if (g && g != "null") {
                f = '<a href="' + r+ '" target="_blank"><img onload="PopUI.imgload(this)" src="' + g + '" style="border: 1px solid rgb(204, 204, 204); margin: 5px 10px 0pt 5px; padding: 1px; float: left;max-width:100px;max-height:100px;_width:expression(this.width > 100 ? 100 : true);_height:expression(this.height > 100 ? 100 : true);"></a>'
            }
            f += '<div style="float:left;text-align:left;width:160px;"><p style="margin: 0pt; padding: 5px;">' + d + '<br/><a style="color:red;" href="' + r + '" target="_blank">查看详情&gt;&gt;</a></p></div>'
        }
        $("#summaryDataBody").html(f)
    },
    imgload: function (b) {
        var a = this;
        var c = a.getPos(a.a_mouseover);
        a.div.css({
            top: c.top,
            left: c.left
        });
    },
    show: function (b) {
        var a = this;
        a.is_show = true;
        a.is_draged = false;
        a.div.css({
            top: b.top,
            left: b.left
        }).show();
        if (a.scroll_hide) {
            a.win.bind("scroll", function () {
                a.hide()
            })
        }
    },
    hide: function () {
        var a = this;
        a.is_show = false;
        a.div.hide();
        if (a.scroll_hide) {
            a.win.unbind("scroll", function () {
                a.hide()
            })
        }
    },
    init: function () {
        if ($("#popup").size() == 0) {
            $("body").append('<div style="width:290px;overflow:hidden;z-index:10;background-color:#d1e7fc;position:absolute;padding:5px; font-size:12px; ine-height:20px;top:-1000px;left:0;" id="popup"><p style="padding:0 0 5px 0;width:290px;margin:0;float:left;" id="headp"></p><div style="float:left;padding:5px;width:280px;background-color:#fff;line-height:18px" id="summaryDataBody"></div></div>')
        } else {
            if ($("#headp").size() == 0 || $("#summaryDataBody").size() == 0) {
                return alert("Momo is black out.")
            }
        }
        var a = this;
        a.div = $(a.id);
        a.dom = $(document);
        a.win = $(window);
        $("a.innerlink").mouseover(function () {
            if (!a.is_active || a.draging) {
                return
            }
            $(this).attr("target", "_blank");
            a.hide();
            var b = this;
            a.a_mouseover = b;
            clearTimeout(a.defer_hide);
            clearTimeout(a.defer_show);
            a.defer_show = setTimeout(function () {
                a.getData(b)
            }, a.timeout)
        }).mouseleave(function () {
            if (!a.is_active || a.draging) {
                return
            }
            clearTimeout(a.defer_show);
            a.defer_hide = setTimeout(function () {
                a.hide()
            }, 1500)
        });
        a.div.mouseover(function () {
            clearTimeout(a.defer_hide)
        }).dblclick(function () {
            a.hide()
        }).mouseleave(function () {
            clearTimeout(a.defer_hide);
            a.defer_hide = setTimeout(function () {
                a.hide()
            }, 1500)
        }).css({
            visibility: "visible"
        }).hide();
        $("#headp").html('<span style="float: left; font-size: 12px; margin-left: 5px;"><img src="http://s1.chinaaet.com/images/pop_search.gif" align="absmiddle"> <a target="_blank" style="color: rgb(255, 0, 0); text-decoration: none;" href="">title</a></span><span style="float: right; margin-right: 5px;cursor: pointer;"><img name="close" src="http://s1.chinaaet.com/images/pop_close.gif" width="14" border="0" height="14"></span>');
        $("#headp").find("img[name=close]").click(function () {
            a.hide()
        });
        $("#headp").mousedown(function (c) {
            c = c || event;
            if (!a.movable) {
                return
            }
            a.draging = 1;
            var d = a.div.offset();
            var b = $(document.body).offset();
            a.offsetClick = {
                width: c.pageX - d.left + b.left,
                height: c.pageY - d.top,
                startX: d.left,
                startY: d.top
            };
            a.div.unbind("mouseleave");
            $(document).mousemove(function (f) {
                a.move(f)
            }).bind("selectstart", function () {
                return false
            });
            $(document).mouseup(function (g) {
                var f = a.offsetClick;
                var h = {
                    left: f.startX,
                    top: f.startY
                };
                a.draging = 0;
                if (g.clientY < 0 || g.clientX < 2 || g.clientX > $(document).width() || g.clientY > $(window).height()) {
                    a.div.css(h)
                }
                $(document).unbind("mousemove").unbind("selectstart").unbind("mouseup");
                a.div.mouseleave(function () {
                    clearTimeout(a.defer_hide);
                    a.defer_hide = setTimeout(function () {
                        a.hide()
                    }, 1500)
                });
                return false
            })
        })
    },
    move: function (f) {
        f = f || event;
        var d, c, b = this;
        d = f.pageX || (f.clientX + $(document).scrollLeft());
        c = f.pageY || (f.clientY + $(document).scrollTop());
        var a = b.offsetClick;
        b.div.css({
            left: d - a.width,
            top: c - a.height
        });
        b.is_draged = true
    },
    setOn: function () {
        this.is_active = 1
    },
    setOff: function () {
        this.is_active = 0;
        this.hide()
    }
};
