// globals to adjust behaviour

var debugKeywords = 1;

var noWeight = false;

var headlineTotal = 64;
var weightGroups = 4;


// recent headlines

function rhItem (name, href, keywords, date) {
  this.name = name;
  this.href = href;
  this.keywords = keywords;
  this.date = date;
  return(this);
}

function rhAddItem (name, href, keywords, date) {
  this.items[this.count] = new rhItem (name, href, keywords, date);
  this.count++;
}

function recentHeadlines () {
  this.items = new Array();
  this.count = 0;
  this.addItem = rhAddItem;
  return(this);
}

var myRecentHeadlines = new recentHeadlines();

function addHeadline (name, href, keywords, date) {
  myRecentHeadlines.addItem (name, href, keywords, date);
  addKeywords (keywords);
}


// list of keywords

function olItem (name, startCount) {
  this.name = name;
  this.count = startCount;
  return(this);
}

function olAddItem (name) {
var inc = 1;

  if (! noWeight) {
  // increase weight of most recent stories
  // use cubic function to accelerate emergence of recent sets of stories
    inc += Math.pow (
             Math.floor (
               (headlineTotal - myRecentHeadlines.count)
               / Math.floor (headlineTotal / weightGroups)
             ),
             3
           );
  }

  for (var i = 0; i < this.count; i++) {
    if (this.items[i].name.toLowerCase() == name.toLowerCase()) {
      this.items[i].count += inc;
      return;
    }
  }
  this.items[this.count] = new olItem (name, inc);
  this.count++;
}

function orderedList() {
  this.items = new Array();
  this.count = 0;
  this.addItem = olAddItem;
  return(this);
}

var myOrderedList = new orderedList();

var recentKeywords = '';

function addKeywords (kw) {
  if (recentKeywords != '') {
    recentKeywords += ', ';
  }
  recentKeywords += kw;
  eK = explodeString (kw, ',', false);
  for (var i=0; i < eK.length; i++) {
    myOrderedList.addItem (eK[i]);
  }
}


// sorting criteria

function byName (a,b) {
  return sortCompare (b.name.toLowerCase(), a.name.toLowerCase());
}

function byCount (a,b) {
  var c = sortCompare (a.count, b.count);
  if (c == 0) {
    c = byName (a,b);
  }
  return c;
}


// show keywords, sorted by frequency

function showKeywordScores (f) {
  var s = "Top keywords, weighted by frequency and freshness:\n";
  // myOrderedList.items.sort (byName);
  myOrderedList.items.sort (byCount);
  var current = 0;
  for (var i=0; i < myOrderedList.count; i++) {
    var item = myOrderedList.items[i];
    if (current != item.count) {
      current = item.count;
      s += '\n['+current+'] ';
    } else {
      s += ', ';
    }
    // if (current > 1) {
      s += item.name;
    // }
  }
  f(s);
}


// display horizontal menubar of headlines sorted under most-frequent keywords

function displayMenubar (before, after) {
  var maxmenus = 32;   // strict upper limit
  var maxLength = 88; // heuristic to make the menubar as long as possible
  var l = 0;
  var ll = 0;
  var maxlines = 16;

  myOrderedList.items.sort (byCount);

  var m = before+ '<ul id="menuDropdown">';
  var m0 = '<ul>';
  var m1 = '<li>';
  var m2 = '</li>\n';
  var m3 = '</ul>';
  var mmli = '';
  var mm = '';

  for (var i=0; (i < myOrderedList.count) && (l+ll < maxLength) && (i < maxmenus); i++) {
    var kw = myOrderedList.items[i].name;
    if (l + kw.length > maxLength) {
      // maybe a shorter keyword follows, but keep moving goalpost
      ll++;
      continue;
    }
    l += kw.length + 2; // space between keywords counts as two chars
    ll = 0;

    m += mmli+mm; // previously built menu
    mmli = '<li class="menubar"' +((i==0)?' id="first"':'')+ ' style="z-index: '+(maxmenus-i)+';">';
    mm = kw.charAt(0).toUpperCase() +kw.substring(1) +m0;
    var jj = 0;
    for (var j=0; (j < myRecentHeadlines.count) && (jj < maxlines); j++) {
      var headline = myRecentHeadlines.items[j];
      if (headline.keywords.indexOf(kw) != -1) {
        var d = (headline.date.length == 0)?'':(headline.date+': ');
        mm += m1+ '<a href="'+ headline.href +'" title="' +d+headline.keywords +'" class="option'+(++jj%2)+'">'+ headline.name +'</a>' +m2;
      }
    }
    mm += m3+m2;
  }
  m += '<li class="menubar" id="last" style="z-index: 1;">' +mm +m3;
  m += '<br clear="all" />' +after;
  document.write (m);
  fixupDropdown();
}


// display a sequence of headlines as links

function visualizeKeywords () {
var s = 
' <a href="#" title="recent keywords" onclick="alert(recentKeywords)">.</a>'
+
'<a href="#" title="keyword scores" onclick="showKeywordScores(alert)">.</a>';
  return s;
}

function displayHeadlines (maxEntries, before, mid, after) {
  var nbEntries = Math.min (maxEntries, myRecentHeadlines.count);

  var h = before;

  for (i=0; i < nbEntries; i++) {
    var headline = myRecentHeadlines.items[i];
    if (i > 0) {
      h += mid;
    }
    h += '<a href="'+ headline.href
         +'" title="' +headline.date+': '+headline.keywords +'">'
         + headline.name +'</a>';
  }

  if (debugKeywords) {
    h += visualizeKeywords();
  }
  h += after;
  document.write (h);
}


// configurable parameters

function displayKeywords () {
  displayMenubar (
    '<div style="margin: -12px; margin-top: -26px;">' ,
    '<small class="credityp">TOP KEYWORDS MENU</small></div><br clear="all" />'
  );
}

function displayRecentHeadlines () {
  displayHeadlines (
    12 ,
    '<div id="recentHeadlines"><small>RECENTLY: ' ,
    '\n&bull; ' ,
    '</small></div>\n'
    + '<br clear="all" />'
    // + '<p class="frontdivider"> </p>\n'
  );
}

