var DivisionResultTable = function () {
  this.unit_row = function(info, i) {
    var row = ['<tr class="unit_row">'];

    var cells = ['<td colspan="2"><p><span class="unit_name">',
                 (info['Web']) ? ('<br><a href="' + info['Web'] + '" target="_new">' + info['Business Unit'] + '</a>') : info['Business Unit'],
                 '</span></p></td>'];
    cells.extend(['<td class="contact"><p>', 
                  info['Address 1'], '<br>',
                  (info['Address 2']) ? info['Address 2'] + '<br>' : '',
                  info['City'], ', ', info['State'], ' ', info['Zip'], '<br>',
                  '<span class="region">', info['Country'], '</span><br>',
                  info['Phone1'],
                  (info['Phone2']) ? '<br>' + info['Phone2'] : '',
                  (info['Fax']) ? '<br>' + info['Fax'] : '',
                  (info['Web']) ? ('<br><a href="' + info['Web'] + '" target="_new">' + info['Web'] + '</a>') : '',
                  (info['Email']) ? ('<br><a href="mailto:' + info['Email'] + '">' + info['Email'] + '</a>') : '',
                  '</p></td>']);

    row.push(cells.join(''));
    row.push('</tr>');
    return row.join('')
  };

  this.product_row = function(info, i) {
    if (!info['Product Highlights']) { return '' } // Non product business unit
    var row = ['<tr class="product_row">'];

    var cells = [];
    if (info['Product Image']) {
      cells.push('<td><br><img class="prod_img" src="/images/products/', info['Product Image'], '" alt="', info['Business Unit'], info['Product Highlights'], '"/></td>');
    } else {
      cells.push('<td><br><img class="prod_img" src="/images/products/no_image.gif" alt="', info['Business Unit'], info['Product Highlights'], '"/></td>');
    }
    cells.extend(['<td><p><span class="prod_name">', info['Product Highlights'], '</span><br>', info['Description'], '</p><p class="hide_from_print"><a onclick="javascript:finder.print_product(this);">Print Virtual Business Card</a> <input type="checkbox" class="print_this_one"/></p></td>']);
    cells.extend(['<td class="contact"><p><span class="unit_name">',
                  (info['Web']) ? ('<br><a href="' + info['Web'] + '" target="_new">' + info['Business Unit'] + '</a>') : info['Business Unit'],
                  '</span><br>', 
                  info['Address 1'], '<br>',
                  (info['Address 2']) ? info['Address 2'] + '<br>' : '',
                  info['City'], ', ', info['State'], ' ', info['Zip'], '<br>',
                  '<span class="region">', info['Country'], '</span><br>',
                  info['Phone1'],
                  (info['Phone2']) ? '<br>' + info['Phone2'] : '',
                  (info['Fax']) ? '<br>' + info['Fax'] : '',
                  (info['Web']) ? ('<br><a class="hide_from_print" href="' + info['Web'] + '" target="_new">' + info['Web'] + '</a><span class="hide_from_screen">' + info['Web'] + '</span>') : '',
                  (info['Email']) ? ('<br><a href="mailto:' + info['Email'] + '">' + info['Email'] + '</a>') : '',
                  '</p></td>']);


    row.push(cells.join(''));
    row.push('</tr>');
    return row.join('')
  };

  this.get_results = function(data, picked_dict) {
    var table = ['<table class="results_table">'];
    var rows = [];
    var unit_row_func = this.unit_row.bind_func(this);
    var prod_row_func = this.product_row.bind_func(this);
    var first = true;
    var curr_unit = '';
    var ranked_units = [[], [], []];
    var seen = {};

    data.each(function (item, i) {
      var rank_market = '';
      var rank = '';
      if (item['market rank'] > 0) {
        rank_market = item['Market'];
        rank = item['market rank'] - 1;
      }

      if (!seen[item['Business Unit']]) {
        seen[item['Business Unit']] = [];
      }

      if (!seen[item['Business Unit']].contains(item['Product Highlights'])) { // filter duplicate products
        if (rank_market && picked_dict['Market'] == rank_market) { // handle ranked results when filtered by market
          if (ranked_units[rank].length == 0) {
            ranked_units[rank].push('<tr class="spacer_row"><td colspan="3">&nbsp;</td></tr>');
            ranked_units[rank].push(unit_row_func(item, i));
          }
          ranked_units[rank].push(prod_row_func(item, i));
          ranked_units[rank].push('<tr class="dotted_row"><td colspan="3"><div class="dotted_hr">&nbsp;</div></td></tr>');
        } else { // handle unranked results
          if (item['Business Unit'] != curr_unit) {
            if (!first) {
              rows.pop(); // remove last dotted line
              rows.push('<tr class="spacer_row"><td colspan="3">&nbsp;</td></tr>');
            } else {
              first = false;
            }
            rows.push(unit_row_func(item, i));
            curr_unit = item['Business Unit'];
          }
          rows.push(prod_row_func(item, i));
          rows.push('<tr class="dotted_row"><td colspan="3"><div class="dotted_hr">&nbsp;</div></td></tr>');
        }
        seen[item['Business Unit']].push(item['Product Highlights']);
      }
    });

    // add ranked results
    ranked_units.each(function (item, j) {
      table.extend(item);
    });

    // add remaining results
    table.extend(rows);

    table.push('</table>');
    return table.join('')
  };
};


function business_only() {
  $('results').addClass('business_only');
  $('print_links').addClass('hide');
  $('business_links').removeClass('hide');
}

function view_all() {
  $('results').removeClass('business_only');
  $('print_links').removeClass('hide');
  $('business_links').addClass('hide');
}
