﻿var nflpa_loginBar = {
    arrow: '<span id="loginTabArrow" style="background:url(https://images.nflplayers.com/mediaResources/loginBar/pixelArrows.gif)' +
    ' no-repeat scroll left center transparent; display: inline-block; position: relative; left: 8px; width: 5px; height: 10px;"></span>',
    _getSite: function () {
        var host = window.location.hostname.toLowerCase();
        var siteList = ['.nfllockout.', 'web2.nflplayers.com', 'secure.nflplayers.com', '.nflplayers.com', 'localhost'];
        var isInList = false;
        for (var x = 0; x < siteList.length; x++) {
            if (host.match(siteList[x]) == siteList[x])
                isInList = true;
        }
        if (isInList)
            nflpa_loginBar.site = 'https://www.nflplayers.com';
        else
            nflpa_loginBar.site = '';
    },
    site: '',
    _loginBar_reload: false,
    _loggingOut: false,
    _userInfo: { fname: '', lname: '', roles: '' },
    mobile: (navigator.userAgent.toLowerCase().indexOf('iphone') != -1 || navigator.userAgent.toLowerCase().indexOf('ipad') != -1),
    _init: function () {
        nflpa_loginBar._getSite();
        if (jQuery('#nflpa_barWrap').length == 0)
            nflpa_loginBar._buildBase();
        jQuery('#nflpaSitesButton').hover(function () {
            jQuery(this).hide();
            jQuery('#nflpaSitesList').show();
        }, function () {
        });
        jQuery('#nflpaSitesList').hover(function () { }, function () {
            jQuery('#nflpaSitesButton').show();
            jQuery('#nflpaSitesList').hide();
        });
        jQuery('.nflpaSiteLinks a').hover(function () {
            jQuery(this).animate({ paddingLeft: 10 }, "fast");
        }, function () {
            jQuery(this).animate({ paddingLeft: 0 }, "fast");
        });
    },
    _buildBase: function () {
        var width = '100%';
        if (navigator.userAgent.toLowerCase().indexOf('ipad') != -1)
            width = '1109px';
        jQuery('body').prepend('<style type="text/css">#loginMessage{height:23px;width:30px;}#nflpa_loginBarDS{display:none;width:' + width + ';position:absolute;z-index:332;top:0px;height:43px;background:transparent url(https://images.nflplayers.com/mediaResources/LoginBar/barDS.png) repeat-x;}#nflpa_barWrap{width:' + width + ';background:transparent url(https://images.nflplayers.com/mediaResources/loginBar/bartopbg.png) repeat-x top;position:absolute;height:85px;top:-42px;font-family:Arial,helvetica,Verdana;z-index:333;}.left{float:left;}.hidden{display:none;}.red{color:#c30000;}#nflpaSitesButton{background-image:url(https://images.nflplayers.com/mediaResources/loginBar/sites_normal.png);background-repeat:no-repeat;width:65px;height:24px;margin-top:6px;margin-left:6px;cursor:pointer;}.nflpaSitesButtonHover{background-image:url(https://images.nflplayers.com/mediaResources/loginBar/sites_hover.png)!important;}#nflpaSitesList{display:none;margin-top:4px;margin-left:4px;}#nflpaSitesListTop{background-image:url(https://images.nflplayers.com/mediaResources/loginBar/sites_list_top.png);background-repeat:no-repeat;width:162px;height:28px;}#nflpaSitesContentWrap{background-image:url(https://images.nflplayers.com/mediaResources/loginBar/sites_list_middle.png);background-repeat:repeat-y;}#nflpaSitesContent{width:150px;padding-left:6px;font-size:10px;}#nflpaSitesListBottom{background-image:url(https://images.nflplayers.com/mediaResources/loginBar/sites_list_bottom.png);background-repeat:no-repeat;width:162px;height:8px;}.nflpaSiteLinks{padding:3px 5px;}.nflpaSiteLinks a{text-decoration:none;color:#777;font-weight:bold;display:block;}.nflpaSiteLinks a:hover{color:#333;}.nflpaSitesBreak{height:1px;width:100%;background:transparent url(https://images.nflplayers.com/mediaResources/loginBar/sites_list_break.png) no-repeat;}#nflpaBarLoginForm{float:right;width:450px;position:absolute;top:9px;right:10px;}#nflpaBarLoginForm input{background:none repeat scroll 0 0 transparent;border:medium none;color:#333;font-size:12px;font-weight:bold;margin-left:30px;position:relative;top:3px;width:135px;}.nflpaUsernameWrap{width:166px;height:23px;background:url(https://images.nflplayers.com/mediaResources/loginBar/username_normal.png) no-repeat;}.nflpaPasswordWrap{margin-left:10px;width:166px;height:23px;background:url(https://images.nflplayers.com/mediaResources/loginBar/password_normal.png) no-repeat;}#nflpaLoginButton{cursor:pointer;background:url(https://images.nflplayers.com/mediaResources/loginBar/loginButton_sprite.png) no-repeat;width:62px;height:23px;margin-left:10px;}#nflpaAccessTabs{float:right;position:relative;top:42px;overflow:hidden;height:39px;padding-right:10px;}.nflpaDropTab{position:relative;top:-45px;z-index:88888;}#nflpaAccessTab{height:39px;position:relative;top:-12px;cursor:pointer;}.tabMiddle a{position:relative;top:15px;color:#777;text-decoration:none;padding:0 13px;}.tabMiddle a:hover{position:relative;top:15px;color:#333;text-decoration:none;}.tabLeft{width:9px;background:url(https://images.nflplayers.com/mediaResources/loginBar/TabLeft.png) no-repeat bottom;position:absolute;left:-9px;top:0;height:39px;}.tabMiddle{background:url(https://images.nflplayers.com/mediaResources/loginBar/TabMiddle.png) repeat-x bottom;font-size:10px;font-weight:bold;margin-left:9px;margin-right:9px;text-align:center;height:39px;position:relative;}#nflpaAccess{padding-top:16px;}.tabRight{background:url(https://images.nflplayers.com/mediaResources/loginBar/TabRight.png) no-repeat bottom;position:absolute;right:-9px;top:0;width:9px;height:39px;}#nflpaLoginFrame{position:absolute;top:-1000px;left:-1000px;}</style>');
        var baseHTML = jQuery('<div id="nflpa_barWrap" class="nflpa_loginBar_dropPanel" style="top:-85px;"><div style="width:960px;position:relative; left:50%;margin-left:-480px;"> ' +
            '        <div class="left" style="padding:3px;">' +
            '            <img src="https://images.nflplayers.com/mediaResources/loginBar/BarLogo.png" id="nflpaBarLogo" />' +
            '        </div>' +
         /*   '        <div class="left" id="sitesDDL"  style="padding-top:4px;">' +
            '            <div id="nflpaSitesButton">' +
            '            </div>' +
            '            <div id="nflpaSitesList">' +
            '                <div id="nflpaSitesListTop">' +
            '                </div>' +
            '                <div id="nflpaSitesContentWrap">' +
            '                    <div id="nflpaSitesContent">' +
            '                        <div class="nflpaSiteLinks">' +
            '                            <a href="https://www.nflplayers.com">NFL<span class="red">PLAYERS</span></a>' +
            '                        </div>' +
            '                        <div class="nflpaSitesBreak">' +
            '                        </div>' +
        /* Lockout  '                <div class="nflpaSiteLinks">' +
            '                            <a href="https://www.nfllockout.com">NFL<span class="red">LOCKOUT</span></a>' +
            '                        </div>' +
            '                        <div class="nflpaSitesBreak">' +
            '                        </div>' +
            '                        <div class="nflpaSiteLinks">' +
            '                            <a href="https://www.nflpatv.com">NFLPA<span class="red">TV</span></a>' +
            '                        </div>' +
            '                  </div>' +
            '                </div>' +
            '                <div id="nflpaSitesListBottom">' +
            '                </div>' +
            '            </div>' +
            '        </div>' +
      */      '        <div id="nflpaBarLoginForm">' +
            '        </div>' +
            '        <div id="nflpaAccessTabs" style="top:85px;">' +
            '            <div id="nflpaAccessTabWrap">' +
            '            </div>' +
            '        </div>' +
            '        <div id="nflpaLoginFrame">' +
            '            <iframe id="lf" src="https://www.nflplayers.com/blank.html" onload="nflpa_loginBar._checkLogin()">' +
            '            </iframe>' +
            '        </div>' +
            '    </div></div><div id="nflpa_loginBarDS" class="nflpa_loginBar_dropPanel"></div>');
        jQuery('body').prepend(baseHTML);
    },

    _buildForm: function () {
        jQuery('#nflpaBarLoginForm').html('<div class="left" id="loginMessage"> </div><div class="nflpaUsernameWrap left">' +
                    '<input type="text" id="nflpaUsername" /></div>' +
                    '<div class="nflpaPasswordWrap left">' +
                        '<input id="nflpaPassword" type="password" /></div>' +
                    '<div id="nflpaLoginButton" class="left">' +
                    '</div>');
    },
    _buildAlerter: function () {
        //build messenger here:
        jQuery('#nflpaBarLoginForm').html('<div class="left" id="nflpa_messageBox"></div>');
    },
    _getAlerts: function () {
        var msgbox = $('#nflpa_messageBox');
    },
    _setTabs_anon: function () {
        jQuery('#nflpaAccessTabWrap').html('<div id="nflpaForgotTab" class="nflpaDropTab left">' +
                '                    <div class="tabMiddle">' +
                '                        <div class="tabLeft">' +
                '                        </div>' +
                '                        <a href="https://www.nflplayers.com/register/reset-password/">Reset Password</a>' +
                '                        <div class="tabRight">' +
                '                        </div>' +
                '                    </div>' +
                '                </div>' +
                '                <div id="nflpaRegisterTab" class="nflpaDropTab left">' +
                '                    <div class="tabMiddle">' +
                '                        <div class="tabLeft">' +
                '                        </div>' +
                '                        <a href="https://www.nflplayers.com/register/">Register Now</a>' +
                '                        <div class="tabRight">' +
                '                        </div>' +
                '                    </div>' +
                '                </div>' +
                '                <div id="nflpaAccessTab" class="left">' +
                '                    <div class="tabMiddle">' +
                '                        <div class="tabLeft">' +
                '                        </div>' +
                '                        <a href="javascript:nflpa_loginBar._void();">Login / Register' + nflpa_loginBar.arrow + '</a>' +
                '                        <div class="tabRight">' +
                '                        </div>' +
                '                    </div>' +
                '                </div>');
        nflpa_loginBar._buildForm();
        nflpa_loginBar._setAccessTab(false);
        jQuery('#nflpaLoginButton').hover(function () {
            jQuery(this).css('background-position', 'center');
        }, function () {
            jQuery(this).css('background-position', 'top');
        }).mousedown(function () {
            jQuery(this).css('background-position', 'bottom');
        }).mouseup(function () {
            jQuery(this).css('background-position', 'center');
            nflpa_loginBar._doLogin();
        });

        jQuery('#nflpaUsername').focus(function () {
            jQuery(this).parent().css('background-image', 'url(https://images.nflplayers.com/mediaResources/loginBar/username_focus.png)');
        }).blur(function () {
            jQuery(this).parent().css('background-image', 'url(https://images.nflplayers.com/mediaResources/loginBar/username_normal.png)');
        }).keydown(function (event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                nflpa_loginBar._doLogin();
            }
        });
        jQuery('#nflpaPassword').focus(function () {
            jQuery(this).parent().css('background-image', 'url(https://images.nflplayers.com/mediaResources/loginBar/password_focus.png)');
        }).blur(function () {
            jQuery(this).parent().css('background-image', 'url(https://images.nflplayers.com/mediaResources/loginBar/password_normal.png)');
        }).keydown(function (event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                nflpa_loginBar._doLogin();
            }
        });
    },

    _setTabs_loggedIn: function () {
        jQuery('#nflpaAccessTabWrap').html('<div id="nflpaForgotTab" class="nflpaDropTab left">' +
                '                    <div class="tabMiddle">' +
                '                        <div class="tabLeft">' +
                '                        </div>' +
                '                        <a href="javascript:nflpa_loginBar._doLogout();">Logout</a>' +
                '                        <div class="tabRight">' +
                '                        </div>' +
                '                    </div>' +
                '                </div>' +
                '                <div id="nflpaAccessTab" class="left">' +
                '                    <div class="tabMiddle">' +
                '                        <div class="tabLeft">' +
                '                        </div>' +
                '                        <a href="javascript:nflpa_loginBar._void();">Welcome <span id="nflpaLoggedInName">' + nflpa_loginBar._userInfo.fname + ' ' +
                                            nflpa_loginBar._userInfo.lname + '</span>' + nflpa_loginBar.arrow + '</a>' +
                '                        <div class="tabRight">' +
                '                        </div>' +
                '                    </div>' +
                '                </div>');

        nflpa_loginBar._setAccessTab(true);
    },
    _void: function () { },
    _setAccessTab: function (loggedIn) {
        var wrap = jQuery('#nflpa_barWrap');
        var ds = jQuery('#nflpa_loginBarDS');
        var tabs = jQuery('#nflpaAccessTabs');
        jQuery('.nflpaDropTab').hover(function () {
            jQuery(this).animate({ top: -9 }, 'fast');
        }, function () {
            jQuery(this).animate({ top: -12 }, 'fast');
        });
        jQuery('#nflpaAccessTab').hover(function () {
            jQuery(this).animate({ top: -9 }, 'fast');
            if (readCookie('nflpaTabDown') == 0) {
                //tabs.css('top', 42);
                //wrap.css('top', -42);
            }
        }, function () {
            //tabs.css('top', 85);
            //wrap.css('top', -85);
            if (readCookie('nflpaTabDown') == 0)
                jQuery(this).animate({ top: -12 }, 'fast');
        }).click(function () {
            if (readCookie('nflpaTabDown') != 1) {
                //lower panel//
                tabs.css('top', 42);
                wrap.css('top', -42);
                setCookie('nflpaTabDown', 1);
                if (!loggedIn)
                    jQuery('#nflpaUsername').focus();
                jQuery('#loginTabArrow').css('background-position', 'right');
                if (jQuery.browser.msie)
                    ds.show();
                ds.stop().fadeIn('fast', function () {
                    wrap.stop().animate({ top: 0 }, 'fast', function () {
                        jQuery('.nflpaDropTab').animate({ top: -12 }, 'fast');
                    });
                    ds.animate({ top: 42 }, 'fast');
                });
            }
            else {
                //raise panel
                setCookie('nflpaTabDown', 0);
                jQuery('#loginTabArrow').css('background-position', 'left');
                jQuery('.nflpaDropTab').animate({ top: -12 }, 'fast');
                wrap.stop().animate({ top: -42 }, 'fast', function () {
                    jQuery('.nflpaDropTab').animate({ top: -42 }, 'fast');
                    tabs.css('top', 85);
                    wrap.css('top', -85);
                });
                ds.stop().animate({ top: 0 }, 'fast', function () {
                    if (jQuery.browser.msie)
                        ds.hide();
                    else
                        ds.fadeOut('fast');
                });
            }
        });
        if (readCookie('nflpaTabDown') == 1) {
            wrap.css({ top: 0 });
            tabs.css('top', 42);
            ds.css({ top: 42 }).show();
            jQuery('#nflpaAccessTab').css('top', -9);
            jQuery('.nflpaDropTab').css('top', -12);
            jQuery('#loginTabArrow').css('background-position', 'right');
        }
        else
            $('#nflpa_loginBarDS').hide();
    },
    _doLogin: function () {
        var u = Base64.encode(jQuery('#nflpaUsername').val());
        var p = Base64.encode(jQuery('#nflpaPassword').val());
        var lf = jQuery('#lf');
        var time = (new Date()).getTime();
        var src = nflpa_loginBar.site + "/services/auth.aspx?";
        nflpa_loginBar._loginBar_reload = true;
        jQuery.getJSON(nflpa_loginBar.site + "/services/auth.aspx?op=doLogin-ajax&u=" + u + "&p=" + p + "&callback=?&time=" + time, function (json) {
            if (json.login == 'success') {
                setCookie('nflpaTabDown', 0);
                window.location = src + 'u=' + u + '&p=' + p + '&op=doLogin-remote&time=' + time + '&referringUrl=' + Base64.encode(window.location.href);
            }
            else
                jQuery('#loginMessage').html('<img src="https://images.nflplayers.com/mediaResources/loginBar/x.png" style="padding-top:6px;padding-left:10px;" />');
        });
    },
    WaitForIFrame: function () {
        iframe = document.getElementById("lf");
        if (iframe.readyState != "complete") {
            setTimeout("nflpa_loginBar.WaitForIFrame()", 200);
        } else {
            nflpa_loginBar._loginBar_reload = true;
            nflpa_loginBar._loginBar_checkLogin();
        }
    },
    _doLogout: function () {
        var lf = jQuery('#lf');
        var time = (new Date()).getTime();
        lf.attr('src', nflpa_loginBar.site + "/services/auth.aspx?op=logout-remote&time=" + time);
        nflpa_loginBar._loginBar_reload = true;
        nflpa_loginBar._loggingOut = true;
        nflpa_loginBar.WaitForIFrame();
    },
    _checkLogin: function () {
        var time = (new Date()).getTime();
        if (nflpa_loginBar._loginBar_reload)
            jQuery('#loginMessage').html('<img src="https://images.nflplayers.com/mediaResources/loginBar/loader.gif"" />');
        jQuery.getJSON(nflpa_loginBar.site + "/services/auth.aspx?op=checkLogin&callback=?&time=" + time,
                 function (d) {
                     if (d.authenticated == 1) {
                         nflpa_loginBar._userInfo.fname = d.fname;
                         nflpa_loginBar._userInfo.lname = d.lname;
                         nflpa_loginBar._userInfo.roles = d.roles;
                         setCookie("nflpaLoggedIn", 1);
                         setCookie("nflpaRoles", d.roles);
                         setCookie("nflpaName", d.fname + " " + d.lname, 365);
                         setCookie("userRole", d.userRole);
                         if (!nflpa_loginBar._loginBar_reload) {
                             //Show Dashboard links or maybe something else??
                             nflpa_loginBar._setTabs_loggedIn();
                         }
                         else {
                             window.location.reload();
                         }
                     }
                     else {
                         eraseCookie("nflpaLoggedIn");
                         eraseCookie("nflpaRoles");
                         //eraseCookie("nflpaName");
                         eraseCookie("userRole");
                         if (!nflpa_loginBar._loginBar_reload) {
                             nflpa_loginBar._setTabs_anon();
                             //nflpa_loginBar._init();
                         }
                         else {
                             if (nflpa_loginBar.mobile) {
                                 window.location = (jQuery('#lf').attr('src'));
                             }
                             if (!nflpa_loginBar._loggingOut)
                                 jQuery('#loginMessage').html('<img src="https://images.nflplayers.com/mediaResources/loginBar/x.png" style="padding-top:6px;padding-left:10px; />');
                             else {
                                 window.location.reload();
                             }
                         }
                     }
                 });
    }

};

var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        var breakAfter = 32;
        input = Base64._utf8_encode(input);
        myy = 0;
        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
            myy = myy + 4;
            if (myy % breakAfter == 0)
                output = output + "\n";
        }

        return output;
    },

    // public method for decoding
    decode: function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        var breakAfter = 10;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
function setCookie(name, value, days) {
    if (days && days != 0) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else if (days == 0) {
        var expires = "; expires=";
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    setCookie(name, "", -1);
}
var jQueryScriptOutputted = false;
function initJQuery() {
    if (typeof (jQuery) == 'undefined') {
        if (!jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
            var script = document.createElement('script')
            script.setAttribute("type", "text/javascript")
            script.setAttribute("src", "https://www.nflplayers.com/JS/jquery.js")
            if (typeof script != "undefined")
                document.getElementsByTagName("head")[0].appendChild(script)
        }
        setTimeout("initJQuery()", 50);
    } else {
        jQuery(function () {
            nflpa_loginBar._init(); // Initialize bar
        });
    }

}
//if (window.location.hostname.toLowerCase() == 'web2.nflplayers.com' || window.location.hostname.toLowerCase() == 'gmt-w7rc1')
initJQuery();
