// create the html for the standings landing page 
// - simply list of leagues linked to standings
function createStandingsLanding() {
    return createLeaguesByClass("l_sta");
}
// html for the scoreboard landing page 
// - simply list of leagues linked to standings
function createScoreboardLanding() {
    return createLeaguesByClass("l_sco");
}

// html for stats landing with 
// league stats and links to individual team stats
function createStatsLanding() {
    return createBothByClass("t_ibp");
}
// html for schedule page with
// links to team schedules, no leagues
function createScheduleLanding() {
    return createBothByClass("t_sch");
}

// returns formatted html list of leagues by class
// assumes global properties and classes objects
function createLeaguesByClass(type) {
   
    // sort objects based on their display_name property
    function sortByDisplayName(a,b) {
        if (a.display_name > b.display_name) { return  1 };
        if (a.display_name < b.display_name) { return -1 };
        return 0; 
    }

    var html = "";
    for (var l=0; l<classes.length; l++) {

        // go thru properties.leagues and build array of teams in this class
        var arrLeagues = new Array();        
        var i = 0;
        for (league in properties.leagues) {
            // dont show leagues flagged "hidden"
            if (properties.leagues[league].hidden != "0") { continue; }
            if (properties.leagues[league].sport_code_display == classes[l].sport) {
                arrLeagues[i] = properties.leagues[league];
                i++;
            }
        }

        // sort the leagues by name
        arrLeagues.sort(sortByDisplayName);

        html += "<img src=\"/milb/images/classheaders/" + classes[l].img + "_header_wide_notop.gif\" width=\"750\" height=\"25\" alt=\"" + classes[l].sport + "\" border=\"0\" /><br /><div style=\"padding: 10px 0px 30px 30px;\">";

		var fileSuffix;
		
        //  iterate thru leagues and create html links
        for(i=0; i<arrLeagues.length; i++) {
		
		fileSuffix = "";
		
            if (i > 0) { // divider between leagues
			if (((i+1) % 4) == 0) {
				html += "<br />";
			}
			else {
				html += "<span style=\"color: #8E8E8E; padding: 0px 10px 0px 10px;\">|</span>";
			}
            }

			if ((type == "l_sta") && (arrLeagues[i].season_type == "split")){
				fileSuffix = splitSeason(arrLeagues[i].season_start, arrLeagues[i].season_2ndh_start, arrLeagues[i].season_state);
			}

            // type picks the stats/js/datawriters module to use
            // also trims off the 'l' from the league ID - should fix these things so id's are always the full version 
            html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=" + type + "&lid=" + arrLeagues[i].id.substring(1)+ fileSuffix + "\" style=\"font-weight: bold;\">" + arrLeagues[i].display_name + "</a>";
        }

        html += "</div>";
    }
    return html;
}

// returns formatted html list of leagues by class
// assumes global properties and classes object
function createBothByClass(type) {

    // sort objects based on their display_name property
    function sortByDisplayName(a,b) {
        if (a.display_name > b.display_name) { return  1 };
        if (a.display_name < b.display_name) { return -1 };
        return 0; 
    }

    var html = "<table>";
    for (var l=0; l<classes.length; l++) {

        // go thru properties and build an array of Leagues that match each class
        var arrLeagues = new Array();        
        var i = 0;
        for (league in properties.leagues) {
            if (properties.leagues[league].hidden != "0") { continue; }
            if (properties.leagues[league].sport_code_display == classes[l].sport) {
                arrLeagues[i] = properties.leagues[league];
                i++;
            }
        }

        html += "<tr><td colspan=\"3\">";
        html += "<img src=\"/milb/images/classheaders/" + classes[l].img + "_header_wide_notop.gif\" border=\"0\" width=\"750\" height=\"25\" alt=\"" + classes[l].sport + "\"><br /><br />";
        html += "</td></tr><tr>";

        // sort by name
        arrLeagues.sort(sortByDisplayName);

        // go thru each league and print the stats links 
        for (i = 0; i<arrLeagues.length; i++) {
            
            var objLeague = arrLeagues[i];

            if (objLeague.sport_code_display == classes[l].sport) {
                objLeague.clubs.sort(sortByDisplayName);
                html += "<td style='vertical-align: top;'>";


                if ((type == "t_sch") && (objLeague.id in _oc(hostedSites))) {
	                html += "<a href=\"/schedule/index.jsp?sid=" + objLeague.id + "\"><img src=\"/milb/images/leaguesubheaders/" + objLeague.id + "_subhead_sm.gif\" alt=\"" + objLeague.display_name + "\" border=\"0\"></a><br />";
                }
                else {
	                html += "<img src=\"/milb/images/leaguesubheaders/" + objLeague.id + "_subhead_sm.gif\" alt=\"" + objLeague.display_name + "\" border=\"0\"><br />";
                }


                // if we're on the stats page, print the links for the league
                if (type == "t_ibp") {
                    html += "<p style=\"text-align: center;width:225px;\">";
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_bat&lid=" + objLeague.id.substring(1) + "\">Batting leaders</a> <span style=\"color: #8E8E8E; padding: 0px 8px 0px 8px;\">|</span>";
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_pit&lid=" + objLeague.id.substring(1) + "\">Pitching leaders</a><br />";
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_tba&lid=" + objLeague.id.substring(1) + "\">Team batting</a> <span style=\"color: #8E8E8E; padding: 0px 8px 0px 8px;\">|</span>";
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_tpi&lid=" + objLeague.id.substring(1) + "\">Team pitching</a><br />";


			if ((objLeague.id.substring(1) == "121") || (objLeague.id.substring(1) == "124") || (objLeague.id.substring(1) == "130") || (objLeague.id.substring(1) == "134")) {
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_trn&lid=" + objLeague.id.substring(1) + "\">Transactions</a><span style=\"color: #8E8E8E; padding: 0px 8px 0px 8px;\">|</span>";
                    html += "<a href=\"/roster/league_rosters.jsp?sid=milb&lid=" + objLeague.id.substring(1) + "\">Rosters</a><p />";
			}
			else {
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_trn&lid=" + objLeague.id.substring(1) + "\">Transactions</a><span style=\"color: #8E8E8E; padding: 0px 8px 0px 8px;\">|</span>";
                    html += "<a href=\"/milb/stats/stats.jsp?sid=milb&t=l_att&lid=" + objLeague.id.substring(1) + "\">Attendance</a><span style=\"color: #8E8E8E; padding: 0px 8px 0px 8px;\">|</span>";
                    html += "<a href=\"/roster/league_rosters.jsp?sid=milb&lid=" + objLeague.id.substring(1) + "\">Rosters</a><br />";

                    html += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>";
			}


                }

                html += "<ul style=\"list-style-image: url(/images/sm_bullet.gif);\">";
                
                // and print the stats links for each club in the league 

                // type picks the stats/js/datawriters module to use
                // also trims off the 'l' from the league ID - should fix these things so id's are always the full version 
                for (club in objLeague.clubs) {
									// if in-network
	                if (type == "t_sch" && (objLeague.clubs[club].id in _oc(hostedSites))) html += "<li><a href=\"/schedule/index.jsp?sid=" + objLeague.clubs[club].id +"\">" + objLeague.clubs[club].display_name + "</a></li>";
									// else stats link or out-of-network team schedule link
/*
  	              else html += "<li><a href=\"/milb/stats/stats.jsp?sid=milb&t=" + type + "&cid=" + objLeague.clubs[club].id.substring(1) +"\">" + objLeague.clubs[club].display_name + "</a></li>";
*/
  	              else {  
  	                   if (objLeague.clubs[club].id) html += "<li><a href=\"/milb/stats/stats.jsp?sid=milb&t=" + type + "&cid=" +   	                	              objLeague.clubs[club].id.substring(1) +"\">" + objLeague.clubs[club].display_name + "</a></li>"; 
  	                   else html += ""; 
  	              } 
   	            }
                
                html += "</ul><br /></td>";

                // create a new row after every three leagues 
                if (((i+1) % 3) == 0) {
                    html += "</tr><tr>";
                }
            }
        }
        html += "</tr>";
    }
    html += "</table>";

    return html;
}

function splitSeason(seasonStart,season2ndHalf, seasonState){

	if (seasonState == 'offseason') return "&period=h2";
	
	if (seasonStart != null && season2ndHalf != null){
		var today =  new Date();
		var sFH = new Date();
		var sSH = new Date();
		sFH.setFullYear(seasonStart.substring(0,4),(seasonStart.substring(5,7) - 1),seasonStart.substring(8,10));
		sSH.setFullYear(season2ndHalf.substring(0,4),(season2ndHalf.substring(5,7) - 1),season2ndHalf.substring(8,10));
		if (today <  sFH) return "&period=h1";
		if (today >  sSH) return "&period=h2";
		return "";
	}
}