(function($) { var defaults = { templates: { loader: '
', overlay: '', container: '', wrap: ' ', windows: '', window: '', windowContent: '', windowFooter: ' ' }, width: 'auto', height: 'auto' }, $wrap, $overlay, $container, $windowContent, $windowFooter, windows = {}, batubox = function() { this.version = '0.0.1'; }, onkeyClose = function(e) { if (e.which === 27) { functions.close('$$last'); } }, functions = { closeOverlay: function() { if (windows.length === 0) { $("body").css({'overflow-y': 'visible'}); $container.remove(); $wrap = null; $overlay = null; $container = null; $windowContent = null; $windowFooter = null; } else { $wrap.append(windows.last); } if ((typeof afterClose) === 'function') { afterClose(); } }, close: function(ID, afterClose, beforeClose) { if ((typeof ID) === 'function') { beforeClose = afterClose; afterClose = ID; delete ID; } if ($container) { if ((typeof beforeClose) === 'function') { beforeClose(); } if ((typeof ID) !== 'undefined') { var id = (ID === '$$last') ? (((typeof windows.last) !== 'undefined') ? windows.last.ID : null) : ID; var $window = ((typeof windows[id]) !== 'undefined') ? windows[id] : null; if ($window) { var options = $window.options; if ((typeof options.beforeClose) === 'function') { options.beforeClose(options); } $window.fadeOut(250, function() { $window.remove(); delete windows[id]; if ((typeof options.afterClose) === 'function') { options.afterClose(options); } functions.closeOverlay(); }); } } else if ((typeof ID) === 'undefined') { for (id in windows) { if (windows.hasOwnProperty(id)) { var $window = windows[id]; var options = $window.options; if ((typeof options.beforeClose) === 'function') { options.beforeClose(options); } $window.remove(); delete windows[id]; if ((typeof options.afterClose) === 'function') { options.afterClose(options); } } } } } } }; Object.defineProperty(windows, "length", { get: function() { var size = 0, key; for (key in this) { if (this.hasOwnProperty(key)) size++; } return size; } }); Object.defineProperty(windows, "last", { get: function() { var last, key; for (key in this) { if (this.hasOwnProperty(key)) last = this[key]; } return last; } }); $.batubox = new batubox(); $.extend($.batubox, { getWindow: function(ID) { if ((typeof ID) !== 'undefined' && ID === '$$last') { return windows.last; } else if ((typeof ID) !== 'undefined' && (typeof windows[ID]) !== 'undefined') { return windows[ID]; } return null; }, open: function(_options, afterOpen, beforeOpen) { $.batubox.showLoading(); if ((typeof _options) === 'function') { beforeOpen = afterOpen; afterOpen = _options; _options = {}; } if ((typeof beforeOpen) === 'function') { beforeOpen(); } var options = this.options = $.extend({}, defaults, _options); $container = $container || $(options.templates.container); $overlay = $overlay || $(options.templates.overlay); if (!$wrap) { $wrap = $(options.templates.wrap); $('body').on('keyup', onkeyClose); $wrap.on('click', function(e) { if (e.target !== this) { return; } functions.close('$$last'); return false; }); } $container.append($overlay); $container.append($wrap); if ((typeof options.id) === 'undefined') { options.id = Math.random().toString(36).substring(7); } options.content = options.content || $('