(function() { var _currOpenDialog = false, _currOpenPos = 0, $dialog = false, $overlay = false; $(document).ready(function() { $dialog = $('#custom-dialog'); $overlay = $('#custom-overlay'); var html = ''; if (!$dialog.length) { html += ''; } if (!$overlay.length) { html += ''; } if (html) { $('body').append(html); } $dialog = $('#custom-dialog'); $overlay = $('#custom-overlay'); }); var Dialog = function() { this.height = 400; this.width = 600; this.offsetTop = 50; this.isOpen = false; this.delay = 200; this.clearOverlay = false; this.closeOnScroll = false; this.contentHtml = ''; this.classes = []; this.fnBeforeOpen = []; this.fnOnOpen = []; this.fnAfterClose = []; this.fnFilter = []; }; Dialog.prototype.redraw = function() { this.size($dialog); this.position($dialog); } Dialog.prototype.setHeight = function(height) { this.height = height; if (this.isOpen) { this.redraw(); } return this; } Dialog.prototype.setWidth = function(width) { this.width = width; if (this.isOpen) { this.redraw(); } return this; } Dialog.prototype.setOffsetTop = function(offset) { this.offsetTop = offset; if (this.isOpen) { this.redraw(); } return this; } Dialog.prototype.setContent = function(html) { this.contentHtml = html; if (this.isOpen) { $dialog.find('.dialog-content').html(html); } return this; } Dialog.prototype.setClearOverlay = function(clear) { this.clearOverlay = clear; return this; }; Dialog.prototype.addClass = function(clazz) { this.classes.push(clazz); return this; }; Dialog.prototype.beforeOpen = function(fn) { this.fnBeforeOpen.push(fn); }; Dialog.prototype.onOpen = function(fn) { this.fnOnOpen.push(fn); }; Dialog.prototype.filter = function(fn) { this.fnFilter.push(fn); }; Dialog.prototype.afterClose = function(fn) { this.fnAfterClose.push(fn); } Dialog.prototype.open = function() { var _this = this; valid = true; $.each(this.fnFilter, function(idx, filter) { if ($.isFunction(filter) && !filter()) { valid = false; } }); if (!valid) { return false; } openDelay = 0; if (_currOpenDialog) { openDelay = _currOpenDialog.delay + 20; _currOpenDialog.close(); } setTimeout(function() { _this.size($dialog); _this.position($dialog); if (_this.classes.length) { $dialog.addClass(_this.classes.join(' ')); } $.each(_this.fnBeforeOpen, function(idx, fn) { if ($.isFunction(fn)) { fn($dialog, $overlay); } }); if (_this.clearOverlay) { $overlay.addClass('clear'); } $dialog.find('.dialog-content').html(_this.contentHtml); $overlay.fadeIn(_this.delay); $dialog.show(_this.delay); _this.isOpen = true; $.each(_this.fnOnOpen, function(idx, fn) { if ($.isFunction(fn)) { fn($dialog, $overlay); } }); _this.bindClose(); }, openDelay); }; Dialog.prototype.position = function($dialog) { var width, height, left, top; width = $('body').width(); height = $('body').height(); $dialog.css({ top: this.offsetTop, left: width / 2 - this.calcWidth() / 2 }); } Dialog.prototype.size = function($dialog) { $dialog.css({ width: this.calcWidth(), //check for screen size < this.width height: this.height }); } Dialog.prototype.calcWidth = function() { var screenWidth = $(window).width(), width = screenWidth - 40 < this.width ? screenWidth - 40 : this.width; return width; } Dialog.prototype.bindClose = function() { var _this = this; $overlay.click(function() { _this.close(); }); $dialog.find('.close').live('click', function() { _this.close(); }); _currOpenDialog = this; _currOpenPos = $(document).scrollTop(); }; Dialog.prototype.close = function() { var _this = this; $overlay.fadeOut(this.delay); $dialog.fadeOut(this.delay); this.isOpen = false; setTimeout(function() { $overlay.removeClass('clear'); if (_this.classes.length) { $dialog.removeClass(_this.classes.join(' ')); } $.each(_this.fnAfterClose, function(idx, fn) { if ($.isFunction(fn)) { fn($dialog, $overlay); } }); _currOpenDialog = false; }, this.delay); }; Dialog.prototype.transition = function(otherDialog) { this.close(); setTimeout(function() { otherDialog.open(); }, this.delay + 50); }; $(document).scroll(function() { var newPos, diagHeight; if (_currOpenDialog && _currOpenDialog.closeOnScroll && _currOpenDialog.isOpen) { newPos = $(document).scrollTop(), diagHeight = $dialog.height(); if (newPos >= (_currOpenPos + diagHeight) || newPos <= _currOpenPos - 80) { _currOpenDialog.close(); } } }); var IframeDialog = function(rel, watchUrl) { //redeclare attributes so they don't change prototype this.rel = rel || ''; this.watchUrl = watchUrl; this.classes = ['iframe-dialog']; this.height = 400; this.width = 600; this.offsetTop = 50; this.isOpen = false; this.delay = 200; this.clearOverlay = false; this.closeOnScroll = false; this.contentHtml = ''; this.fnBeforeOpen = []; this.fnOnOpen = []; this.fnAfterClose = []; this.fnFilter = []; this.beforeOpen(this.getLoadFn()); }; IframeDialog.prototype = new Dialog(); IframeDialog.prototype.setRel = function(rel) { this.rel = rel; }; IframeDialog.prototype.getLoadFn = function() { var _this = this; return function($dialog) { var data = false; _this.fetchData(function() { $dialog.addClass('loading'); }, function(data) { $dialog.removeClass('loading'); _this.setContent(data); _this.watchHeight(); }, function() { alert('Error loading dialog'); $dialog.removeClass('loading'); _this.close(); }); } } IframeDialog.prototype.fetchData = function(before, success, error) { $.ajax({ url: 'remote?method=getInfo', type: 'post', dataType: 'json', data: 'data=' + this.rel + '&tmp=1', beforeSend: function() { if (before && $.isFunction(before)) { before(); } }, success: function(data) { if (success && $.isFunction(success)) { success(data.data); } }, error: function() { if (error && $.isFunction(error)) { error(); } } }); } IframeDialog.prototype.watchHeight = function() { var _this = this; if (!this.watchUrl) { return false; } $.receiveMessage(function(e) { if (e.data.indexOf("clientHash") != -1) { var res = e.data.split("?"); var currentUrl = this.document.location.href.split("?"); document.location.href = currentUrl[0] + '?' + res[1]; return true; } $dialog.find('iframe').height(e.data + 'px'); }, this.watchUrl); }; var MenuDialog = function(element, watchUrl) { //redeclare attributes so they don't change prototype this.$el = $(element); this.watchUrl = watchUrl; this.rel = this.$el.attr('rel') this.classes = ['iframe-dialog', 'menu-dialog']; this.closeOnScroll = true; this.height = 400; this.width = 600; this.offsetTop = 50; this.isOpen = false; this.delay = 200; this.clearOverlay = true; this.contentHtml = ''; this.fnBeforeOpen = []; this.fnOnOpen = []; this.fnAfterClose = []; this.fnFilter = []; this.beforeOpen(this.getLoadFn()); } MenuDialog.prototype = new IframeDialog(); MenuDialog.prototype.position = function($dialog) { var offset = this.$el.offset(), elHeight = this.$el.height(), elWidth = this.$el.innerWidth(); var innerHeight = this.$el.innerHeight(); elHeight += (innerHeight - elHeight) / 2; $dialog.css({ top: offset.top + elHeight + 35, left: offset.left + elWidth / 2 - this.width + 120 }); } MenuDialog.prototype.openOn = function(element) { this.$el = $(element); this.open(); } window.Dialog = Dialog; window.IframeDialog = IframeDialog; window.MenuDialog = MenuDialog; })();