﻿(function ($) {
    cl = carrot.utils.listener;

    cl.listen("tsso_goto_registration_page", function (token, returnUrl) {
        if (token || returnUrl) {
            var tokenParam = token ? ['tst=', token].join('') : '';
            var returnUrlParam = returnUrl
                                    ? ['returnUrl=', returnUrl].join('')
                                    : ($(document).data('events')['open_registration'] == undefined) //use this method to see if this is in Contest page
                                        ? ''
                                        : ['returnUrl=', encodeURIComponent((typeof g_Click_To_Enter !== "undefined" && g_Click_To_Enter) && (document.location.toString().indexOf("RBX") < 0)
                                                            ? [document.location.toString(), '&RBX=1'].join('')
                                                            : document.location.toString())].join('');

            window.location = ['/asp2/register.aspx', '?', tokenParam ? tokenParam : '', (tokenParam && returnUrlParam) ? '&' : '', returnUrlParam ? returnUrlParam : ''].join('');
        }
        else {
        	var returnUrlParam = ($(document).data('events')['open_registration'] == undefined) //use this method to see if this is in Contest page
																	? ''
                                  : ['returnUrl=', encodeURIComponent((typeof g_Click_To_Enter !== "undefined" && g_Click_To_Enter) && (document.location.toString().indexOf("RBX") < 0)
																		? [document.location.toString(), '&RBX=1'].join('')
                                    : document.location.toString())].join('');

        	window.location = ['/asp2/register.aspx', '?', returnUrlParam ? returnUrlParam : ''].join('');
        }
    });

    cl.listen("tsso_email_found", function (token, bPwdRecorverd) {
        //When popup fancybox, it will clean the g_Click_To_Enter value, so in order to keep the value not lost, we save the value before we pop the new fancybox (it will close the existing fancybox)
        //and then reassign later.
        if (typeof g_Click_To_Enter !== "undefined") {
            var tempCTE = g_Click_To_Enter;
        }


        //need to popup account merging popup in email_found mode
        $.fancybox({
            'width': 730,
            'height': 449,
            'padding': 0,
            'scrolling': 'no',
            'hideOnOverlayClick': false,
            'titleShow': false,
            'transitionIn': 'none',
            'type': 'iframe',
            'enableEscapeButton': false,
            'showCloseButton': false,
            'href': ['/tsso/TSSOAccountAuthentication.aspx?tst=', token, bPwdRecorverd ? '&pr=true' : '', '&d=', new Date().getTime()].join('')
        });

        if (typeof g_Click_To_Enter !== "undefined" && typeof tempCTE !== "undefined") {
            g_Click_To_Enter = tempCTE
        }
    });

    cl.listen("tsso_social_auth_succ", function (token) {
        if (typeof g_Click_To_Enter !== "undefined") {
            var tempCTE = g_Click_To_Enter;
        }

        $.fancybox({
            'width': 730,
            'height': 449,
            'padding': 0,
            'scrolling': 'no',
            'hideOnOverlayClick': false,
            'titleShow': false,
            'transitionIn': 'none',
            'type': 'iframe',
            'enableEscapeButton': false,
            'showCloseButton': false,
            'href': ['/tsso/SocialAccountAuthenticated.aspx?tst=', token, '&d=', new Date().getTime()].join('')
        });

        if (typeof g_Click_To_Enter !== "undefined" && typeof tempCTE !== "undefined") {
            g_Click_To_Enter = tempCTE
        }
    });

    cl.listen("tsso_merge_succ", function (token) {
        $.fancybox({
            'width': 730,
            'height': 449,
            'padding': 0,
            'scrolling': 'no',
            'hideOnOverlayClick': false,
            'titleShow': false,
            'transitionIn': 'none',
            'type': 'iframe',
            'enableEscapeButton': false,
            'showCloseButton': false,
            'href': '/tsso/TSSOAccountMerged.aspx'
        });
    });

    cl.listen("tsso_forgot_password", function (token) {
        $.fancybox({
            'width': 730,
            'height': 449,
            'padding': 0,
            'scrolling': 'no',
            'hideOnOverlayClick': false,
            'titleShow': false,
            'transitionIn': 'none',
            'type': 'iframe',
            'enableEscapeButton': false,
            'showCloseButton': false,
            'href': ['/tsso/TSSOForgetPassword.aspx?tst=', token, '&d=', new Date().getTime()].join('')
        });
		});

})(jQuery);
