var popup = false,
    _popup = {},
    Notify = false,
    hashC = false,
    _IE8 = false,
    _oldIE = ($.browser.msie && parseInt($.browser.version, 10) <= 9) ? true : false,
    _isDevice = /iphone|ipad|android/gi.test(navigator.appVersion),
    _isChange = false,
    _isError = {},
    _Change = false,
    _sslTest = /^https:\/\//i,
    _SSL = _sslTest.test(location.href);
if (!$.support.transition) $.fn.transition = $.fn.animate;

function PopUp(a) {
    var _ths = this,
        _opt,
        _overlay, _popup, _core, _def = {
            margin: 0,
            width: '100%',
            height: '100%',
            effect: "circle",
            opt: "left",
            easing: "swing",
            timer: 700,
            call: function() {}
        },
        _set = {
            open: false,
            title: false,
            content: false,
            margin: _def.margin,
            width: _def.width,
            height: _def.height,
            onBeforeShow: function() {},
            onAfterShow: function() {},
            onBeforeHide: function() {},
            onAfterHide: function() {},
            show: {
                effect: _def.effect,
                opt: _def.opt,
                easing: _def.easing,
                timer: _def.timer,
                call: _def.call
            },
            hide: {
                effect: _def.effect,
                opt: _def.opt,
                easing: _def.easing,
                timer: _def.timer,
                call: _def.call
            }
        };
    _core = {
        effect: {
            show: {},
            hide: {}
        },
        env: {
            top: 0,
            left: 0,
            w: 0,
            h: 0,
            win_w: 0,
            win_h: 0,
            pos_w: 0,
            pos_h: 0,
            border: {
                top: 0,
                right: 0,
                bottom: 0,
                left: 0
            }
        },
        circle: function(a) {
            _popup.hide();
            if (!_core.env.pos_w) {
                _core.env.pos_w = 1;
                effect = {
                    'width': 0,
                    'height': 0,
                    'margin-left': _core.env.win_w / 2,
                    'margin-top': _core.env.win_h / 2
                };
            } else {
                _core.env.pos_w = 0;
                effect = {
                    'width': '100%',
                    'height': '100%',
                    'margin-left': 0,
                    'margin-top': 0
                };
            }

            _overlay.transition(effect, a.timer, a.easing, function() {
                _popup.fadeIn();
                a.call();
            })
        },
        slide: function(a) {
            var _pos = 0,
                _border_w = _core.env.border.left + _core.env.border.right,
                _border_h = _core.env.border.top + _core.env.border.bottom;

            switch (a.opt) {
                case "top":
                    if (_core.env.pos_h == 0) {
                        _pos = _core.env.win_h;
                        _core.env.pos_h = _pos
                    } else {
                        _pos = _core.env.top;
                        _core.env.pos_h = 0
                    }
                    effect = {
                        top: _pos
                    };
                    break;
                case "right":
                    if (_core.env.pos_w == 0) {
                        _pos = 0 - _core.env.left - _border_w - _core.env.w;
                        _core.env.pos_w = _pos
                    } else {
                        _pos = _core.env.left;
                        _core.env.pos_w = 0
                    }
                    effect = {
                        left: _pos
                    };
                    break;
                case "bottom":
                    if (_core.env.pos_h == 0) {
                        _pos = 0 - _core.env.top - _border_h - _core.env.h;
                        _core.env.pos_h = _pos
                    } else {
                        _pos = _core.env.top;
                        _core.env.pos_h = 0
                    }
                    effect = {
                        top: _pos
                    };
                    break;
                default:
                    if (_core.env.pos_w == 0) {
                        _pos = _core.env.win_w;
                        _core.env.pos_w = _pos
                    } else {
                        _pos = _core.env.left;
                        _core.env.pos_w = 0
                    }
                    effect = {
                        left: _pos
                    };
                    break
            }
            _popup.transition(effect, a.timer, a.easing, a.call)
        },
        setStart: function() {
            var _def = $.extend({}, _core.effect.show);
            _core.env.pos_w = 0;
            _core.env.pos_h = 0;
            _core[_def.effect]({
                opt: _def.opt,
                easing: _def.easing,
                timer: 0,
                call: function() {}
            })
        },
        setPopup: function() {
            _core.env.border = {
                top: parseInt(_popup.css("borderTopWidth"), 10),
                right: parseInt(_popup.css("borderRightWidth"), 10),
                bottom: parseInt(_popup.css("borderBottomWidth"), 10),
                left: parseInt(_popup.css("borderLeftWidth"), 10)
            };
            _overlay.hide();
            _core.setPopSize();
            _core.setEffect(_opt.show, _opt.hide);
            _core.setStart();
            $(window).resize(function(event) {
                _core.setPopSize()
            })
        },
        setPopSize: function() {
            var _win = $(window),
                _width = _opt.width,
                _height = _opt.height,
                _border_w = _core.env.border.left + _core.env.border.right,
                _border_h = _core.env.border.top + _core.env.border.bottom,
                _margin = _opt.margin,
                _margin_left = 0,
                _margin_top = 0;
            _core.env.win_w = _win.width();
            _core.env.win_h = _win.height();
            if ($.type(_opt.width) != "number") _width = _core.env.win_w - _margin * 2;
            if ($.type(_opt.height) != "number") _height = _core.env.win_h - _margin * 2;
            _core.env.w = _width - _border_w;
            _core.env.h = _height - _border_h;
            _core.env.left = _margin;
            _core.env.top = _margin;
            _margin_left = (_core.env.win_w - _core.env.w - _border_w) / 2;
            _margin_top = (_core.env.win_h - _core.env.h - _border_h) / 2;
            if (_margin_left < _margin) _core.env.w = _core.env.win_w - _border_w - _margin * 2;
            else _core.env.left = _margin_left;
            if (_margin_top < _margin) _core.env.h = _core.env.win_h - _border_h - _margin * 2;
            else _core.env.top = _margin_top;
            _popup.css({
                top: _core.env.top,
                left: _core.env.left,
                width: _core.env.w,
                height: _core.env.h
            })
        },
        setEffect: function(a, b) {
            $.each({
                show: a,
                hide: b
            }, function(i, action) {
                if ($.type(action.effect) == "string" && _core[action.effect]) {
                    if ($.type(action.opt) !== "string") action.opt = _def.opt;
                    if ($.type(action.easing) !== "string") action.easing = _def.easing;
                    if ($.type(action.timer) !== "number") action.timer = _def.timer;
                    if ($.type(action.call) !== "function") action.call = _def.call;
                    _core.effect[i] = action
                } else _core.effect[i] = {
                    effect: _def.effect,
                    opt: _def.opt,
                    easing: _def.easing,
                    timer: _def.timer,
                    call: _def.call
                }
            })
        },
        showPopup: function(a, b) {
            var _effect = _core.effect.show,
                _beforeCall = $.type(a) == "function" ? a : _opt.onBeforeShow,
                _afterCall = $.type(b) == "function" ? b : _opt.onAfterShow;

            _beforeCall();
            _overlay.parents("body,html").css({
                overflow: "hidden"
            });
            _overlay.show();
            _core[_effect.effect]({
                opt: _effect.opt,
                timer: _effect.timer,
                call: function() {
                    _effect.call();
                    _afterCall()
                }
            })
        },
        hidePopup: function(a, b) {
            var _effect = _core.effect.hide,
                _beforeCall = $.type(a) == "function" ? a : _opt.onBeforeHide;
            _afterCall = $.type(a) == "function" ? b : _opt.onAfterHide;
            if (_beforeCall() == false) return false;
            _core[_effect.effect]({
                opt: _effect.opt,
                timer: _effect.timer,
                call: function() {
                    _overlay.hide();
                    _overlay.parents("body,html").css({
                        overflow: "auto"
                    });
                    _core.setStart();
                    _effect.call();
                    _afterCall();
                    if(0 && hashC){
                        hashC.sethash('');
                    }
                }
            })
        }
    };
    _ths.show = _core.showPopup;
    _ths.hide = _core.hidePopup;
    _ths.setTitle = function(a) {
        var _title = $(".contenter > .title", _popup);
        if (a && $.type(a) == "string") {
            $(".text", _title).html(a);
            _title.css("height", "").children().show();
        } else _title.css("height", "auto").children().hide()
    };
    _ths.setContent = function(a) {
        if (a) {
            $(".content", _popup).html(a);
            $(".contenter", _popup).scrollTop(0)
        }
    };
    _ths.getpopup = function() {
        return _popup
    };
    _ths.reset = function(a) {
        _opt = $.extend(true, {}, _opt, a);
        _core.setPopSize();
        _ths.setTitle(_opt.title);
        _ths.setContent(_opt.content);

        if(_opt.open === true){
            if(_overlay.is(":hidden")){
                _core.setStart();
                _ths.show();
            }else{
                _opt.onBeforeShow();
                _opt.onAfterShow();
            }
        }else{
            _opt.onBeforeHide();
            _opt.onAfterHide();
        }
    };
    _ths.set = function(a) {
        _opt = $.extend(true, {}, _set, a);
        if ($(".overlay").length){
            _overlay = $(".overlay");
            $(".popup","body").on("click", function(e) {
                e.stopPropagation()
            });
            $(".popup-close","body").on("click" , _core.hidePopup);
        }else {
            _overlay = $('<div class="overlay popup-close"><div class="popup"><div class="popup-close close"></div><div class="contenter"><ul class="title"><li class="hr"><hr></li><li class="text"></li><li class="hr"><hr></li></ul><div class="content"></div></div></div></div>').prependTo("body");
            $("body").on("click", ".popup", function(e) {
                e.stopPropagation()
            }).on("click", ".popup-close", _core.hidePopup)
        }
        _popup = $(".popup", _overlay);
        _ths.setTitle(_opt.title);
        _ths.setContent(_opt.content);
        _core.setPopup();
        if (_opt.open === true) _ths.show()
    };
    if (a) _ths.set(a);
    return _ths
}

$(document).ready(function() {
    popup = new PopUp({});
    _popup = popup.getpopup();
});