var $nbaNav = {
	
    /******* private fields *********/

    _target: 'nbaGlobalNav',
    _onBodyDoneTimerId: null,


    /******* private methods ********/

	_onBodyDone: function() {
		$nbaNav._init();
	},
    _init: function(){
        var nav;
        if (document.getElementById(this._target)) {
            nav = document.getElementById(this._target);
        }
        else {
            nav = document.createElement('div');
            nav.id = this._target;
            var body = document.getElementsByTagName('body')[0];
            body.insertBefore(nav, body.childNodes[0]);
        }
        nav.innerHTML = this._getHTML();
        this._hookHovers();
        if (document.removeEventListener) {
            document.removeEventListener('DOMContentLoaded', this._onBodyDone, false);
        }
        if (this._onBodyDoneTimerId) {
            clearInterval(this._onBodyDoneTimerId);
        }
        if (this._onCssTimerId) {
            clearInterval(this._onCssTimerId);
        }
    },
    _hookHovers: function(){
        var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function(){
                this.className = (this.className ? this.className + " " : "") + "sfhover";
            }
            sfEls[i].onmouseout = function(){
                this.className = this.className.replace(/\s*?\bsfhover\b/, "");
            }
        }
    },
    _getHTML: function(){
        var out = '';
        out += '<div id="nbaHeaderNav"><div id="nbaHeaderNavContain"><ul id="nav">';
        for (var t = 0; t < this._nav.length; t++) {
            var tab = this._nav[t];
			var url = (tab.url.indexOf("http") == 0 ? "" : "http://www.nba.com") + tab.url;
            out += '<li><a id="' + tab.id + '" href="' + url + '">'+ tab.id + '</a>';
            if (tab.items && tab.items.length > 0) {
                out += '<ul' + (tab.style ? ' style="' + tab.style + '"' : '') + '>';
                for (var li = 0; li < tab.items.length; li++) {
                    if (tab.items[li].label) {
                        var url = (tab.items[li].url.indexOf("http") == 0 ? "" : "http://www.nba.com") + tab.items[li].url;
                        out += '<li><div class="nbaMiddleSubLi"><a href="' + url + '"' + (tab.items[li].style ? ' style="' + tab.items[li].style + '"' : '') + '>' + tab.items[li].label + '</a></div></li>';
                    }
                    else 
                        if (tab.items[li].items) {
                            var item = tab.items[li];
                            out += '<li><div class="' + (item.clazz || '') + '">';
                            for (var i = 0; i < item.items.length; i++) {
                                var url = (item.items[i].url.indexOf("http") == 0 ? "" : "http://www.nba.com") + item.items[i].url;
                                out += '<a href="' + url + '" class="' + (item.items[i].clazz || '') + '">' + item.items[i].label + '</a>';
                            }
                            out += (item.post || '') + '</div></li>';
                        }
                }
                out += '</ul></li>';
            }
        }
        out += '</ul>'+'</div>'+'</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';
                }
    },
    img: function(src, alt, attributes){
        alt = typeof(alt) != 'undefined' ? alt : '';
        attributes = typeof(attributes) != 'undefined' ? attributes : '';
        return '<img src="' + this.getCDN() + src + '" alt="' + alt + '" ' + attributes + '/>';
    },

    
    init: function(){
        for (i = 0; i < document.styleSheets.length; i++) {
            var css = document.styleSheets[i];
        }
        if (!this._cssDone && document.createStyleSheet) {
            document.createStyleSheet(this.getCDN() + '/.element/css/2.0/global/nbaGlobalNav.css');
        }
        else 
            if (!this._cssDone) {
                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]);
            }
        if (document.addEventListener) {
            document.addEventListener('DOMContentLoaded', this._onBodyDone, false);
        }
        this._onBodyDoneTimerId = setInterval(function(){
            if (/loaded|complete/.test(document.readyState)) {
                $nbaNav._onBodyDone();
            }
        }, 50);
    },
    
    
    /******** navigation *********/
    
    _nav: [{
        id: "NEWS",
        url: "/news/index.html",
        items: [{
            label: "News",
            url: "/news/index.html"
        }, {
            label: "News Archive",
            url: "/news/news_archive.html"
        }, {
            label: "Transactions",
            url: "/transactions/"
        }, {
            label: "NBA.com Personalities",
            url: "/personalities/"
        }, {		
            label: "Referee Assignments",
            url: "/news/referee.html"
        }, {		
			label: "Blogs",
            url: "http://my.nba.com/go/browse/blogs"
        }, {
            label: "2009 Playoffs",
            url: "/finals2009/"
        }, {
            label: "Newsletters",
            url: "/allaccess/newsletters.html"
        }]
    }, {	
        id: "SCORES &amp; SCHEDULES",
        url: "/gameline/"
    }, {
       id: "VIDEO",
        url: "/video/index.html",
        items: [{
            label: "Highlights",
            url: "/video/highlights"
        }, {	
            label: "Top Plays",
            url: "/video/topplays"
        }, {
            label: "Most Recent",
            url: "/video"
        }, {		
            label: "NBA TV",
            url: "/video/nbatv"
        }, {
            label: "TNT OT",
            url: "/video/tntot"
        }, {		
            label: "Editor's Picks",
            url: "/video/featured"		
        }, {
            label: "Video Rulebook",
            url: "/video/"		
        }, {		
			label: "2009 Playoffs",
            url: "/video/playoffs"
        }, {
            label: "2009 Draft",
            url: "/video/draft"
        }]
    }, {	
        id: "PLAYERS",
        url: "/players/index.html",
        items: [{
            label: "Players",
            url: "/players/index.html"
        }, {
            label: "T-Mobile Rookie Report",
            url: "/rookies"			
        }, {
			label: "Coaches",
            url: "/coaches"
        }, {				
            label: "Team Rosters",
            url: "/teams/teamIndividualLinks.html?title=Team%20Roster&file=roster"
        }, {
            label: "Historical Player Search",
            url: "/historical/search/index.jsp"
        }, {
            label: "International Players",
            url: "/players/int_players_0809.html"
        }, {			
            label: "Player Blogs",
            url: "http://my.nba.com/category.jspa?categoryID=5800000012"
        }]
    }, {	
        id: "STANDINGS",
        url: "/standings/team_record_comparison/conferenceNew_Std_Cnf.html",
        items: [{
            label: "By Division",
            url: "/standings/team_record_comparison/conferenceNew_Std_Div.html"
        }, {
            label: "By Conference",
            url: "/standings/team_record_comparison/conferenceNew_Std_Cnf.html"
        }, {
            label: "Team Record Comparison",
            url: "/standings/team_record_comparison/conferenceNew_Stk_Cnf.html"
        }, {
            label: "Streaks and Last 10",
            url: "/standings/team_record_comparison/conferenceNew_Stk_Div.html"
        }, {
            label: "Ahead and Behind",
            url: "/standings/team_record_comparison/conferenceNew_Ahd_Div.html"
        }, {
            label: "Margin and Statistics",
            url: "/standings/team_record_comparison/conferenceNew_Mrg_Div.html"
		}, {	
            label: "2008-09 Standings",
            url: "/standings/2008/team_record_comparison/conferenceNew_Std_Div.html"
        }, {
            label: "2007-08 Standings",
            url: "/standings/2007/team_record_comparison/conferenceNew_Std_Div.html"
        }]
    }, {
        id: "STATISTICS",
        url: "/statistics/",
        items: [{
            label: "Daily Leaders",
            url: "/statistics/"
        }, {
            label: "Individual Stats",
            url: "/statistics/index.html"
        }, {
            label: "Team Stats",
            url: "/statistics/sortable_team_statistics/sortable1.html"
        }, {
            label: "+/- Stats",
            url: "/statistics/plusminus/plusminus.jsp"
        }, {		
            label: "Team-by-Team",
            url: "/teams/teamStatisticsLinks.html"
        }, {
            label: "Sortable Team Stats",
            url: "/statistics/sortable_team_statistics/sortable1.html"
        }, {
            label: "Team-by-Team Splits",
            url: "/teams/teamSplitsLinks.html"
        }, {		
            label: "International",
            url: "/statistics/international/interPlayersStatsAVG.html"
        }, {
            label: "Milestones",
            url: "/teams/milestones.html"
        }, {			
            label: "2008-09 Leaders",
            url: "/statistics/player/Scoring.jsp?league=00&season=22008&conf=OVERALL&position=0&splitType=9&splitScope=GAME&qualified=N&yearsExp=-1&splitDD="
        }, {
            label: "2009 Playoff Stats",
            url: "/statistics/player/Scoring.jsp?league=00&season=42008&conf=OVERALL&position=0&splitType=9&splitScope=GAME&qualified=N&yearsExp=-1&splitDD="
        }]
    }, {
        id: "FANTASY",
        url: "/fantasy/",
        items: [{
			label: "ESPN NBA.com Fantasy Basketball",
			url: "http://games.espn.go.com/fba/welcome?ex_cid=2009_fbaxx_xxx_nba_fan"
		}, {
			label: "NBA Stock Exchange",
			url: "http://nbase.nba.com"
		},{
			label: "NBA Pick One Challenge",
			url: "http://fantasy.nba.com/pick_one"
		}, {		
			label: "News",
            url: "/fantasy/fantasy_playernews.jsp"
        }, {
            label: "Injuries",
            url: "/fantasy/fantasy_playerinjury.jsp"
        }, {
            label: "Blogs",
            url: "http://my.nba.com/go/browse/blogs/?tag=Fantasy%20Blogs"
        }, {
            label: "Forums",
            url: "http://my.nba.com/go/forum/viewcategory/81021/Fantasy_Forums"
        }]		
    }, {
        id: "FAN STUFF",
        url: "#",
        items: [{
            label: "Fan Voice",
            url: "http://my.nba.com"
        }, {
            label: "All Access",
            url: "/allaccess/"
        }, {
            label: "NBA Twitter",
            url: "http://twitter.com/nba"
        }, {	
            label: "NBA on Facebook",
            url: "http://www.facebook.com/nba"
        }, {
            label: "Amazing Playlist",
            url: "/amazing/"
        }, {  	
            label: "NBA Game Downloads",
            url: "/downloads/"
        }, {
            label: "Online Games",
            url: "/games/"
        }, {
            label: "NBA Live 10",
            url: "/videogames/nbalive10_overview.html"
        }, {
            label: "Contests",
            url: "/special_offers/index.html"
        }, {
            label: "NBA Avatars",
            url: "http://www.urfooz.com/NBA"
        }, {
            label: "NBA Kids",
            url: "/kids/"			
        }]
    }, {	
        id: "PHOTOS",
        url: "/photos/index.html",
        items: [{
            label: "Photo Galleries",
            url: "/photos/index.html"
        }, {
            label: "Buy Photos",
            url: "/photostore/"
        }]
    }, {
        id: "MOBILE",
        url: "/mobile/",
        items: [{
            label: "Alerts",
            url: "/mobile/alerts.html"
        }, {
            label: "NBA.com WAP",
            url: "/mobile/nbacom.html"
        }, {
        	label: "Game Time",
            url: "/mobile/gametime/index.html"
		}]
    }, {
		id: "TV",
        url: "/nba_tv/index.html",
        items: [{
            label: "NBA TV",
            url: "/nbatv/index.html"
        }, {
            label: "NBA TV Schedule",
            url: "/nbatv/schedule/"
        }, {
            label: "Canadian TV Schedule",
            url: "/schedules/national_tv_schedule/canada/"
        }, {
            label: "International TV",
            url: "/schedules/international_nba_tv_schedule.html"
        }, {		
			label: "TNT Overtime",
			url: "/tntovertime"
		}, {
            label: "NBA LEAGUE PASS",
            url: "/leaguepass/index.html"
        }]
    }, {
        id: "LEAGUE PASS",
        url: "/leaguepass/index.html",
        items: [{
            label: "NBA LEAGUE PASS",
            url: "/leaguepass/index.html"
        }, {
            label: "NBA LEAGUE PASS Broadband",
            url: "/leaguepass/online.html?cid=nba46"
        }, {
            label: "International LEAGUE PASS Broadband",
            url: "http://ilp.nba.com"
        }, {
            label: "Audio LEAGUE PASS",
            url: "/allaccess/watchListen.html"			
        }]
    }, {
        id: "STORE",
        url: "/shop/",
        style: "background: url(http://i.cdn.turner.com/nba/nba/.element/img/2.0/global/red.arrow.gif) no-repeat left center;color:#CD0720 !important;padding:3px 0px;padding-left:9px !important; "
    }]
}

$nbaNav.init();
