var nba_embed;
if (nba_embed === undefined) {
	nba_embed = {
		parseEmbedded: function() {
			var arr_scripts = document.getElementsByTagName("script");
			var ct = arr_scripts.length;
			
			for (var i=0; i < ct; i++) {
				var script = arr_scripts[i].src;
				if (/\/\.element\/js\/1\.1\/xmp\/module.js\?/.test(script)) {
					var script_id = arr_scripts[i].getAttribute('id');
					if (!script_id) {
						var seed = Math.random();
						var url = nba_embed.parseVars(script);
						var vid_div = 'nba_' + seed;
						document.write('<div id=' + vid_div + '>' + script + '</div>');
						nba_embed.embedVideo(vid_div, url);
	                    arr_scripts[i].setAttribute('id', 'nba_' + (seed+1));
					}
				}
			}
		},
		embedVideo: function(div_id, url) {
			var div = document.getElementById(div_id);
			var vid_id = url.vid; 
			var loc_id = url.loc;
			var onsite_re = /nba\.com$/i;
			if (onsite_re.test(location.host)) {
				if (/^\//.test(location.pathname)) {
					var index = 1;
				} else {
					var index = 0;
				}
				var first_path = location.pathname.split('/')[index];
				if (typeof(nba_embed.teamSites[first_path]) != 'undefined') {
					var iframe_src = 'http://www.nba.com/video/savp1.1/exmp_onsite.html?loc=team&team=' + first_path + '&vid=' + vid_id;
				} else {
					var iframe_src = 'http://www.nba.com/video/savp1.1/exmp_onsite.html?loc=onsite&vid=' + vid_id;
				}

				div.innerHTML = '<iframe id="_nba_iframe_' + Math.random() + '" src="' + iframe_src + '" height="350" width="388" allowtransparency="true" frameborder="0" scrolling="no"></iframe>';
			} else {
				// strip www from domain
				var domain_parsed=window.location.hostname.replace(/^www\./,''); 
				var iframe_src = 'http://www.nba.com/video/savp1.1/exmp.html?loc=external&domain=' + domain_parsed + '&vid=' + vid_id;
				div.innerHTML = '<iframe id="_nba_iframe_' + Math.random() + '" src="' + iframe_src + '" height="393" width="388" allowtransparency="true" frameborder="0" scrolling="no"></iframe>';
			}
		},
		parseVars: function(url) {
			var turl = url;
			turl = turl.slice(turl.indexOf('?')+1);
			var ar = turl.split('&');
			var b = {vid:'undefined', loc:'dom'};
			for (var i = 0; i < ar.length; i++) {
				var c = ar[i].split('=');
				if (c[0].indexOf('vid') > -1) {
					b['vid'] = c[1];
				} else if (c[0].indexOf('loc') > -1) {
					b['loc'] = c[1];
				}
			}
			return b;
		},
		teamSites: {
			hawks: 'Atlanta Hawks',
			celtics: 'Boston Celtics',
			bobcats: 'Charlotte Bobcats',
			bulls: 'Chicago Bulls',
			cavaliers: 'Cleveland Cavaliers',
			mavericks: 'Dallas Mavericks',
			nuggets: 'Denver Nuggets',
			pistons: 'Detroit Pistons',
			warriors: 'Golden State Warriors',
			rockets: 'Houston Rockets',
			pacers: 'Indiana Pacers',
			clippers: 'Los Angeles Clippers',
			lakers: 'Los Angeles Lakers',
			grizzlies: 'Memphis Grizzlies',
			heat: 'Miami Heat',
			bucks: 'Milwaukee Bucks',
			timberwolves: 'Minnesota Timberwolves',
			nets: 'New Jersey Nets',
			hornets: 'New Orleans Hornets',
			knicks: 'New York Knicks',
			thunder: 'Oklahoma City Thunder',
			magic: 'Orlando Magic',
			sixers: 'Philadelphia 76ers',
			suns: 'Phoenix Suns',
			blazers: 'Portland Trail Blazers',
			kings: 'Sacramento Kings',
			spurs: 'San Antonio Spurs',
			raptors: 'Toronto Raptors',
			jazz: 'Utah Jazz',
			wizards: 'Washington Wizards'
		}
	};
}
nba_embed.parseEmbedded();
