var teamSched_template = '\n<div style="text-align: center; font-size: 9px;">TEAM SCHEDULE</div>\n<div style="background-color: #C9CC93; padding: 2px 4px 2px 4px;">\n<div style="float: right; color: blue; cursor: hand;cursor: pointer;" onclick="hideTeamSchedule(\'teamsched_@team_id@\');">[X]</div>\n@team_name@\n</div>\n<div style="padding: 6px 4px 6px 4px;text-align: center;">Loading ...</div>';

// AJAX CODE 
var ajax_one = new Ajax();

// set timeout only runs the method once. setInterval runs it every N seconds
window.setTimeout( "getData()" );

function getData() {
	getStandings();
}

// REQUESTS
function getStandings() {
	var url = "middle_man.php";
	var args = "action=standings&season_display_mode=" + season_display_mode + "&money_display_mode=" + money_display_mode + "&league_season_id=" + league_season_id + "&week_id=" + week_id + "&first_playoff_week_id=" + first_playoff_week_id;
	ajax_one.makeRequest( "POST", url, getStandingsCallback, args );
}
function toggleTeamSchedule( team ) {
	var url = "middle_man.php";
	// parse schedule team id from the team link that was clicked [format=team_\d+]
	var team_id = team.id.substring( 5 );
	var teamsched_id = "teamsched_" + team_id;
	// if requested team schedule does not exist, request it, otherwise hide it
	if( document.getElementById( teamsched_id ) == null ) {
		// before submitting a request, show skeleton as placeholder while we wait 
		var tsc = document.getElementById( 'teamSchedulesContainer' );
		var newDiv = document.createElement( 'div' );
		newDiv.setAttribute( 'id', teamsched_id );
		newDiv.setAttribute( 'class', 'teamScheduleContainer' );		// works for all but IE
		newDiv.setAttribute( 'className', 'teamScheduleContainer' );	// works for IE
		newDiv.innerHTML = teamSched_template.replace( /@team_id@/, team_id ).replace( /@team_name@/, team.innerHTML );
		tsc.appendChild( newDiv );
		
		// create new ajax object each time to allow for multiple concurrent requests 
		var ajax_dyn = new Ajax();
		var args = "action=team_schedule&league_season_id=" + league_season_id + "&team_id=" + team_id + "&first_playoff_week_id=" + first_playoff_week_id;
		ajax_dyn.makeRequest( "POST", url, 
			function() {
				if ( ajax_dyn.request.readyState == 4 ) {
					showTeamSchedule( ajax_dyn.request.responseText );
				}
			}, args );
	} else {
		hideTeamSchedule( teamsched_id );
	}
}

// CALLBACKS
function getStandingsCallback() {
	if ( ajax_one.request.readyState == 4 ) {
		document.getElementById( 'standingsContainer' ).innerHTML = ajax_one.request.responseText;
	}
}

// OTHER
function showTeamSchedule( responseText ) {
	// id to use for team schedule div is first item in response, followed by a space
	var content_start = responseText.indexOf( ' ' ) + 1;
	var teamsched_id = responseText.substring( 0, content_start - 1  );
	responseText = responseText.substring( content_start );
	
	// team schedule div (placeholder) must already be present, or we removed it during request
	var newDiv = document.getElementById( teamsched_id );
	if( newDiv != null ) {
		newDiv.innerHTML = responseText;
	}
}
function hideTeamSchedule( teamsched_id ) {
	var tsc = document.getElementById( 'teamSchedulesContainer' );
	var teamDiv = document.getElementById( teamsched_id );
	tsc.removeChild( teamDiv );
}
function hiliteMatchup( teamNameDiv, oppTeamNameId ) {
	teamNameDiv.style.fontWeight = 'bold';
	document.getElementById( oppTeamNameId ).style.fontWeight = 'bold';
}
function unhiliteMatchup( teamNameDiv, oppTeamNameId ) {
	teamNameDiv.style.fontWeight = 'normal';
	document.getElementById( oppTeamNameId ).style.fontWeight = 'normal';
}

/* check/uncheck top tab options from a click on a tab option name */
function toggleChecksFromText( checkID,otherCheckID ) {
	return toggleChecks( document.getElementById( checkID ), otherCheckID );
}

/* check/uncheck top tab options from a click on a tab check box */
function toggleChecks( check, otherCheckID ) {
	var otherCheck = document.getElementById( otherCheckID );
	if( check.src.indexOf( "unchecked.gif" ) != -1 ) {
		check.src = "images/checked.gif";
		otherCheck.src = "images/unchecked.gif";
	}				
	if( otherCheckID == "confCheck" ) {
		season_display_mode = 1;
	} else if ( otherCheckID == "divCheck" ) {
		season_display_mode = 2;
	} else if ( otherCheckID == "balCheck" ) {
		money_display_mode = 1;
	} else if ( otherCheckID == "wonCheck" ) {
		money_display_mode = 2;
	}
}

// init the checkbox so the correct one is checked off based on what the display_mode is
function initCheckboxes() {
	if ( season_display_mode == 2 ) {
		document.getElementById( "divCheck" ).setAttribute( 'src', "images/unchecked.gif" );
		document.getElementById( "confCheck" ).setAttribute( 'src', "images/checked.gif" );
	} else {
		document.getElementById( "divCheck" ).setAttribute( 'src', "images/checked.gif" );
		document.getElementById( "confCheck" ).setAttribute( 'src', "images/unchecked.gif" );
	}
	if ( money_display_mode == 2 ) {
		document.getElementById( "wonCheck" ).setAttribute( 'src', "images/unchecked.gif" );
		document.getElementById( "balCheck" ).setAttribute( 'src', "images/checked.gif" );
	} else {
		document.getElementById( "wonCheck" ).setAttribute( 'src', "images/checked.gif" );
		document.getElementById( "balCheck" ).setAttribute( 'src', "images/unchecked.gif" );
	}
}
