var CSB = {

	today_game_data_location : "/csb/today_game_data.jsp",
	standings_data_location : "/csb/standings_data.jsp",

	team : null,
	league_id : null,
	local_tv_first : null,
	is_canadian : null,
	gamedate : null,
	
	today_frame_holder : null,
	standings_frame_holder : null,
	
	constantUpdate : true,
	interval : 30000,
	
	updateInterval : null,

	init : function() {

		CSB.today_frame_holder = document.getElementById ? document.getElementById('today_frame_holder') : document.all['today_frame_holder'];
		CSB.standings_frame_holder = document.getElementById ? document.getElementById('standings_frame_holder') : document.all['standings_frame_holder'];
	
		if (CSB.today_frame_holder) { 
			CSB.loadTodayGame();
	
			if (CSB.constantUpdate) {
				CSB.updateInterval = setInterval(CSB.loadTodayGame, CSB.interval);
			}
		}
		
		if (CSB.standings_frame_holder) { CSB.loadStandings(); }
	},
	
	setTeam : function(team) {
		CSB.team = team;
	},
	
	setLeagueId : function(league_id) {
		CSB.league_id = league_id;
	},
		
	setTeamAndLeagueId : function(team, league_id) {
		CSB.team = team;
		CSB.league_id = league_id;
	},
	
	setLocalTvFirst : function(local_tv_first) {
		CSB.local_tv_first = local_tv_first;
	},
	
	setIsCanadian : function(is_canadian) {
		CSB.is_canadian = is_canadian;
	},
	
	setGamedate : function(gamedate) {
		CSB.gamedate = gamedate;
	},
	
	setRefreshInterval : function(seconds) {
		CSB.interval = seconds * 1000;
		
		clearInterval(CSB.updateInterval);
		if (CSB.constantUpdate) {
			CSB.updateInterval = setInterval("CSB.loadTodayGame()", CSB.interval);
		}
	},
	
	setConstantlyUpdating : function(constantlyUpdating) {
		CSB.constantUpdate = constantlyUpdating;
	},
	
	XMLHttpFactories : [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	],

	createXMLHTTPObject : function() {
		var xmlhttp = false;
		for (var i=0; i < CSB.XMLHttpFactories.length; i++) {
			try {
				xmlhttp = CSB.XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
			break;
		}
		return xmlhttp;
	},

	loadTodayGame : function() {
	
		if (CSB.today_game_data_location && CSB.team && CSB.league_id) {
			var http_request = CSB.createXMLHTTPObject();
			
			if (!http_request) return;

			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}

			var method = "GET";

			url = CSB.today_game_data_location + "?" + "teamcode=" + CSB.team + "&league_id=" + CSB.league_id;
			if (CSB.local_tv_first == true) { url += "&local_tv_first=true"; }
			if (CSB.is_canadian == true) { url += "&is_canadian=true"; }
			if (CSB.gamedate) { url += "&gamedate=" + CSB.gamedate; }

			http_request.open(method, url, true);
			http_request.setRequestHeader('User-Agent','XMLHTTP/1.0');

			http_request.onreadystatechange = function(thisHttpRequest) { return function() {
				if (thisHttpRequest.readyState != 4) { return false; }
				if (thisHttpRequest.status != 200) { return false; }

				result = thisHttpRequest.responseText;
				CSB.today_frame_holder.innerHTML = result;
				
				// clear request from memory if possible
				thisHttpRequest = null;
			}; }(http_request);
			if (http_request.readyState == 4) return;
			http_request.send(null);
		}

	},

	loadStandings : function() {
	
		if (CSB.standings_data_location && CSB.team && CSB.league_id) {
			var http_request = CSB.createXMLHTTPObject();
			
			if (!http_request) return;

			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}

			var method = "GET";

			url = CSB.standings_data_location + "?" + "teamcode=" + CSB.team + "&league_id=" + CSB.league_id;
			if (CSB.local_tv_first == true) { url += "&local_tv_first=true"; }
			if (CSB.is_canadian == true) { url += "&is_canadian=true"; }
			if (CSB.gamedate) { url += "&gamedate=" + CSB.gamedate; }

			http_request.open(method, url, true);
			http_request.setRequestHeader('User-Agent','XMLHTTP/1.0');

			http_request.onreadystatechange = function(thisHttpRequest) { return function() {
				if (thisHttpRequest.readyState != 4) { return false; }
				if (thisHttpRequest.status != 200) { return false; }

				result = thisHttpRequest.responseText;
				CSB.standings_frame_holder.innerHTML = result;
				
				// clear request from memory if possible
				thisHttpRequest = null;
			}; }(http_request);
			if (http_request.readyState == 4) return;
			http_request.send(null);
		}

	},
	
	openCourtSideLive : function(params) {
		var path, title, opts;
		
		path = '/csl/index.html?gamecode=' + params.gamecode;
		title = 'CourtsideLive';
		opts = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,screenX=12,screenY=12,top=12,left=12,copyhistory=0,width=760,height=643';

		var csl_window = window.open(path,title,opts);
	},
	
	openLiveAudio : function(params) {
		var path, title, opts;
		
		path = 'http:/\/\www.wnba.com/broadband/wnbaAudioSched.html';
		title = 'WNBALiveAudio';
		opts = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=413,height=310';

		var live_audio_window = window.open(path,title,opts);
	},
	
	openLiveVideo : function(params) {
		window.location = "/" + CSB.team + "/live.jsp?gamecode=" + params.gamecode;
	},
	
	openArchive : function(params) {
		CSB.openLiveVideo(params);
	},
	
	openHighlight : function(params) {
		if (params && params.highlight) {
			wnbaVideoShort(params.highlight);
		} else {
			wnbaVideoShort();
		}
	}
}