﻿//消息框
//需要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的右下角
*/

$.maxindex = function() {
    var els = $.tagObj("*");
    var max = 0;
    var z;
    els.each(function(el) {
        if (el.style && el.style.zIndex) {
            z = el.css("zIndex");
            if (z != null) {
                if (typeof z == "string")
                    z = z.toInt();
                if (z > max)
                    max = z;
            }
        }
    });
    return max;
}

Array.prototype.clearNull = function() {//清除所有空值
//    var arr = [];
//    this.each(function(index, item) {
//        if (item == null)
//            return;
//        else arr.add(item);
//    });
//    return arr
    return this;
}

//预先下载图片
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();
    },
    ajaxDialog: function(title, url, targetObj, showPos, callback) {

        var dialog = $.msgBox("消息", '<div align=center style="padding-top:15px;">正在处理</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);

        if (tipDialog != null) {
            tipDialog.close();
            tipDialog.css("visibility", "hidden");
            tipDialog.mask.css("visibility", "hidden");
        }
        this.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 = document.createElement('div');
        dialog.id = 'dialog_' + uniqueID;
        dialog.className = "dialog";
        dialog.style.zIndex = maxzIndex + 10;
        dialog = $(dialog);

        //创建dialogheader
        dialogheader = document.createElement('div');
        dialogheader.id = 'dialog-header_' + uniqueID;
        dialogheader = $(dialogheader);

        //创建dialogtitle
        dialogtitle = document.createElement('div');
        dialogtitle.id = 'dialog-title_' + uniqueID;
        dialogtitle.className = "dialog-title";
        dialogtitle = $(dialogtitle);

        //创建dialogclose
        dialogclose = document.createElement('div');
        dialogclose.id = 'dialog-close_' + uniqueID;
        dialogclose.className = "dialog-close";
        dialogclose = $(dialogclose);

        dialogcontentcontiner = document.createElement('div');
        dialogcontentcontiner.id = 'dialog-content-continer_' + uniqueID;
        dialogcontentcontiner.className = "dialog-content-continer";
        dialogcontentcontiner = $(dialogcontentcontiner);

        //创建dialogcontent
        dialogcontent = document.createElement('div');
        dialogcontent.id = 'dialog-content_' + uniqueID;
        dialogcontent.className = "dialog-content";
        dialogcontent = $(dialogcontent);

        //创建dialogmask
        dialogmask = document.createElement('div');
        dialogmask.id = 'dialog-mask_' + uniqueID;
        dialogmask.className = "dialog-mask";
        dialogmask = $(dialogmask);

        var _screen = $.avail();
        with (dialogmask[0].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 = dialogmask.appendTo(document.body);
        dialogmask.html('<iframe id="dialogBgFrame_{0}" allowtranseparency=true style="z-index:-1;width:100%;filter:alpha(opacity=0);-moz-opacity:0"></iframe>'.format(uniqueID));
        dialogmask.bgFrameObj = $("#dialogBgFrame_{0}".format(uniqueID));
        dialogmask.bgFrameObj.css("width", dialogmask[0].offsetWidth + "px");
        dialogmask.bgFrameObj.css("height", dialogmask[0].offsetHeight + "px");

        //向页面追加dialog
        dialog.appendTo(document.body);

        //向dialog追加dialogheader
        dialogheader.appendTo(dialog);

        //向dialogheader追加dialogtitle
        dialogtitle.appendTo(dialogheader);

        //向dialogheader追加dialogclose
        dialogclose.appendTo(dialogheader);

        //向dialog追加dialogcontentcontiner
        dialogcontentcontiner.appendTo(dialog);

        //向dialogcontentcontiner追加dialogcontent
        dialogcontent.appendTo(dialogcontentcontiner);

        dialogclose[0].title = "关闭";

        //关闭事件
        dialog.close = dialogclose[0].onclick = function() {
            var dialogObj = dialog;
            dialogObj.scrollObj.attr("status", true);
            var status;
            var oldHeight = parseInt(dialogObj.scrollObj[0].offsetHeight);
            var initHeight = oldHeight;
            var showHeight = 0;
            var alpha;
            dialogObj.scrollObj.attr("oldHeight", oldHeight);

            if ($.expandTimer != null)
                clearInterval($.expandTimer);

            var scrollObjTimer = setInterval(function() {
                status = dialogObj.scrollObj.attr("status") != null && dialogObj.scrollObj.attr("status");
                if (status) {
                    if (oldHeight > (initHeight * 0.05)) //5%的高度
                    {
                        oldHeight = oldHeight * 0.7;
                        dialogObj.scrollObj.css("height", oldHeight + "px");
                    }
                    else {
                        dialogObj.scrollObj.css("height", showHeight + "px");
                        dialogObj.scrollObj.attr("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)
                            dialogObj.remove();
                        if (dialogmask != null)
                            dialogmask.remove();
                    }
                }
                else {
                    complete = true;
                    dialogObj.scrollObj.css("height", showHeight + "px");
                    dialogObj.scrollObj[0].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)
                        dialogObj.remove();
                    if (dialogmask != null)
                        dialogmask.remove();
                }
            }, 10);
        }
        //alert()
        //标题样式
        dialogheader[0].className = (type + "header");
        //消息体样式
        dialogcontent[0].className = (type);
        dialog[0].alpha = 0;
        with (dialog[0].style) {
            position = "absolute";
            top = "-50px";
            left = "0px";
            visibility = "visible";
        }

        with (dialogcontentcontiner[0].style) {
            position = "relative";
            height = "1px";
            overflow = "hidden";
        }
        with (dialogcontent[0].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.css("width", showWidth + "px");
        dialogObj.headerObj.css("width", (showWidth - 2) + "px");

        dialogObj.attr("showWidth", showWidth);
        dialogObj.attr("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 = $(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.css("left", l);
        dialogObj.css("top", t);

        dialogObj.attr("tp", t);
        dialogObj.attr("lf", l);

        var s = speed || 1.2;
        var status = dialogObj.scrollObj.attr("status") == null;
        var oldHeight = parseInt(dialogObj.scrollObj[0].offsetHeight);

        dialogObj.scrollObj.css("height", oldHeight + "px");
        dialogObj.scrollObj.css("display", "block");
        dialogObj.scrollObj.css("overflowX", "hidden");
        dialogObj.scrollObj.css("overflowY", "hidden");

        if (dialogObj.scrollObj.attr("oldHeight") == null)
            dialogObj.scrollObj.attr("oldHeight", oldHeight);

        if (doScroll.scrollX)
            dialogObj.scrollObj.css("overflowX", "scroll");
        if (doScroll.scrollY)
            dialogObj.scrollObj.css("overflowY", "scroll");

        $.expandTimer = setInterval(function() {
            if (status) {
                if (oldHeight < showHeight) {
                    oldHeight = Math.ceil(showHeight - (showHeight - oldHeight) / s);
                    dialogObj.scrollObj.css("height", oldHeight + "px");
                }
                else {
                    dialogObj.scrollObj.css("height", showHeight + "px");
                    dialogObj.scrollObj.attr("status", false);
                    clearInterval($.expandTimer);
                }
            }
            else {
                dialogObj.scrollObj.css("height", showHeight + "px");
                dialogObj.scrollObj[0].removeAttribute("status");
                clearInterval($.expandTimer);
            }
        }, 10);
        return status;
    },
    scrollWin: function(_center, dialogObj, showWidth, showHeight) {
        var l, ofl, t, oft;
        oft = dialogObj.attr("tp");
        ofl = dialogObj.attr("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.css("left", l + "px");
        dialogObj.css("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 = document.createElement('div');
            dialog.id = 'dialog';
            dialog.style.zIndex = maxzIndex + 10;

            //创建dialogheader
            dialogheader = document.createElement('div');
            dialogheader.id = 'dialog-header';
            //创建dialogtitle
            dialogtitle = document.createElement('div');
            dialogtitle.id = 'dialog-title';
            //创建dialogclose
            dialogclose = document.createElement('div');
            dialogclose.id = 'dialog-close'
            dialogclose.setAttribute('onclick', '$$.hideDialog()');
            dialogclose.onclick = $.hideDialog;
            //创建dialogcontent
            dialogcontent = document.createElement('div');
            dialogcontent.id = 'dialog-content';
            //创建dialogmask
            dialogmask = document.createElement('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 = $(dialogmask);
            dialog = $(dialog);
            dialogheader = $(dialogheader);
            dialogtitle = $(dialogtitle);
            dialogclose = $(dialogclose);
            dialogcontent = $(dialogcontent);

        }
        else {
            //获取dialog
            dialog = $('#dialog');
            dialogheader = $('#dialog-header');
            dialogtitle = $('#dialog-title');
            dialogclose = $('#dialog-close');
            dialogcontent = $('#dialog-content');
            dialogmask = $('#dialog-mask');
            //隐藏dialogmask
            dialogmask.css("visibility", "visible");
            //隐藏dialog
            dialog.css("visibility", "visible");
        }

        var p = this.dialogPosition();

        //对话框宽
        var dialogwidth = dialog[0].offsetWidth;
        //对话框高
        var dialogheight = dialog[0].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.className = (type + "header");
        //标题内容
        dialogtitle.html(title);
        //消息体样式
        dialogcontent.className = (type);
        //消息体内容
        dialogcontent.html(message);

        var content = $("#" + this.WRAPPER);

        if (content == null || content.length == 0) {
            content = document.createElement("div");
            content.id = this.WRAPPER;
            document.body.appendChild(content);
            content = $(content);
        }

        dialogmask.css("height", content[0].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");
        $('#dialog-mask').css("visibility", "visible");

        return dialog;
    },
    hideDialog: function() {
        var dialog = $("#dialog");
        dialog.css("visibility", "hidden");
        $('#dialog-mask').css("visibility", "hidden");

        return;
    },
    fadeDialog: function(flag) {
        var SPEED = 50;
        if (flag == null)
            flag = 1;

        var dialog = $("#dialog");
        var value;
        if (flag == 1)
            value = dialog[0].alpha + SPEED;
        else
            value = dialog[0].alpha - SPEED;

        dialog[0].alpha = value;
        dialog[0].style.opacity = (value / 100);
        dialog[0].style.filter = 'alpha(opacity=' + value + ')';

        if (value >= 99) {
            clearInterval(dialog.timer);
            dialog.timer = null;
        }
        else if (value <= 1) {
            dialog.css("visibility", "hidden");
            $('#dialog-mask').css("visibility", "hidden");
            clearInterval(dialog.timer);
        }
    }
}
onresize = function() {
    var group = $.array(dialogFn.dialogGroup);
    group.each(function(index, 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.attr("showWidth");
            showHeight = d.attr("showHeight");
            dialogFn.scrollWin(_center, d, showWidth, showHeight)
        }
    });
}

jQuery.extend(dialogFn);
