var $chrome = {
    /******* private fields *********/
    
    _version: 2.0,
	_target:'nbaChromeLogin',
    _onBodyDoneTimerId: null,
    _user: {},
    
    
    /******* private methods ********/
    _onBodyDone: function(){
        $chrome._init();
    },
    _init: function(){
        var chr;
        if (document.getElementById(this._target)) {
            chr = document.getElementById(this._target);
        }		
        
        if (document.removeEventListener) { 
            document.removeEventListener('DOMContentLoaded', this._onBodyDone, false);
        }
        if (this._onBodyDoneTimerId) {
            clearInterval(this._onBodyDoneTimerId);
        }
    },
    _getUrl: function(){
        var url = window.location.href.match(/\.element\/ssi\/global\/2\.0\/chrome\.html/) ? '' : '?url=' + encodeURIComponent(window.location);
        return url;
    },		
	
    _getHTML: function(){
        var out = '';
        out += '      <div class="Action">' + (this.isUserAuthenticated() ? ('<a href="' + this.getMSIB() + '/flow/logout' + this._getUrl() + '" target="_top">Logout</a>') : ('<a href="' + (this.getSite() == 'nba.com' ? this.getNBABaseUrl() : this.getWNBABaseUrl()) + '/allaccess/about.html" target="_top">Get News &amp; Offers</a>')) + '</div>';		
        out += '      <div class="User">' + (this.isUserAuthenticated() ? ('<a href="' + this.getMSIB() + '/flow/editUser' + this._getUrl() + '" target="_top">' + (this.getUserDisplayName()) + '</a>') : ('<a href="' + this.getMSIB() + '/flow/login' + this._getUrl() + '" target="_top">Login</a>')) + '</div>';
        return out;
    },
    		
		
	
    getCDN: function(){
        var server = window.location.hostname;
        var secure = window.location.protocol.match(/https/i);
        if (server.match(/webdev|payp1|sasd/i)) {
            return 'http://nba-webdev-preview.nba.com';
        }
        else 
            if (server.match(/ref/i) && !secure) {
                return 'http://nba-ref-preview.nba.com';
            }
            else 
                if (secure) {
                    return 'https://s.cnn.net/nba/nba';
                }
                else {
                    return 'http://i.cdn.turner.com/nba/nba';
                }
    },
    getSite: function(){
        var server = window.location.hostname;
        if (server.match(/\.wnba.com$/i)) {
            return 'wnba.com';
        }
        else {
            return 'nba.com';
        }
    },
    getMSIB: function(){
        var server = window.location.hostname;

        //Adding in option for WNBA vs. NBA
        if (server.match(/wnba\.com/i)) {
	        if (server.match(/webdev|payp1/i)) {
	            return 'http://payp1dev1.turner.com:1085/services/msib';
	        }
	        else 
	            if (server.match(/ref|ite/)) {
	                return 'https://aud-ite.wnba.com/services/msib';
	            }
	            else {
	                return 'https://audience.wnba.com/services/msib';
	            }
        } else {
	        if (server.match(/webdev|payp1/i)) {
	            return 'http://payp1dev1.turner.com:1085/services/msib';
	        }
	        else 
	            if (server.match(/ref|ite/)) {
	                return 'https://aud-ite.nba.com/services/msib';
	            }
	            else {
	                return 'https://audience.nba.com/services/msib';
	            }
        }
    },
    getNBABaseUrl: function(){
        var server = window.location.hostname;
        if (server.match(/webdev|payp1/i)) {
            return 'http://nba-webdev-preview.turner.com';
        }
        else 
            if (server.match(/ref|ite/)) {
                return 'http://nba-ref-preview.nba.com';
            }
            else 
                if (server.match(/qa/)) {
                    return 'http://nba-qa-preview.nba.com';
                }
                else {
                    return 'http://www.nba.com';
                }
    },
    getWNBABaseUrl: function(){
        var server = window.location.hostname;
        if (server.match(/webdev|payp1/i)) {
            return 'http://wnba-webdev-preview.turner.com';
        }
        else 
            if (server.match(/ref|ite/)) {
                return 'http://wnba-ref-preview.wnba.com';
            }
            else 
                if (server.match(/beta/)) {
                    return 'http://beta.wnba.com';
                }
                else {
                    return 'http://www.wnba.com';
                }
    },
    img: function(src, alt, attributes){
        alt = typeof(alt) != 'undefined' ? alt : '';
        attributes = typeof(attributes) != 'undefined' ? attributes : '';
        return '<img src="' + this.getCDN() + src + '" alt="' + alt + '" ' + attributes + '/>';
    },
    isUserAuthenticated: function(){
        return this._user.TSid && this._user.TSid.match(/^G[\da-f]+-\d+-\d+-\d+$/) && this._user.authid && this._user.displayName ? true : false;
    },
    isUserSecure: function(){
        return this.isUserAuthenticated() && this._user.authpass ? true : false;
    },
    getUserDisplayName: function(){
        return this.isUserAuthenticated() ? this._user.displayName : '';
    },
    // initializer
    init: function(){
       
        var a, c;
        c = document.cookie || '';
        a = c.split('; ');
        for (var i = 0; i < a.length; i++) {
            var k, v;
            try {
                k = a[i].split('=')[0] || null;
                v = a[i].split('=')[1] || null;
                // Check for errantly encoded values in these cookies and decode them
                if (k && k == 'authid') {
                    if (v != decodeURI(v)) {
                        var cookie = k + '=' + decodeURI(v) + '; ' +
                        'expires=Tue, 23 Oct 2012 04:00:00 GMT; path=/; ' +
                        'domain=.' +
                        this.getSite();
                        document.cookie = cookie;
                    }
                }
                if (k && (k == 'TSid' || k == 'authid' || k == 'authpass')) {
                    this._user[k] = v;
                }
                if (k && k == 'AA-Member') {
                    var u = decodeURIComponent(v).split("|");
                    if (u.length >= 3) {
                        this._user.displayName = u[0];
                        this._user.gender = u[1];
                        this._user.age = u[2];
                        this._user.status = u[3];
                    }
                }
                if (k && k == 's_vi') {
                    var svi = encodeURIComponent(v);
                    if (svi && svi[1]) {
                        var exp = new Date();
                        exp.setTime(exp.getTime() + 3600000);
                        var cookie = 'iv_s=' + svi + '; ' +
                        'expires=' +
                        exp.toGMTString() +
                        '; path=/; ' +
                        'domain=.' +
                        this.getSite();
                        document.cookie = cookie;
                    }
                }
            } 
            catch (e) {
            }
        }
        
        if (document.addEventListener) {
            document.addEventListener('DOMContentLoaded', this._onBodyDone, false);
        }
        this._onBodyDoneTimerId = setInterval(function(){
            if (/loaded|complete/.test(document.readyState)) {
                $chrome._onBodyDone();
            }
        }, 50);
    }
};

$chrome.init();


