$(document).ready(function JT_init() {
    $("a.jTip").hover(function() { JT_show(this.href, this.id, this.name) }, function() { $('#JT').remove() }).click(function() { return false });
});

//主显示函数
function JT_show(url, linkId, title) {
    if (title == false) title = "&nbsp;"; //标题

    var de = document.documentElement; //浏览器
    var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth; //浏览器宽度
    var h = self.innerHeight || (de && de.clientHeight) || document.body.clientHeight; //浏览器高度

    var clickElementx = getAbsoluteLeft(linkId); //set x position
    var clickElementy = getAbsoluteTop(linkId); //set y position

    var hasArea = w - clickElementx; //点击对象左边空余
    var hasAreaY = h - clickElementy;

    var queryString = url.replace(/^[^\?]+\??/, '');
    var params = parseQuery(queryString);
    var jtWidth = params['width'];
    if (jtWidth === undefined) { jtWidth = 250 };
    if (params['link'] !== undefined) {
        $('#' + linkId).bind('click', function() { window.location = params['link'] });
        $('#' + linkId).css('cursor', 'pointer');
    }

    var JT_position = "left";
    var JT_arrow_style = "";

    if (hasArea > ((jtWidth * 1) + 75)) {
        clickElementx = clickElementx + getElementWidth(linkId) + 11;
    }
    else {
        JT_position = "right";
        JT_arrow_style = "style='left:" + ((jtWidth * 1) + 1) + "px'";
        clickElementx = clickElementx - ((jtWidth * 1) + 15);
    }

    $("body").append("\
    <div id='JT' style='width:" + jtWidth * 1 + "px; left: " + clickElementx + "px; top: " + (clickElementy - 3) + "px;'>\
        <div id='JT_arrow_" + JT_position + "'" + JT_arrow_style + "></div>\
        <div id='JT_close_" + JT_position + "' name='title'>" + title + "</div>\
        <div id='JT_copy'><div class='JT_loader'><div></div>\
    </div>");

    $('#JT_copy').load(url);
    $('#JT').show();
    //实现拖拽
    if ($('#JT').Drags) {
        $('#JT [name="title"]').css('cursor', 'move');
        $('#JT').Drags({ opacity: .9, handle: '[name="title"]' });
    }

    //$('#JT_copy').css("background", "white");
    //$('#JT_copy').css("margin-top", $("#JT").height());
    //alert(hasArea);


    //对象绝对宽度
    function getElementWidth(objectId) {
        x = document.getElementById(objectId);
        return x.offsetWidth;
    }
    //对象绝对位置：left
    function getAbsoluteLeft(objectId) {
        o = document.getElementById(objectId)
        oLeft = o.offsetLeft            // 当前对象左侧偏移
        while (o.offsetParent != null) {   // 判断是否有上层元素影响位置
            oParent = o.offsetParent    // 申明上层元素
            oLeft += oParent.offsetLeft // 增加父层元素偏移
            o = oParent
        }
        return oLeft
    }
    //对象绝对位置：top
    function getAbsoluteTop(objectId) {
        // Get an object top position from the upper left viewport corner
        o = document.getElementById(objectId)
        oTop = o.offsetTop            // Get top position from the parent object
        while (o.offsetParent != null) { // Parse the parent hierarchy up to the document element
            oParent = o.offsetParent  // Get parent object reference
            oTop += oParent.offsetTop // Add parent top position
            o = oParent
        }
        return oTop
    }
    //参数
    function parseQuery(query) {
        var Params = new Object();
        if (!query) return Params; // return empty object
        var Pairs = query.split(/[;&]/);
        for (var i = 0; i < Pairs.length; i++) {
            var KeyVal = Pairs[i].split('=');
            if (!KeyVal || KeyVal.length != 2) continue;
            var key = unescape(KeyVal[0]);
            var val = unescape(KeyVal[1]);
            val = val.replace(/\+/g, ' ');
            Params[key] = val;
        }
        return Params;
    }
    //鼠标失效
    function blockEvents(evt) {
        if (evt.target) {
            evt.preventDefault();
        } else {
            evt.returnValue = false;
        }
    }
}