qs.defineNS('app'); app.EmailTo = qs.createObject(); app.EmailTo.prototype = { options: { emailToLinkId: 'email-to-popup', sendSuccessMsg: 'Email was successfully sent to recipient.' }, initialize: function (options) { this.options = $.extend(this.options, options); this.initEmailPopupLink(); }, initEmailPopupLink: function() { $('a[id="' + this.options.emailToLinkId + '"]').bind('click', _.bind(this.onEmailPopupLinkClick, this)); }, onEmailPopupLinkClick: function(e) { var obj = this, actionParam = 'emailPopup', actionUrl = qs.constant('BASE_URL_LANGUAGE') + '/' + qs.constant('CURRENT_PAGE'); qs.showLoading(); var request = { action: actionParam }; qs.ajax(actionUrl, request, {qsShowLoading: false}) .fail(function(status) {alert(status.responseText);}) .done(function(data) { if (data && data.form) { var contentObj = $(data.form); delete data['form']; var scripts = []; if (data.scripts) { var loadedScripts = []; var iteration = 0; $('script').each(function() { if ($(this).attr('src')) { loadedScripts[iteration] = $(this).attr('src'); iteration++; } }); iteration = 0; $(data.scripts).each(function() { if ($.inArray($(this).attr('src'), loadedScripts) < 0 && $(this).attr('src')) { scripts[iteration] = $(this).attr('src'); iteration++; } }); } delete data['scripts']; delete data['stylesheets']; var initFunctions = []; for (var key in data) { if (data[key].length) { initFunctions[key] = function() { eval(data[key]); } } } obj._initContent(contentObj, scripts, initFunctions, e.currentTarget); } else { alert('Error loading data'); } }); }, _initContent: function(content, scripts, initFunctions, target) { if (scripts.length) { var obj = this; this._scriptLoaderIterator = 0; for (var key in scripts) { this._loadScript(scripts[key], scripts.length, function() { obj._loadContent(content, initFunctions, target); }); } } else { this._loadContent(content, initFunctions, target); } }, _loadContent: function(content, initFunctions, target) { var self = this; this._scriptLoaderIterator = 0; $.fancybox({ scrolling: 'auto', closeBtn: true, overlayOpacity: 0.1, maxWidth: 800, autoCenter: true, autoResize: true, margin: 0, padding: 10, content: $('
').html(content), afterShow: function () { $('.fancybox-overlay input:text:first').focus(); }, afterClose: function () { $(target).focus(); } }); content.find('.btn-cancel').removeAttr('onclick').click(function(e) { e.preventDefault(); $.fancybox.close(); }); for (var key in initFunctions) { initFunctions[key](); } qs.hideLoading(); }, _loadScript: function(script, scriptsCount, callback) { var obj = this; $.getScript(script, function() { obj._scriptLoaderIterator++; if (scriptsCount == obj._scriptLoaderIterator) { callback(); } }); }, onSendEmailSuccessCallback: function() { $('.fancybox-inner').html('
' + this.options.sendSuccessMsg + '
'); } };