﻿/*
    title: null,           // titlebar text. titlebar will not be visible if not set.
    closeable: true,           // display close link in titlebar?
    draggable: true,           // can this dialog be dragged?
    clone: false,          // clone content prior to insertion into dialog?
    actuator: null,           // element which opened this dialog
    center: true,           // center dialog in viewport?
    show: true,           // show dialog immediately?
    modal: false,          // make dialog modal?
    fixed: true,           // use fixed positioning, if supported? absolute positioning used otherwise
    closeText: '[关闭]',      // text to use for default close link
    closeDelay: 0,
    width:0,//设置窗口宽度
    height:0,//设置窗口高度
    submiting: false,       //form正在提交处理
    unloadOnHide: false,          // should this dialog be removed from the DOM after being hidden?
    clickToFront: false,          // bring dialog to foreground on any click (not just titlebar)?
    behaviours: Boxy.EF,        // function used to apply behaviours to all content embedded in dialog.
    afterDrop: Boxy.EF,        // callback fired after dialog is dropped. executes in context of Boxy instance.
    afterShow: Boxy.EF,        // callback fired after dialog becomes visible. executes in context of Boxy instance.
    afterHide: Boxy.EF,        // callback fired after dialog is hidden. executed in context of Boxy instance.
    beforeUnload: Boxy.EF,      // callback fired after dialog is unloaded. executed in context of Boxy instance.
    onFormSubmit: Boxy.EF

    DEFAULT_X: 50,
    DEFAULT_Y: 50,
    zIndex: 1337,
    dragConfigured: false, // only set up one drag handler for all boxys
    resizeConfigured: false,
    dragging: null,
*/

$(document).ready(function(){
	$(".sodao-dialog").boxy();
});

var sodaoBox = Boxy;
var loadingBox;
var dialogList = new Array();
var sodaoCheck = {
    _u: function() {
        for (var i = 0; i < arguments.length; i++)
            if (typeof arguments[i] != 'undefined') return false;
        return true;
    },
    _un: function() {
        var _tmp = arguments[0];
        if (_tmp == 'undefined' || _tmp == null)
            return true;
        return false;
    },
    _une: function() {
        var _tmp = arguments[0];
        if (_tmp == 'undefined' || _tmp == null || _tmp == '')
            return true;
        return false;
    },
    _nau: function() {
        var _uc = 0;
        for (var i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] != 'undefined' && arguments[i] != null) { _uc++; }
        }
        return _uc < arguments.length ? true : false;
    },
    _gt0: function() {
        var _tmp = arguments[0];
        if (_tmp == 'undefined' || _tmp == null)
            return false;
        if (isNaN(_tmp)) { return false; }
        return _tmp > 0 ? true : false;
    },
    _gtN: function(_num, _min) {
        if (_num > _min) { return _num; }
        return _min;
    }
}
var sDialog = {
    //activePoint
    activeLevel: function(level, title, callback) {
        if (sodaoCheck._une(level) && sodaoCheck._une(title)) {
            return this.message('调用此函数，请设置text, title参数！', "调用错误提示", 3000);
        }
        var html = '<p>' + title + '</p><div class="tip-d-grade">用户等级 +' + level + '</div>';
        return this.activeBox(html, 1800, '', callback, "tip-d");
    },
    //activePoint
    activePoint: function(point, exp, title, callback) {
        if ((!sodaoCheck._gt0(point) && !sodaoCheck._gt0(exp)) || sodaoCheck._une(title)) {
            return this.message('调用此函数，请设置point, exp, title参数且point, exp至少一个大于0！', "调用错误提示", 3000);
        }
        var html = '<p>' + title + '</p><div class="tip-d-integral">积分 <img src="http://css.sodao.com/dialog/ico-integral.png" width="16" height="16" /> +' + point + '</div>'
                + '<div class="tip-d-exp">经验 <img src="http://css.sodao.com/dialog/ico-experience.png" width="16" height="16" /> +' + exp + '</div>';
        return this.activeBox(html, 1800, '', callback, "tip-d");
    },
    //active message
    activeMsg: function(objId, msg, align, width, timeout, action, callback) {
        var obj = $('#' + objId);
        if (sodaoCheck._un(obj) && sodaoCheck._une(msg)) {
            return this.message('调用此函数，请设置objId, msg参数且objId必须存在！', "调用错误提示", 3000);
        }

        var html = $('#activeBox_' + objId);
        if (!html.attr('id')) {
            var css;
            if (align == 'L') { css = 'tips-box-t'; }
            else if (align == 'T') { css = 'tips-box-b'; }
            else if (align == 'R') { css = 'tips-box-t'; }
            else { css = 'tips-box-l'; }
            var msg = '<span></span><div class="tips-content">' + msg + '</div>';
            html = this._getBox(objId, msg, css);
            if (sodaoCheck._gt0(width)) { html.css({ width: width }); }
            html.appendTo(document.body);
        } else {
            html.find('.tips-content').html(msg);
            return false;
        }
        var xy = SoDao.getLayerXYByObj(objId, html.width(), align == 'W' ? 'R' : align);
        var left = xy[0], top = xy[1];
        if (align == 'W') {
            left = left + html.width() + 5; top = top - html.height() - 3;
            if (html.height() > obj.height()) { top += (html.height() - obj.height()) / 2; }
            else { top -= (html.height() - obj.height()) / 2; }
        } else if (align == 'T') {
            top = top - obj.height() - html.height() - 7;
        } else if (align == 'R') {
            left = left - 10;
        }
        if (sodaoCheck._une(action) || action == "fade") {
            html.css({ left: left, top: top }).stop().fadeIn("slow", function() { sDialog._closeActive(objId, timeout, callback); });
        } else if (action == "slide-L") {
            html.css({ left: (left - 60), top: top }).stop().animate({
                left: left, opacity: 'show'
            }, { duration: 500, complete: function() { sDialog._closeActive(objId, timeout, callback); } });
        }
        else if (action == "slide-R") {
            html.css({ left: (left + 60), top: top }).stop().animate({
                left: left, opacity: 'show'
            }, { duration: 500, complete: function() { sDialog._closeActive(objId, timeout, callback); } });
        }
        return false;
    },
    //close active msg
    activeClose: function(objId) {
        try {
            $('#activeBox_' + objId).stop().fadeOut("slow", function() { $('#activeBox_' + objId).remove(); });
        } catch (e) { }
    },
    //_close active
    _closeActive: function(objId, timeout, callback) {
        if (sodaoCheck._gt0(timeout)) {
            setTimeout(function() { sDialog.activeClose(objId); }, timeout);
        }
        if (callback) { callback(); }
    },
    //get div full content
    _getBox: function(objId, msg, cssName) {
        var html = $('<div></div>').attr("class", cssName).attr("id", "activeBox_" + objId).css({
            display: "none", position: "absolute",
            zIndex: 1949, opacity: 1.0
        }).append(msg);
        return html;
    },
    //activeBox
    activeBox: function(msg, timeout, action, callback, cssName) {
        if (!cssName) { cssName = ''; }
        var html = this._getBox('', msg, cssName);
        var v = sodaoBox._viewport();
        html.appendTo(document.body);
        var left = v.left + (v.width) / 2 - html.width() / 2;
        var top = v.top + (v.height) / 2 - html.height() / 2;
        var tarTop = top - 120;
        if (tarTop < 0) { tarTop = 0; }
        html.css({ left: left, top: top }).stop().animate({
            top: tarTop, opacity: 'show'
        }, { duration: 900, complete: function() { sDialog._closeActive('', timeout, callback); } });
        return false;
    },
    //loading
    loading: function(msg, timeout, width, height, modal) {
        if (sodaoCheck._un(msg)) {
            return this.message('调用此函数，请设置msg参数！', "调用错误提示", 3000);
        }
        if (msg == '') { msg = '数据载入中，请稍候...'; }
        if (sodaoCheck._un(width)) { width = 250; }
        //if (sodaoCheck._un(modal)) { modal = true; }
        var options = this._getOptions(width, height, modal);
        if (sodaoCheck._gt0(timeout)) { options = jQuery.extend(options, { closeDelay: sodaoCheck._gtN(timeout, 10000) }) }
        else { options = jQuery.extend(options, { closeDelay: 10000 }) };
        loadingBox = sodaoBox.loading(msg, options);
        return false;
    },
    //alert
    alert: function(msg, title, callback) {
        if (sodaoCheck._une(msg)) {
            return this.message('调用此函数，请设置msg参数！', "调用错误提示", 3000);
        }
        var options = { modal: true };
        if (!sodaoCheck._un(title)) { options = jQuery.extend(options, { title: title }) };
        sodaoBox.alert(msg, callback, options);
        return false;
    },
    //message
    message: function(msg, title, delay, width, height, X, Y) {
        if (sodaoCheck._une(msg) || sodaoCheck._un(title)) {
            sodaoBox.popTip("调用错误提示", '调用此函数，请设置msg,title参数！', 3000);
            return false;
        }
        this._msgbox(msg, title, '', delay, width, height, X, Y);
        return false;
    },
    //error
    error: function(msg, title, width, height) {
        if (sodaoCheck._une(msg) || sodaoCheck._un(title)) {
            sodaoBox.popTip("调用错误提示", '调用此函数，请设置msg,title参数！', 3000);
            return false;
        }
        this._msgbox(msg, title, 'Error', 0, width, height);
        return false;
    },
    //info
    info: function(msg, title, width, height, delay) {
        if (sodaoCheck._une(msg) || sodaoCheck._un(title)) {
            sodaoBox.popTip("调用错误提示", '调用此函数，请设置msg,title参数！', 3000);
            return false;
        }
        this._msgbox(msg, title, 'Info', delay, width, height);
        return false;
    },
    //msgbox
    _msgbox: function(msg, title, type, delay, width, height, X, Y) {
        if (sodaoCheck._une(msg) || sodaoCheck._un(title)) {
            sodaoBox.popTip("调用错误提示", '调用此函数，请设置msg,title参数！', 3000);
            return false;
        }
        var options = jQuery.extend({ imageType: type }, this._getOptions(width, height));
        if (sodaoCheck._gt0(delay)) { options = jQuery.extend(options, { closeDelay: delay }) };
        if (sodaoCheck._gt0(X)) { options = jQuery.extend(options, { x: X }) };
        if (sodaoCheck._gt0(Y)) { options = jQuery.extend(options, { y: Y }) };
        sodaoBox.popTip(title, msg, 0, options);
        return false;
    },
    //confirm(callback无参数，回调确定后的函数）
    confirm: function(msg, callback, title) {
        if (sodaoCheck._une(msg) || sodaoCheck._un(callback)) {
            return this.message('调用此函数，请设置msg, callback参数！', "调用错误提示", 3000);
        }
        var options = { modal: true };
        if (!sodaoCheck._un(title)) { options = jQuery.extend(options, { title: title }) };
        sodaoBox.confirm(msg, callback, options);
        return false;
    },
    //ask: answers数组，回调callback(返回选择的值)
    ask: function(question, answers, callback) {
        if (sodaoCheck._nau(question, answers, callback)) {
            return this.message('调用此函数，请设置question, answers, callback参数！', "调用错误提示", 3000);
        }
        sodaoBox.ask(question, answers, callback);
        return false;
    },
    //loadId
    loadId: function(id, title, width, height, modal, customerCallback, dependId, align) {
        if (sodaoCheck._une(id) || sodaoCheck._un(title)) {
            return this.message('调用此函数，请设置id, title参数！', "调用错误提示", 3000);
        }
        if (id.substring(0, 1) != '#') { id = '#' + id; }
        var _obj = $(id);
        if (!_obj.attr('id')) {
            return this.message('指定的ID=' + id.replace('#', '') + '的元素未找到，请检查！', "调用错误提示", 3000);
        }
        var options = jQuery.extend({ title: title }, this._getOptions(width, height, modal));
        if (customerCallback) { options = jQuery.extend(options, { customerClosed: customerCallback }); }
        var xy = this._getDialogXY(modal, width, dependId, align);
        if (xy[0] != 0 && xy[1] != 0) {
            options = jQuery.extend(options, { center: false, x: xy[0], y: xy[1] });
        }
        sodaoBox.popLinked(_obj, options);
        return false;
    },
    //load
    load: function(url, title, width, height, modal, showCallBack, customerCallback, dependId, align) {
        if (sodaoCheck._une(url) || sodaoCheck._un(title)) {
            return this.message('调用此函数，请设置url, title参数！', "调用错误提示", 3000);
        }
        var options = jQuery.extend({ title: title }, this._getOptions(width, height, modal));
        if (showCallBack) { options = jQuery.extend(options, { afterShow: showCallBack }); }
        if (customerCallback) { options = jQuery.extend(options, { customerClosed: customerCallback }); }
        var xy = this._getDialogXY(modal, width, dependId, align);
        if (xy[0] != 0 && xy[1] != 0) {
            options = jQuery.extend(options, { center: false, x: xy[0], y: xy[1] });
        }        
        sodaoBox.load(url, options);
        return false;
    },
    //loadForm 回调函数方法（action, formData, callback, source）
    loadForm: function(url, title, callback, width, height, modal, dependId, align) {
        if (sodaoCheck._une(url) || sodaoCheck._nau(title, callback)) {
            return this.message("调用此函数，请设置url, title, callback参数！");
        }
        var options = { title: title, onFormSubmit: callback }; //test submiting submiting: true
        options = jQuery.extend(options, this._getOptions(width, height, modal));
        var xy = this._getDialogXY(modal, width, dependId, align);
        if (xy[0] != 0 && xy[1] != 0) {
            options = jQuery.extend(options, { center: false, x: xy[0], y: xy[1] });
        }
        sodaoBox.load(url, options);
        return false;
    },
    //loadIFrame
    loadIFrame: function(url, title, width, height, modal, dependId, align) {
        if (sodaoCheck._une(url) || sodaoCheck._un(title)) {
            return this.message("调用此函数，请设置url, title参数！");
        }
        var options = jQuery.extend({ title: title }, this._getOptions(width, height, modal));
        var xy = this._getDialogXY(modal, width, dependId, align);
        if (xy[0] != 0 && xy[1] != 0) {
            options = jQuery.extend(options, { center: false, x: xy[0], y: xy[1] });
        }
        var _wid = Math.random();
        if (url.indexOf('?') != -1) { url += '&window=' + _wid; }
        else { url += '?window=' + _wid; }
        var dialog = sodaoBox.loadFrame(url, options);
        dialogList[_wid] = dialog;
        return false;
    },
    change: function(obj, title, content) {
        sodaoBox.get(obj).setTitle(title);
        var html = $("<div></div>").append(content);
        sodaoBox.get(obj).setContent(html);
        return false;
    },
    //针对Iframe窗口自动关闭问题
    closef: function(_wid) {
        if (dialogList[_wid]) {
            dialogList[_wid].hideAndUnload();
        }
        return false;
    }, //关闭
    close: function(obj) {
        var _box = sodaoBox.get(obj);
        try {
            if (_box.options.customerClosed) {
                _box.hide();
                _box.options.customerClosed(obj);
                _box.unload();
                return false;
            }
        } catch (e) { _box.unload(); return false; }

        try {
            _box.hideAndUnload();
        } catch (e) { }
        return false;
    },
    //hideLoading
    hideLoading: function() {
        try {
            loadingBox.hideAndUnload();
        } catch (e) { }
    },
    //return options
    _getOptions: function(width, height, modal) {
        var options = {};
        if (sodaoCheck._gt0(width)) { options = jQuery.extend(options, { width: sodaoCheck._gtN(width, 200) }) };
        if (sodaoCheck._gt0(height)) { options = jQuery.extend(options, { height: sodaoCheck._gtN(height, 50) }) };
        if (!sodaoCheck._un(modal)) { options = jQuery.extend(options, { modal: modal }) };
        return options;
    },
    //获取依赖的位置
    _getDialogXY: function(modal, width, dependId, align) {
        if (modal || !dependId) { return [0, 0]; }
        var obj = $('#' + dependId);
        if (sodaoCheck._un(obj) || !sodaoCheck._gt0(width)) {
            return [0, 0];
        }
        var xy = SoDao.getLayerXYByObj(dependId, width, align == 'R' ? 'R' : 'L');
        return xy;
    }
};
