var $chrome = {

    /******* private fields *********/
    
    _version: 1.41,
    _target: 'nbaChromeNav',
    _onBodyDoneTimerId: null,
    _images: {},
    _user: {},
    
    
    /******* private methods ********/
    
    _onBodyDone: function(){
        $chrome._init();
    },
    _init: function(){
        var chr;
        if (document.getElementById(this._target)) {
            chr = document.getElementById(this._target);
        }
        else {
            chr = document.createElement('div');
            var body = document.getElementsByTagName('body')[0];
            body.insertBefore(chr, body.childNodes[0]);
        }
        chr.innerHTML = this._getHTML();
        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 id="nbaChrome"><div id="nbaChromeContent"><ul>';
		out += '<li><a href="http://www.nba.com">NBA</a></li>';
        out += '<li class="nbaChromeTeams"><a href="' + this.getNBABaseUrl() + '/teams" target="_top" title="TEAMS">' + '<strong>TEAMS</strong>' + '</a>';
		out += '<ul><li><strong>TEAM SITES</strong><ul>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/hawks" target="_top" title="Atlanta">' + 'Atlanta' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/celtics" target="_top" title="Boston">' + 'Boston' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/bobcats" target="_top" title="Charlotte">' + 'Charlotte' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/bulls" target="_top" title="Chicago">' + 'Chicago' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/cavaliers" target="_top" title="Cleveland">' + 'Cleveland' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/mavericks" target="_top" title="Dallas">' + 'Dallas' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/nuggets" target="_top" title="Denver">' + 'Denver' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/pistons" target="_top" title="Detroit">' + 'Detroit' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/warriors" target="_top" title="Golden State">' + 'Golden State' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/rockets" target="_top" title="Houston">' + 'Houston' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/pacers" target="_top" title="Indiana">' + 'Indiana' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/clippers" target="_top" title="LA Clippers">' + 'LA Clippers' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/lakers" target="_top" title="LA Lakers">' + 'LA Lakers' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/grizzlies" target="_top" title="Memphis">' + 'Memphis' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/heat" target="_top" title="Miami">' + 'Miami' + '</a>' + '</li>';
        out += '</ul>' + '<ul>';			
		out += '<li><a href="' + this.getNBABaseUrl() + '/bucks" target="_top" title="Milwaukee">' + 'Milwaukee' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/timberwolves" target="_top" title="Minnesota">' + 'Minnesota' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/nets" target="_top" title="New Jersey">' + 'New Jersey' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/hornets" target="_top" title="New Orleans">' + 'New Orleans' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/knicks" target="_top" title="New York">' + 'New York' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/thunder" target="_top" title="Oklahoma City">' + 'Oklahoma City' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/magic" target="_top" title="Orlando">' + 'Orlando' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/sixers" target="_top" title="Philadelphia">' + 'Philadelphia' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/suns" target="_top" title="Phoenix">' + 'Phoenix' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/blazers" target="_top" title="Portland">' + 'Portland' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/kings" target="_top" title="Sacramento">' + 'Sacramento' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/spurs" target="_top" title="San Antonio">' + 'San Antonio' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/raptors" target="_top" title="Toronto">' + 'Toronto' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/jazz" target="_top" title="Utah">' + 'Utah' + '</a>' + '</li>';
		out += '<li><a href="' + this.getNBABaseUrl() + '/wizards" target="_top" title="Washington">' + 'Washington' + '</a>' + '</li>';		
        out += '</ul>' + '</li>' + '</ul>' + '</li>';		
        out += '<li><a href="' + this.getNBABaseUrl() + '/tickets" target="_top" title="TICKETS">' + 'TICKETS' + '</a>' + '</li>';
        out += '<li><a href="' + this.getNBABaseUrl() + '/global" target="_top" title="GLOBAL">' + 'GLOBAL' + '</a>' + '</li>';
        out += '<li><a href="' + this.getNBABaseUrl() + '/dleague" target="_top" title="NBA D-LEAGUE">' + 'NBA D-LEAGUE' + '</a>' + '</li>';		
        out += '<li><a href="' + this.getWNBABaseUrl() + '" target="_top" title="WNBA.com">' + 'WNBA' + '</a>' + '</li>'; 
        out += '<li><a href="' + this.getNBABaseUrl() + '/enebea" target="_top" title="ENE-BE-A">' + 'ENE-BE-A' + '</a>' + '</li>';
		out += '</ul>';
        out += '<div id="nbaChromeSearch">';
        out += '      <form style="nbaSrchForm" method="get" action="' + (this.getSite() == 'nba.com' ? this.getNBABaseUrl() : this.getWNBABaseUrl()) + '/search" target="_top">';
        out += '        <input type="text" name="text" class="Text" />';
        out += '        <input type="submit" class="nbaSearchButton" value="" />';
        out += '        <input type="hidden" name="hl" value="en" />';
        out += '      </form>';
        out += '    </div>';		
        out += '    <div id="nbaChromeLogin">';
        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() ? (this.getUserDisplayName()) : ('<a href="' + this.getMSIB() + '/flow/login' + this._getUrl() + '" target="_top">Login</a>')) + '</div>';
        out += '    </div>';
        out += '  </div>';
        out += '</div>';
        return out;
    },
    
    
    /******** public fields *********/
    
    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(/beta/)) {
                    return 'http://beta.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 : '';
    },
    /*
     * url = destination
     * n = window name
     * w = width
     * r = resizable
     * h = height
     * t = toolbars (includes address, location, etc.)
     * s = status
     * sc = scroll
     */
    popup: function(url, n, r, w, h, t, s, sc){
        var opts = "";
        opts = "width=" + w + ",height=" + h;
        opts += ",resizable=" + r;
        opts += ",location=" + t + ",toolbars=" + t + ",menubar=" + t + ",titlebar=" + t;
        opts += ",status=" + s;
        opts += ",scrollbars=" + sc;
        var popup = window.open(url, n, opts);
        popup.focus();
        return popup;
    },
    
    
    // initializer
    init: function(){
        if (document.createStyleSheet) {
            document.createStyleSheet(this.getCDN() + '/.element/css/2.0/global/nbaGlobalNav.css');
        }
        else {
            var ss = document.createElement('link');
            ss.rel = 'stylesheet';
            ss.href = this.getCDN() + '/.element/css/2.0/global/nbaGlobalNav.css';
            var head = document.getElementsByTagName('head')[0];
            head.insertBefore(ss, head.childNodes[0]);
        }
        
        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();

