/***************************************************
 	Calendar Script
	* calendar picker
	- Last update: 9/28/2007, CT

	SET GLOBAL VARIABLES OUTSIDE THIS JS
	1. seasonYearWNBA  ->  calendarScheduleGlobals.js
	2. seasonStartMonth  ->  calendarScheduleGlobals.js
	3. cal_navigator  -> not used...anymore
	4. cal_data  ->  returned JSON
	5. calendar_site  ->  index.html
***************************************************/

var team_cal = {

	mo_31: new Array(),

	mo_30: new Array(),

	max_days: 0,

	m_start: new Object(),

	nav: new Object(),

	passVar: new Object(),

	loadCounter: 0,

	temp_id: new Array(),

	today: new Date(),

	cur_brd_id: null,

	brd_info: new Object(),
	
	buildCal: function(mo, yr) {

		this.loadCounter++;

		if (this.loadCounter==1) {

			this.checkForVars();
			this.loadInc('show');
			try {
				var xmlhttpJ = this.xmlHttpRequest();
				var current_Season_Year= (new Date()).getFullYear();
				//use jsp for previous seasons
				if (seasonYearWNBA<current_Season_Year)
				{
				  var useJSP = '/scores/calendarJSON_WNBA.jsp?team=' + calendar_site + '&season=' + seasonYearWNBA + '&leagueid=' + leagueid;
				}
				else
				{
				//replace with flat file json for current season
                var useJSP = '/' + calendar_site + '/schedule/schedule.json';   
                }
				//var useJSP = '/esi-test/wayne/sun.js';
				if (xmlhttpJ){

					xmlhttpJ.open("GET",useJSP,false);
					xmlhttpJ.send(null);
					if (xmlhttpJ.readyState==4) {
						cal_data = eval('(' + xmlhttpJ.responseText + ')');

						/* all star */
						//cal_data["2"].push({ "date" : "02/15/2008", "isHome" : "0", "gType" : "F", "gTypeDesc" : "REG", "poRnd" : "0", "poNec" : "0", "isWin" : "", "hmePts" : "0", "oppPts" : "0", "gTime" : "9:00pm ET", "gCode" : "20080416/DETCLE", "wins":"0", "loss":"0", "oppCode":"allstar2008", "oppCity":"2008 All Star Weekend", "oppAbr":"2008 All-Star", "tix":"", "nattv":'TNT', "loctv":"", "locradio":"", "arena":""});

						//cal_data["2"].push({ "date" : "02/17/2008", "isHome" : "0", "gType" : "F", "gTypeDesc" : "REG", "poRnd" : "0", "poNec" : "0", "isWin" : "", "hmePts" : "0", "oppPts" : "0", "gTime" : "8:00pm ET", "gCode" : "20080217/ESTWST", "wins":"0", "loss":"0", "oppCode":"allstar2008", "oppCity":"2008 All Star Weekend", "oppAbr":"2008 All-Star", "tix":"", "nattv":'TNT', "loctv":"", "locradio":"", "arena":""});

						//cal_data["2"].sort(function(a,b){
						 //	var x = a.date;
    					//	var y = b.date;
    					//	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
						//})
					}

				}
			}
			catch (e) {
				alert("An error has occured while loading data. Please refresh the page.: " + e); return;
			}

			this.loadInc('hide');

			this.passVar["view"] = "calendar";
			this.mo_31 = [1,3,5,7,8,10,12];
			this.mo_30 = [2,4,6,9,11];
			this.nav["year"] = (this.today).getFullYear();

			/* set month from latest month from team */
			mo = parseInt(cal_data["maxM"]);

			/* reset dropdowns */
			document.tixselection.byteam.options[1].selected=true;
			document.tixselection.byday.options[1].selected=true;

			document.tixselection2.byteam.options[1].selected=true;
			document.tixselection2.byday.options[1].selected=true;

			/* if playoff months exist */
			thisID = helper.htmlElement("mLinks");
			thisID.style.display = 'block';

			var str ='';
			var monthform_select = document.getElementById("monthform_select");

			// Force the DOM to be loaded before attempting to modify
			//   it and potentially crashing the browser
			document.body.innerHTML;
			try{

				if (cal_data["4"] && cal_data["4"].length>0) {

					str = '<span id="mLink_4"><a href="javascript:team_cal.buildCal(4,seasonYearWNBA);">April</a> |&nbsp;</span> '


					var newOption = document.createElement("option");
					var monthText = document.createTextNode("April");
					newOption.setAttribute("value", "4");
					newOption.appendChild(monthText);
					//Insert before 2nd childNode because of the "Monthly Schedule" option
					monthform_select.insertBefore(newOption, monthform_select.childNodes[2]);
				}

			}
			catch (e) {}

			str += '<span id="mLink_5"><a href="javascript:team_cal.buildCal(5,seasonYearWNBA);">May</a> |&nbsp;</span> <span id="mLink_6"><a href="javascript:team_cal.buildCal(6,seasonYearWNBA);">June</a> |&nbsp;</span> <span id="mLink_7"><a href="javascript:team_cal.buildCal(7,seasonYearWNBA);">July</a> |&nbsp;</span> <span id="mLink_8"><a href="javascript:team_cal.buildCal(8,seasonYearWNBA);">August</a> &nbsp;</span>';

			try {
				if (cal_data["9"] && cal_data["9"].length>0) {
					str += '<span id="mLink_9">| <a href="javascript:team_cal.buildCal(9,seasonYearWNBA);">September</a>&nbsp;</span> ';

					var newOption = document.createElement("option");
					var monthText = document.createTextNode("September");
					newOption.setAttribute("value", "9");
					newOption.appendChild(monthText);
					monthform_select.appendChild(newOption);
				}
			}
			catch (e) { alert(e); }

			try {
				if (cal_data["10"] && cal_data["10"].length>0) {
					str += '<span id="mLink_10">| <a href="javascript:team_cal.buildCal(10,seasonYearWNBA);">October</a>&nbsp;</span> ';

					var newOption = document.createElement("option");
					var monthText = document.createTextNode("October");
					newOption.setAttribute("value", "10");
					newOption.appendChild(monthText);
					monthform_select.appendChild(newOption);
				}
			}
			catch (e) { alert(e); }

			helper.checkElementExist(thisID, str, "innerHTML");
			thisID = helper.htmlElement("mLinksBottom");
			helper.checkElementExist(thisID, str, "innerHTML");

			/*
			thisID = helper.htmlElement("seasonText");
			helper.checkElementExist(thisID, this.passVar["seasonText"] , "innerHTML");
			*/

		}

		/* always set the fullview to calendar */
		this.passVar["fullview"] = "calendar";

		if (yr== null || yr==undefined) {

			this.nav["month"] = (mo==null || mo==undefined)?this.checkCalLoc():mo;


			switch (this.nav["month"]) {

				case (0) : {
					this.nav["month"] = 12;
					(this.nav["year"])--;
					break;
				}

				case (13) : {
					this.nav["month"] = 1;
					(this.nav["year"])++;
					break;
				}

			}

		}
		else {

			if (mo!=null || mo!=undefined) {
				//this.nav["month"] = (cal_data[mo] != null && cal_data[mo].length>0)?mo:5;
				this.nav["month"] = mo;
			}

			this.nav["year"] = yr;

		}

		thisID = helper.htmlElement("dispMonth");
		thisID.style.display = 'block';
		helper.checkElementExist(thisID, this.calMonthStr(this.nav["month"]), "innerHTML");

		if (this.passVar["view"] == "calendar") {

			thisID = helper.htmlElement("calendarview");
			thisID.style.display = 'block';
			thisID = helper.htmlElement("listview");
			thisID.style.display = 'none';


			/* DEFAULT TO LIST VIEW */
			if (this.loadCounter==1)
				this.fullViewSelect('list');
			else
				this.buildCalView();
		}
		else {

			thisID = helper.htmlElement("calendarview");
			thisID.style.display = 'none';
			thisID = helper.htmlElement("listview");
			thisID.style.display = 'block';

			if (this.passVar["view"]=="all") {
				this.buildListViewAll();
			}
			else {
				this.buildListView();
			}
		}

	},

	buildCalView: function() {

		this.clearCal();

		/*
			cal_navigator should be a global variable set within the page to enable ability for prev/next
			id to use : previousbutton, nextbutton - disabled, not used right now
		*/

		/*
		if (cal_navigator) {

			var showprev = true;
			var shownext = true;


			switch (parseInt(this.nav["year"])) {

				case (parseInt(seasonYearWNBA)) : {
					showprev = (parseInt(this.nav["month"]) < 11)?false:true; break;
				}

				case ((parseInt(seasonYearWNBA))+1) : {
					shownext = (parseInt(this.nav["month"]) > 5)?false:true; break;
				}

			}

			thisID = helper.htmlElement("previousbutton");
			thisID.style.display = 'block';
			if (!showprev) thisID.style.display = 'none';

			thisID = helper.htmlElement("nextbutton");
			thisID.style.display = 'block';
			if (!shownext) thisID.style.display = 'none';

		}
		*/

		var start_date = this.nav["month"] + '/01/' + this.nav["year"];
		var st_date = new Date(start_date);

		this.m_start["month"] = st_date.getMonth()+1;
		this.m_start["day"] = st_date.getDay();

		this.max_days = (this.m_start["month"]==2)?(((this.nav["year"])%4==0)?29:28):(this.checkMDays31(this.m_start["month"])?31:30);

		var dy = 0;

		for (wk = 1; wk <= 6; wk++) {

			if (wk==1) {

				for (x = this.m_start["day"]; x < 7; x++) {
					dy++;

					thisID = helper.htmlElement("1_" + x);
					tmp_date = ((parseInt(this.nav["month"])<10)?"0":"") + this.nav["month"] + "/" + ((dy<10)?"0":"") + dy + "/" + this.nav["year"];
					this.temp_id[dy-1] = ["1_" + x, tmp_date];
					thisID.id = tmp_date;

					str = '<div class="sup">' + dy + '&nbsp;</sup>';
					helper.checkElementExist(thisID, str, "innerHTML");

				}

			}
			else {

				for (x = 0; x < 7; x++) {
					dy++;

					if (dy <= this.max_days) {

						thisID = helper.htmlElement(wk + "_" + x);
						tmp_date = ((parseInt(this.nav["month"])<10)?"0":"") + this.nav["month"] + "/" + ((dy<10)?"0":"") + dy + "/" + this.nav["year"];
						this.temp_id[dy-1] = [wk + "_" + x, tmp_date];
						thisID.id = tmp_date;

						str = '<div class="sup">' + dy + '&nbsp;</sup>';
						helper.checkElementExist(thisID, str, "innerHTML");

					}

				}

			}


		}


		try {
			/* populate */
			if (cal_data[this.nav["month"]].length>0)
				this.selGames(this.nav["month"]);
		}
		catch (e) {
				return;
		}

	},

	fullViewCalendar: function() {
		/* set month from latest month from team */
		mo = parseInt(cal_data["maxM"]);
		this.buildCal(mo);
	},

	fullViewSelect: function(s) {

		/* hide brd window */
		thisID = helper.htmlElement("brdWindow");
		thisID.style.display = 'none';

		if (this.passVar["fullview"] != s || this.passVar["fullview"]==undefined) {

			if (s=="calendar") {
				this.passVar["fullview"] = "calendar";
				this.fullViewCalendar();
			}
			else {
				thisID = helper.htmlElement("dispMonth");
				thisID.style.display = 'none';
				this.passVar["fullview"] = "list";
				this.fullViewList();
			}

		}

	},

	buildListView: function() {

		var str = "";
		var mData = new Object();
		var hasData = false;

		calendar_site = cal_data.team;
		mData = cal_data[this.nav["month"]];
		var status = false;

		try {
			status = (mData.length>0)?true:false;
		}
		catch (e) { }

		if (status) {

			if (mData.length>0) {

				var cntr = 0;
				for (x = 0; x < mData.length; x++) {

					isPast = (mData[x].gType=="P")?true:false;
					isArena = (mData[x].arena!="")?true:false;

					if (!isPast) {

						if (!hasData) hasData = true;

						cntr++;

						if (cntr==1) {
							str += '<table class="listTable" border=1 cellpadding=0 cellspacing=0 width="100%" bordercolor="'+ listViewBorderColor + '">';
							str += '<tr class="listTableTitle"><td>' + this.calMonthStr(this.nav["month"]) + '</td><td>Opponent</td><td>Time</td><td>Local TV</td><td>Nat TV</td><td>Radio</td>';
						}

						isHome = (mData[x].isHome=="1")?true:false;
						isTix = (mData[x].tix != "away" && mData[x].tix != "notix" && (mData[x].tix).length>1)?true:false;
						isNatBrd = (mData[x].nattv!="")?true:false;
						isLocBrd = (mData[x].loctv!="")?true:false;
						isRadio = (mData[x].locradio!="")?true:false;

						isTravel = (mData[x].travel!="nolinks" && mData[x].travel!=null)?true:false;
						if(isTravel && ((calendar_site == 'cavaliers')||(calendar_site=='nets')||(calendar_site=='knicks')||(calendar_site=='grizzlies')))
						{
								travel="&nbsp;<a href='"+mData[x].travel+"' target='_blank'><img class='travel_icon' src='/media/"+calendar_site+"/travel_icon.gif' alt='"+mData[x].travelAirline+" Airlines' title='"+mData[x].travelAirline+" Airlines' border=0></a>";

								if(mData[x].travelPixel.length > 5 && mData[x].travelPixel!='nolinks')
									travel += "<img src = '"+mData[x].travelPixel+"' border=0>";
						}
						else
							travel="";

						//tixlink = (isTix)?'<a href="' + mData[x].tix +'" target="_blank"><img src="/media/' + calendar_site + '/ticket_icon.gif" border=0 title="BUY TICKET">':'&nbsp;' + '</a>'+link;
						tixlink = (isTix)?'<a href="' + mData[x].tix +'" target="_blank"><img src="/media/' + calendar_site + '/ticket_icon.gif" border=0 title="BUY TICKET">':'&nbsp;' + '</a>';
						natTV = (isNatBrd)?(this.checkNatTV(mData[x].nattv)):'';
						if (mData[x].is_webcast) {
							natTV = this.liveAccessLink(mData[x].gCode, cal_data.team) + " " + natTV;
						}

						str += '<tr>';
						str += '<td class="lt_date">&nbsp;' + this.listDate(mData[x].date) + '</d>';
						str += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ' ' + (((mData[x].oppCity).length>1)?mData[x].oppCity:mData[x].oppAbr) + ' ' + tixlink + travel + ((isArena)?('<br>' + mData[x].arena):'') + '</td>';
						//str += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ' ' + (((mData[x].oppCity).length>1)?mData[x].oppCity:mData[x].oppAbr) + ' ' + tixlink + travel + '</td>';
						str += '<td class="lt_time">&nbsp;' + mData[x].gTime + '</td>';
						str += '<td class="lt_localtv">&nbsp;' + mData[x].loctv + '</td>';
						str += '<td class="lt_nattv">&nbsp;' + natTV +'</td>';
						str += '<td class="lt_radio">&nbsp;' + mData[x].locradio + '</td>';
						str += '</tr>';

					}

				}

				if (cntr > 0)
					str += '</table>';

				thisID = helper.htmlElement("listviewContent");
				helper.checkElementExist(thisID, str, "innerHTML");

			}

			if (!hasData) {
				str = '<table class="listTable" border=1 width="100%" bordercolor="'+ listViewBorderColor + '"><tr><td class="nodata">NO UPCOMING GAMES</td></tr></table>';
				thisID = helper.htmlElement("listviewContent");
				helper.checkElementExist(thisID, str, "innerHTML");
			}

		}
		else {

			str = '<table class="listTable" border=1 width="100%" bordercolor="'+ listViewBorderColor + '"><tr><td class="nodata">NO UPCOMING GAMES</td></tr></table>';
			thisID = helper.htmlElement("listviewContent");
			helper.checkElementExist(thisID, str, "innerHTML");

		}



	},

	fullViewList: function() {

		var str = "";
		var pastStr = "";
		var futureStr = "";
		var pastM = "";
		var futureM = "";
		var mData = new Object();



		var tmpArr = ["4", "5", "6", "7", "8", "9", "10"];

		for (z = 0; z < tmpArr.length; z++) {

			try {

				if (cal_data[tmpArr[z]].length>0) {

					mData = cal_data[tmpArr[z]];
					fcntr = 0;
					pcntr = 0;



					for (x = 0; x < mData.length; x++) {

						var isHome = (mData[x].isHome=="1")?true:false;
						var isPast = (mData[x].gType=="P")?true:false;
						var isArena = (mData[x].arena!="")?true:false;

						if (!isPast) {

							fcntr++;
							isTix = (mData[x].tix != "away" && mData[x].tix != "notix" && (mData[x].tix).length>1)?true:false;
							isNatBrd = (mData[x].nattv!="")?true:false;
							isLocBrd = (mData[x].loctv!="")?true:false;
							isRadio = (mData[x].locradio!="")?true:false;

						isTravel = (mData[x].travel!="nolinks" && mData[x].travel!=null)?true:false;
						//if(isTravel && ((calendar_site == 'cavaliers')||(calendar_site=='nets')||(calendar_site=='knicks')||(calendar_site=='grizzlies')))
						if(isTravel)
						{
								travel="&nbsp;<a href='"+mData[x].travel+"' target='_blank'><img class='travel_icon' src='/media/"+calendar_site+"/travel_icon.gif' alt='"+mData[x].travelAirline+" Airlines' title='"+mData[x].travelAirline+" Airlines' border=0></a>";

								if(mData[x].travelPixel.length > 5 && mData[x].travelPixel!='nolinks')
									travel += "<img src = '"+mData[x].travelPixel+"' border=0>";
						}
						else
							travel="";


							tixlink = (isTix)?'<a href="' + mData[x].tix +'" target="_blank"><img src="/media/' + calendar_site + '/ticket_icon.gif" border=0 title="BUY TICKET">':'&nbsp;' + '</a>';
							natTV = (isNatBrd)?(this.checkNatTV(mData[x].nattv)):'';
							if (mData[x].is_webcast) {
								natTV = this.liveAccessLink(mData[x].gCode, cal_data.team) + " " + natTV;
							}


							futureM = tmpArr[z];
							if (futureM != tmpArr[z] || fcntr == 1) {
								futureStr += '<tr class="listTableTitle"><td>' + this.calMonthStr(tmpArr[z]) + '</td><td>Opponent</td><td>Time</td><td>Local TV</td><td>Nat TV</td><td>Radio</td></tr>';
							}

							/* customization for LA Lakers/Clippers */
							var oppStr = "";
							if ((mData[x].oppCity).length > 1)
								//oppStr = (mData[x].oppCode == "lakers")?"LA Lakers":((mData[x].oppCode == "clippers")?"LA Clippers":mData[x].oppCity);
								oppStr = mData[x].oppCity;
							else
								oppStr = mData[x].oppAbr;

							futureStr += '<tr>';
							futureStr += '<td class="lt_date">&nbsp;' + this.listDate(mData[x].date) + '</d>';
							futureStr += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ((mData[x].oppCode!="")?' <a href="/' + mData[x].oppCode + '/">':' ') + oppStr + '</a> ';
							if (mData[x].gTypeDesc != "REG") {
								futureStr += '<span class="gtype">' + ((mData[x].gTypeDesc == "PRE")?" Preseason":(this.playoffsText(mData[x].poRnd))) + '</span>';
							}


							futureStr += tixlink + travel+((isArena)?('<br>' + mData[x].arena):'') +'</td>';
							//futureStr += tixlink + travel +'</td>';
							futureStr += '<td class="lt_time">&nbsp;' + mData[x].gTime + '</td>';
							futureStr += '<td class="lt_localtv">&nbsp;' + mData[x].loctv + '</td>';
							futureStr += '<td class="lt_nattv">&nbsp;' + natTV +'</td>';
							futureStr += '<td class="lt_radio">&nbsp;' + mData[x].locradio + '</td>';
							futureStr += '</tr>';

						}
						else {

							pcntr++;
							pastM = tmpArr[z];
							if (pastM != tmpArr[z] || pcntr == 1) {
								pastStr += '<tr class="listTableTitle"><td>' + this.calMonthStr(tmpArr[z]) + '</td><td>Opponent</td><td>Result</td><td>Archive</td></tr>';
							}

							pastStr += '<tr>';
							pastStr += '<td class="lt_date">&nbsp;' + this.listDate(mData[x].date) + '</d>';

							/* customization for LA Lakers/Clippers */
							var oppStr = "";
							if ((mData[x].oppCity).length > 1)
								//oppStr = (mData[x].oppCode == "lakers")?"LA Lakers":((mData[x].oppCode == "clippers")?"LA Clippers":mData[x].oppCity);
								oppStr = mData[x].oppCity;
							else
								oppStr = mData[x].oppAbr;

							pastStr += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ((mData[x].oppCode!="")?' <a href="/' + mData[x].oppCode + '/">':' ') + oppStr + '</a>';
							if (mData[x].gTypeDesc != "REG") {
								pastStr += '<span class="gtype">' + ((mData[x].gTypeDesc == "PRE")?" Preseason":(this.playoffsText(mData[x].poRnd))) + '</span>';
							}

							pastStr += ((isArena)?('<br>' + mData[x].arena):'') + '</td>';

							pastStr += '</td>';
							pastStr += '<td class="lt_time">&nbsp; ';
							if (parseInt(mData[x].oppPts) != 0 && parseInt(mData[x].hmePts) != 0) {
								pastStr += '<a href="/games/' + mData[x].gCode + '/boxscore.html">' + mData[x].isWin + ' ' + ((isHome)?(mData[x].hmePts + '-' + mData[x].oppPts):(mData[x].oppPts + '-' + mData[x].hmePts)) + '</a>';
								if (mData[x].gTypeDesc == "REG") {
									//pastStr += '&nbsp;(' + mData[x].wins + '-' + mData[x].loss + ')</td>';
								}
							}
							
							pastStr += '<td class="lt_nattv">';
							if (mData[x].archive_link != "") {
								pastStr += this.archiveLink(mData[x].gCode, cal_data.team);
							}
							pastStr += '</td>';

							pastStr += '</tr>';

						}

					}

				}

			}
			catch (e) {
				/* do nothing */
			}

		}

		if (futureStr != "") {
			str = '<table class="listTable" border=1 cellpadding=0 cellspacing=0 width="100%" bordercolor="'+ listViewBorderColor + '"><tr><td class="fullviewTitle" colspan=6>Upcoming Games</td></tr>' + futureStr  + '</table><br><br>';
		}
		if (pastStr != "") {
			str += '<table class="listTable" border=1 cellpadding=0 cellspacing=0 width="100%" bordercolor="'+ listViewBorderColor + '"><tr><td class="fullviewTitle" colspan=4>Previous Games</td></tr>' + pastStr  + '</table>';
		}

		thisID = helper.htmlElement("listviewContent");
		helper.checkElementExist(thisID, str, "innerHTML");

		thisID = helper.htmlElement("calendarview");
		thisID.style.display = 'none';

		thisID = helper.htmlElement("listview");
		thisID.style.height = '100%';
		thisID.style.display = 'block';

	},

	buildListViewAll: function() {

		var str = "";
		var tmpArr = ["4", "5", "6", "7", "8", "9"];
		var hasData = false;

		thisID = helper.htmlElement("dispMonth");
		thisID.style.display = 'none';

		calendar_site = cal_data.team;

		str += '<table class="listTable" border=1 cellpadding=0 cellspacing=0 width="100%" bordercolor="'+ listViewBorderColor + '">';

		for (z  = 0; z < tmpArr.length; z++) {

			try {

				if (cal_data[tmpArr[z]].length>0) {

					mData = cal_data[tmpArr[z]];

					var cntr = 0;

					for (x = 0; x < mData.length; x++) {

						isPast = (mData[x].gType=="P")?true:false;

						if (!isPast) {

							if (!hasData) hasData = true;

							cntr++;
							if (cntr==1) {
								str += '<tr class="listTableTitle"><td>' + this.calMonthStr(tmpArr[z]) + '</td><td>Opponent</td><td>Time</td><td>Local TV</td><td>Nat TV</td><td>Radio</td></tr>';
							}

							isHome = (mData[x].isHome=="1")?true:false;
							isTix = (mData[x].tix != "away" && mData[x].tix != "notix" && (mData[x].tix).length>1)?true:false;
							isNatBrd = (mData[x].nattv!="")?true:false;
							isLocBrd = (mData[x].loctv!="")?true:false;
							isRadio = (mData[x].locradio!="")?true:false;
							isArena = (mData[x].arena!="")?true:false;

						isTravel = (mData[x].travel!="nolinks" && mData[x].travel!=null)?true:false;
						if(isTravel && ((calendar_site == 'cavaliers')||(calendar_site=='nets')||(calendar_site=='knicks')||(calendar_site=='grizzlies')))
						{
								travel="&nbsp;<a href='"+mData[x].travel+"' target='_blank'><img class='travel_icon' src='/media/"+calendar_site+"/travel_icon.gif' alt='"+mData[x].travelAirline+" Airlines' title='"+mData[x].travelAirline+" Airlines' border=0></a>";

								if(mData[x].travelPixel.length > 5 && mData[x].travelPixel!='nolinks')
									travel += "<img src = '"+mData[x].travelPixel+"' border=0>";
						}
						else
							travel="";

							tixlink = (isTix)?'<a href="' + mData[x].tix +'" target="_blank"><img src="/media/' + calendar_site + '/ticket_icon.gif" border=0 title="BUY TICKET">':'&nbsp;' + '</a>';
							natTV = (isNatBrd)?(this.checkNatTV(mData[x].nattv)):'';
							if (mData[x].is_webcast) {
								natTV = this.liveAccessLink(mData[x].gCode, cal_data.team) + " " + natTV;
							}

							str += '<tr>';
							str += '<td class="lt_date">&nbsp;' + this.listDate(mData[x].date) + '</d>';
							str += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ' ' + (((mData[x].oppCity).length>1)?mData[x].oppCity:mData[x].oppAbr) + ' ' + tixlink + travel+((isArena)?('<br>' + mData[x].arena):'') + '</td>';
							//str += '<td class="lt_opponent">&nbsp;' + ((isHome)?'vs':'@') + ' ' + (((mData[x].oppCity).length>1)?mData[x].oppCity:mData[x].oppAbr) + ' ' + tixlink + travel + '</td>';
							str += '<td class="lt_time">&nbsp;' + mData[x].gTime + '</td>';
							str += '<td class="lt_localtv">&nbsp;' + mData[x].loctv + '</td>';
							str += '<td class="lt_nattv">&nbsp;' + natTV +'</td>';
							str += '<td class="lt_radio">&nbsp;' + mData[x].locradio + '</td>';
							str += '</tr>';

						}

					}

				}


			}
			catch (e) {
				/* do nothing */
			}

		}

		if (!hasData) {
			str += '<tr><td class="nodata">NO UPCOMING GAMES</td></tr>';
		}

		str += '</table>';

		thisID = helper.htmlElement("listviewContent");
		helper.checkElementExist(thisID, str, "innerHTML");

	},


	tixByDay: function(day) {

		this.loadInc('show');

		try {
			var xmlhttpJ = this.xmlHttpRequest();
			var useJSP = '/scores/calendarJSON_WNBA.jsp?leagueid=' + leagueid + '&team=' + calendar_site + '&season=' + seasonYearWNBA;
			useJSP = (day=="All")?useJSP:useJSP + '&day=' +day;
			linkDisplay = (day=="All")?"none":"block";

			if (xmlhttpJ){

				xmlhttpJ.open("GET",useJSP,false);
				xmlhttpJ.send(null);
				if (xmlhttpJ.readyState==4) {
					cal_data = eval('(' + xmlhttpJ.responseText + ')');
				}
			}

		}
		catch (e) {
			alert("An error has occured while loading data. Please refresh the page."); return;
		}

			thisID = helper.htmlElement("viewLinks");
			thisID.style.display = linkDisplay;
			thisID = helper.htmlElement("custDesc");
			thisID.style.display = linkDisplay;
			thisID = helper.htmlElement("fullViewLinks");
			thisID.style.display = (linkDisplay=="block")?"none":"block";

			if (linkDisplay=="none") {
				this.passVar["view"] = "calendar";
			}

			if (day != "All") {
				this.passVar["desc"] = day + " Games";
				str = this.passVar["desc"];
				str = (this.passVar["view"] == "all")?"All " + str:str;
				thisID = helper.htmlElement("custDesc");
				thisID.style.display = 'block';
				helper.checkElementExist(thisID, str, "innerHTML");
			}


			team_cal.buildCal();

		this.loadInc('hide');
		document.tixselection.byteam.options[1].selected=true;

	},

	tixByOpponent: function(opp) {

		this.loadInc('show');

		try {
			var xmlhttpJ = this.xmlHttpRequest();
			var useJSP = '/scores/calendarJSON_WNBA.jsp?leagueid=' + leagueid + '&team=' + calendar_site + '&season=' + seasonYearWNBA;
			useJSP = (opp=="All" || opp==calendar_site)?useJSP:useJSP + '&opponent=' +opp;
			linkDisplay = (opp=="All" || opp==calendar_site)?"none":"block";

			if (xmlhttpJ){

				xmlhttpJ.open("GET",useJSP,false);
				xmlhttpJ.send(null);
				if (xmlhttpJ.readyState==4) {
					cal_data = eval('(' + xmlhttpJ.responseText + ')');
				}

			}

		}
		catch (e) {
			alert("An error has occured while loading data. Please refresh the page."); return;
		}

			thisID = helper.htmlElement("viewLinks");
			thisID.style.display = linkDisplay;
			thisID = helper.htmlElement("custDesc");
			thisID.style.display = linkDisplay;
			thisID = helper.htmlElement("fullViewLinks");
			thisID.style.display = (linkDisplay=="block")?"none":"block";

			if (linkDisplay=="none") {
				this.passVar["view"] = "calendar";
			}

			if (opp != "All") {
				this.passVar["desc"] = " Games vs " + opp ;
				str = this.passVar["desc"];
				str = (this.passVar["view"] == "all")?"All " + str:str;
				thisID = helper.htmlElement("custDesc");
				helper.checkElementExist(thisID, str, "innerHTML");
			}

			team_cal.buildCal();


		if (opp == calendar_site)  {
			document.tixselection.byteam.options[1].selected=true;
		}

		this.loadInc('hide');
		document.tixselection.byday.options[1].selected=true;

	},

	checkMDays31: function(m) {

		for (x=0; x < (this.mo_31).length; x++) {
			if (m == this.mo_31[x]) return true;
		}

	},

	checkCalLoc: function() {



		if (!this.passVar["firstload"]) {

			this.passVar["firstload"] = true;

			this.nav["year"] = seasonYearWNBA;
			return seasonStartMonth;

		}
		else {

			ret = this.nav["month"];
			return ret;

		}

	},

	clearCal: function() {

		/* reset */

		for (z = 0; z < (this.temp_id).length; z++) {

			thisID = helper.htmlElement(this.temp_id[z][1]);
			thisID.id = this.temp_id[z][0];


		}

		this.temp_id = new Array();

		for (y = 1; y <=6; y++) {

			for (x = 0; x < 7; x++) {

				thisID = helper.htmlElement(y + "_" + x);
				helper.checkElementExist(thisID, "", "innerHTML");
				thisID.scope = ""; /* clear scope */
				thisID.title = ""; /* clear title */
				thisID.abbr  = ""; /* clear background color */
				helper.checkElementExist(thisID, "", "className");  /* clear class names */

			}

		}

		/* hide brd window */
		thisID = helper.htmlElement("brdWindow");
		thisID.style.display = 'none';

	},

	selGames: function(dmonth) {

		dmonth = "" + dmonth;
		if (cal_data[dmonth]!=undefined) {

			for (x = 0; x < cal_data[dmonth].length; x++) {

				id = cal_data[dmonth][x].date;

				//checkers
				isHome = (cal_data[dmonth][x].isHome=="1")?true:false;
				isPast = (cal_data[dmonth][x].gType=="P")?true:false;
				isTix = (cal_data[dmonth][x].tix != "away" && cal_data[dmonth][x].tix != "notix" && (cal_data[dmonth][x].tix).length>1)?true:false;
				isNatBrd = (cal_data[dmonth][x].nattv!="")?true:false;
				isLocBrd = (cal_data[dmonth][x].loctv!="")?true:false;
				isRadio = (cal_data[dmonth][x].locradio!="")?true:false;

				mData = cal_data[dmonth];



				isTravel = (cal_data[dmonth][x].travel!="nolinks" && cal_data[dmonth][x].travel!=null)?true:false;


				gScore = (isHome)?cal_data[dmonth][x].hmePts+"-"+cal_data[dmonth][x].oppPts:cal_data[dmonth][x].oppPts+"-"+cal_data[dmonth][x].hmePts;

				cs = "";
				if (!isHome)
					cs = "Aw";


				var dat = (cal_data[dmonth][x].date).split("/");

				dy = (parseInt(dat[1])>=10)?dat[1]:dat[1].charAt(1);




				//if(isTravel && ((calendar_site == 'cavaliers')||(calendar_site=='nets')||(calendar_site=='knicks')||(calendar_site=='grizzlies')))
				if(isTravel)
				{
					str = '<div class="topDate' + cs + '" style="width: 100%;"><div style="text-align: left; float: left;"><a href="'+mData[x].travel+'" target="_blank"><img src="/media/'+calendar_site+'/travel_icon.gif" border=0 alt="'+mData[x].travelAirline+' Airlines" title="'+mData[x].travelAirline+' Airlines"></a></div><div class="topDate' + cs + '" style="float: right; width: 50%;">' + dy + '&nbsp;</div></div><div style="clear: both;"></div>';
				}
				else
				{
					str = '<div class="topDate' + cs + '">' + dy + '&nbsp;</div>';
				}




				str += (isTix && !isPast)?('<div class="topTix' + cs + '" ><a target="_blank" href="' + cal_data[dmonth][x].tix + '"><img id="tixicon_' + id + '" onmouseover="javascript:team_cal.tixicon(\'on\', \'tixicon_' + id + '\');" onmouseout="javascript:team_cal.tixicon(\'off\', \'tixicon_' + id + '\');" " src="/media/schedules/buytix_off.gif" border=0></a></div>'):('<div class="topTix' + cs + '" ><img src="/images/blank.gif" height="18px"></div>');
				str += '<div class="gmeData' + cs + '">';
				str += '<div>' + ((isHome)?'vs':'@') + ' ' + (((cal_data[dmonth][x].oppAbr).length>1)?cal_data[dmonth][x].oppAbr:cal_data[dmonth][x].oppCity); + '</div>';
				//str += '<div style="padding-left: 15px;"><div style="background: url(/media/schedules/schedule_' + cal_data[dmonth][x].oppCode +'_color.gif) no-repeat;"><img src="/images/blank.gif" border=0 height=48 width=56></div></div>';
				str += '<div><div style="background: url(/media/schedules/schedule_' + cal_data[dmonth][x].oppCode +'_color.gif) no-repeat center center; width: 100%; height: 48px;"></div></div>';
				str += '<div>' + ((cal_data[dmonth][x].gTypeDesc=='REG')?'':((cal_data[dmonth][x].gTypeDesc=='PLO')?(this.playoffsText(cal_data[dmonth][x].poRnd)):cal_data[dmonth][x].gTypeDesc)) + '</div>';

				if (isPast) {
					str += (cal_data[dmonth][x].oppPts=="0" && cal_data[dmonth][x].hmePts == "0")?('<div>&nbsp;</div>'):('<div class="topBox' + cs + '"><a href="/games/' + cal_data[dmonth][x].gCode + '/boxscore.html">' + cal_data[dmonth][x].isWin + ' ' + gScore +'</a></div>');
				}
				else {
					str += '<div class="topTime' + cs + '">' + cal_data[dmonth][x].gTime + '</div>';
				}

				if ((isNatBrd || isRadio || isLocBrd) && !isPast) {

					natTV = (isNatBrd)?(this.checkNatTV(cal_data[dmonth][x].nattv)):'';
					if (mData[x].is_webcast) {
						natTV = this.liveAccessLink(cal_data[dmonth][x].gCode, cal_data.team) + " " + natTV;
					}

					str+= '<div><a href="javascript:team_cal.showBrd(\'' + id + '\');"><img src="/media/schedules/tv-radio.gif" border=0 title="Click to show broadcasters"></a></div>';

					brd = '<div class="brdWindowBar" style="height: 14px; padding: 2px 2px 2px 0px; width: 100%; display: block; position: relative; "><div style="float:left; display: block;">' + cal_data[dmonth][x].date + '</div><div style="float: right; display: block;"><a title="close" href="javascript: team_cal.closeBrd();"><img src="/images/cal_close.gif" border=0></a></div></div>';
					brd += '<div style="display: block; ">' + ((isHome)?'vs':'@') + ' ' + (((cal_data[dmonth][x].oppCity).length>1)?cal_data[dmonth][x].oppCity:cal_data[dmonth][x].oppAbr);
					brd += (isNatBrd)?('<br>NAT : ' + natTV):'';
					brd += (isLocBrd)?('<br>LOC : ' + cal_data[dmonth][x].loctv):'';
					brd += (isRadio)?('<br>RADIO : ' + cal_data[dmonth][x].locradio):'';
					brd += '</div>';
					this.brd_info[id] = brd;


				}

				for(m=0; m < cal_data["promos"].length; m++)
				{
					if(cal_data["promos"][m].date == cal_data[dmonth][x].date)
					{
						str += "<div><a href='"+cal_data["promos"][m].link+"'><img src='/media/promo_icon.gif' alt='"+cal_data["promos"][m].title+"' title='"+cal_data["promos"][m].title+"' style='border: 0px;'></a></div>";
					}

				}


				str += '</div>';


				thisID = helper.htmlElement(id);
				helper.checkElementExist(thisID, str, "innerHTML");
				boxBGColor = (isHome)?"homeBoxBG":"awayBoxBG";
				helper.checkElementExist(thisID, boxBGColor, "className");


			}

		}

	},

	listDate: function(d) {

		var dt = d.split("/");

		dateObj = new Date(d);
		dow = dateObj.getDay();
		ret = this.dayOfWeek(dow) + " " + dt[1];
		return ret;

	},

	showBrd: function(id) {
		//console.log('in showBrd');
		var setShow = false;

		thisID = helper.htmlElement("brdWindow");

		if (thisID.style.display == 'block') {
			if (id == this.cur_brd_id) {
				thisID.style.display = 'none';
			}
			else {
				setShow = true;
			}

		}
		else {
			setShow = true;
		}

		if (setShow) {

			var padding = 10;

			brd = this.brd_info[id];

			brdWin = helper.htmlElement("brdWindow");

			brdID = helper.htmlElement("brdInside");
			helper.checkElementExist(brdID, brd, "innerHTML");

			// hide it until all calculations are done
			thisID.style.visibility = document.layers ? 'hide' : 'hidden';

			thisID.style.display = 'block';

			var mRelativePos = mPos_Var.getRelativePosToParentElement(brdWin);

			var teamCalContainer = brdWin.offsetParent;

			var topBrd = mRelativePos.yPos;
			topBrd += padding;

			var leftBrd = mRelativePos.xPos;
			leftBrd += padding;


			//restrict brdWindow to only appear over parent calendar to prevent other surrounding content from covering it
			if ((topBrd + brdWin.offsetHeight) > teamCalContainer.offsetHeight) {
				topBrd -= topBrd + brdWin.offsetHeight - teamCalContainer.offsetHeight;
			}

			if ((leftBrd + brdWin.offsetWidth) > teamCalContainer.offsetWidth) {
				leftBrd -= leftBrd + brdWin.offsetWidth - teamCalContainer.offsetWidth;
			}

			// add px for IE
			if (!ie) topBrd = topBrd+'px';
			if (!ie) leftBrd = leftBrd+'px';

			// set top and left
			thisID.style.top = topBrd;
			thisID.style.left =  leftBrd;

			// show it now that we know where to put it
			thisID.style.visibility = 'visible';

			this.cur_brd_id = id;

		}

	},

	loadInc: function(s) {

		if (s=="show") {
			thisID = helper.htmlElement("loadInc");
			thisID.style.display = 'block';
		}
		else {
			tempFunc = function() {
				thisID = helper.htmlElement("loadInc");
				thisID.style.display = 'none';
			}

			setTimeout(tempFunc, 250);
		}

	},

	closeBrd: function() {
		thisID = helper.htmlElement("brdWindow");
		thisID.style.display = 'none';
	},

	calMonthStr: function(m) {

		var s = "";
		m = parseInt(m);
		switch (m) {

			case 1 : s = "January"; break;
			case 2 : s = "February"; break;
			case 3 : s = "March"; break;
			case 4 : s = "April"; break;
			case 5 : s = "May"; break;
			case 6 : s = "June"; break;
			case 7 : s = "July"; break;
			case 8 : s = "August"; break;
			case 9 : s = "September"; break;
			case 10 : s = "October"; break;
			case 11 : s = "November"; break;
			case 12 : s = "December"; break;

		}

		return s;

	},

	dayOfWeek: function(n) {

		var ret = "";

		switch (n) {

			case 0 : ret = "Sun"; break;
			case 1 : ret = "Mon"; break;
			case 2 : ret = "Tue"; break;
			case 3 : ret = "Wed"; break;
			case 4 : ret = "Thu"; break;
			case 5 : ret = "Fri"; break;
			case 6 : ret = "Sat"; break;

		}

		return ret;

	},

	changeView: function(v) {

		var curr = this.passVar["view"];

		if (curr!=v) {

			thisID = helper.htmlElement("listview");
			thisID.style.height = '500px';

			if (v=="calendar") {
				this.passVar["view"] = "calendar";
				str = this.passVar["desc"];
			}
			else if (v=="list") {
				this.passVar["view"] = "list";
				str = this.passVar["desc"];
			}
			else {
				this.passVar["view"] = "all";
				str = "All" + this.passVar["desc"];
			}

			thisID = helper.htmlElement("custDesc");
			helper.checkElementExist(thisID, str, "innerHTML");
			thisID = helper.htmlElement("fullViewLinks");
			thisID.style.display = 'none';

			team_cal.buildCal();

		}

		return;

	},


	xmlHttpRequest: function() {
		var xmlhttpJ;

		if (window.XMLHttpRequest) {
			xmlhttpJ = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			xmlhttpJ = new ActiveXObject("Msxml2.XMLHTTP");
		}
		return xmlhttpJ;
	},

	reset: function() {

		thisID = helper.htmlElement("fullViewLinks");
		thisID.style.display = 'block';
		thisID = helper.htmlElement("viewLinks");
		thisID.style.display = 'none';
		thisID = helper.htmlElement("custDesc");
		thisID.style.display = 'none';
		this.loadCounter = 0;
		this.passVar = new Object();
		this.buildCal();

	},

	checkForVars: function() {

		var link = window.location.search;
		link = link.substr(1);
		var dPairs = link.split("&");

		for (x = 0; x < dPairs.length; x++) {

				var spl = dPairs[x].split("=");
				this.passVar[spl[0]] = spl[1];
		}

		if (this.passVar["season"]!=undefined) {
			seasonYearWNBA =  parseInt(this.passVar["season"]);
		}


	},

	playoffsText: function (p) {

		p = parseInt(p);

		switch (p) {

			case 1 : return " Conf. Semi-Finals"; break;
			case 2 : return " Conf. Finals"; break;
			case 3 : return " Finals"; break;
			default: return ""; break;

		}

	},

	seasonText: function (y) {

		y = parseInt(y);
		y2 = ""+(y+1);
		y2 = y2.charAt(y2.length-2) + y2.charAt(y2.length-1);
		this.passVar["seasonText"] = y + '-' + y2;

	},

	tixicon: function(t, id) {

		document.getElementById(id).src = '/media/schedules/buytix_' + t + '.gif';

	},

	checkNatTV: function(tv) {

		return tv;

	},
	
	liveAccessLink : function(gamecode, teamcode) {
		return "<a href='/" + teamcode + "/live.jsp?gamecode=" + gamecode + "'>Live Access</a>";
	},
	
	archiveLink: function(gamecode, teamcode) {
		return "<a href='/" + teamcode + "/live.jsp?gamecode=" + gamecode + "'>Archive</a>";
	}

}


