/*!
 * jQuery MsgBox v1.0 - for jQuery 1.3+
 * http://codecanyon.net/user/aeroalquimia/portfolio
 *
 * Copyright 2010, Eduardo Daniel Sada
 * You need to buy a license if you want use this script.
 * http://codecanyon.net/wiki/buying/howto-buying/licensing/
 *
 * Date: Mar 10 2010
 *
 * Includes jQuery Easing v1.1.2
 * http://gsgd.co.uk/sandbox/jquery.easIng.php
 * Copyright (c) 2007 George Smith
 * Released under the MIT License.
 */

(function($) {
    var ie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4);
    if ($.proxy === undefined) {
        $.extend({
            proxy: function(fn, thisObject) {
                if (fn) {
                    proxy = function() {
                        return fn.apply(thisObject || this, arguments);
                    };
                };
                return proxy;
            }
        });
    };
    $.extend(jQuery.easing, {
        easeOutBack: function(x, t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
        }
    });
    $.extend($.expr[':'], {
        value: function(a) {
            return $(a).val();
        }
    });
    $.extend({
        MsgBoxConfig: function(options) {
            var defaults = {
                name: 'jquery-msgbox',
                formaction: '#',
                zIndex: 10000,
                width: 420,
                height: 'auto',
                background: '#FFFFFF',
                modal: true,
                overlay: {
                    'background-color': '#7f7f7f',
                    'opacity': 0.1
                },
                showDuration: 200,
                closeDuration: 100,
                moveDuration: 0,
                emergefrom: 'top',
                shake: {
                    distance: 10,
                    duration: 100,
                    transition: 'easeOutBack',
                    loops: 2
                }
            };
            if ($.aerOptions === undefined) {
                return $.aerOptions = $.extend(true, defaults, options);
            } else {
                return $.aerOptions = $.extend(true, $.aerOptions, options);
            }
        },
        MsgBoxObject: {
            options: {},
            instance: {
                msgbox: [],
                wrapper: [],
                form: [],
                buttons: [],
                inputs: []
            },
            visible: false,
            i: 0,
            animation: false,
            overlay: {
                create: function(options) {
                    this.options = options;
                    this.element = $('<div id="' + new Date().getTime() + '"></div>');
                    this.element.css($.extend({},
                    {
                        'position': 'fixed',
                        'top': 0,
                        'left': 0,
                        'opacity': 0,
                        'display': 'none',
                        'z-index': this.options.zIndex
                    },
                    this.options.style));
                    this.element.click($.proxy(function(event) {
                        if (this.options.hideOnClick) {
                            if (!this.options.callback === undefined) {
                                this.options.callback();
                            } else {
                                this.hide();
                            }
                        }
                        event.preventDefault();
                    },
                    this));
                    this.hidden = true;
                    this.inject();
                    return this;
                },
                inject: function() {
                    this.target = $(document.body);
                    this.target.append(this.element);
                    if (ie6) {
                        this.element.css({
                            'position': 'absolute'
                        });
                        var zIndex = parseInt(this.element.css('zIndex'));
                        if (!zIndex) {
                            zIndex = 1;
                            var pos = this.element.css('position');
                            if (pos == 'static' || !pos) {
                                this.element.css({
                                    'position': 'relative'
                                });
                            }
                            this.element.css({
                                'zIndex': zIndex
                            });
                        }
                        zIndex = ( !! (this.options.zIndex || this.options.zIndex === 0) && zIndex > this.options.zIndex) ? this.options.zIndex: zIndex - 1;
                        if (zIndex < 0) {
                            zIndex = 1;
                        }
                        this.shim = $('<iframe id="IF_' + new Date().getTime() + '" scrolling="no" frameborder=0 src=""></div>');
                        this.shim.css({
                            zIndex: zIndex,
                            position: 'absolute',
                            top: 0,
                            left: 0,
                            border: 'none',
                            width: 0,
                            height: 0,
                            opacity: 0
                        });
                        this.shim.insertAfter(this.element);
                        $('html, body').css({
                            'height': '100%',
                            'width': '100%',
                            'margin-left': 0,
                            'margin-right': 0
                        });
                    }
                },
                resize: function(x, y) {
                    this.element.css({
                        'height': 0,
                        'width': 0
                    });
                    if (this.shim) this.shim.css({
                        'height': 0,
                        'width': 0
                    });
                    var win = {
                        x: $(document).width(),
                        y: $(document).height()
                    };
                    this.element.css({
                        'width': '100%',
                        'height': y ? y: win.y
                    });
                    if (this.shim) {
                        this.shim.css({
                            'height': 0,
                            'width': 0
                        });
                        this.shim.css({
                            'position': 'absolute',
                            'left': 0,
                            'top': 0,
                            'width': this.element.width(),
                            'height': y ? y: win.y
                        });
                    }
                    return this;
                },
                show: function() {
                    if (!this.hidden) return this;
                    if (this.transition) this.transition.stop();
                    this.target.bind('resize', $.proxy(this.resize, this));
                    this.resize();
                    if (this.shim) this.shim.css({
                        'display': 'block'
                    });
                    this.hidden = false;
                    this.transition = this.element.fadeIn(this.options.showDuration, $.proxy(function() {
                        this.element.trigger('show');
                    },
                    this));
                    return this;
                },
                hide: function() {
                    if (this.hidden) return this;
                    if (this.transition) this.transition.stop();
                    this.target.unbind('resize');
                    if (this.shim) this.shim.css({
                        'display': 'none'
                    });
                    this.hidden = true;
                    this.transition = this.element.fadeOut(this.options.closeDuration, $.proxy(function() {
                        this.element.trigger('hide');
                        this.element.css({
                            'height': 0,
                            'width': 0
                        });
                    },
                    this));
                    return this;
                }
            },
            create: function() {
                this.options = $.MsgBoxConfig();
                this.overlay.create({
                    style: this.options.overlay,
                    hideOnClick: !this.options.modal,
                    zIndex: this.options.zIndex - 1,
                    showDuration: this.options.showDuration,
                    closeDuration: this.options.closeDuration
                });
                this.instance.msgbox = $('<div class="' + this.options.name + '"></div>');
                this.instance.msgbox.css({
                    display: 'none',
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: this.options.width,
                    height: this.options.height,
                    'z-index': this.options.zIndex,
                    'word-wrap': 'break-word',
                    '-moz-box-shadow': '0 0 10px rgba(0, 0, 0, 0.5)',
                    '-webkit-box-shadow': '0 0 10px rgba(0, 0, 0, 0.5)',
                    'box-shadow': '0 0 10px rgba(0, 0, 0, 0.5)',
                    '-moz-border-radius': '6px',
                    '-webkit-border-radius': '6px',
                    'border-radius': '6px',
                    'background-color': this.options.background
                });
                this.instance.wrapper = $('<div class="' + this.options.name + '-wrapper"></div>');
                this.instance.msgbox.append(this.instance.wrapper);
                this.instance.form = $('<form action="' + this.options.formaction + '" method="post"></form>');
                this.instance.wrapper.append(this.instance.form);
                this.instance.wrapper.css({
                    height: (ie6 ? 80 : 'auto'),
                    'min-height': 80,
                    'zoom': 1
                });
                $('body').append(this.instance.msgbox);
                this.addevents();
                return this.instance.msgbox;
            },
            addevents: function() {
                $(window).bind('resize', $.proxy(function() {
                    if (this.visible) {
                        this.overlay.resize();
                        this.moveBox();
                    }
                },
                this));
                $(window).bind('scroll', $.proxy(function() {
                    this.moveBox();
                },
                this));
                this.instance.msgbox.bind('keydown', $.proxy(function(event) {
                    if (event.keyCode == 27) {
                        this.close(false);
                    }
                },
                this));
                this.instance.form.bind('submit', $.proxy(function(event) {
                    event.preventDefault();
                },
                this));
                this.overlay.element.bind('show', $.proxy(function() {
                    $(this).triggerHandler('show');
                },
                this));
                this.overlay.element.bind('hide', $.proxy(function() {
                    $(this).triggerHandler('close');
                },
                this));
            },
            show: function(txt, options, callback) {
                var types = ['alert', 'info', 'error', 'prompt', 'confirm', 'mask'];
                this.instance.msgbox.queue(this.options.name, $.proxy(function(next) {
                    options = $.extend(true, {
                        type: 'alert'
                    },
                    options || {});
                    if (options.buttons === undefined) {
                        if (options.type == 'confirm' || options.type == 'prompt') {
                            var buttons = [{
                                type: 'submit',
                                value: 'ОК'
                            },
                            {
                                type: 'cancel',
                                value: 'Отмена'
                            }];
                        }else if(options.type == 'mask'){
                        	var buttons = [];
                        } else {
                            var buttons = [{
                                type: 'submit',
                                value: 'ОК'
                            }];
                        }
                    } else {
                        var buttons = options.buttons;
                    };
                    if (options.inputs === undefined && options.type == 'prompt') {
                        var inputs = [{
                            type: 'text',
                            name: 'prompt',
                            value: ''
                        }];
                    } else {
                        var inputs = options.inputs;
                    };
                    this.callback = $.isFunction(callback) ? callback: function(e) {};
                    if (inputs !== undefined) {
                        this.instance.inputs = $('<div class="' + this.options.name + '-inputs"></div>');
                        this.instance.form.append(this.instance.inputs);
                        $.each(inputs, $.proxy(function(i, input) {
                            if (input.type == 'text' || input.type == 'password') {
                                iLabel = input.label ? '<label class="' + this.options.name + '-label">' + input.label: '';
                                fLabel = input.label ? '</label>': '';
                                input.value = input.value === undefined ? '': input.value;
                                iRequired = input.required === undefined || input.required == false ? '': 'required="true"';
                                this.instance.inputs.append($(iLabel + '<input type="' + input.type + '" name="' + this.options.name + '-label-' + i + '" value="' + input.value + '" autocomplete="off" ' + iRequired + '/>' + fLabel));
                            } else if (input.type == 'checkbox') {
                                iLabel = input.label ? '<label class="' + this.options.name + '-label">': '';
                                fLabel = input.label ? input.label + '</label>': '';
                                input.value = input.value === undefined ? '1': input.value;
                                this.instance.inputs.append($(iLabel + '<input type="' + input.type + '" style="display:inline; width:auto;" name="' + this.options.name + '-label-' + i + '" value="' + input.value + '" autocomplete="off"/> ' + fLabel));
                            }
                        },
                        this));
                    }
                    if(options.type != 'mask'){
	                    this.instance.buttons = $('<div class="' + this.options.name + '-buttons"></div>');
	                    this.instance.form.append(this.instance.buttons);
	                    if (options.type == 'alert' || options.type == 'info' || options.type == 'error' || options.type == 'confirm') {
	                        $.each(buttons, $.proxy(function(i, button) {
	                            if (button.type == 'submit') {
	                                this.instance.buttons.append($('<button type="submit" onfocus="this.blur()">' + button.value + '</button>').bind('click', $.proxy(function(e) {
	                                    this.close(button.value);
	                                    e.preventDefault();
	                                },
	                                this)));
	                            } else if (button.type == 'cancel') {
	                                this.instance.buttons.append($('<button type="button" onfocus="this.blur()">' + button.value + '</button>').bind('click', $.proxy(function(e) {
	                                    this.close(false);
	                                    e.preventDefault();
	                                },
	                                this)));
	                            }
	                        },
	                        this));
	                    } else if (options.type == 'prompt') {
	                        $.each(buttons, $.proxy(function(i, button) {
	                            if (button.type == 'submit') {
	                                this.instance.buttons.append($('<button type="submit" onfocus="this.blur()">' + button.value + '</button>').bind('click', $.proxy(function(e) {
	                                    if ($('input[required="true"]:not(:value)').length > 0) {
	                                        $('input[required="true"]:not(:value):first').focus();
	                                        this.shake();
	                                    } else {
	                                        this.close(this.toArguments($('input', this.instance.inputs)));
	                                    }
	                                    e.preventDefault();
	                                },
	                                this)));
	                            } else if (button.type == 'cancel') {
	                                this.instance.buttons.append($('<button type="button" onfocus="this.blur()">' + button.value + '</button>').bind('click', $.proxy(function(e) {
	                                    this.close(false);
	                                    e.preventDefault();
	                                },
	                                this)));
	                            }
	                        },
	                        this));
	                    };
	                    this.instance.form.prepend(txt);
	                    this.instance.msgbox.removeClass(this.options.name + '-loader');
                    }else{
                    	this.instance.msgbox.addClass(this.options.name + '-loader');
                    }
                    var mw = (options.type != 'mask') ? this.options.width : 105;
                	this.instance.msgbox.css({
                		width:mw
                	});
                    $.each(types, $.proxy(function(i, e) {
                        this.instance.wrapper.removeClass(this.options.name + '-' + e);
                    },
                    this));
                    this.instance.wrapper.addClass(this.options.name + '-' + options.type);
                    this.setCurrentOptions(options);
                    this.moveBox();
                    this.visible = true;
                    this.overlay.show();
                    this.instance.msgbox.css({
                        display: 'block',
                        left: (($(document).width() - mw) / 2)
                    });
                    this.moveBox();
                    setTimeout($.proxy(function() {
                        var b = $('input, button', this.instance.msgbox);
                        if (b.length) {
                            b.get(0).focus();
                        }
                    },
                    this), this.options.moveDuration);
                },
                this));
                this.i++;
                if (this.i == 1) {
                    this.instance.msgbox.dequeue(this.options.name);
                }
            },
            toArguments: function(array) {
                return $.map(array,
                function(a) {
                    return $(a).val();
                });
            },
            currentOptions : {},
            setCurrentOptions:function(options){
            	this.currentOptions = options;
            },
            getCurrentOptions:function(){
            	return this.currentOptions;
            },
            moveBox: function() {
                var size = {
                    x: $(window).width(),
                    y: $(window).height()
                };
                var scroll = {
                    x: $(window).scrollLeft(),
                    y: $(window).scrollTop()
                };
                var height = this.instance.msgbox.outerHeight();
                var y = 0;
                var x = 0;
                var options = this.getCurrentOptions();
                var mw = (options.type != 'mask') ? this.options.width : 105;
                y = scroll.x + ((size.x - mw) / 2);
                if (this.options.emergefrom == "bottom") {
                    x = (scroll.y + size.y + 80);
                } else {
                    x = (scroll.y - height) - 80;
                }
                if (this.visible) {
                    if (this.animation) {
                        this.animation.stop;
                    }
                    this.animation = this.instance.msgbox.animate({
                        left: y,
                        top: scroll.y + ((size.y - height) / 2)
                    },
                    {
                        duration: this.options.moveDuration,
                        queue: false,
                        easing: 'easeOutBack'
                    });
                } else {
                    this.instance.msgbox.css({
                        top: x,
                        left: y
                    });
                }
            },
            close: function(param) {
                this.instance.msgbox.css({
                    display: 'none',
                    top: 0
                });
                this.visible = false;
                if ($.isFunction(this.callback)) {
                    this.callback.apply(this, $.makeArray(param));
                }
                setTimeout($.proxy(function() {
                    this.i--;
                    this.instance.msgbox.dequeue(this.options.name);
                },
                this), this.options.closeDuration);
                if (this.i == 1) {
                    this.overlay.hide();
                }
                this.moveBox();
                this.instance.form.empty();
            },
            shake: function() {
                var x = this.options.shake.distance;
                var d = this.options.shake.duration;
                var t = this.options.shake.transition;
                var o = this.options.shake.loops;
                var l = this.instance.msgbox.position().left;
                var e = this.instance.msgbox;
                for (i = 0; i < o; i++) {
                    e.animate({
                        left: l + x
                    },
                    d, t);
                    e.animate({
                        left: l - x
                    },
                    d, t);
                };
                e.animate({
                    left: l + x
                },
                d, t);
                e.animate({
                    left: l
                },
                d, t);
            }
        },
        msgbox: function(txt, options, callback) {
            return $.MsgBoxObject.show(txt, options, callback);
        },
        msgboxclose:function(){
        	$.MsgBoxObject.close(false);
        },
        mask:function(){
        	return $.MsgBoxObject.show('', {type:'mask'});
        },
        unmask:function(){
        	$.MsgBoxObject.close(false);
        }
    });
    $(function() {
        $.MsgBoxObject.create();
    });
})(jQuery);

