<?php if (!defined('PmWiki')) exit();
/*  
+----------------------------------------------------------------------+
| hovermenu.php for PmWiki.  
| Copyright 2008 Hans Bracker, Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com).
| This program is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License, Version 2, as
| published by the Free Software Foundation.
| http://www.gnu.org/copyleft/gpl.html
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
| GNU General Public License for more details.
+----------------------------------------------------------------------+
| This script adds a pagelist format fmt=hoverindex, which displays a pagelist
| as an alphabetic hover menu, with page sorted by title and displayed by title.
| Copy file to cookbook/ folder and add to config.php
| include_once("$FarmD/cookbook/hovermenu.php");
+----------------------------------------------------------------------+
*/
$RecipeInfo['ABCHoverIndex']['Version'] = '2008-04-20';

# standard styles from linkcsstooltip.php (Cookbook.LinkCSSToolTip):
$HTMLStylesFmt['ttip'] = "
	div.ttwrap { position:relative; display:inline; }
	div.ttwrap span.ttterm { color:#006; border-bottom:1px dotted gray; }
	div.ttwrap span.ttip { display:none; }
	div.ttwrap:hover span.ttip {
		display:block; 
		position:absolute;
		left:1.5em; 
		top:1.3em;
		min-width:120px;
		border:1px solid #cc9;
		-moz-border-radius:8px;
		-webkit-border-radius: 8px;
		padding:3px 5px;
		background:#ffc;
		color: #006;
		font-size:75%;
		line-height:1.3em;
	   /*text-align: center;*/
	   text-decoration:none;
	}
	* html div.ttwrap:hover { z-index:50; background:; } /* background:; for tricking IE*/
	* html div.ttwrap:hover span.ttip { width:120px; } /* min-width for IE */
	div.ttwrap:hover span.ttip div { display:inline; padding:0; margin:0; } /* for images wrapped in a div */
	/* values for Opera */
	@media all and (min-width: 120px) { 
		div.ttwrap:hover span.ttip { left:1.5em; top:0.6em; }  
	}
"; 

# modified styles:
$HTMLStylesFmt['abcindex'] = "
	span.abcindex { padding:2px 4px; border-bottom:1px solid #ccc;}
	div.abcwrap { background:none; }
	div.abcwrap span.abcterm { padding:0 2px; font-size:100%; border-bottom:none; }
	div.abcwrap:hover span.abcmenu {
		left:-1em; 
		top:1.3em;
		padding: 3px 1em;
		color:none;
		font-size:85%;
		border:1px solid #ccc;
		background:#f9f9f9;
	}
"; 

$FPLFunctions['hoverindex'] = 'FPLABCIndex';

function FPLABCIndex($pagename,&$matches,$opt) {
	global $ABCStartFmt, $ABCEndFmt, $ABCIndexFmt, $ABCIndexEndFmt, $ABCIndexSeparatorFmt,
			 $ABCSectionFmt, $ABCItemFmt, $FmtV;
	$opt['order']='title';
	$matches = MakePageList($pagename, $opt);
	$pgcnt = count($matches); 
	for($n=0; $n<$pgcnt; $n++) 
		$matches[$n]['title'] = FmtPageName('$Title',$matches[$n]['pagename']);
	$cmp = create_function('$x,$y', "return strcasecmp(\$x['title'],\$y['title']);");
	usort($matches,$cmp);
	
	SDV($ABCStartFmt, "<a name='hovermenu'></a><span class='abcindex'>");
	SDV($ABCEndFmt, "</span></div></span>");
	SDV($ABCIndexFmt, "<div class='ttwrap abcwrap'><span class='ttterm abcterm'>\$IndexLetter</span>");
	SDV($ABCIndexSeparatorFmt,"&middot;");
	SDV($ABCIndexEndFmt, "</span></div>");
	SDV($ABCSectionFmt, "<span id='\$IndexLetter' name='\$IndexLetter' class='ttip abcmenu' style='z-index:\$IndexCounter'>");
	SDV($ABCItemFmt, "<a class='abclink' href='\$PageUrl' >\$Titlespaced</a><br />");
	
	$out = array(); $cnt=100; $i=0;
	foreach($matches as $k => $item) {
		$pletter = strtoupper(substr($item['title'],0,1));
		$FmtV['$IndexLetter'] = $pletter;
		if ($pletter!=@$lletter) { 
			$cnt++; $i++;
			$FmtV['$IndexCounter'] = $cnt;
			if($k>0) $out[] = $ABCIndexEndFmt;
			$out[] = FmtPageName($ABCIndexFmt,$item['pagename']);	
			$out['s'.$i] = $ABCIndexSeparatorFmt; 
			$out[] = FmtPageName($ABCSectionFmt,$item['pagename']);
			$lletter = $pletter; 
		}
		$out[] = FmtPageName($ABCItemFmt,$item['pagename']);
	}
	unset($out['s'.$i]); //remove last index separator
	return Keep(FmtPageName($ABCStartFmt,$pagename).implode('',$out).
					FmtPageName($ABCEndFmt,$pagename));
}