﻿/*
 * 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 _xOffset = 10;
var _yOffset = 30;

var initjDiv = function() {
    var jDiv =  '\
<div id="jDiv">\
  <div class="jDivTitleStyle">\
    <span class="jDivTitleLeft" id="jDivTitle">\
    </span>\
    <span class="jDivTitleRight" onclick="hidejDiv()">\
      关闭\
    </span>\
  </div>\
  <div class="jDivBox" id="jDivBody">\
  </div>\
</div>\
            ';
    
    jQuery(jDiv).appendTo("body");
    _jDiv = jQuery("#jDiv");
    _jDivTitle = jQuery("#jDivTitle");
    _jDivBody = jQuery("#jDivBody");
}

//显示
var showjDiv = function(obj, title, content, width, height) {
    jDivMouse(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);
		_jDiv.css("left", (_leftpos + _yOffset) + "px");
    }
    else {
        //_jDiv.css("left", _leftpos + _objEntity.width());
		_jDiv.css("left", (_leftpos + _yOffset) + "px");
    }
    if((_toppos + _jdivheight) > _pageheight) {
        //_jDiv.css("top", _pageheight - _jdivheight);
		_jDiv.css("top", (_toppos - _xOffset) + "px");
    }
    else {
        //_jDiv.css("top", _toppos);
		_jDiv.css("top", (_toppos - _xOffset) + "px");
    }
    
    _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 jDivMouse = function(obj){
    _jDivMouse = new jDivMouseEvent(obj);
    _leftpos = _jDivMouse.x + _divoffset;
    _toppos = _jDivMouse.y + _divoffset;
}

//获取鼠标坐标函数
var jDivMouseEvent = function(obj) {
    var offset = jQuery(obj).offset();
    this.x = offset.left;
    this.y = offset.top;
}

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();
    }
}