var Capture = Capture || {}; Capture.urlHelpers = { getOptinsUrl: function(token,returnUrl,optional) { optional = (optional === undefined) ? '' : '&' + optional; var packedReturnUrl = Capture.urlHelpers.packUrl(returnUrl); return "http://ucid.stage.mgroove-hosting.com/user/optins/" + token + "?returnUrl=" + Capture.urlHelpers.urlEncode(packedReturnUrl) + "&bundle=" + "&xdcomm=http%3A%2F%2Fwww.capitolrecords.com%2Fwp-content%2Fplugins%2Fumg-sso%2Fmgxdcomm.html" + optional; }, getOptinsRedirectUrl: function(returnUrl,optional) { optional = (optional === undefined) ? '' : '&' + optional; var packedReturnUrl = Capture.urlHelpers.packUrl(returnUrl); return "http://www.capitolrecords.com/wp-content/plugins/umg-sso/capture_optins.php?returnUrl=" + Capture.urlHelpers.urlEncode(packedReturnUrl) + optional; }, redirectToOptinsUrl: function(token,returnUrl,optional) { var url = Capture.urlHelpers.getOptinsUrl(token,returnUrl,optional); document.location.href = url; }, updateDirection: { memberToUser: 0, userToMember: 1 }, getAccessTokenUrl: function() { return "http://www.capitolrecords.com/wp-content/plugins/umg-sso/capture_refresh.php"; }, getSignInUrl: function(options) { var optional = "", returnUrl; if (typeof options === 'string') { options = { returnUrl: options }; // backward-compatibility } else if (typeof options !== 'object') { options = {}; } if (typeof Backplane !== 'undefined') { optional += "&bp_channel=" + Capture.urlHelpers.urlEncode(Backplane.getChannelID()); } if (options.thinRegistration) { optional += "&thin_registration=1"; } var defaultRedirectUri = Capture.urlHelpers.appendParamToQueryString('http://www.capitolrecords.com/wp-content/plugins/umg-sso/oauth_redirect.php', 'origin=' + Capture.urlHelpers.urlEncode(window.location.href)); if (options.noclose) { defaultRedirectUri = Capture.urlHelpers.appendParamToQueryString(defaultRedirectUri, 'noclose=1'); } if (options.callback) { defaultRedirectUri = Capture.urlHelpers.appendParamToQueryString(defaultRedirectUri, 'callback=' + Capture.urlHelpers.urlEncode(options.callback)); } returnUrl = (options.returnUrl === undefined) ? defaultRedirectUri : options.returnUrl; return "https://umg.dev.janraincapture.com" + (options.smallForm ? "/oauth/signin_small" : "/oauth/signin") + "?response_type=code" + "&client_id=" + "&xd_receiver=http%3A%2F%2Fwww.capitolrecords.com%2Fwp-content%2Fplugins%2Fumg-sso%2Fxdcomm.html" + "&redirect_uri=" + Capture.urlHelpers.urlEncode(returnUrl) + optional; }, getEditProfileUrl: function(token, callback) { return "https://umg.dev.janraincapture.com/oauth/profile?client_id=&xd_receiver=http%3A%2F%2Fwww.capitolrecords.com%2Fwp-content%2Fplugins%2Fumg-sso%2Fxdcomm.html&access_token=" + Capture.urlHelpers.urlEncode(token) + "&callback=" + Capture.urlHelpers.urlEncode(callback); }, getUpdateUrl: function(returnUrl, direction) { var packedReturnUrl = Capture.urlHelpers.packUrl(returnUrl); return "http://www.capitolrecords.com/wp-content/plugins/umg-sso/capture_refresh.php?origin=" + Capture.urlHelpers.urlEncode(packedReturnUrl) + "&direction=" + direction; }, appendParamToQueryString: function(url, param) { if (url.indexOf('?') == -1) { url += '?'; } else { url += '&'; } url += param; return url; }, generateUniqueId: (function() { var unique = 0; return function() { unique++; return "capture" + unique; }; })(), urlEncode: function(plaintext) { // The Javascript escape and unescape functions do not correspond // with what browsers actually do... var SAFECHARS = "0123456789" + // Numeric "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()"; // RFC2396 Mark characters var HEX = "0123456789ABCDEF"; var encoded = ""; for (var i = 0; i < plaintext.length; i++) { var ch = plaintext.charAt(i); if (ch == " ") { encoded += "+"; // x-www-urlencoded, rather than %20 } else if (SAFECHARS.indexOf(ch) != -1) { encoded += ch; } else { var charCode = ch.charCodeAt(0); if (charCode > 255) { alert("Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" + "(URL encoding only supports 8-bit characters.)\n" + "A space (+) will be substituted."); encoded += "+"; } else { encoded += "%"; encoded += HEX.charAt((charCode >> 4) & 0xF); encoded += HEX.charAt(charCode & 0xF); } } } // for return encoded; }, replaceString: function(fullS, oldS, newS) { for (var i = 0; i < fullS.length; i++) { if (fullS.substring(i, i + oldS.length) == oldS) fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length); } return fullS; }, packUrl: function(url) { return Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString(url, "?", "!"), "&", "^"), "=", "|"), "/", "$"); }, unPackUrl: function(url) { return Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString( Capture.urlHelpers.replaceString(url, "!", "?"), "^", "&"), "|", "="), "$", "/"); } }; (function($) { var iframes = []; if (typeof window.CAPTURE !== 'object') { window.CAPTURE = {}; } CAPTURE.resize = function(json) { var o = $.parseJSON(json); $.each(iframes, function(i, frame) { if (o.w) { frame.removeAttr('width').width(o.w); } if (o.h) { frame.removeAttr('height').height(o.h); } }); }; Capture.iframeHelper = { create: function(options) { var frame; var settings = { width: "254px", height: "58px", resize: true, transparent: true, frameBorder: 0 }; if (options) { $.extend(settings, options); } frame = $('').attr({ src: settings.src, frameborder: settings.frameBorder }); if (settings.id) { frame.attr('id', settings.id); } if (settings.width) { frame.css({'width': settings.width}); } if (settings.height) { frame.css({'height': settings.height}); } if (typeof settings.transparent !== 'undefined') { frame.attr('allowtransparency', settings.transparent); } if (settings.resize) { iframes.push(frame); } return frame; }, remove: function(frame) { var i; frame.detach(); i = $.inArray(frame, iframes); if (i !== -1) { iframes.splice(i, 1); } } }; })(jQuery); Capture.windowHelpers = function () { var didInit = false; var _addSignInWindow = function () { var window = { "id": "LogInWindow", "title": "Welcome!", "width": 580, "height": 500, "reloadOnClose": false }; Microgroove.windowManager.add(window); }; var _addOptinsWindow = function () { var window = { "id": "OptinsWindow", "title": "Get Exclusive Updates", "url": null, "width": 580, "height": 460, "reloadOnClose": false }; Microgroove.windowManager.add(window); }; var _onEditProfileComplete = function () { var redirectUrl = Capture.urlHelpers.getUpdateUrl(document.location.href, Capture.urlHelpers.updateDirection.userToMember); Microgroove.helpers.redirectLocation(redirectUrl); }; var _init = function () { if ( didInit ) { return; } _addSignInWindow(); _addOptinsWindow(); didInit = true; }; return { init: _init, onEditProfileComplete: _onEditProfileComplete }; } (); (function( $ ){ function getSigninIframe(options) { var settings = { width: '218px', height: '64px' }; if (options) { $.extend(settings, options); } settings.smallForm = true; settings.src = Capture.urlHelpers.getSignInUrl(settings); return Capture.iframeHelper.create(settings); } var methods = { init: function() { alert( "enableCapture() must be called with a method name." ); return this; }, signinLink: function(options) { var settings = { thinRegistration: true }; if (options) { $.extend(settings, options); } callbackFunction = Capture.urlHelpers.generateUniqueId(); window[callbackFunction] = function(o) { var closeCallback; Microgroove.windowManager.close(); if (o) { $(document.body).trigger('login', o); if (typeof Backplane !== 'undefined') { Backplane.expectMessagesWithin(60, ['identity/login']); } if (o.showOptIns) { closeCallback = Capture.urlHelpers.generateUniqueId(); window[closeCallback] = function() { Microgroove.windowManager.close(); }; Microgroove.windowManager.open(Capture.urlHelpers.getOptinsUrl(o.token, window.location.href, '&callback=' + closeCallback + (typeof o.id === 'undefined' ? '' : '&id=' + o.id)), "OptinsWindow"); } } }; settings.callback = callbackFunction; return this.attr('href', 'javascript:void(0);').bind("click", function () { Microgroove.windowManager.open(Capture.urlHelpers.getSignInUrl(settings), "LogInWindow"); }); }, mobileSigninLink: function(options) { var settings = { }; return this.each(function() { if ( options ) { jQuery.extend( settings, options ); } var $this = jQuery(this); $this.attr('href', 'javascript:void(0);').bind("click", function () { var origin = "?origin=" + Capture.urlHelpers.urlEncode(document.location.href); var bp_channel = (typeof Backplane === "undefined") ? "" : "&bp_channel=" + Capture.urlHelpers.urlEncode(Backplane.getChannelID()); window.location = "http://www.capitolrecords.com/wp-content/plugins/umg-sso/mobile_signin.php" + origin + bp_channel; }); }); }, signinFrame: function(options) { var settings = { thinRegistration: true, noclose: true }; return this.each(function() { var iframe, callbackFunction, $this = $(this); if ( options ) { $.extend( settings, options ); } callbackFunction = Capture.urlHelpers.generateUniqueId(); window[callbackFunction] = function(o) { var closeCallback; $this.empty(); if (o) { $(document.body).trigger('login', o); if (typeof Backplane !== 'undefined') { Backplane.expectMessagesWithin(60, ['identity/login']); } if (o.showOptIns) { closeCallback = Capture.urlHelpers.generateUniqueId(); window[closeCallback] = function() { Microgroove.windowManager.close(); }; Microgroove.windowManager.open(Capture.urlHelpers.getOptinsUrl(o.token, window.location.href, '&callback=' + closeCallback + (typeof o.id === 'undefined' ? '' : '&id=' + o.id)), "OptinsWindow"); } } else { $this.append(getSigninIframe(settings)); } }; settings.callback = callbackFunction; getSigninIframe(settings).appendTo(this); }); }, checkSSO: function(options) { var timeout, callbackFunction, url, iframe; var settings = { timeout: 10000 }; if ( options ) { $.extend( settings, options ); } callbackFunction = Capture.urlHelpers.generateUniqueId(); window[callbackFunction] = function(o) { if (o) { $(document.body).trigger('login', o); if (typeof Backplane !== 'undefined') { Backplane.expectMessagesWithin(60, ['identity/login']); } Capture.iframeHelper.remove(iframe); clearTimeout(timeout); if (o.showOptIns) { closeCallback = Capture.urlHelpers.generateUniqueId(); window[closeCallback] = function() { Microgroove.windowManager.close(); }; Microgroove.windowManager.open(Capture.urlHelpers.getOptinsUrl(o.token, window.location.href, '&callback=' + closeCallback + (typeof o.id === 'undefined' ? '' : '&id=' + o.id)), "OptinsWindow"); } } else { $(document.body).trigger('logout'); } }; url = 'http://www.capitolrecords.com/wp-content/plugins/umg-sso/capture_sso_iframe.php?callback=' + Capture.urlHelpers.urlEncode(callbackFunction); if (typeof Backplane !== 'undefined') { url += '&bp_channel=' + Backplane.getChannelID(); } iframe = Capture.iframeHelper.create({src: url, width: 0, height: 0, resize: false}).appendTo(document.body); timeout = setTimeout(function() { Capture.iframeHelper.remove(iframe); }, settings.timeout); return this; }, signoutLink: function(options) { var settings = { 'returnUrl': document.location.href }; return this.each(function() { if ( options ) { $.extend( settings, options ); } var $this = $(this); $this.attr('href', 'javascript:void(0);').bind("click", function () { Microgroove.helpers.killCookie('__hLoggedIn', '/'); Microgroove.helpers.killCookie('__hDetails', '/'); Microgroove.helpers.killCookie('_CToken', '/'); if ( typeof Backplane != 'undefined' ) { Backplane.resetCookieChannel(); } if ( (typeof JANRAIN != 'undefined') && (typeof JANRAIN.SSO != 'undefined') ) { JANRAIN.SSO.CAPTURE.logout({ sso_server: 'https://umg-dev.janrainsso.com', logout_uri: settings.returnUrl }); } else { Microgroove.helpers.redirectLocation(settings.returnUrl); } }); }); }, optinsLink: function(options) { var settings = { 'returnUrl': document.location.href }; return this.each(function() { if ( options ) { $.extend( settings, options ); } var closeCallback = Capture.urlHelpers.generateUniqueId(); window[closeCallback] = function() { Microgroove.windowManager.close(); }; var $this = $(this); var optinsUrl = Capture.urlHelpers.getOptinsRedirectUrl(settings.returnUrl, 'callback=' + closeCallback); $this.attr('href', 'javascript:void(0);').bind("click", function () { Microgroove.windowManager.open(optinsUrl, "OptinsWindow"); }); }); }, mobileOptinsLink: function(options) { var settings = { 'returnUrl': document.location.href }; return this.each(function() { if ( options ) { jQuery.extend( settings, options ); } var $this = jQuery(this); var optinsUrl = Capture.urlHelpers.getOptinsRedirectUrl(settings.returnUrl); $this.attr('href', 'javascript:void(0);').bind("click", function () { var packedReturnUrl = "?returnUrl=" + Capture.urlHelpers.packUrl(settings.returnUrl); window.location = "http://www.capitolrecords.com/wp-content/plugins/umg-sso/mobile_optins.php" + packedReturnUrl; }); }); }, editProfileLink: function(options) { var settings = { 'page': '/capture.aspx' }; return this.each(function() { if ( options ) { $.extend( settings, options ); } var $this = $(this); $this.attr('href', 'javascript:void(0);').bind("click", function () { Microgroove.helpers.redirectLocation(settings.page); }); }); }, mobileEditProfileLink: function(options) { var settings = { 'page': '/capture.aspx' }; return this.each(function() { if ( options ) { jQuery.extend( settings, options ); } var $this = jQuery(this); $this.attr('href', 'javascript:void(0);').bind("click", function () { var origin = "?origin=" + Capture.urlHelpers.urlEncode(document.location.href); window.location = "http://www.capitolrecords.com/wp-content/plugins/umg-sso/mobile_profile.php" + origin; }); }); }, editProfileForm: function(options) { var settings = { 'callback': 'Capture.windowHelpers.onEditProfileComplete', 'width': 580, 'height': 400, 'transparent': true, 'id': 'capture_edit_profile' }; return this.each(function() { if ( options ) { $.extend( settings, options ); } var $this = $(this); $.getJSON(Capture.urlHelpers.getAccessTokenUrl(), function(data) { if ( data.statusCode === 200 ) { settings.src = Capture.urlHelpers.getEditProfileUrl(data.accessToken, settings.callback); $this.append(Capture.iframeHelper.create(settings)); } else { alert(data.message); } }); }); }, editAccountForm: function(options) { var settings = { 'hiddenFields': [ 'usernameinput', 'fnameinput', 'lnameinput', 'birthdateinput', 'cityinput', 'statedropdown' ], 'optionalFields': [ 'genderradio', 'zipinput', 'countrydropdown' ], 'optionalFieldSelectors': [ 'span#mg-genderradioid input[checked="checked"]', 'span#mg-zipinputid input', 'span#mg-countrydropdownid select', ] }; return this.each(function() { if ( options ) { $.extend( settings, options ); } $.each(settings.hiddenFields, function(index, value) { $("span#mg-" + value + "id").hide(); }); $.each(settings.optionalFieldSelectors, function(index, value) { if ( $(value).val() ) { $("span#mg-" + settings.optionalFields[index] + "id").hide(); } }); if ((typeof Microgroove.state !== "undefined") && Microgroove.state === "post") { var updateUrl = Capture.urlHelpers.getUpdateUrl(document.location.href, Capture.urlHelpers.updateDirection.memberToUser); $.get(updateUrl); }; }); } }; $.fn.enableCapture = function(method) { Capture.windowHelpers.init(); if ( methods[method] ) { return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); } else if ( typeof method === 'object' || ! method ) { return methods.init.apply( this, arguments ); } else { $.error( 'Method ' + method + ' does not exist on jQuery.enableCapture' ); } }; })( jQuery );