//import .FOTW;
//inherit "roxenlib";
#include "FOTW.pmod"

constant timestamp = __DATE__ + "; " + __TIME__;
object fotw;


string parse(object id)
{
  //add_module_path(dirname(__FILE__));
  string lang = id->variables->lang || "en";
  if(search(({"en","pl"}), lang)==-1)
    lang = "en";
  fotw = Fotw();
  if(!fotw)
    return "ERROR: connection to fotw database FAILED.";
  int count = sizeof(fotw);
  int skip = (int)(id->variables->skip);
  if(skip<0)
    skip = 0;
  if(skip>=count)
    skip = count -1;
  string res = "";  
  switch(lang)
    {
    case "en":
      array stuff;
      string search;
      if(id->variables->search && sizeof(id->variables->search))
	{
	  search = id->variables->search;
	  stuff = fotw->
	    query("select id, url, title_en, desc_en, cdate, match(title_en, desc_en) against ('" + fotw->db->quote(search) +
		  "') as score from fotw_main having score != 0 order by score desc limit " + (string)skip + ", 5");
	  count = (int)(fotw->query("select count(*) as count from fotw_main where match(title_en, desc_en) against ('" + fotw->db->quote(search) +
				    "')")[0]->count);
	  if(!count)
	    return "No results for search! Hit the \"Back\" button and try again...";
	  if(skip>=count)
	    skip = count -1;
	}
      else
	{
	  stuff = fotw->
	    query("select id, url, title_en, desc_en, cdate from fotw_main order by cdate desc, id desc limit " + (string)skip + ", 5");
	}
      if(!stuff || !sizeof(stuff))
	return "ERROR: failed to read from fotw database (main table empty?)";
      foreach(stuff, mapping row)
	{
	  res += "<li><a href=\"" + row->url + "\">" + _Roxen.html_encode_string(row->title_en) + "</a><br />" +
	    _Roxen.html_encode_string(row->desc_en) + "<br /><span style=\"font-size:8pt;\"><em>Added: " + 
	    row->cdate + ";</em> URL: <em>" + row->url + ";</em> ID=<em>" +
	    (string)(row->id) + "</em></span><br /></li><br />";
	}
      
      res= "<html><head><title>fotw by rjb</title><style type=\"text/css\">"
	"body{background:#f8f8ff;color:#000088;margin-left:5%;margin-right:5%;font-family:Verdana,Arial,Helvetica,sans-serif;"
	"font-size:10pt;scrollbar-base-color:#e0f8ff;}a:link { font-size:12pt;text-decoration: none; background: #ffffc0; color: #0000ee; }"
	"a:visited { font-size:12pt;text-decoration: none; background: #ffffc0; color: #0000ee; }"
	"a:hover { font-size:12pt;text-decoration: none; background: #e0f8ff ; color: #000000; }"
	"a:active { font-size:12pt;text-decoration: none; background: #e0f8ff ; color: #ffff00; }"
    "a img { border:none; }"
	"</style></head><body>\n<!-- COMPILED AT: " + timestamp + " -->\n<div align=\"center\">"
	"<a href=\"/~rjb/fotw/\" title=\"Click here to view most recent entries\">" +
	"<gtext bg=\"#f8f8ff\" fg=\"#0000ee\" shadow=\"90,1\" nfont=\"Lucida Typewriter\" glow=\"#ffff88\" scale=\"1.3\">"
	"fotw by rjb</gtext></a><p>" + 
	"<tt>fotw</tt> == \"found on the web\"</p></div><div style=\"margin-left:3em; margin-right:3em\">"
	"<div style=\"font-size:8pt;width:50%;position:relative;left:0%;top:3em;height:7em;\">" +
	"<p><form action=\"/~rjb/fotw/\" method=\"GET\">Keyword search:<br /><input style=\"width:15em;background:#e0f8ff;color:#000000\" type=\"text\" name=\"search\" " +
	(search ? ("value=\""+_Roxen.html_encode_string(search) + "\"") : "" ) + " size=\"20\">" +
	"&nbsp;<input type=\"image\" src=\"go.gif\" alt=\"go!\" align=\"bottom\" width=\"37\" height=\"23\"></form></p><p>" +
	"Some high-scoring keywords: <form action=\"/~rjb/fotw/\" method=\"GET\"><select style=\"width:15em;background:#e0f8ff;color:#000000\" name=\"search\">" +
	"<option>" + (fotw->query("select kw from fotw_kw order by score desc limit 24")->kw) * "</option><option> " +
	"</option></select>&nbsp;<input type=\"image\" src=\"go.gif\" alt=\"go!\" align=\"bottom\" width=\"37\" height=\"23\"></form></p>" +
	"</div>"+
	"<div style=\"width:50%;font-size:6pt;position:relative;left:50%; top:-3em;padding:2em;background:#ffffc0;color:#000066;border:thin #000066 solid;\">"+
	"This is a personal link collection.  It certainly reflects my interests to some extent, but does not imply any endorsement of nor "+
	"agreement with the content of the listed resources. In plain words: it's the web, live with it. Have fun. Exercise your own judgment.<br />"+
	"-- <br />/rjb" +
	"</div></div>" +

	"<p><strong>" + ((skip>0) ? "Less " : "Most ") +
	((search)? "matching " : "recently added ") + "items: ( " + (string)(skip+1) + " - " + (string)(skip+sizeof(stuff)) +
	" )</strong></p><ul>" + res +
	"</ul><table width=\"100%\"><tr><td align=\"left\">" +
	(skip ? ("<a href=\"/~rjb/fotw/?skip="+(string)max(0, skip-5)+
		 (search ? ("&search="+Caudium.http_encode_url(search)+"\">&lt;&lt;Better matches</a> |") :
		  "\">&lt;&lt;Newer items</a> |")) : "&nbsp;") + "</td><td align=\"right\">" +
	(((skip+5)<count) ? "| <a href=\"/~rjb/fotw/?skip="+(string)(skip+5) + (search ? ("&search=" + Caudium.http_encode_url(search) + "\">Not so good matches&gt;&gt;</a>") :  "\">Older items&gt;&gt;</a>") : "&nbsp;") + "</td></tr></table>" +
	"<div style=\"text-align:right;border-top:thin solid;font-size:8pt\">Generated: " + (ctime(time())-"\n") + 
	"<br />There are currently " + (string)count + " items "+ (search ? "matching your query " : "") + "in <tt>fotw</tt>"
	"<br /><a href=\"mailto:robert.budzynski@fuw.edu.pl?subject=fotw\">Comments?</a>"
	"</div><p><em><insert file=\"/~rjb/oneliner.cgi\" nocache /></em><a href=\"http://pike.ida.liu.se/\">"
    "<img src=\"power280x25.gif\" alt=\"Powered by Pike\" title=\"Pike Home Page\" align=\"right\" /></a>"
    "</body></html>";
      break;
    case "pl":
      res = "NIE ZROBIONE!";
      break;
    }
  return res;
}
