    /*
     * 中文名:PDbox弹出DIV窗口插件
     * 英文名:PDbox
     *
     * By : pandao QQ:272383090 2009.07.27
     * UpdateTime : 2011-09-07 16:28:11
     * Website : http://www.ipandao.com/
     * 本着共享的精神开放源代码,禁止用于商业用途,否则后果自负!
     *
     * 兼容情况:
     * IE6 大部分兼容，只是引入iframe横向出现滚动条,能遮住select下拉列表
     * IE7,IE8 全兼容
     * Safari 4.0,Chrome 大部分兼容，只是不支持拉动滚动条时自动定位
     * Firefox 3.1,Firefox 3.5  当页面为框架页时透明背景的高度取值有问题
     * Opera 9.6 全兼容
     *
     */

    var PDbg = 'PDbox_hidebg';//透明背景ID名称
    var PDiframe = 'PDboxiframe';//iframe透明背景ID名称

    //$PID公共函数
    function $PID(id)
    {
       return document.getElementById(id);
    }

    //PDbox函数,用法PDbox(id,标题,宽度,高度,内容,类型)
    //类型分为,默认为文字,iframe为引入iframe,id为写入指定id的内容
    function PDbox(box, title, width, height, html, boxtype, bg, hideClose, opacity, mousemove)
    {
       var boxbgcolor = '#fff';//窗口背景色
       var titlecolor = '#E21F2B';//窗口标题栏背景色
       var title_fontcolor = '#fff';//窗口标题栏文字颜色
       var bordercolor = '#E21F2B';//边框颜色
       var pageheight;//页面的高度
       var titleid = box + "_PDbox_title";

       bg = bg || true;
       hideClose = hideClose || false;
       opacity = opacity || 0.5;
       mousemove = mousemove || true;

       if(document.body.clientHeight<window.screen.availHeight) //当网页可见高度小于浏览器可用高度时
       {
           if(document.documentElement.scrollTop > 0) //在框架页里
           {
               //alert('1、有滚动条');
               if(isFirefox=navigator.userAgent.indexOf("Firefox") > 0)
               {
                   //alert('火狐');
                   pageheight = window.screen.availHeight + document.body.clientHeight + document.body.scrollHeight +  document.body.scrollTop + document.body.offsetHeight;
               }
               else
               {
                   //alert('IE');
                   pageheight = document.body.scrollHeight;
               }
           }
           else
           {
               //alert('2、没有滚动条');
               pageheight = document.documentElement.clientHeight; //网页高度为浏览器可用高度
           }
       }
       else
       {
           //alert('3、有滚动条');
           pageheight = document.body.clientHeight + 100; //网页高度为网页可见高度
       }

       if(bg == true)
       {
            //创建背景层（大小与窗口有效区域相同，即当弹出对话框时，背景显示为透明灰色）
           //定义透明背景层的属性，即相当于<div id="hidebg_div" style="position:absolute;top:0;background:#000;filter:filter:alpha(opacity=80);opacity:0.6;left:0; width:100%;z-index:1;"></div>

           var bgObj = document.createElement("div");//创建一个div对象（背景层）
           bgObj.setAttribute('id', PDbg);
           bgObj.style.position = "absolute";
           bgObj.style.top = "0";
           bgObj.style.left = "0";
           bgObj.style.background = "#000";
           bgObj.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
           bgObj.style.opacity = opacity;
           bgObj.style.width = document.body.scrollWidth + "px"; //宽度浏览器工作区域内页面宽度
           bgObj.style.zIndex = "1";
           bgObj.style.display = "block";

           if(isFirefox=navigator.userAgent.indexOf("Firefox") > 0)
           {
               bgObj.style.height = pageheight + document.body.scrollHeight + "px";
           }
           else
           {
               bgObj.style.height = document.body.scrollHeight + "px";
           }

           if(!$PID(PDbg))
           {
               document.body.appendChild(bgObj);//在body内添加该div对象
           }

           if(!window.XMLHttpRequest) //如果浏览器是IE6，创建iframe用于在IE6下隐藏select下拉列表
           {
               var iframeObj = document.createElement("iframe");//创建一个iframe对象
               iframeObj.id = PDiframe; //或者setAttribute('id','PDboxiframe');
               iframeObj.setAttribute("frameborder", "0");
               iframeObj.style.position = "absolute";
               iframeObj.style.top = "0";
               iframeObj.style.left = "0";
               iframeObj.style.width = document.body.offsetWidth + "px"; //宽度浏览器工作区域内页面宽度
               iframeObj.style.height = document.body.scrollHeight + "px";
               iframeObj.style.background = "#000";
               iframeObj.style.filter = "alpha(opacity = 20)";
               document.body.appendChild(iframeObj); //在body内添加该iframe对象
           }
       }

       var browser_width = document.documentElement.clientWidth;    //浏览器可见宽度
       var browser_height = document.documentElement.clientHeight;  //浏览器可见高度
       var scrollTop = document.documentElement.scrollTop;          //滚动条位置
       var scrollLeft = document.documentElement.scrollLeft;        //滚动条位置

       //生成窗口
       if(!$PID(box) && boxtype !== 'id') //如果窗口对象不存在时创建,否则直接显示
       {
           //创建一个窗口div对象
           var boxObj = document.createElement("div");
           boxObj.setAttribute("id", box);
           boxObj.style.display = '';
           boxObj.style.width = width+"px";
           boxObj.style.height = height+"px";
           boxObj.style.border = "10px solid "+bordercolor;
           boxObj.style.borderTop = "none";
           boxObj.style.zIndex = "10000";
           boxObj.style.overflow = "hidden";
           boxObj.style.background = boxbgcolor;
           boxObj.style.textAlign = "left";
           boxObj.style.position = "absolute";
           boxObj.style.top = ((browser_height - height) / 2) - 10 + scrollTop + "px";
           boxObj.style.left = ((browser_width - width) / 2) - 10 + "px";

           //创建窗口标题栏(h1标签)
           var hObj = document.createElement("h1");
           hObj.setAttribute("id", titleid);
           hObj.style.height = "14px";
           hObj.style.background = titlecolor;
           hObj.style.margin = "0 auto";
           hObj.style.padding = "8px";
           hObj.style.fontSize = "13px";
           hObj.style.color = title_fontcolor;

           //是否显示关闭按钮
           if(hideClose == true)
           {
               hObj.innerHTML = "<span style=\"float:right;cursor:pointer;\" onclick=\"PDboxClose('" + box + "');\">关闭</span><strong id=\"view_box_title\">" + title + "</strong>";
           }
           else
           {
               hObj.innerHTML = "<strong id=\"view_box_title\">" + title + "</strong>";
           }

           //创建窗口容器DIV
           var conObj = document.createElement("div");
           conObj.setAttribute("id", box + "_PDboxcon");
           conObj.style.overflow = "auto";
           conObj.style.margin = "0 auto";
           conObj.style.padding = "10px";
           conObj.style.fontSize = "14px";
           conObj.style.width = (width - 20) + 'px';
           conObj.style.height = (height - 50) + 'px';
           conObj.style.lineHeight = '22px';

           switch(boxtype)
           {
               case "confirm":
                   var arr = html.split('-');
                   var btnstyle = 'style="padding:3px;margin:0 3px;"';
                   var Yesbtn = '<input id="YES" type="button" onclick="' + arr[1] + ';PDboxClose(\'' + box + '\'); return true;" value="确定" ' + btnstyle + '/>';
                   var Nobtn = '<input type="button" onclick="PDboxClose(\'' + box + '\');return false;" value="取消" ' + btnstyle + '/>';
                   conObj.innerHTML += arr[0];
                   conObj.innerHTML += "<div style='padding:3px;text-align:center;margin:0 auto;display:block;'>";
                   conObj.innerHTML += Yesbtn + Nobtn;
                   conObj.innerHTML += "</div>";
               break;
               case "iframe":
                  conObj.style.padding = "0";
                  conObj.style.width = width + 'px';
                  conObj.style.height = (height - 30) + 'px';
                  conObj.innerHTML = '<iframe name="PDbox2iframe" frameborder="0" scrolling="auto" height="' + (height - 30) + '" width="' + width + '" src="' + html + '" style="overflow:auto;padding:0;margin:0;"></iframe>';
               break;
               default:
                  conObj.innerHTML = html;
               break;
           }

           document.body.appendChild(boxObj); //在body内添加该窗口DIV对象
           boxObj.appendChild(hObj);          //在BOX里添加h1对象
           boxObj.appendChild(conObj);        //在BOX里添加容器DIV对象
       }
       else if(boxtype=='id') //当类型为ID时，为了解决表单项取不到值的问题
       {
           //alert("类型为ID --->"+titleid);
           $PID(box).style.display = '';
           $PID(box).style.width = width+"px";
           $PID(box).style.height = height+"px";
           $PID(box).style.border = "10px solid " + bordercolor;
           $PID(box).style.zIndex = "10000";
           $PID(box).style.overflow = "hidden";
           $PID(box).style.background = boxbgcolor;
           $PID(box).style.textAlign = "left";
           $PID(box).style.position = "absolute";
           $PID(box).style.top = ((browser_height - height) / 2) - 10 + scrollTop + "px";
           $PID(box).style.left = ((browser_width - width) / 2) - 10 + "px";

           if(!$PID(titleid))
           {
               var htObj = document.createElement("h1");
               htObj.setAttribute("id", titleid);
               htObj.style.width = width - 16 + "px";
               htObj.style.height = "14px";
               htObj.style.background = titlecolor;
               htObj.style.margin = "0 auto";
               htObj.style.padding = "8px";
               htObj.style.fontSize = "13px";
               htObj.style.cursor = "move";
               htObj.style.color = title_fontcolor;
               htObj.style.position = "absolute";
               htObj.style.top = "0";
               htObj.style.left = "0";
               htObj.innerHTML = "<span style=\"float:right;cursor:pointer;\" onclick=\"PDboxClose('" + box + "');\">关闭</span><strong>" + title + "</strong>";

               $PID(box).appendChild(htObj);  //在BOX里添加h1对象
           }

           $PID(box+'_con').style.overflow = "auto";
           $PID(box+'_con').style.margin = "0 auto";
           $PID(box+'_con').style.padding = "10px";
           $PID(box+'_con').style.textAlign = "left";
           $PID(box+'_con').style.width = (width - 20) + 'px';
           $PID(box+'_con').style.height = (height - 50) + 'px';
           $PID(box+'_con').style.position = "absolute";
           $PID(box+'_con').style.bottom = "0";
           $PID(box+'_con').style.left = "0";
       }
       else
       {
           $PID(box).style.display = 'block';
           if(boxtype == 'iframe' && document.frames('PDbox2iframe'))
           {
               if(navigator.userAgent.indexOf("MSIE") > 0 ) //当浏览器是IE内核的时候
               {
                   document.frames('PDbox2iframe').location.reload(); //解决ID浏览器不刷新iframe的问题
               }
           }
       }

       if(boxtype == 'iframe')
       {
           if(navigator.userAgent.indexOf("MSIE") > 0) //当浏览器是IE内核的时候
           {
               document.frames('PDbox2iframe').location.reload(); //解决ID浏览器不刷新iframe的问题
           }
       }

       window.onresize = function(){ //当调整浏览器大小时执行
            if($PID(box))
            {
               $PID(box).style.left = ((document.documentElement.clientWidth-width)/2) - 10 + document.documentElement.scrollLeft + "px";
            }
        };

        window.onscroll = function(){ //当拉动滚动条时执行
            if($PID(box))
            {
               var ptop = ((document.documentElement.clientHeight - height) / 2) - 10 + document.documentElement.scrollTop;
               if(isFirefox = navigator.userAgent.indexOf("Firefox") > 0 &&
                 (document.documentElement.clientHeight-height) < 150 )
               {
                   $PID(box).style.top = document.documentElement.scrollTop + 25 + "px";
               }
               else
               {
                   $PID(box).style.top = ptop + "px";
               }
            }
        };

        if(mousemove == true) //开启标题栏拖动时
        {
            //窗口拖动
            var posX;
            var posY;
            $PID(titleid).style.cursor="move";

            $PID(titleid).onmousedown = function(e){
                if(!e) e = window.event;  //IE

                posX = e.clientX - parseInt($PID(box).style.left);
                posY = e.clientY - parseInt($PID(box).style.top);

                document.onmousemove = function mousemove(ev){
                    if(ev == null) ev = window.event; //IE

                    $PID(box).style.left = (ev.clientX - posX) + "px";
                    $PID(box).style.top = (ev.clientY - posY) + "px";
                }
            }

            $PID(titleid).onmouseup = function(){
                document.onmousemove = null;
            }
        }
        else
        {
            alert('NULL');
        }

    }

    //关闭窗口
    function PDboxClose(box)
    {
       $PID(box).style.display='none';
       //$PID(box).parentNode.removeChild($PID(box));//删除背景层
       $PID(PDbg).parentNode.removeChild($PID(PDbg));//删除背景层

       if(!window.XMLHttpRequest)//如果浏览器是IE6
       {
           $PID(PDiframe).parentNode.removeChild($PID(PDiframe));//删除iframe
       }
    }
