//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

//---------------------------------------------------//

function isLeaving(curValue) {
  if (curValue[4] == "-S" || curValue[4] == "-T" || curValue[4] == "Ret" || curValue[4] == "Rel") {return true;}
  else {return false;}
}

function isArriving(curValue) {
  if (curValue[4] == "+S" || curValue[4] == "+T") {return true;}
  else {return false;}
}

function isARookie(curValue) {
  if (curValue[4] == "D") {return true;}
  else {return false;}
}

function isCut(curValue) {
  if (curValue[4] == "-X" || curValue[4] == "-W") {return true;}
  else {return false;}
}

function isInjured(curValue) {
  if (curValue[4] == "I") {return true;}
  else {return false;}
}

function rookieSort(a, b) {
  if (a[5] > b[5]) {return 1;}
  if (a[5] < b[5]) {return -1;}
  else {return 0;}
}

function isReturning(curValue) {
  if (curValue[4] == "RS" || curValue[4] == "FT") {return true;}
  else {return false;}
}

function isFreeAgent(curValue) {
  if (curValue[4] == "F") {return true;}
  else {return false;}
}

function freeAgentSort(a, b) {
  if (a[6] > b[6]) {return -1;}
  if (a[6] < b[6]) {return 1;}
  else {return 0;}
}

function isUnderContract(curValue) {
  if (curValue[4] == "C") {return true;}
  else {return false;}
}

function contractSort(a, b) {
  if (a[5] > b[5]) {return 1;}
  if (a[5] < b[5]) {return -1;}
  else {return 0;}
}


function playerName(rosterCategory, j) {

  document.write("<tr height='30'><td class='player'>" + rosterCategory[j][2]);

  if (rosterCategory[j][1].length >= 9) {document.write(" <b><nobr><small>" + rosterCategory[j][1] + "</small></nobr>");}
  else {document.write(" <b><nobr>" + rosterCategory[j][1] + "</nobr>");} 

  if (rosterCategory[j][0].length >= 12) {document.write(" <nobr><small>" + rosterCategory[j][0] + "</small></nobr></b></td>");}
  else {document.write(" <nobr>" + rosterCategory[j][0] + "</nobr></b></td>");} 
}


function listLeaving(rosterTeam) {

  var leaving = rosterTeam.filter(isLeaving);

  document.write("<tr height='30'><th colspan='3'>Departures</th></tr>");

  if (leaving.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < leaving.length; i++) {

      playerName(leaving, i);

      document.write("<td class='icon'><big>&raquo;</big></td>");

      switch(leaving[i][4]) {

        case "-S":
        document.write("<td class='status'>signed by <b>" + leaving[i][5] + "</b></td></tr>");
        break;

        case "-T":
        document.write("<td class='status'>traded to <b>" + leaving[i][5] + "</b><br/><small>for <b>" + leaving[i][6] + "</b></small></td></tr>");
        break;

        case "Rel":
        document.write("<td class='status'>released</td></tr>");
        break;

        case "Ret":
        document.write("<td class='status'>retired</td></tr>");
        break;

        default:
        document.write("<td class='icon'></td><td class='status'>error</td>");
      }
    }
  }
}


function listArriving(rosterTeam) {

  var arriving = rosterTeam.filter(isArriving);

  document.write("<tr height='30'><th colspan='3'>Arrivals</th></tr>");

  if (arriving.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < arriving.length; i++) {

      playerName(arriving, i);

      document.write("<td class='icon'><big>&laquo;</big></td>");

      switch(arriving[i][4]) {

        case "+S":
        if (arriving[i][7] == "") {document.write("<td class='status'>signed<br/><small>");}
        else {document.write("<td class='status'>signed from <b>" + arriving[i][7] + "</b><br/><small>");}
        break;

        case "+T":
        document.write("<td class='status'>traded from <b>" + arriving[i][6] + "</b><br/><small>for <b>" + arriving[i][7] + "</b><br/>");
        break;
      }

      switch(arriving[i][5]) {

        case "?":
        document.write("undisclosed</small></td></tr>");
        break;

        case "R":
        document.write("<i>reserve contract</i></small></td></tr>");
        break;

        default:
        if (arriving[i][4] == "+S") {document.write("through <b>" + arriving[i][5] + "</b></small></td></tr>");}
        else {document.write("signed thru <b>" + arriving[i][5] + "</b></small></td></tr>");}
      }
    }
  }
}


function listRookies(rosterTeam) {

  var rookies = (rosterTeam.filter(isARookie)).sort(rookieSort);

  document.write("<tr height='30'><th colspan='3'>Rookies</th></tr>");

  if (rookies.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < rookies.length; i++) {

      playerName(rookies, i);

      if (rookies[i][5] == "UFA") {document.write("<td class='icon'><b>R</b></td><td class='status'>undrafted FA<br/><small>");}
      else {
        if (rookies[i][6] == "U") {document.write("<td class='icon'>&reg;</td><td class='status'>" + rookies[i][5] + "-round pick<br/><small>");}
        else {document.write("<td class='icon'><b>R</b></td><td class='status'>" + rookies[i][5] + "-round pick<br/><small>");}
      }

      switch (rookies [i][6]) {

        case "U":
        document.write("<b>unsigned</b></small></td></tr>");
        break;

        case "?":
        document.write("</small></td></tr>");
        break;

        case "R":
        document.write("<i>reserve contract</i></small></td></tr>");
        break;

        default:
        document.write("signed thru <b>" + rookies[i][6] + "</b></small></td></tr>");
      }
    }
  }
}


function listCuts(rosterTeam) {

  var cut = rosterTeam.filter(isCut);

  document.write("<tr height='30'><th colspan='3'>Cuts</th></tr>");

  if (cut.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < cut.length; i++) {

      playerName(cut, i);

      document.write("<td class='icon'><small><b>&times;</b></small></td>");

      switch(cut[i][4]) {

        case "-X":
        document.write("<td class='status'>released");
        break;

        case "-W":
        document.write("<td class='status'>waived");
        break;

        default:
        document.write("<td class='icon'></td><td class='status'>error</td></tr>");
        break;
      }

      if (cut[i][6] == "") {document.write("</td></tr>");}
      else {document.write("<br/><small>signed by <b>" + cut[i][6] + "</b></small></td></tr>");}
    }
  }
}


function listInjuries(rosterTeam) {

  var injured = rosterTeam.filter(isInjured);

  document.write("<tr height='30'><th colspan='3'>Injuries</th></tr>");

  if (injured.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < injured.length; i++) {

      playerName(injured, i);

      document.write("<td class='icon'><b>+</b></td>");

      switch(injured[i][5]) {

        case "IR":
        document.write("<td class='status'>injured reserve");
        break;

        case "PUP":
        document.write("<td class='status'>PUP list");
        break;

        default:
        document.write("<td class='icon'></td><td class='status'>error</td></tr>");
        break;
      }

      if (injured[i][7] == "") {document.write("</td></tr>");}
      else {document.write("<br/><small>" + injured[i][7] + "</small></td></tr>");}
    }
  }
}


function listReturning(rosterTeam) {

  var returning = rosterTeam.filter(isReturning);

  document.write("<tr height='30'><th colspan='3'>Re-Signings</th></tr>");

  if (returning.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < returning.length; i++) {

      playerName(returning, i);

      document.write("<td class='icon'><small>&#10004;</small></td>");

      switch(returning[i][4]) {

        case "RS":
        document.write("<td class='status'>re-signed<br/><small>");
        switch(returning[i][5]) {

          case "?":
          document.write("undisclosed</small></td></tr>");
          break;

          case "R":
          document.write("<i>reserve contract</i></small></td></tr>");
          break;

          default:
          document.write("through <b>" + returning[i][5] + "</b></small></td></tr>");
        }
        break;

        case "FT":
        document.write("<td class='status'>franchise tagged</td></tr>");
        break;

        default:
        document.write("<td class='icon'></td><td class='status'>error</td>");
      }
    }
  }
}


function listFreeAgents(rosterTeam)  {

  var freeAgents = (rosterTeam.filter(isFreeAgent)).sort(freeAgentSort);

document.write("<tr height='30'><th colspan='3'>Free Agents</th></tr>");

  if (freeAgents.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < freeAgents.length; i++) {

      playerName(freeAgents, i);

      document.write("<td class='icon'><b>?</b></td>");
      
      switch(freeAgents[i][6]) {

	case "UFA":
	document.write("<td class='status'><b>free agent</b></td>")
	break;

	case "RFA":
	document.write("<td class='status'>restricted<br/>free agent</td>")
	break;

	case "EFA":
	document.write("<td class='status'><small>exclusive rights<br/>free agent</small></td>")
	break;

	default:
	document.write("<td class='status'>error</td>");
      }
    }
  }
}


function listUnderContract(rosterTeam) {

document.write("<tr height='30'><th colspan='3'>Under Contract</th></tr>");

  var underContract = (rosterTeam.filter(isUnderContract)).sort(contractSort);

  if (underContract.length === 0) {document.write("<tr height='30'><td colspan='3' class='status'>currently none</td></tr>");}
  else {
    for (var i = 0; i < underContract.length; i++) {

      playerName(underContract, i);

      document.write("<td class='icon'><b><small>" + underContract[i][3] + "</small></b></td>");

      if (underContract[i][5] == "2010") {
        document.write("<td class='status'><small><i><b>final contract year<br/></b>")
	switch(underContract[i][6]) {

	  case "UFA":
	  document.write("<b>free agent in 2011</b></i></small></td>")
	  break;

	  case "RFA":
	  document.write("restricted FA in 2011</i></small></td>")
	  break;

	  case "EFA":
	  document.write("exclusive FA in 2011</i></small></td>")
	  break;

	  default:
	  document.write("error</td>");
	}
      }

      else if (underContract[i][5].match(/\?/)) {document.write("<td class='status'>undisclosed</td>");}

      else {document.write("<td class='status'>through <b>" + underContract[i][5] + "</b></td>");}

    }
  }
}



function showDiv(divname) {

document.getElementById("balroster").style.display = "none";
document.getElementById("cinroster").style.display = "none";
document.getElementById("cleroster").style.display = "none";
document.getElementById("pitroster").style.display = "none";
document.getElementById("allroster").style.display = "none";

document.getElementById(divname).style.display = "block";


}

