﻿//消息框
//需要fn.js,drag.js,dialog_box.css
//黄起程
//2008-10-15
//在targetObj参数不为空时，可以指定以下显示位置
/*
    left:置左,顶对齐，对话框的右边靠着targetObj的左边
    right:置右,顶对齐，对话框的左边靠着targetObj的右边
    bottom:置底,左对齐，对话框的顶边靠着targetObj的底边
    top::置顶,左对齐，对话框的底边靠着targetObj的顶边
    left-top:置左上角，对话框的右下角靠着targetObj的左上角
    left-bottom:置左下角，对话框的右上角靠着targetObj的左下角
    right-top:置右上角，对话框的左下角靠着targetObj的右上角
    right-bottom:置右下角，对话框的左上角靠着targetObj的右下角
*/

//预先下载图片
var img01 = new Image();
var img02 = new Image();
var img03 = new Image();
var img04 = new Image();
var img05 = new Image();

img01.src = "http://www.byecity.com/fn/images/progress.gif";
img02.src = "http://www.byecity.com/fn/images/dialog_close.gif";
img03.src = "http://www.byecity.com/fn/images/prompt_bg.gif";
img04.src = "http://www.byecity.com/fn/images/prompt_header.gif";
img05.src = "http://www.byecity.com/fn/images/custom_header.gif";

var dialogFn = {
    WRAPPER: 'msgBoxContent',
    dialogGroup: [],
    progressSrc: null,
    expandTimer: null,
    tidyDialogGroup: function()
    {
        this.dialogGroup = $$.array(this.dialogGroup).clearNull();
    },
    htmlDialog: function(title, url, targetObj, showPos, callback)
    {
        if (this.progressSrc == null)
        {
            $$.progressSrc = "http://www.byecity.com/fn/images/loading.gif?t=" + Math.random();
            var dialog = $$.msgBox("消息", '<div align=center style="padding-top:15px;">正在处理</div>', 'prompt');
            var img = new Image();
            var doFlag = false;
            img.onerror = img.onload = function()
            {
                if (!doFlag)
                {
                    doFlag = true;
                    dialog.contentObj.innerHTML = '<div align=center style="padding-top:15px;"><table border=0 width=100%><tr><td aligh=right><img src="' + $$.progressSrc + '"></td><td align=left>正在处理</td></tr></table></div>';
                    $$.req(url, function(rsp)
                    {
                        var dialogObj = $$.textDialog(title, rsp, dialog, targetObj, showPos);
                        if (callback != null)
                            callback(dialogObj, rsp);
                    });
                }
            }
            img.src = $$.progressSrc;
        }
        else
        {
            var dialog = $$.msgBox("消息", '<div align=center style="padding-top:15px;"><table border=0 width=100%><tr><td aligh=right><img src="' + $$.progressSrc + '"></td><td align=left>正在处理</td></tr></table></div>', 'prompt');
            $$.req(url, function(rsp)
            {
                var dialogObj = $$.textDialog(title, rsp, dialog, targetObj, showPos);
                if (callback != null)
                    callback(dialogObj, rsp);
            });
        }
    },
    frameDialog: function(title, url, w, h, targetObj, showPos)
    {
        var dialog = $$.msgBox("消息", '<div align=center style="padding-top:15px;">正在处理"></div>', 'prompt');
        if (w == null)
            w = "800px";
        if (h == null)
            h = "600px";
        $$.textDialog(title, '<iframe frameborder="1" style="border:0px red solid;" scrolling="auto" width="{0}" height="{1}" src="{2}"></iframe>'.format(w, h, url), dialog, targetObj, showPos);
    },
    imageDialog: function(title, imgSrc, speed, targetObj, showPos)
    {
        var dialog = this.msgBox("提示", '<div align=center style="padding-top:15px;">正在加载图片,请稍候片刻</div>', 'prompt');
        var img = new Image();
        var doFlag = false;
        img.onload = function()
        {
            $$.textDialog(title, "<img src='{0}' width='{1}px' height='{2}px'>".format(imgSrc, img.width, img.height), dialog, targetObj, showPos);
        }
        img.onerror = function()
        {
            $$.textDialog(title, "图片加载错误", dialog, targetObj, showPos);
        }
        img.src = imgSrc;
    },
    textDialog: function(title, message, tipDialog, targetObj, showPos)
    {
        var dialog = this.createDialog(title, message);
        var imgs = dialog.contentObj.images;
        if (imgs.length == 0)
        {
            if (tipDialog != null)
            {
                tipDialog.close();
                tipDialog.style.visibility = "hidden";
                tipDialog.mask.style.visibility = "hidden";
            }
            this.expandDialog(dialog, 1.9, targetObj, showPos);
        }
        else
        {
            $$.preload(imgs, function()
            {
                if (tipDialog != null)
                {
                    tipDialog.close();
                    tipDialog.style.visibility = "hidden";
                    tipDialog.mask.style.visibility = "hidden";
                }
                $$.expandDialog(dialog, 1.9, targetObj, showPos);
            });
        }
        return dialog;
    },
    createDialog: function(title, message)
    {
        this.tidyDialogGroup();

        var type = "custom";
        var uniqueID = this.dialogGroup.length;
        var dialog;
        var dialogheader;
        var dialogclose;
        var dialogtitle;
        var dialogcontentcontiner;
        var dialogcontent;
        var dialogmask;
        var maxzIndex = $$.maxindex();

        //创建dialog
        dialog = $$.el('div');
        dialog.id = 'dialog_' + uniqueID;
        dialog.className = "dialog";
        dialog.style.zIndex = maxzIndex + 2;

        //创建dialogheader
        dialogheader = $$.el('div');
        dialogheader.id = 'dialog-header_' + uniqueID;

        //创建dialogtitle
        dialogtitle = $$.el('div');
        dialogtitle.id = 'dialog-title_' + uniqueID;
        dialogtitle.className = "dialog-title";

        //创建dialogclose
        dialogclose = $$.el('div');
        dialogclose.id = 'dialog-close_' + uniqueID;
        dialogclose.className = "dialog-close";

        dialogcontentcontiner = $$.el('div');
        dialogcontentcontiner.id = 'dialog-content-continer_' + uniqueID;
        dialogcontentcontiner.className = "dialog-content-continer";

        //创建dialogcontent
        dialogcontent = $$.el('div');
        dialogcontent.id = 'dialog-content_' + uniqueID;
        dialogcontent.className = "dialog-content";

        //创建dialogmask
        dialogmask = $$.el('div');

        dialogmask.id = 'dialog-mask_' + uniqueID;
        dialogmask.className = "dialog-mask";
        var _screen = $$.avail();
        with (dialogmask.style)
        {
            zIndex = maxzIndex + 1;
            width = parseInt(document.body.scrollWidth) > _screen.width ? document.body.scrollWidth : _screen.width;
            height = parseInt(document.body.scrollHeight) > _screen.height ? document.body.scrollHeight : _screen.height;
        }
        //向页面追加dialogmask
        dialogmask = $$.addel(dialogmask);
        dialogmask.innerHTML = '<iframe id="dialogBgFrame_{0}" allowtranseparency=true style="z-index:-1;width:100%;filter:alpha(opacity=0);-moz-opacity:0"></iframe>'.format(uniqueID);
        dialogmask.bgFrameObj = $$.idObj("dialogBgFrame_{0}".format(uniqueID));
        dialogmask.bgFrameObj.css("width", dialogmask.offsetWidth + "px");
        dialogmask.bgFrameObj.css("height", dialogmask.offsetHeight + "px");

        //向页面追加dialog
        dialog = $$.addel(dialog);

        //向dialog追加dialogheader
        dialogheader = $$.addel(dialogheader, dialog);

        //向dialogheader追加dialogtitle
        dialogtitle = $$.addel(dialogtitle, dialogheader);

        //向dialogheader追加dialogclose
        dialogclose = $$.addel(dialogclose, dialogheader);

        //向dialog追加dialogcontentcontiner
        dialogcontentcontiner = $$.addel(dialogcontentcontiner, dialog);

        //向dialogcontentcontiner追加dialogcontent
        dialogcontent = $$.addel(dialogcontent, dialogcontentcontiner);

        dialogclose.title = "关闭";
        //关闭事件
        dialog.close = dialogclose.onclick = function()
        {
            var dialogObj = $$.idObj(dialog);
            dialogObj.scrollObj.att("status", true);
            var status;
            var oldHeight = parseInt(dialogObj.scrollObj.offsetHeight);
            var initHeight = oldHeight;
            var showHeight = 0;
            var alpha;
            dialogObj.scrollObj.att("oldHeight", oldHeight);

            if ($$.expandTimer != null)
                clearInterval($$.expandTimer);

            var scrollObjTimer = setInterval(function()
            {
                status = dialogObj.scrollObj.att("status") != null && dialogObj.scrollObj.att("status");
                if (status)
                {
                    if (oldHeight > (initHeight * 0.05)) //5%的高度
                    {
                        oldHeight = oldHeight * 0.7;
                        dialogObj.scrollObj.style.height = oldHeight + "px";
                    }
                    else
                    {
                        dialogObj.scrollObj.style.height = showHeight + "px";
                        dialogObj.scrollObj.setAttribute("status", false);
                        clearInterval(scrollObjTimer);
                        dialogObj.scrollObj.css("overflowY", "hidden");
                        for (var i = 0; i < $$.dialogGroup.length; i++)
                        {
                            if ($$.dialogGroup[i] != null && dialogObj.uid == $$.dialogGroup[i].uid)
                                $$.dialogGroup[i] = null;
                        }
                        $$.tidyDialogGroup();
                        if (dialogObj != null)
                            document.body.removeChild(dialogObj);
                        if (dialogmask != null)
                            document.body.removeChild(dialogmask);
                    }
                }
                else
                {
                    complete = true;
                    dialogObj.scrollObj.css("height", showHeight + "px");
                    dialogObj.scrollObj.removeAttribute("status");
                    clearInterval(scrollObjTimer);
                    for (var i = 0; i < $$.dialogGroup.length; i++)
                    {
                        if ($$.dialogGroup[i] != null && dialogObj.uid == $$.dialogGroup[i].uid)
                            $$.dialogGroup[i] = null;
                    }
                    $$.tidyDialogGroup();
                    if (dialogObj != null)
                        document.body.removeChild(dialogObj);
                    if (dialogmask != null)
                        document.body.removeChild(dialogmask);
                }
            }, 10);
        }
        //alert()
        //标题样式
        dialogheader.cls(type + "header");
        //消息体样式
        dialogcontent.cls(type);
        dialog.alpha = 0;
        with (dialog.style)
        {
            position = "absolute";
            top = "-50px";
            left = "0px";
            visibility = "visible";
        }

        with (dialogcontentcontiner.style)
        {
            position = "relative";
            height = "1px";
            overflow = "hidden";
        }
        with (dialogcontent.style)
        {
            width = $$.ie6 ? "0px" : "auto";
        }

        //标题内容
        //dialogtitle.html( "{0} - [窗口{1}]".format(title,(parseInt(uniqueID) + 1) ) );
        dialogtitle.html("{0}".format(title));
        //消息体内容
        dialogcontent.html(message);

        dialog.headerObj = dialogheader
        dialog.titleObj = dialogtitle;
        dialog.scrollObj = dialogcontentcontiner;
        dialog.contentObj = dialogcontent;
        dialog.contentObj.images = $$.tagObj("img", dialogcontent);
        dialog.coverObj = dialogmask;
        dialog.uid = uniqueID;

        dialogclose.css("visibility", "visible");
        this.dialogGroup[uniqueID] = dialog;
        dragFn.init(dialog.headerObj, dialog); //handle和dragBody都是一样的 这是就相当于是拖动handle本身
        return dialog;
    },
    expandDialog: function(dialogObj, speed, targetObj, showPos)
    {
        var wh = dialogObj.contentObj.wh();
        var targetHeight = wh.height;
        var targetWidth = wh.width;
        var showWidth, showHeight;
        showWidth = targetWidth;
        showHeight = targetHeight;
        var _screen = $$.avail(); //窗口宽高
        var doScroll = {}; //是否需要滚动条
        doScroll.scrollX = false;
        doScroll.scrollY = false;

        if (showWidth > _screen.width)
        {
            showWidth = _screen.width * 9 / 10;
            doScroll.scrollX = true;
        }
        if (showHeight > _screen.height)
        {
            showHeight = _screen.height * 9 / 10;
            doScroll.scrollY = true;
        }

        dialogObj.scrollObj.style.width = showWidth + "px";
        dialogObj.headerObj.style.width = (showWidth - 2) + "px";

        dialogObj.setAttribute("showWidth", showWidth);
        dialogObj.setAttribute("showHeight", showHeight);

        if (targetObj == null)
        {
            var winObj = document.documentElement.scrollTop > document.body.scrollTop ? document.documentElement : document.body;
            var _center = $$.center(); //中心点
            var l = (_center.left - showWidth / 2) + "px";
            var t = (_center.top - showHeight / 2) - (_center.top - showHeight / 2) * 2 / 5 + parseInt(winObj.scrollTop) + "px";
        }
        else
        {
            targetObj = $$.idObj(targetObj);
            var l, t;
            var _p = targetObj.offsetPos();
            if (showPos != null)
            {
                switch (showPos)
                {
                    case "left": //置左,顶对齐，对话框的右边靠着targetObj的左边
                        l = (_p.left - showWidth - 2) + "px";
                        t = _p.top + "px";
                        break;
                    case "right": //置右,顶对齐，对话框的左边靠着targetObj的右边
                        l = (targetObj.right() + 2) + "px";
                        t = _p.top + "px";
                        break;
                    case "bottom": //置底,左对齐，对话框的顶边靠着targetObj的底边
                        l = _p.left + "px";
                        t = (targetObj.bottom() + 2) + "px";
                        break;
                    case "top": //置顶,左对齐，对话框的底边靠着targetObj的顶边
                        l = _p.left + "px";
                        t = (_p.top - showHeight - 27) + "px";
                        break;
                    case "left-top": //置左上角，对话框的右下角靠着targetObj的左上角
                        l = (_p.left - showWidth - 2) + "px";
                        t = (_p.top - showHeight - 27) + "px";
                        break;
                    case "left-bottom": //置左下角，对话框的右上角靠着targetObj的左下角
                        l = (_p.left - showWidth - 2) + "px";
                        t = (targetObj.bottom() + 2) + "px";
                        break;
                    case "right-top": //置右上角，对话框的左下角靠着targetObj的右上角
                        l = (targetObj.right() + 2) + "px";
                        t = (_p.top - showHeight - 27) + "px";
                        break;
                    case "right-bottom": //置右下角，对话框的左上角靠着targetObj的右下角
                        l = (targetObj.right() + 2) + "px";
                        t = (targetObj.bottom() + 2) + "px";
                        break;
                    default: //置底,左对齐，对话框的顶边靠着targetObj的底边
                        l = _p.left + "px";
                        t = targetObj.bottom() + "px";
                        break;
                }
            }
            else
            {
                l = _p.left + "px";
                t = (targetObj.bottom() + 2) + "px";
            }
        }
        dialogObj.style.left = l;
        dialogObj.style.top = t;

        dialogObj.setAttribute("tp", t);
        dialogObj.setAttribute("lf", l);
        var s = speed || 1.2;
        var status = dialogObj.scrollObj.getAttribute("status") == null;
        var oldHeight = parseInt(dialogObj.scrollObj.offsetHeight);

        dialogObj.scrollObj.style.height = oldHeight + "px";
        dialogObj.scrollObj.style.display = "block";
        dialogObj.scrollObj.style.overflowX = "hidden";
        dialogObj.scrollObj.style.overflowY = "hidden";

        if (dialogObj.scrollObj.getAttribute("oldHeight") == null)
            dialogObj.scrollObj.setAttribute("oldHeight", oldHeight);

        if (doScroll.scrollX)
            dialogObj.scrollObj.style.overflowX = "scroll";
        if (doScroll.scrollY)
            dialogObj.scrollObj.style.overflowY = "scroll";

        $$.expandTimer = setInterval(function()
        {
            if (status)
            {
                if (oldHeight < showHeight)
                {
                    oldHeight = Math.ceil(showHeight - (showHeight - oldHeight) / s);
                    dialogObj.scrollObj.style.height = oldHeight + "px";
                }
                else
                {
                    dialogObj.scrollObj.style.height = showHeight + "px";
                    dialogObj.scrollObj.setAttribute("status", false);
                    clearInterval($$.expandTimer);
                }
            }
            else
            {
                dialogObj.scrollObj.style.height = showHeight + "px";
                dialogObj.scrollObj.removeAttribute("status");
                clearInterval($$.expandTimer);
            }
        }, 10);
        return status;
    },
    scrollWin: function(_center, dialogObj, showWidth, showHeight)
    {
        var l, ofl, t, oft;
        oft = dialogObj.getAttribute("tp");
        ofl = dialogObj.getAttribute("lf");
        var winObj = document.documentElement.scrollTop > document.body.scrollTop ? document.documentElement : document.body;
        if (oft == null || oft == "" || ofl == null || ofl == "")
        {
            l = (_center.left - showWidth / 2) + parseInt(winObj.scrollLeft);
            t = (_center.top - showHeight / 2) - (_center.top - showHeight / 2) * 2 / 5 + parseInt(winObj.scrollTop);
        }
        else
        {
            l = parseInt(ofl) + parseInt(winObj.scrollLeft);
            t = parseInt(oft) + parseInt(winObj.scrollTop);
        }
        dialogObj.style.left = l + "px";
        dialogObj.style.top = t + "px";
    },
    dialogPosition: function()
    {
        var position = {};

        position.width = window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
        position.height = window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
        position.top = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
        position.left = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;

        return position;
    },
    msgBox: function(title, message)
    {
        var type = "prompt";
        var autohide = false;
        if (!type)
            type = 'error';

        var dialog;
        var dialogheader;
        var dialogclose;
        var dialogtitle;
        var dialogcontent;
        var dialogmask;
        var maxzIndex = $$.maxindex();

        if (!document.getElementById('dialog'))
        {
            //创建dialog
            dialog = $$.el('div');
            dialog.id = 'dialog';
            dialog.style.zIndex = maxzIndex + 2;

            //创建dialogheader
            dialogheader = $$.el('div');
            dialogheader.id = 'dialog-header';
            //创建dialogtitle
            dialogtitle = $$.el('div');
            dialogtitle.id = 'dialog-title';
            //创建dialogclose
            dialogclose = $$.el('div');
            dialogclose.id = 'dialog-close'
            dialogclose.setAttribute('onclick', '$$.hideDialog()');
            dialogclose.onclick = $$.hideDialog;
            //创建dialogcontent
            dialogcontent = $$.el('div');
            dialogcontent.id = 'dialog-content';
            //创建dialogmask
            dialogmask = $$.el('div');
            dialogmask.id = 'dialog-mask';
            dialogmask.style.zIndex = maxzIndex + 2;

            //向页面追加dialogmask
            document.body.appendChild(dialogmask);
            //向页面追加dialog
            document.body.appendChild(dialog);
            //向dialog追加dialogheader
            dialog.appendChild(dialogheader);
            //向dialogheader追加dialogtitle
            dialogheader.appendChild(dialogtitle);
            //向dialogheader追加dialogclose
            dialogheader.appendChild(dialogclose);
            //向dialog追加dialogcontent
            dialog.appendChild(dialogcontent);

            dialogmask = $$.idObj(dialogmask);
            dialog = $$.idObj(dialog);
            dialogheader = $$.idObj(dialogheader);
            dialogtitle = $$.idObj(dialogtitle);
            dialogclose = $$.idObj(dialogclose);
            dialogcontent = $$.idObj(dialogcontent);

        }
        else
        {
            //获取dialog
            dialog = $$.idObj('dialog');
            dialogheader = $$.idObj('dialog-header');
            dialogtitle = $$.idObj('dialog-title');
            dialogclose = $$.idObj('dialog-close');
            dialogcontent = $$.idObj('dialog-content');
            dialogmask = $$.idObj('dialog-mask');
            //隐藏dialogmask
            dialogmask.css("visibility", "visible");
            //隐藏dialog
            dialog.css("visibility", "visible");
        }

        //完全透明
        //dialog.style.opacity = .00;
        //dialog.style.filter = 'alpha(opacity=0)';
        //dialog.alpha = 0;

        var p = this.dialogPosition();

        //对话框宽
        var dialogwidth = dialog.offsetWidth;
        //对话框高
        var dialogheight = dialog.offsetHeight;
        //对话框最终位置顶
        var topposition = p.top + (p.height / 3) - (dialogheight / 2);
        //对话框最终位置左
        var leftposition = p.left + (p.width / 2) - (dialogwidth / 2);

        dialog.css("top", topposition + "px");
        dialog.css("left", leftposition + "px");
        //标题样式
        dialogheader.cls(type + "header");
        //标题内容
        dialogtitle.html(title);
        //消息体样式
        dialogcontent.cls(type);
        //消息体内容
        dialogcontent.html(message);

        var content = $$.idObj(this.WRAPPER);

        if (content == null)
        {
            content = $$.el("div");
            content.id = this.WRAPPER;
            document.body.appendChild(content);
        }
        dialogmask.css("height", content.offsetHeight + 'px');
        //dialog.timer = $$.tick( function(){ $$.fadeDialog(1);},0.005);        

        dialogclose.css("visibility", "visible");

        dialog.close = this.hideDialog;
        dialog.contentObj = dialogcontent;
        dialog.mask = dialogmask;

        dialog.css("visibility", "visible");
        $$.idObj('dialog-mask').css("visibility", "visible");

        return dialog;
    },
    hideDialog: function()
    {
        var dialog = $$.idObj("dialog");
        dialog.css("visibility", "hidden");
        $$.idObj('dialog-mask').css("visibility", "hidden");

        return;
        if (!dialog.timer)
            dialog.timer = $$.tick(function() { $$.fadeDialog(0); }, 0.005);
    },
    fadeDialog: function(flag)
    {
        var SPEED = 50;
        if (flag == null)
            flag = 1;

        var dialog = $$.idObj("dialog");
        var value;
        if (flag == 1)
            value = dialog.alpha + SPEED;
        else
            value = dialog.alpha - SPEED;

        dialog.alpha = value;
        dialog.style.opacity = (value / 100);
        dialog.style.filter = 'alpha(opacity=' + value + ')';

        if (value >= 99)
        {
            $$.tickend(dialog.timer);
            dialog.timer = null;
        }
        else if (value <= 1)
        {
            dialog.css("visibility", "hidden");
            $$.idObj('dialog-mask').css("visibility", "hidden");
            $$.tickend(dialog.timer);
        }
    }
}
onresize = function(){
    var group = $$.array( dialogFn.dialogGroup );
    group.each( function(d){
        if ( d != null )
            dialogFn.expandDialog( d,1.8);
    });
}
onscroll = function(){
    var _center = $$.center();//中心点
    var showWidth,showHeight;
    
    var group = $$.array( dialogFn.dialogGroup );
    group.each( function(d){
        if ( d != null )
        {
            showWidth  = d.getAttribute("showWidth");
            showHeight = d.getAttribute("showHeight");            
            dialogFn.scrollWin(_center,d,showWidth,showHeight)
        }
    });
}

Object.extendFn( $$,dialogFn );
