<?php
function wikiplugin_switchmenu_help() {
	return tra("Insert a Dynamic Drive switchmenu onto a page").
":<br />~np~{SWITCHMENU(persistmenu=>yes|no , persisttype=>sitewide|local , menu=>V|H|any , item=>V|H|any )}<br />".tra("wiki text with optional CSS followed by headers (!) and links")."{SWITCHMENU} ".tra("All parameters optional, default is the first value listed in choices")."~/np~";
}
function wikiplugin_switchmenu($data, $params) {
global $tikilib;
static $current = 0; // menu numbering
//	extract $params just what we want with default
	foreach (array(	'persistmenu'=>'yes',
			   		'persisttype'=>'sitewide',
					'menu'=>'V',
					'item'=>'V') as $nam=>$default) {
		$$nam = isset($params[$nam])? $params[$nam]: $default;
	}
	$menu = ($menu == 'V')? "\n":
				 (($menu == 'H')? '': $menu);
	$item = ($item == 'V')? "\n":
				 (($item == 'H')? '': $item);
	$switchmenu = $topmenu = '';
	if (!$menu) {
		$addmenu = &$topmenu;
	} else {
		$addmenu = &$switchmenu;
	}
	$css  = <<<EOD
.menutitle{
cursor:pointer;
margin-bottom: 5px;
background-color:#ECECFF;
color:#000000;
width:140px;
padding:2px;
text-align:center;
font-weight:bold;
}

.submenu{
margin-bottom: 0.5em;
}
EOD;
	preg_match('/(.*?)(^!.*)/ms', $data, $match);
	$beforemenu = '<div id="masterdiv"><style type="text/css">'.
		str_replace('&quot;', '"', $css.$match[1]).'</style>';
					   
	$lines = explode("\n", $match[2]);
	$search = '/^\s*(?:
				 (?:!(.*))			# a menu !Mon menu (miam miam) or !image menu
				|(?:\(\((.*)\)\))	# perhaps a wiki link
				|(?:\[(.*)])		# or an external link
			   )(.*)/ix';
	$keyM = md5($tikilib->genPass());
	$keyI = md5($tikilib->genPass());
	$keyS = md5($tikilib->genPass());

	foreach ($lines as $line) {
		if (!preg_match($search, $line, $match)) {
			$switchmenu .= $line."\n";
			continue;
		}
		if ($current and $match[1]) { // close last menu
			$switchmenu .= $keyS;
		}
		if (($tit = $match[1]) or (!$current and
			($tit = 'wikiplugin_switchmenu'))) { // menu or image title
			++$current;
			$addmenu .= $keyM.$current.'/'.$tit.$menu.$keyS;
			$switchmenu .= $keyI.$current.'/';
			if ($match[1]) {
				continue;
			}
		}
		$switchmenu .= $line.$item;
	}
	$switchmenu = $beforemenu.preg_replace(
		 array("#{$keyM}(\d+)/#", "#{$keyI}(\d+)/#", "#{$keyS}#",
		 '#<div class="img">(.*?)</div>#'),
		 array("<span class='menutitle' onclick='SwitchMenu(\"sub$1\")'>\n",
		 "<span class='submenu' id='sub$1'>\n", '</span>',
		 "<span class='img'>$1</span>"),
			$tikilib->parse_data($topmenu.$switchmenu)).
			 ($current? '</span>': ''). '</div> ~/np~ '; // close last menu if any 
$core  = <<<EOD
~np~ <script type="text/javascript">

/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="{$persistmenu}" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="{$persisttype}" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\\n')
document.write('.submenu{display: none;}\\n')
document.write('</style>\\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

</script>
EOD;

return $core . $switchmenu;
}
?>
