﻿
var tWait=50;//停留tWait豪秒后显示提示。
var tShow=5000;//显示sHow豪秒后关闭提示
var showStep=10;//渐出递增值
var hiddenStep=10;//渐入递增值
var opacity = 100; //最大不透明值
var buttonObj = null;
var showObj=null;//要显示的对象
var tFadeOut=null;
var tFadeIn=null;
var tFadeWaiting = null;

//显示或隐藏对象
function showObject(obj, btnObj, position, toggle) {
    if (obj.style.display == "block") {
        if (toggle)
            fadeIn();
        return;
    }
    clearTimeout(tFadeOut);
    clearTimeout(tFadeIn);
    clearTimeout(tFadeWaiting);

    if (showObj != obj) {
        showObj = obj;
        buttonObj = btnObj;

        showObj.style.filter = "Alpha(Opacity=0)";
        showObj.style.opacity = 0;
        showObj.style.MozOpacity = 0;
    }

    if (btnObj != null && typeof (btnObj) != "undefined") {

        var x = 0, y = 0;
        for (var o = btnObj; o; o = o.offsetParent) {
            x += parseInt(o.offsetLeft);
            y += parseInt(o.offsetTop);
        }


        var winInfo = getWindowInfo();


        var div = document.createElement("DIV");
        div.innerHTML = showObj.innerHTML;
        div.style.cssText = showObj.style.cssText;
        div.className = showObj.className;
        div.setAttribute("class", showObj.getAttribute("class"));
        div.style.display = "block";
        div.style.visibility = "hidden";
        div.style.position = "absolute";
        div.style.top = 0;
        div.style.left = 0;

        document.body.appendChild(div);

        var showObjWidth = div.offsetWidth;
        var showObjHeight = div.offsetHeight;

        document.body.removeChild(div);

        if (position == "bottom") {
            showObj.style.posTop = y + btnObj.offsetHeight;
            showObj.style.posLeft = x;
            
            if (showObj.style.posTop-winInfo.ScrollY + showObjHeight > winInfo.Height) {
                showObj.style.posTop = y - showObjHeight;
            }

            if (showObj.style.posLeft - winInfo.ScrollX + showObjWidth > winInfo.Width) {
                showObj.style.posLeft = x + btnObj.offsetWidth - showObjWidth;
            }
        }
        if (position == "left") {
            showObj.style.posTop = y;
            showObj.style.posLeft = x - showObj.style.posWidth;
        }
        if (position == "right") {
            showObj.style.posTop = y;
            showObj.style.posLeft = x + btnObj.offsetWidth;
        }
    }

    if (showObj.style.display == "none" || showObj.style.display == "") {
        showObj.style.display = "block";
        fadeOut();
    }
}
//淡出
function fadeOut() {
    clearTimeout(tFadeIn);
    
    var opacity1 = (opacity / 100);

    if (showObj.style.opacity < opacity1) {
        if (showObj.filters)
            showObj.filters.Alpha.opacity += showStep;

        var showStep1 = (showStep / 100);
        var opacity2 = Math.round((parseFloat(showObj.style.opacity, 10) + showStep1) * 10) / 10;

        showObj.style.opacity = opacity2;
        showObj.style.MozOpacity = opacity2;
        tFadeOut = setTimeout("fadeOut()", 1);
    }
    else {
        if (showObj.filters)
            showObj.filters.Alpha.opacity = opacity;
        showObj.style.opacity = opacity1;
        showObj.style.MozOpacity = opacity1;
        //如果要自动隐藏则不注释下面这一句
        //tFadeWaiting=setTimeout("fadeIn()",tShow);
    }
}
//淡入
function fadeIn() {
    clearTimeout(tFadeOut);
    
    var opacity1 = (opacity / 100);

    if (showObj.style.opacity > 0) {
        if (showObj.filters)
            showObj.filters.Alpha.opacity -= hiddenStep;

        var hiddenStep1 = (hiddenStep / 100);
        var opacity2 = Math.round((parseFloat(showObj.style.opacity, 10) - hiddenStep1) * 10) / 10;

        showObj.style.opacity = opacity2;
        showObj.style.MozOpacity = opacity2;
        tFadeIn = setTimeout("fadeIn()", 1);
    }
    else {
        showObj.style.display = "none";
    }
}

//隐藏对象
function hideObj(evt) {
    if (buttonObj == null)
        return;

    var pos = getMousePos(evt);

    var top = 0, left = 0;
    for (var obj = buttonObj; obj; obj = obj.offsetParent) {
        left += parseInt(obj.offsetLeft);
        top += parseInt(obj.offsetTop);
    }

    var bottom = top + buttonObj.offsetHeight;
    var right = left + buttonObj.offsetWidth;
    
    var top1 = showObj.style.posTop;
    var left1 = showObj.style.posLeft;
    var bottom1 = showObj.style.posTop + showObj.offsetHeight;
    var right1 = showObj.style.posLeft + showObj.offsetWidth;


    if ((pos.x >= left && pos.x <= right && pos.y >= top && pos.y <= bottom)||(pos.x >= left1 && pos.x <= right1 && pos.y >= top1 && pos.y <= bottom1)) {
        return;
    }

    fadeIn();
}

//if (document.all) {
//    document.attachEvent('onclick', hideObj);
//}
//else {
//    document.addEventListener("click", hideObj, false);
//}

//if (document.all) {
//    document.attachEvent('onmousemove', hideObj);
//}
//else {
//    document.addEventListener("mousemove", hideObj, false);
//}

function getMousePos(e) {

    var agt = navigator.userAgent.toLowerCase();
    var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));

    var body = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;

    var curX = (!ie) ? e.pageX : event.clientX + body.scrollLeft;
    var curY = (!ie) ? e.pageY : event.clientY + body.scrollTop;

    return { x: curX, y: curY };
}

function getWindowInfo() {
    scrollX = 0, scrollY = 0, width = 0, height = 0, contentWidth = 0, contentHeight = 0;
    if (typeof (window.pageXOffset) == 'number')
    { scrollX = window.pageXOffset; scrollY = window.pageYOffset; }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    { scrollX = document.body.scrollLeft; scrollY = document.body.scrollTop; }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    { scrollX = document.documentElement.scrollLeft; scrollY = document.documentElement.scrollTop; }

    if (typeof (window.innerWidth) == 'number')
    { width = window.innerWidth; height = window.innerHeight; }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    { width = document.documentElement.clientWidth; height = document.documentElement.clientHeight; }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    { width = document.body.clientWidth; height = document.body.clientHeight; }

    if (document.documentElement && (document.documentElement.scrollHeight || document.documentElement.offsetHeight)) {
        if (document.documentElement.scrollHeight > document.documentElement.offsetHeight)
        { contentWidth = document.documentElement.scrollWidth; contentHeight = document.documentElement.scrollHeight; }
        else
        { contentWidth = document.documentElement.offsetWidth; contentHeight = document.documentElement.offsetHeight; } 
    }
    else if (document.body && (document.body.scrollHeight || document.body.offsetHeight)) {
        if (document.body.scrollHeight > document.body.offsetHeight)
        { contentWidth = document.body.scrollWidth; contentHeight = document.body.scrollHeight; }
        else
        { contentWidth = document.body.offsetWidth; contentHeight = document.body.offsetHeight; } 
    }
    else
    { contentWidth = width; contentHeight = height; }

    if (height > contentHeight)
        height = contentHeight;

    if (width > contentWidth)
        width = contentWidth;

    var rect = new Object(); rect.ScrollX = scrollX; rect.ScrollY = scrollY; rect.Width = width; rect.Height = height; rect.ContentWidth = contentWidth; rect.ContentHeight = contentHeight; 
    return rect;
}