﻿
function CrimeClusterLoader(id, isSummary, inDetailsFunction, isIe6)
{
  this.ClusterId = id;
  this.Request = null;
  this.IsSummary = isSummary;
  this.DetailsFunction = inDetailsFunction;
  if (isIe6 == null)
    this.IsIE6 = false;
  else
    this.IsIE6 = isIe6;
  var Me = this;
}


CrimeClusterLoader.prototype.loadCluster = function(inCallback)
{ 
  this.CallBack = inCallback;
  var now = new Date();
  var milliseconds = now.getTime();
  var url = "RetrieveCluster.aspx";
  var args = "crimeIndex=" + this.ClusterId + "&ie_crap=" + milliseconds;
  var loader = new AjaxLoader(url, args, false);
  loader.load(this.processClusterCallback, this);
}

CrimeClusterLoader.prototype.processClusterCallback = function(inResults, me)
{
  if (inResults.length > 0)
    me.processCluster(eval('(' + inResults + ')'));
}

CrimeClusterLoader.prototype.processCluster = function(thisCluster)
{
  var description = "<div id=\"identifyWindowHolder\">";
  description += "<table class=\"identifyWindow\" width=\"260\" border=0>";
  if (this.DetailsFunction != null)
  {
    description += "  <tr>";
    description += "    <td align=\"right\"><a id=\"printLink\" href=\"javascript:" + this.DetailsFunction + "(" + this.ClusterId + ");\">View All</a>";
    description += "    </td>";
    description += "  </tr>";
  }
  if (thisCluster.Crimes.length > 3)
  {
    //display a summary if we have more than a few crimes on here
    description += "  <tr>";
    description += "    <td align=\"center\">";
    description += "      <table class=\"identifyWindowStats\">";
    
    //sort the stats so we see the highest first
    thisCluster.Statistics.sort(function (a,b){return b.CrimeCount - a.CrimeCount;});
    for (var statistic in thisCluster.Statistics)
    {
      description += "      <tr>";
      if (this.IsIE6)
        description += "      <td><div style=\"{display:inline; width:15px; height:15px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/offense_" + thisCluster.Statistics[statistic].OffenseCode + ".png', sizingMethod='scale');}\"></div>&nbsp;" + thisCluster.Statistics[statistic].CrimeCount + " " + thisCluster.Statistics[statistic].OffenseName + "</td>";
      else
        description += "      <td><img src=\"images/offense_" + thisCluster.Statistics[statistic].OffenseCode + ".png\">&nbsp;" + thisCluster.Statistics[statistic].CrimeCount + " " + thisCluster.Statistics[statistic].OffenseName + "</td>";
      description += "      </tr>";
    }
    description += "      </table>";
    description += "    </td>";
    description += "  </tr>";
  }
  if (!this.IsSummary ||
      thisCluster.Crimes.length < 6)
  {
    description += "  <tr>";
    description += "    <td id=\"identifyCell\">";
    description += "      <div id=\"identifyListDiv\" class=\"identifyListDiv\">";
    description += "        <table class=\"identifyWindow\" width=\"100%\" cellpadding=2 cellspacing=0 border=0>";
    var offenseCode = "";
    for (var crimeIndex in thisCluster.Crimes)
    {
      var thisCrime = thisCluster.Crimes[crimeIndex];
      description += "<tr>";
      description += "  <td>" + thisCrime.Date + "</td>";
      //alert("  <td><div style=\"{display:inline; width:15px; height:15px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/offense_" + thisCrime.OffenseCode + ".png', sizingMethod='scale');}\"></div>&nbsp;<b>" + thisCrime.Offense + "</b></td>");
      if (this.IsIE6)
        description += "  <td><div style=\"{display:inline; width:15px; height:15px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/offense_" + thisCrime.OffenseCode + ".png', sizingMethod='scale');}\"></div>&nbsp;<b>" + thisCrime.Offense + "</b></td>";
      else
        description += "  <td><img src=\"images/offense_" + thisCrime.OffenseCode + ".png\"> <b>" + thisCrime.Offense + "</b></td>";
      
      description += "</tr>";
      description += "<tr>";
      var border = "";
      if (crimeIndex < thisCluster.Crimes.length-1)
        border = "class=\"BottomBorder\"";
      description += "  <td " + border + ">" + thisCrime.Premises + "</td>";
      description += "  <td " + border + ">" + thisCrime.Address + "</td>";
      description += "</tr>";
      offenseCode = thisCrime.OffenseCode;
    }
    description += "        </table>";
    description += "      </div>";
    description += "    </td>";
    description += "  </tr>";
    description += "</table>";
    description += "</div>";
  }
  this.CallBack(description, thisCluster.Crimes.length);
}
