// ===================================================================
// Class for showing paged list of filtered Parol.
//

function FilteredParolView(aModel, aController)
{
  try {
    PageView.call(this, "FilteredParolList", "", "", "FilteredParolLast", "selectedParol");

    this._Model = aModel;
    this._Controller = aController;
    this._prefixForID = "filteredParolLine";
    this._PageSize = 50000; // show one page only (bible has ~ 30000 verses)
  }
  catch (e) {
    alert("FilteredParolView: " + e);
  }
}

FilteredParolView.prototype = new PageView();
FilteredParolView.prototype.constructor = FilteredParolView;

FilteredParolView.prototype.toString = function()
{
  return "FilteredParolView";
}

FilteredParolView.prototype.getCurParol = function()
{
  return this.getCurCollectionItem();
}

// set, no visual update
FilteredParolView.prototype.setFilteredParol = function()
{
  this.setCollection(this._Model.getFilteredParol());
}


// set index, no visual update
//FilteredParolView.prototype.gotoParolQual= function(parolQual)
//{
//  var aSecondPairSet = this.getCollection();
//  var index = null;
//  for (var i = 0, len = aSecondPairSet.length; i < len; ++i) {
//    var aPair = aSecondPairSet[i];
//    if (aPair.getSecond() == parolQual) {
//      index = i;
//      break;
//    }
//  }
//  this.setCurIndex(index);// handles null
//}

FilteredParolView.prototype.findParolIndex = function(aParol)
{
  for (var i = 0, Coll = this.getCollection(), len = Coll.length; i < len; ++i) {
    var curParol = Coll[i];
    if (aParol == curParol) {
      return i;
    }
  }
  return null;
}

// --- overrides ---

FilteredParolView.prototype.notifyUpdatePage = function()
{
  this._Controller.selectFilteredParolLine(this.getCurIndex() - this.getPageStartIndex(), false);// no SecondView update
}

FilteredParolView.prototype.getElementForLineIndex = function(lineIndex)
{
  return document.getElementById(this._prefixForID + lineIndex);
}

FilteredParolView.prototype._computeEmptyPage = function()
{
  return "<p>Sie haben noch keinen Bibelspruch gesucht / es wurde kein passender Bibelspruch gefunden.</p>";
}

FilteredParolView.prototype._computePageHeader = function()
{
  return "<table class='parolTable'>"
    + "<colgroup>"
    + "<col class='fpInfo'/>"
    + "<col class='parolRef'/>"
    + "<col class='parolText'/>"
    + "</colgroup>";
}

// computePageLines:
// Optimization: overwrite PageView.computePageLines, 
// so that we can put common code outside the for loop
// Hardly a difference in FF 3.5 (few seconds),
// IE6 takes 2min instead of 1min???
FilteredParolView.prototype.computePageLines = function(start, endIndex)
{
  var html = "";
  var coll = this.getCollection();
  if (!coll.length) {
    return "";
  }
  var filter = this._Model.getFilter();
  var RE = filter ? new RegExp(filter, "g") : null; // create once

  var aParolFormatter = new ParolFormatter(this._Model);
  for (var index = start; index < endIndex; ++index) {
    var lineIndex = index;
    var aParol = coll[index];
    var parolQual = aParol.getID();
  
    //TODO low HS 2009-12-31 - emphasis of filter term still replaces within embedded <em> in IL / L (see e.g. 1K8v5-6 with filter "e")
    // Do not replace within markup!
    // if filter empty, returns s, otherwise marks occurrences of filter in s using <span>
    var ref = aParol.getSL();
    if (filter) {
      ref = ref.replace(RE, "<span class='match'>" + filter + "</span>");
    }
    
    var text = "";
    var il = aParol.getHtmlIL();
    if (il) {
      if (filter) {
        il = il.replace(RE, "<span class='match'>" + filter + "</span>");
      }
      text = "<em>" + il + "</em> ";
    }
    var l = aParol.getHtmlL().replace(/\s+/g, " ");
    if (filter) {
      l = l.replace(RE, "<span class='match'>" + filter + "</span>");
    }
    text += l;

    var title = "";
    var author = aParol.getAuthor();
    if (author != "Admin") {
      title = author + " " + aParol.getUpdated().substring(0, 10);
    }
    var Info = aParolFormatter.getInfo(aParol) || [100];
    var stateImg = "";
    if (Info[0] != 100) {
      title += " " + Info[2];
      stateImg = " <img src='images/" + Info[1] + "' alt='' />";
    }
    if (Info[3]) {
      title += " (" + parolQual + ")";
      ref += " (" + Info[3] + ")";
    }
    
    var onclick = "";
    html += "<tr class='parolRow' id='" + this._prefixForID + lineIndex + "' "
      // pass 'true' for SecondView update
      + "onclick=\"document._Controller.selectFilteredParolLine(" + lineIndex + ", true); return false;\""
      + "ondblclick=\"document._Controller.addPair(); return false;\""
      + ">"
      + "<td class='fpInfo'></td>"
      + "<td class='parolRef' title='" + title + "'>" + ref + stateImg + "</td>"
      + "<td class='parolText'>" + text + "</td>"
      + "</tr>\n";
  }
  return html;
}

FilteredParolView.prototype._computePageFooter = function()
{
  return "</table>";
}


