﻿/*
 * jDiv 1.0
 * Make By Lee(2008.10.25)
 *================================
 使用方法：
 
 显示jDiv
 obj：触发jDiv显示的对象
 title：标题
 content：内容
 width：jDiv宽度
 height：jDiv高度
 showjDiv(obj, title, content, width, height);
 
 隐藏jDiv
 hidejDiv();
 
 设置jDiv标题
 setjDivTitle(title)
 
 设置jDiv内容
 setjDivContent(content) 
 *================================
*/

var $jDiv = null;
var $jDivTitle = null;
var $jDivBody = null;

var $objEntity = null;
var pagewidth = 1000;//页面宽
var pageheight = 800;//页面高
var jdivwidth = 0;//jDiv宽
var jdivheight = 0;//jDiv高
var divoffset = 0;//jDiv的间隔
var animatetim = 100;//显示动画时间(毫秒)

var initjDiv = function() {
    var jDiv =  '\
            <DIV id="jDiv" class="x-window x-window-plain x-window-dlg" style="Z-INDEX:9003;DISPLAY:none;POSITION:absolute;">\
                <DIV class="x-window-tl">\
                    <DIV class="x-window-tr">\
                        <DIV class="x-window-tc">\
                            <DIV class="x-window-header x-unselectable x-window-draggable" style="MozUserSelect: none; KhtmlUserSelect: none" unselectable="on">\
                                <DIV class="x-tool x-tool-close " onclick="hidejDiv()">\
                                    &nbsp;\
                                </DIV>\
                                <SPAN id="jDivTitle" class="x-window-header-text">\
                                </SPAN>\
                            </DIV>\
                        </DIV>\
                    </DIV>\
                </DIV>\
                <DIV class="x-window-bwrap">\
                    <DIV class="x-window-ml">\
                        <DIV class="x-window-mr">\
                            <DIV class="x-window-mc">\
                                <!-- Start-->\
		                        <div id="jDivBody">\
		                        </div>\
                                <!-- End-->\
                            </DIV>\
                        </DIV>\
                    </DIV>\
                    <DIV class="x-window-bl">\
                        <DIV class="x-window-br">\
                            <DIV class="x-window-bc">\
                                <DIV class="x-window-footer"></DIV>\
                            </DIV>\
                        </DIV>\
                    </DIV>\
                </DIV>\
            </DIV>\
            ';
    
    jQuery(jDiv).appendTo("body");
    $jDiv = jQuery("#jDiv");
    $jDivTitle = jQuery("#jDivTitle");
    $jDivBody = jQuery("#jDivBody");
}

//显示
var showjDiv = function(obj, title, content, width, height) {
    Mouse(obj);
    $objEntity = jQuery(obj);
    
    $jDiv.css("width", width);
    $jDivBody.css("height", height);
    
    setjDivTitle(title);
    setjDivContent(content);
    
    var jwidth = $jDiv.width();
    var jheight = $jDiv.height();
    if(jdivwidth < jwidth) jdivwidth = jwidth;
    if(jdivheight < jheight) jdivheight = jheight;
    
    if(((leftpos + jdivwidth) > pagewidth) && pagewidth > jdivwidth) {
        $jDiv.css("left", leftpos - jdivwidth - divoffset * 2);
    }
    else {
        $jDiv.css("left", leftpos + $objEntity.width());
    }
    if((toppos + jdivheight) > pageheight) {
        $jDiv.css("top", pageheight - jdivheight);
    }
    else {
        $jDiv.css("top", toppos);
    }
    
    $jDiv.slideDown(animatetim);//可设置自己的动画效果
}

//隐藏
var hidejDiv = function() {
    $jDiv.slideUp(animatetim);//可设置自己的动画效果
}

//设置标题
var setjDivTitle = function(title) {
    $jDivTitle.html(title);
}

//设置内容
var setjDivContent = function(content) {
    $jDivBody.hide();
    $jDivBody.html(content);
    $jDivBody.fadeIn(animatetim);
}

//计算坐标函数
var Mouse = function(obj){
    mouse = new MouseEvent(obj);
    leftpos = mouse.x + divoffset;
    toppos = mouse.y + divoffset;
}

//获取鼠标坐标函数
var MouseEvent = function(obj) {
    var offset = jQuery(obj).offset();
    this.x = offset.left;
    this.y = offset.top;
	//alert(obj);
	//alert(this.x);
	//alert(this.y);
}

var resizePage = function() {
    pagewidth = jQuery(document.documentElement).attr("clientWidth")
    pageheight = jQuery(document.documentElement).attr("clientHeight")
}

jQuery(document).ready(function() {
    resizePage();
    initjDiv();
});

if(jQuery.browser.mozilla) {
    window.addEventListener("resize",function() {
        resizePage();
    }, false);
}
else {
    window.attachEvent("onresize",function() {
        resizePage();
    });
}

var timeDiv = null;
var objMouse = null;
jQuery(document).mousemove(function(e) {
    if($jDiv && $jDiv.css("display") != "none") {
        objMouse = e;
        if(timeDiv) clearTimeout(timeDiv);
        timeDiv = setTimeout(resetjDiv, 50);
    }
});

var resetjDiv = function(e) {
    if($jDiv && $jDiv.css("display") != "none") {
        if(!objMouse) return;
        e = objMouse;
        var offset = $jDiv.offset();
        var leftmin = offset.left;
        var leftmax = leftmin + $jDiv.width();
        var topmin = offset.top;
        var topmax = topmin + $jDiv.height();
        var blInbox = (e.pageX >= leftmin && e.pageX <= leftmax) && (e.pageY >= topmin && e.pageY <= topmax);
        var blInobj = false;
        if($objEntity) {
            offset = $objEntity.offset();
            leftmin = parseInt(offset.left, 10);
            leftmax = leftmin + parseInt($objEntity.width(), 10);
            topmin = parseInt(offset.top, 10);
            topmax = topmin + parseInt($objEntity.height(), 10);
            var blInobj = (e.pageX >= leftmin && e.pageX <= leftmax) && (e.pageY >= topmin && e.pageY <= topmax);
        }
        if(!blInbox && !blInobj) hidejDiv();
    }
}